C0 code coverage information
Generated on Fri Aug 25 11:26:30 PDT 2006 with rcov 0.7.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 module UserSystem
2
3 protected
4
5 # overwrite this if you want to restrict access to only a few actions
6 # or if you want to check if the user has the correct rights
7 # example:
8 #
9 # # only allow nonbobs
10 # def authorize?(user)
11 # user.login != "bob"
12 # end
13 def authorize?(user)
14 return false unless user.enabled?
15 return false if user.deleted?
16 return false if user.expires? and Time.now > user.expires_at
17 return true
18 end
19
20 # overwrite this method if you only want to protect certain actions of the controller
21 # example:
22 #
23 # # don't protect the login and the about method
24 # def protect?(action)
25 # if ['action', 'about'].include?(action)
26 # return false
27 # else
28 # return true
29 # end
30 # end
31 def protect?(action)
32 if ['article', 'page', 'images', 'rss', 'atom', 'search', 'old_links'].include? @params[:controller]
33 return false
34 end
35 true
36 end
37
38 # login_required filter. add
39 #
40 # before_filter :login_required
41 #
42 # if the controller should be under any rights management.
43 # for finer access control you can overwrite
44 #
45 # def authorize?(user)
46 #
47 def login_required
48 if not protect?(action_name)
49 return true
50 end
51
52 if user?
53 #check and see if the user's data in the session became invalid
54 if @session['user_state'] and @session['user_state'] == 'INVALID'
55 @session['user'] = User.find(@session['user'].id)
56 @session['user_state'] = 'VALID'
57 end
58 if authorize?(@session['user'])
59 return true
60 end
61 end
62
63 if action_name == 'not_enabled' and action_name != 'logout' and @session and @session['user'] and @session['user'].verified?
64 return true
65 end
66 # store current location so that we can
67 # come back after the user logged in
68 store_location
69
70 # call overwriteable reaction to unauthorized access
71 access_denied
72 return false
73 end
74
75 # overwrite if you want to have special behavior in case the user is not authorized
76 # to access the current operation.
77 # the default action is to redirect to the login screen
78 # example use :
79 # a popup window might just close itself for instance
80 def access_denied
81 user = @session['user']
82
83 if user and (!user.enabled?) and (user.verified?) and action_name != 'logout'
84 redirect_to :controller => "users", :action => "not_enabled"
85 else
86 redirect_to :controller => "users", :action => "login"
87 end
88 end
89
90 # store current uri in the session.
91 # we can return to this location by calling return_location
92 def store_location
93 @session['return-to'] = @request.request_uri
94 end
95
96 # move to the last store_location call or to the passed default one
97 def redirect_back_or_default(default)
98 if @session['return-to'].nil?
99 redirect_to default
100 else
101 redirect_to_url @session['return-to']
102 @session['return-to'] = nil
103 end
104 end
105
106 def user?
107 # First, is the user already authenticated?
108 return true if not @session['user'].nil?
109
110 # If not, is the user being authenticated by a token?
111 return false if not @params['user_id']
112 id = @params['user_id']
113 key = @params['key']
114 if id and key
115 @session['user'] = User.authenticate_by_token(id, key)
116 if not @session['user'].nil?
117 UserNotify.deliver_requesting_approval(@session['user'],
118 url_for(:controller => 'users', :action => 'approve', :id => @session['user'].id, :role => '0'),
119 url_for(:controller => 'users', :action => 'approve', :id => @session['user'].id, :role => '1'),
120 url_for(:controller => 'users', :action => 'edit', :id => @session['user'].id),
121 url_for(:controller => 'users', :action => 'awaiting_approval')
122 ) unless @params[:action] == 'change_password'
123 return true
124 end
125 end
126
127 # Everything failed
128 return false
129 end
130 end
Generated using the rcov code coverage analysis tool for Ruby version 0.7.0.