Added the RoR Login-Engine and activated it on the site.
[selectricity-live] / vendor / plugins / login_engine / lib / login_engine / authenticated_system.rb
1 module LoginEngine
2   module AuthenticatedSystem
3     
4     protected
5
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  
8     # example:
9     #
10     #  # only allow nonbobs
11     #  def authorize?(user)
12     #    user.login != "bob"
13     #  end
14     def authorize?(user)
15        true
16     end
17   
18     # overwrite this method if you only want to protect certain actions of the controller
19     # example:
20     # 
21     #  # don't protect the login and the about method
22     #  def protect?(action)
23     #    if ['action', 'about'].include?(action)
24     #       return false
25     #    else
26     #       return true
27     #    end
28     #  end
29     def protect?(action)
30       true
31     end
32    
33     # login_required filter. add 
34     #
35     #   before_filter :login_required
36     #
37     # if the controller should be under any rights management. 
38     # for finer access control you can overwrite
39     #   
40     #   def authorize?(user)
41     # 
42     def login_required
43       if not protect?(action_name)
44         return true  
45       end
46
47       if user? and authorize?(session[:user])
48         return true
49       end
50
51       # store current location so that we can 
52       # come back after the user logged in
53       store_location
54   
55       # call overwriteable reaction to unauthorized access
56       access_denied
57     end
58
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
62     # example use :
63     # a popup window might just close itself for instance
64     def access_denied
65       redirect_to :controller => "/user", :action => "login"
66     end  
67   
68     # store current uri in  the session.
69     # we can return to this location by calling return_location
70     def store_location
71       session['return-to'] = request.request_uri
72     end
73
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?
77         redirect_to default
78       else
79         redirect_to_url session['return-to']
80         session['return-to'] = nil
81       end
82     end
83
84     def redirect_back_or_default(default=nil)
85       if request.env["HTTP_REFERER"].nil?
86         redirect_to default
87       else
88         redirect_to(request.env["HTTP_REFERER"]) # same as redirect_to :back
89       end
90     end
91
92     def user?
93       # First, is the user already authenticated?
94       return true if not session[:user].nil?
95
96       # If not, is the user being authenticated by a token?
97       id = params[:user_id]
98       key = params[:key]
99       if id and key
100         session[:user] = User.authenticate_by_token(id, key)
101         return true if not session[:user].nil?
102       end
103
104       # Everything failed
105       return false
106     end
107   
108     # Returns the current user from the session, if any exists
109     def current_user
110       session[:user]
111     end
112   end
113 end  

Benjamin Mako Hill || Want to submit a patch?