-
Notifications
You must be signed in to change notification settings - Fork 17
Cream Guest user
kristianmandrup edited this page Jan 28, 2011
·
2 revisions
The Guest user and current_user
is currently set up like this:
module Cream
module UserControl
def current_user
if !session[:user_id]
@guest ||= Guest.create(guest_options)
return @guest
end
if session[:user_id]
begin
clazz = session[:user_class_name].constantize
@current_user ||= clazz.find session[:user_id]
return @current_user
rescue Exception => e
puts "Error with current_user: user_class_name = '#{session[:user_class_name]}' error: #{e}"
end
end
end
def set_language language_code
current_user.language_code = language_code if current_user # for non-guest user
guest_options[:language_code] = language_code # for guest user
end
def guest_options
session[:guest_options] ||= {}
end
end
end
The
UserControl
module is then included into the ApplicationController
like this:
ApplicationController.send :include, Cream::UserControl
The idea is to create a new simple Guest user if session[:user_id]
is nil. The Guest user is then populated with whatever you put in the session[:guest_options]
. This can be used to set locale, language, country etc.
If there is a session[:user_id]
, it will be used to retrieve the user of the given type from the database, using the session[:user_class_name]
(now set on sign_in)
def sign_in(resource_or_scope, *args)
...
# set user id
session[:user_id] = resource.id
session[:user_class_name] = resource.class.name
end
Please provide suggestions or patches to improve this logic. Thanks!