/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 45 by dpavlin, Sun Jan 21 12:19:58 2007 UTC revision 69 by dpavlin, Thu Jan 25 20:42:11 2007 UTC
# Line 4  use warnings; Line 4  use warnings;
4  package SQLSession::CurrentUser;  package SQLSession::CurrentUser;
5  use base qw/Jifty::CurrentUser/;  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 {  sub _init {
15          my $self = shift;          my $self = shift;
16          my %args = (@_);          my %args = (@_);
# Line 15  sub _init { Line 22  sub _init {
22          } elsif( keys %args ) {          } elsif( keys %args ) {
23                  $self->user_object( SQLSession::Model::User->new( current_user => $self ) );                  $self->user_object( SQLSession::Model::User->new( current_user => $self ) );
24                  $self->user_object->load_by_cols( %args );                  $self->user_object->load_by_cols( %args );
25    
26          }          }
27    
28          $self->SUPER::_init(%args);          $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            # honor AdminMode from config.yml
43            $self->is_superuser(1) if Jifty->config->framework('AdminMode');
44  }  }
45    
46    =head2 role
47    
48    Test if user has role
49    
50      Jifty->web->current_user->role( 'edit' );
51    
52    =cut
53    
54  sub role {  sub role {
55          my $self = shift;          my $self = shift;
56          my $role = shift;          my $role = shift;
57    
58          return 0 unless ($self->current_user->id);          return 0 unless ($self->current_user->id);
59    
60          warn "role is '$role' for ", $self->current_user->user_object->email, "?";          return 1 if ($role eq 'admin' && $self->current_user->is_superuser);
61    
62          return 1 if ($self->current_user->user_object->email eq 'dpavlin@rot13.org');          return 1 if ($role eq 'edit' && $self->current_user->user_object->email_confirmed);
63    
64          return 0;          my $r = SQLSession::Model::Role->new();
65  }          $r->load_by_cols(
66                    user_id => $self->current_user->id,
67                    role => $role
68            );
69    
70            warn "role is '$role' for ", $self->current_user->user_object->email, " ",
71                    $r->id ? 'OK' : 'DENY', $/;
72    
73            return $r->id;
74    }
75    
76    1;

Legend:
Removed from v.45  
changed lines
  Added in v.69

  ViewVC Help
Powered by ViewVC 1.1.26