File Coverage

blib/lib/CGI/Application/Plugin/Authorization/Driver.pm
Criterion Covered Total %
statement 39 39 100.0
branch 4 4 100.0
condition n/a
subroutine 11 11 100.0
pod 8 8 100.0
total 62 62 100.0


line stmt bran cond sub pod time code
1             package CGI::Application::Plugin::Authorization::Driver;
2              
3 8     8   5357 use strict;
  8         15  
  8         282  
4 8     8   49 use warnings;
  8         15  
  8         218  
5              
6 8     8   42 use UNIVERSAL::require;
  8         16  
  8         85  
7              
8             =head1 NAME
9              
10             CGI::Application::Plugin::Authorization::Driver - Base module for building driver classes
11             for CGI::Application::Plugin::Authorization
12              
13              
14             =head1 SYNOPSIS
15              
16             package CGI::Application::Plugin::Authorization::Driver::MyDriver;
17             use base qw(CGI::Application::Plugin::Authorization::Driver);
18              
19             sub authorize_user {
20             my $self = shift;
21             my @params = @_;
22              
23             if ( >>> Valid Access Permissions <<< ) {
24             return 1;
25             }
26             return;
27             }
28              
29              
30             =head1 DESCRIPTION
31              
32             This module is a base class for all driver classes for the L
33             plugin. Each driver class is required to provide only one method to authorize the given parameters.
34             Often this will be a list of groups that the user needs to be a part of, although it could be anything.
35              
36              
37             =head1 METHODS
38              
39             =head2 new
40              
41             This is a constructor that can create a new Driver object. It requires an Authorization object as its
42             first parameter, and any number of other parameters that will be used as options depending on which
43             Driver object is being created. You shouldn't need to call this as the Authorization plugin takes care
44             of creating Driver objects.
45              
46             =cut
47              
48             sub new {
49 26     26 1 48 my $class = shift;
50 26         46 my $self = {};
51 26         48 my $authz = shift;
52 26         56 my @options = @_;
53              
54 26         84 bless $self, $class;
55 26         101 $self->{authz} = $authz;
56 26         93 Scalar::Util::weaken($self->{authz}); # weaken circular reference
57 26         51 $self->{options} = \@options;
58 26         106 $self->initialize;
59 26         116 return $self;
60             }
61              
62             =head2 initialize
63              
64             This method will be called right after a new Driver object is created. So any startup customizations
65             can be dealt with here.
66              
67             =cut
68              
69             sub initialize {
70 26     26 1 41 my $self = shift;
71             # override this in the subclass if you need it
72 26         45 return;
73             }
74              
75             =head2 options
76              
77             This will return a list of options that were provided when this driver was configured by the user.
78              
79             =cut
80              
81 34     34 1 41 sub options { return (@{$_[0]->{options}}) }
  34         123  
82              
83             =head2 find_option
84              
85             This method will search the Driver options for a specific key and return
86             the value it finds. This method assumes that the Driver configuration contains
87             a hash of information. If it does not, then you will have to parse the option
88             manually in the subclass.
89              
90             =cut
91              
92             sub find_option {
93 3     3 1 1357 my $self = shift;
94 3         5 my $key = shift;
95 3         13 my @options = $self->options;
96 3         5 my $marker = 0;
97 3         7 foreach my $option (@options) {
98 10 100       29 if ($marker) {
    100          
99 2         12 return $option;
100             } elsif ($option eq $key) {
101             # We need the next element
102 2         4 $marker = 1;
103             }
104             }
105 1         15 return;
106             }
107              
108             =head2 authz
109              
110             This will return the underlying L object. In most cases it will
111             not be necesary to access this.
112              
113             =cut
114              
115 36     36 1 155 sub authz { return $_[0]->{authz} }
116              
117             =head2 username
118              
119             This will return the name of the current logged in user by calling the
120             C method documented in L.
121              
122             =cut
123              
124             sub username {
125 36     36 1 50 my $self = shift;
126              
127 36         95 return $self->authz->username;
128             }
129              
130             =head2 authorize
131              
132             # User must be in the admin group to have access to this runmode
133             return $self->authz->forbidden unless $self->authz->authorize('admin');
134              
135             This method will verify that the currently logged in user (as found through L)
136             passes the authorization checks based on the given parameters, usually a list of groups.
137              
138             =cut
139              
140             sub authorize {
141 36     36 1 53 my $self = shift;
142 36         68 my @groups = @_;
143 36         106 return $self->authorize_user($self->username, @groups);
144             }
145              
146             =head2 authorize_user
147              
148             This method needs to be provided by the driver class. It needs to be an object method
149             that accepts a username, followed by a list of parameters, and will verify that the
150             user passes the authorization checks based on the given parameters. It should return
151             a true value if the checks succeed.
152              
153             =cut
154              
155             sub authorize_user {
156 1     1 1 63 die "authorize_user must be implemented in the subclass";
157             }
158              
159              
160             =head1 SEE ALSO
161              
162             L, perl(1)
163              
164              
165             =head1 AUTHOR
166              
167             Cees Hek
168              
169              
170             =head1 LICENCE AND COPYRIGHT
171              
172             Copyright (c) 2005, SiteSuite. All rights reserved.
173              
174             This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
175              
176              
177             =head1 DISCLAIMER OF WARRANTY
178              
179             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
180              
181             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
182              
183             =cut
184              
185             1;