Jifty/CurrentUser.pm

filter: Statementstmt 22.5% 14 / 62 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 26.6% 4 / 15 Podpod 100% 10 / 10 totaltotal 23.1% 28 / 121 CC 15 SCAR 21.2
CC
15
Cov
15%
user_object
20.0
new
12.0
_init
12.0
CRAP 8.3
« Jifty/Plugin/PubSub/Connection.pm ↑ Summary Jifty/CAS/Store/LocalFile.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::CurrentUser;
5  
6 1 ▶use base qw/Jifty::Object Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 __PACKAGE__->mk_accessors(qw(is_superuser is_bootstrap_user));
10  
11 =head1 NAME
12  
13 Jifty::CurrentUser - Base class and basic implementation of current user object
14  
15 =head1 DESCRIPTION
16  
17 Most applications need to have a concept of who the current user
18 is. So Jifty supports this concept internally. Every L<Jifty::Object>
19 (which most things in Jifty are descended from) except the CurrentUser
20 itself is instantiated with a L<Jifty::CurrentUser> subclass as a
21 parameter to the creator.
22  
23 This class describes (and implements a trivial version) of the access
24 control API that a Jifty application needs to implement to provide
25 user-based access control
26  
27 It's generally expected that your application will override this class
28 if you want any sort of access control.
29  
30 =head2 new
31  
32 Creates a new L<Jifty::CurrentUser> object. Calls L<_init>, an
33 app-specific initialization routine.
34  
35 If you call it with the C<_bootstrap> argument, Jifty will set the user up as a bootstrap user, who's usually allowed to do just about anything without any access control
36  
37 =cut
38  
39 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
40 0 my $class = shift;
41 0 my $self = {};
42 0 ▶ bless $self, (ref $class || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
43 0 my %args = (@_);
44  
45 # Make this user a bootstrap user if in bootstrap mode
46 0 ▶ if ( delete $args{'_bootstrap'} ) { $self->is_bootstrap_user(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args{"_bootstrap"}) 0 0 0
47  
48 # Call _init for app-specific initialization
49 0 $self->_init(%args);
50  
51 0 return $self;
52 }
53  
54 =head2 _init
55  
56 Applications should override this method to provide any application-specific user loading code. The built-in
57  
58 If you do nothing, code similar to this will be called by _init.
59  
60 sub _init {
61 my $self = shift;
62 my %args = (@_);
63 if (keys %args and UNIVERSAL::can(Jifty->app_class('Model', 'User'), 'new')) {
64 $self->user_object(Jifty->app_class('Model', 'User')->new(current_user => $self));
65 $self->user_object->load_by_cols(%args);
66 }
67 return 1;
68 }
69  
70 That is, it will attempt to load the columns given in the model named C<App::Model::User> (where I<App> is the name of your application class). If your notion of a user object isn't a typical Jifty model or named something else, you will definitely need to override this method. If you need to perform any additional initialization for user objects, you may want to override this as well.
71  
72 =cut
73  
74 0 ▶sub _init {
Subroutine _init: not called CC=3 SCAR=24.8
75 0 my $self = shift;
76 0 my %args = (@_);
77  
78 # Duck-typing to check to for a user class
79 0 my $user_class = Jifty->app_class({require => 0}, 'Model', 'User');
80 0 ▶ if (keys %args and UNIVERSAL::can($user_class, 'new') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args and UNIVERSAL::can($user_class, "new")) 0 0 0
Condition0%0/3
keys %args and UNIVERSAL::can($user_class, "new")
!ll&&!rl&&r
000
MC/DC0%0/2
keys %args and UNIVERSAL::can($user_class, "new")
keys %args UNIVERSAL::can($user_class, "new")
Truth table0%0/3
A and B
A= keys %argsB= UNIVERSAL::can($user_class, "new")
ABresult
0X0
100
111
81 0 $self->user_object($user_class->new(current_user => $self));
82 0 $self->user_object->load_by_cols(%args);
83 }
84  
85 0 return 1;
86 }
87  
88 =head2 superuser
89  
90 A convenience constructor that returns a new CurrentUser object that's
91 marked as a superuser. Can be called either as a class or object method.
92  
93 =cut
94  
95 0 ▶sub superuser {
Subroutine superuser: not called CC=2 SCAR=17.9
Pod documented
96 0 my $class = shift;
97 0 ▶ $class = ref( $class ) if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
98  
99 # Create the current user object
100 0 my $self = $class->new();
101  
102 # Make it superuser and send it out
103 0 $self->is_superuser(1);
104 0 return $self;
105 }
106  
107 =head2 user_object
108  
109 This gets or sets your application's user object for the current
110 user. Generally, you're expected to set and load it in the L</_init> method
111 in your L<Jifty::CurrentUser> subclass.
112  
113 =cut
114  
115 0 ▶sub user_object {
Subroutine user_object: not called CC=4 SCAR=30.0
Pod documented
116 0 my $self = shift;
117 0 ▶ return $self->{'user_object'} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
118 0 $self->{'user_object'} = shift;
119  
120 # protect ourself from circular refereces to prevent memory leaks
121 0 ▶ if ( $self->{'user_object'}{'_current_user'} == $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"user_object"}{"_current_user"} == $self) 0 0 0
122 Scalar::Util::weaken( $self->{'user_object'}{'_current_user'} )
123 0 ▶ unless Scalar::Util::isweak( $self->{'user_object'}{'_current_user'} );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::is_weak($self->{"user_object"}{"_current_user"}) 0 0 0
124 0 $self->{'user_object'}{'_resurrect_current_user'} = 1;
125 }
126  
127 0 return $self->{'user_object'};
128 }
129  
130 =head2 id
131  
132 Returns C<0> if we don't have a L<user_object>. When we I<do> have a
133 user_object, return that user's id.
134  
135 =cut
136  
137 0 ▶sub id {
Subroutine id: not called CC=2 SCAR=17.9
Pod documented
138 0 my $self = shift;
139  
140 # This can be a hotspot, so we don't use method calls, instead
141 # directly accessing the value.
142  
143 # Make sure we have a user object before trying to ID it
144 0 ▶ return $self->{user_object}->id if $self->{user_object};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"user_object"} 0 0 0
145  
146 # No user object, return a null ID
147 0 return 0;
148 }
149  
150 =head2 current_user
151  
152 Every class in a Jifty application has a L</current_user> method that
153 returns the user who's doing things, in the form of a
154 L<Jifty::CurrentUser> object a subclass thereof. For the somewhat
155 obvious reason that you can't actually lift yourself up by tugging on
156 your own bootstraps, a L<Jifty::CurrentUser> object return I<itself>
157 rather than another C<Jifty::CurrentUser> object.
158  
159 =cut
160  
161 0 ▶sub current_user {
Subroutine current_user: not called CC=1 SCAR=6.9
Pod documented
162 0 my $self = shift;
163 0 return $self;
164 }
165  
166 =head1 AUTHENTICATION AND AUTHORIZATION
167  
168 To use Jifty's built-in authentication and authorization system, your
169 user objects need to implement the following API methods:
170  
171 =head2 password_is STRING
172  
173 Your L<user_object> should have a method called C<password_is> which
174 returns true if passed a string that matches the user's current
175 password.
176  
177 =cut
178  
179 0 ▶sub password_is {
Subroutine password_is: not called CC=2 SCAR=17.9
Pod documented
180 0 my $self = shift;
181 0 ▶ return undef unless ($self->user_object);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user_object 0 0 0
182 0 return($self->user_object->password_is(@_));
183  
184 }
185  
186 =head2 username
187  
188 Return a string which identifies the user in some way.
189  
190 =cut
191  
192 0 ▶sub username {
Subroutine username: not called CC=2 SCAR=17.9
Pod documented
193 0 my $self = shift;
194 0 ▶ return undef unless ($self->user_object);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user_object 0 0 0
195 0 return($self->user_object->brief_description(@_));
196 }
197  
198 =head2 auth_token
199  
200 Return a string which proves that the user is who they claim to be. A
201 simple way to do this, for example, would be to hash the username and
202 some server-side secret.
203  
204 =cut
205  
206 0 ▶sub auth_token {
Subroutine auth_token: not called CC=2 SCAR=17.9
Pod documented
207 0 my $self = shift;
208 0 ▶ return undef unless ($self->user_object);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user_object 0 0 0
209 0 return ($self->user_object->auth_token);
210  
211 }
212  
213 =head1 RIGHTS AND ACCESS CONTROL
214  
215 In any system that relies on users' rights to perform actions, it's
216 sometimes necessary to walk around the access control system. There
217 are two primary cases for this:
218  
219 =cut
220  
221 =head2 is_superuser
222  
223 Sometimes, while the system is running, you need to do something on
224 behalf of a user that they shouldn't be able to do themselves. Maybe
225 you need to let a new user sign up for your service (You don't want to
226 let any user create more users, right?) or to write an entry to a
227 changelog. If the user has the C<is_superuser> flag set, things still
228 get read from the database, but the user can walk around any and all
229 ACL checks. Think "Neo" from the Matrix. The superuser can walk
230 through walls, stop bullets and so on.
231  
232  
233 =cut
234  
235 =head2 is_bootstrap_user
236  
237 When your system is first getting going, you can't assume
238 B<anything>. There probably aren't any rights in the system to
239 check. A user with the L</is_bootstrap_user> flag set is a
240 self-reliant superuser. Nothing is read from the database, no ACLs are
241 checked. You probably never need to do anything with bootstrap users.
242  
243 =cut
244  
245 =head2 current_user_can ACTION
246  
247 For a current user object, the current user can always C<read>, but
248 never write or do anything else.
249  
250 =cut
251  
252 # XXX Is this actually used?
253 0 ▶sub current_user_can {
Subroutine current_user_can: not called CC=2 SCAR=17.9
Pod documented
254 0 my $self = shift;
255 0 my $action = shift;
256 0 ▶ return (1) if $action eq 'read';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "read" 0 0 0
257 0 return (0);
258 }
259  
260 =head2 jifty_serialize_format
261  
262 Serializes as the user_object.
263  
264 =cut
265  
266 0 ▶sub jifty_serialize_format {
Subroutine jifty_serialize_format: not called CC=2 SCAR=17.9
Pod documented
267 0 my $self = shift;
268 0 ▶ return {} if !$self->user_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user_object 0 0 0
269 0 return $self->user_object->jifty_serialize_format(@_);
270 }
271  
272 =head1 SEE ALSO
273  
274 L<Jifty::Object>, L<Jifty::Plugin::User>
275  
276 =head1 LICENSE
277  
278 Jifty is Copyright 2005-2010 Best Practical Solutions, LLC.
279 Jifty is distributed under the same terms as Perl itself.
280  
281 =cut
282  
283 1 1;
« Jifty/Plugin/PubSub/Connection.pm ↑ Summary Jifty/CAS/Store/LocalFile.pm »