Authorization/Driver.pm

filter: Statementstmt 100% 40 / 40 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 8 / 8 totaltotal 100% 63 / 63 CC 4 SCAR 6.2
CC
4
Cov
100%
find_option
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.9
« Authorization/Driver/HTGroup.pm ↑ Summary Authorization/Driver/Dummy.pm »
1 package CGI::Application::Plugin::Authorization::Driver;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
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<CGI::Application::Plugin::Authorization>
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 26 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
49 26 my $class = shift;
50 26 my $self = {};
51 26 my $authz = shift;
52 26 my @options = @_;
53  
54 26 bless $self, $class;
55 26 $self->{authz} = $authz;
56 26 Scalar::Util::weaken($self->{authz}); # weaken circular reference
57 26 $self->{options} = \@options;
58 26 $self->initialize;
59 26 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 26 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
70 26 my $self = shift;
71 # override this in the subclass if you need it
72 26 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 ▶sub options { return (@{$_[0]->{options}}) }
Subroutine options: called CC=1 SCAR=0.0
Pod documented
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 3 ▶sub find_option {
Subroutine find_option: called CC=4 SCAR=13.9
Pod documented
93 3 my $self = shift;
94 3 my $key = shift;
95 3 my @options = $self->options;
96 3 my $marker = 0;
97 3 foreach my $option (@options) {
98 10 ▶ if ($marker) {
Branch: 2 branches
BranchTrueFalseTotal
if ($marker) { } 2 8 10
elsif ($option eq $key) { } 2 6 8
99 2 return $option;
100 } elsif ($option eq $key) {
101 # We need the next element
102 2 $marker = 1;
103 }
104 }
105 1 return;
106 }
107  
108 =head2 authz
109  
110 This will return the underlying L<CGI::Application::Plugin::Authorization> object. In most cases it will
111 not be necesary to access this.
112  
113 =cut
114  
115 36 ▶sub authz { return $_[0]->{authz} }
Subroutine authz: called CC=1 SCAR=0.0
Pod documented
116  
117 =head2 username
118  
119 This will return the name of the current logged in user by calling the
120 C<username> method documented in L<CGI::Application::Plugin::Authorization>.
121  
122 =cut
123  
124 36 ▶sub username {
Subroutine username: called CC=1 SCAR=0.0
Pod documented
125 36 my $self = shift;
126  
127 36 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<username>)
136 passes the authorization checks based on the given parameters, usually a list of groups.
137  
138 =cut
139  
140 36 ▶sub authorize {
Subroutine authorize: called CC=1 SCAR=0.0
Pod documented
141 36 my $self = shift;
142 36 my @groups = @_;
143 36 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 1 ▶sub authorize_user {
Subroutine authorize_user: called CC=1 SCAR=0.0
Pod documented
156 1 die "authorize_user must be implemented in the subclass";
157 }
158  
159  
160 =head1 SEE ALSO
161  
162 L<CGI::Application::Plugin::Authorization>, perl(1)
163  
164  
165 =head1 AUTHOR
166  
167 Cees Hek <ceeshek@gmail.com>
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 8 1;
« Authorization/Driver/HTGroup.pm ↑ Summary Authorization/Driver/Dummy.pm »