Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require_dependency "user"
- module AuthSystem
- include AuthHelper
- # store current uri in the cookies
- # we can return to this location by calling return_location
- def store_location
- cookies[:return_to] = {:value => request.request_uri, :expires => 5.minutes.from_now }
- end
- protected
- def del_location
- cookies[:return_to] = {:value => nil, :expires => nil }
- end
- # move to the last store_location call or to the passed default one
- def redirect_back_or_default(default)
- if cookies[:return_to].nil?
- redirect_to default
- else
- redirect_to cookies[:return_to]
- cookies[:return_to] = nil
- end
- end
- def require_auth(*options)
- logger.info "checking if user is logged in: " + @user.logged_in?.to_s
- auth_intercept('login') unless @user.logged_in?
- logger.info 'user passing authentication first stage'
- access_granted = false
- case options.first
- when true || false
- restrict_time, credentials = options.shift, options.first
- else
- credentials, restrict_time = options.shift, (options.first || false)
- end
- case credentials.class.to_s
- when NilClass # simple authentication
- access_granted = restrict_time ? @user.acl?("USERS", restrict_time) : user_logged_in?
- when Array # check against any of the credentials
- credentials.each { |cred|
- if @user.access_granted_for?(cred, restrict_time); access_granted = true; break; end
- }
- else # check against all of the credentials
- access_granted = @user.access_granted_for?(credentials, restrict_time)
- end
- auth_intercept('denied') unless access_granted == true
- logger.info 'user passing authentication final stage'
- return access_granted
- end
- # insert interceptor action before current action
- def auth_intercept(interceptor_action = 'login')
- store_location
- redirect_to new_session_path
- throw :abort
- end
- # override if you want to have special behavior in case the user is not authorized
- # to access the current operation.
- # the default action is to redirect to the login screen
- # example use :
- # a popup window might just close itself for instance
- def access_denied
- flash[:notice] = "You don't have the right to access this page"
- redirect_to auth_url(:action => 'denied')
- end
- def app_config
- @app ||= YAML.load_file("#{RAILS_ROOT}/config/auth_generator.yml").symbolize_keys
- User.config @app
- end
- def ident
- require_dependency "user"
- if cookies[:user]
- @user = User.fromString(cookies[:user]) # fromString may return nil!
- end
- if @user.nil?
- @user = User.new
- @user.ident = false
- end
- # !!! Leave that true !!!
- true
- end
- end
Add Comment
Please, Sign In to add comment