/[SQLSession]/trunk/lib/SQLSession/CurrentUser.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/SQLSession/CurrentUser.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 58 - (show annotations)
Thu Jan 25 13:08:06 2007 UTC (17 years, 3 months ago) by dpavlin
File size: 1331 byte(s)
make current user superuser and create docs+admin navigation for it if (s)he has
admin role. Interesting example of overriding Jifty templates
1 use strict;
2 use warnings;
3
4 package SQLSession::CurrentUser;
5 use base qw/Jifty::CurrentUser/;
6
7 =head2 _init
8
9 This function will load user data from database and turn on admin mode in
10 Jifty if user has C<admin> role.
11
12 =cut
13
14 sub _init {
15 my $self = shift;
16 my %args = (@_);
17
18 warn "_init";
19
20 if ( delete $args{'_bootstrap'} ) {
21 $self->is_bootstrap_user(1);
22 } elsif( keys %args ) {
23 $self->user_object( SQLSession::Model::User->new( current_user => $self ) );
24 $self->user_object->load_by_cols( %args );
25
26 }
27
28 $self->SUPER::_init(%args);
29
30 if ( $self->current_user->id ) {
31 my $r = SQLSession::Model::Role->new();
32 $r->load_by_cols(
33 user_id => $self->current_user->id,
34 role => 'admin',
35 );
36 if ($r->id) {
37 warn "turn on AdminMode for ", $self->current_user->user_object->email, $/;
38 $self->is_superuser(1);
39 }
40 }
41 }
42
43 =head2 role
44
45 Test if user has role
46
47 Jifty->web->current_user->role( 'edit' );
48
49 =cut
50
51 sub role {
52 my $self = shift;
53 my $role = shift;
54
55 return 0 unless ($self->current_user->id);
56
57 return 1 if ($role eq 'admin' && $self->current_user->is_superuser);
58
59 my $r = SQLSession::Model::Role->new();
60 $r->load_by_cols(
61 user_id => $self->current_user->id,
62 role => $role
63 );
64
65 warn "role is '$role' for ", $self->current_user->user_object->email, " ",
66 $r->id ? 'OK' : 'DENY', $/;
67
68 return $r->id;
69 }
70
71

  ViewVC Help
Powered by ViewVC 1.1.26