Module: Cream::UserControl
- Defined in:
- lib/cream/controller/user_control.rb
Instance Method Summary collapse
- #current_user ⇒ Object
- #guest_options ⇒ Object
- #set_language(language_code) ⇒ Object
-
#sign_in(resource_or_scope, *args) ⇒ Object
Sign in an user that already was authenticated.
-
#sign_out(resource_or_scope) ⇒ Object
Sign out a given user or scope.
- #user_session ⇒ Object
- #user_signed_in? ⇒ Boolean
Instance Method Details
#current_user ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cream/controller/user_control.rb', line 4 def current_user # puts "current_user" if !session[:user_id] # puts "make guest" @guest ||= Guest.create() # puts "guest: #{@guest}" return @guest end if session[:user_id] begin clazz = session[:user_class_name].constantize @current_user ||= clazz.find session[:user_id] # puts "logged in user: #{@current_user}" return @current_user rescue Exception => e puts "Error with current_user: user_class_name = '#{session[:user_class_name]}' error: #{e}" end end end |
#guest_options ⇒ Object
29 30 31 |
# File 'lib/cream/controller/user_control.rb', line 29 def session[:guest_options] ||= {} end |
#set_language(language_code) ⇒ Object
24 25 26 27 |
# File 'lib/cream/controller/user_control.rb', line 24 def set_language language_code current_user.language_code = language_code if current_user # for non-guest user [:language_code] = language_code # for guest user end |
#sign_in(resource_or_scope, *args) ⇒ Object
Sign in an user that already was authenticated. This helper is useful for logging users in after sign up.
Examples:
sign_in :user, @user # sign_in(scope, resource)
sign_in @user # sign_in(resource)
sign_in @user, :event => :authentication # sign_in(resource, options)
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cream/controller/user_control.rb', line 50 def sign_in(resource_or_scope, *args) = args. scope = Devise::Mapping.find_scope!(resource_or_scope) resource = args.last || resource_or_scope expire_session_data_after_sign_in! warden.set_user(resource, .merge!(:scope => scope)) # set user id session[:user_id] = resource.id session[:user_class_name] = resource.class.name end |
#sign_out(resource_or_scope) ⇒ Object
Sign out a given user or scope. This helper is useful for signing out an user after deleting accounts.
Examples:
sign_out :user # sign_out(scope)
sign_out @user # sign_out(resource)
69 70 71 72 73 74 75 76 77 |
# File 'lib/cream/controller/user_control.rb', line 69 def sign_out(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) warden.user(scope) # Without loading user here, before_logout hook is not called warden.raw_session.inspect # Without this inspect here. The session does not clear. warden.logout(scope) # user id session[:user_id] = nil @current_user = nil end |
#user_session ⇒ Object
37 38 39 |
# File 'lib/cream/controller/user_control.rb', line 37 def user_session user_signed_in? ? super : session end |
#user_signed_in? ⇒ Boolean
33 34 35 |
# File 'lib/cream/controller/user_control.rb', line 33 def user_signed_in? current_user && !current_user.has_role?(:guest) end |