2 module AuthenticatedSystem
6 # overwrite this if you want to restrict access to only a few actions
7 # or if you want to check if the user has the correct rights
10 # # only allow nonbobs
11 # def authorize?(user)
18 # overwrite this method if you only want to protect certain actions of the controller
21 # # don't protect the login and the about method
22 # def protect?(action)
23 # if ['action', 'about'].include?(action)
33 # login_required filter. add
35 # before_filter :login_required
37 # if the controller should be under any rights management.
38 # for finer access control you can overwrite
40 # def authorize?(user)
43 if not protect?(action_name)
47 if user? and authorize?(session[:user])
51 # store current location so that we can
52 # come back after the user logged in
55 # call overwriteable reaction to unauthorized access
59 # overwrite if you want to have special behavior in case the user is not authorized
60 # to access the current operation.
61 # the default action is to redirect to the login screen
63 # a popup window might just close itself for instance
65 redirect_to :controller => "/user", :action => "login"
68 # store current uri in the session.
69 # we can return to this location by calling return_location
71 session['return-to'] = request.request_uri
74 # move to the last store_location call or to the passed default one
75 def redirect_to_stored_or_default(default=nil)
76 if session['return-to'].nil?
79 redirect_to_url session['return-to']
80 session['return-to'] = nil
84 def redirect_back_or_default(default=nil)
85 if request.env["HTTP_REFERER"].nil?
88 redirect_to(request.env["HTTP_REFERER"]) # same as redirect_to :back
93 # First, is the user already authenticated?
94 return true if not session[:user].nil?
96 # If not, is the user being authenticated by a token?
100 session[:user] = User.authenticate_by_token(id, key)
101 return true if not session[:user].nil?
108 # Returns the current user from the session, if any exists