File Coverage

blib/lib/CGI/Builder/Auth/Group.pm
Criterion Covered Total %
statement 55 56 98.2
branch 15 22 68.1
condition 7 12 58.3
subroutine 18 19 94.7
pod 6 7 85.7
total 101 116 87.0


line stmt bran cond sub pod time code
1             package CGI::Builder::Auth::Group
2             ; use strict
3              
4 2     2   20478 ; our $VERSION = '0.05'
  2         6  
  2         102  
5              
6             ; our $_group_admin;
7              
8             ; use CGI::Builder::Auth::GroupAdmin
9 2     2   1279 ; use CGI::Builder::Auth::User
  2         5  
  2         21  
10 2     2   553 ; use Class::constr
  2         5  
  2         89  
11 2         21 ( { name => 'load', init => '_init', copy => 1 }
12             , { name => 'new', init => '_factory' }
13             )
14              
15 2     2   10 ; use Class::groups
  2         3  
16 2         20 ( { name => 'config'
17             , default =>
18             { DBType => 'Text' # type of database, one of 'DBM', 'Text', or 'SQL'
19             , DB => '.htgroup' # database name
20             # , Server => 'apache'
21             # , Locking => 1
22             # , Path => '.' # Path does not seem to work as documented -VV
23             , Debug => 0
24             # read, write and create flags. There are four modes: rwc - the default,
25             # open for reading, writing and creating. rw - open for reading and
26             # writing. r - open for reading only. w - open for writing only.
27             # , Flags => 'rwc'
28              
29             # FOR DBI
30             # , Host => 'localhost'
31             # , Port => ???
32             # , User => ''
33             # , Auth => ''
34             # , Driver => 'SQLite'
35             # , GroupTable => 'groups'
36             # , NameField => 'user_id'
37             # , GroupField => 'group_id'
38            
39             # FOR DBM Files
40             # , DBMF => 'NDBM'
41             # , Mode => 0644
42             }
43             }
44             )
45 2     2   169 ; use Class::props
  2         3  
46             ( { name => '_group_admin'
47 17         1367 , default => sub { CGI::Builder::Auth::GroupAdmin->new(%{$_[0]->config}) }
  17         66  
48             }
49 2         29 , { name => 'realm'
50             , default => 'main'
51             }
52             )
53 2     2   238 ; use Object::props
  2         3  
54 2         16 ( { name => 'id'
55             }
56             )
57              
58 2     2   149 ; use overload
  2         4  
59 2         12 ( '""' => 'as_string'
60             , fallback => 1
61             )
62              
63 2     2 0 129 ; sub as_string { $_[0]->id }
  2     21   5  
  21         2168  
64              
65             #---------------------------------------------------------------------
66             # Initializers
67             #---------------------------------------------------------------------
68              
69             # Cancel construction if requested group does not exist
70 16 100   16   206 ; sub _init { $_[0] = undef unless $_[0]->_exists }
71              
72             # When constructing a factory, id must be undef
73 0     0   0 ; sub _factory { $_[0]->id(undef) }
74              
75             #---------------------------------------------------------------------
76             # Factory Methods
77             #---------------------------------------------------------------------
78 7     7 1 733 ; sub list { $_[0]->_group_admin->list }
79              
80             ; sub add
81 4     4 1 1021 { my ($self, $data) = @_
82 4 50       20 ; my $group = ref $data ? $data->{group} : $data;
83            
84 4 100       12 ; return if $self->_exists($group);
85              
86 3 50       15 ; $self->_group_admin->create($group) or warn "Creation Failed"
87 3         14 ; return $self->load(id => $group)
88             }
89            
90             #---------------------------------------------------------------------
91             # Instance Methods
92             #---------------------------------------------------------------------
93             ; sub _exists
94 35 100   35   151 { defined $_[1]
95             ? $_[0]->_group_admin->exists($_[1])
96             : $_[0]->_group_admin->exists($_[0]->id)
97             }
98             ; sub delete
99 3 50   3 1 33 { defined $_[1]
100             ? $_[0]->_group_admin->remove($_[1])
101             : $_[0]->_group_admin->remove($_[0]->id)
102             }
103              
104             #
105             # FIXME add_member & remove_member appear to succeed when !exists user
106             #
107             ; sub add_member
108 4     4 1 10 { my ($self, @users) = @_
109 4   66     16 ; my $group = $self->id || shift(@users);
110            
111 4 50       70 ; return if !$self->_exists($group)
112              
113 4         33 ; my $user_factory = CGI::Builder::Auth::User->new
114 4         160 ; for my $user (@users) {
115 4 50       21 next unless $user_factory->_exists($user)
116 4         17 ; $self->_group_admin->add($user, $group)
117             }
118 4         17 ; 1
119             }
120             ; sub remove_member
121 4     4 1 371 { my ($self, @users) = @_
122 4   66     13 ; my $group = $self->id || shift(@users)
123            
124 4 50       53 ; return if !$self->_exists($group)
125            
126 4         14 ; for my $user (@users)
127 4         12 { $self->_group_admin->delete($user, $group)
128             }
129 4         16 ; 1
130             }
131             ; sub member_list
132 7     7 1 16 { my ($self, $group) = @_
133 7   33     32 ; $group = $group || $self->id
134            
135 7 50       218 ; return if !$self->_exists($group)
136            
137 7         28 ; $self->_group_admin->list($group)
138             }
139              
140             ; sub DESTROY
141 17 100 66 17   975 { ref($_group_admin)
142             and !Scalar::Util::isweak($_group_admin)
143             and Scalar::Util::weaken($_group_admin)
144             }
145            
146              
147             =head1 NAME
148              
149             CGI::Builder::Auth::Group - Provide access to a group table and its rows
150              
151             =head1 DESCRIPTION
152              
153             This Class provides an API for manipulating a Group table. The implementation
154             stores the table in a text file, but developers are free to create their own
155             implementations of this API that wrap SQL databases or other resources.
156              
157             Developers using the library probably will not need to manipulate the user
158             objects directly, since the L
159             provides a wrapper around all the common functions. However, developers
160             creating their own user classes need to pay special attention to implementing
161             this API correctly.
162              
163             This document describes the default implementation, and includes many notes
164             about mandatory and optional features for alternate implementations.
165              
166             WARNING: This interface is experimental. Developers may create their own
167             implementations, but are advised to subscribe to the mailing list to be
168             notified of changes. Backward compatibility is a goal, but is not guaranteed
169             for future releases.
170              
171              
172             =head1 SPECIAL PROPERTIES
173              
174             The group object C's the string operator so that it prints the group name
175             in string context rather than the usual reference information. As a result, you
176             may use the group object in your code as if it were a (read-only) scalar
177             containing the group name.
178              
179             This is required behavior for all implementations. See L for details.
180              
181              
182             =head1 CONSTRUCTORS
183              
184              
185             =head2 C $id)>
186              
187             Class method, takes a hash where the key is 'id' (literal) and the value is the
188             group name you wish to load.
189              
190             Return a group object with the group name of C<$id>. Return C if the group
191             does not exist in the database.
192              
193             Note that the group name is required to be unique in a given table.
194              
195              
196             =head2 C
197              
198             Add a group to the group table.
199              
200             Class method, takes a scalar that is either the name of the group to add, or a
201             reference to a hash of group attributes. Attributes supported in this
202             implementation:
203              
204             =over
205              
206             =item B
207              
208             =back
209              
210             All implementations are required to support the C attribute, and may
211             support as many more as they like. Note that the group name is required to be
212             unique in a given table.
213              
214             Return the group object on success, undef on failure.
215              
216              
217             =head1 OTHER CLASS METHODS
218              
219              
220             =head2 C
221              
222             Class method, takes one or two scalar arguments.
223              
224             Store and retrieve configuration options. With one argument C<$opt>, returns
225             the value of the config option. With two arguments, stores C<$new_val> as the
226             new value for config option C<$opt>. Returns C if the option is unset.
227              
228              
229             =head2 C
230              
231             Class method, takes no arguments.
232              
233             Return an array of all groups (as objects) in the group table, or C on
234             error.
235              
236              
237              
238             =head1 INSTANCE (OBJECT) METHODS
239              
240              
241             =head2 C
242              
243             Instance method, takes a list of @users arguments. The users may be user
244             objects, usernames, or a combination of the two.
245              
246             Create a relationship between the group and user such that the user is added to
247             the C.
248              
249             Return void (currently always returns true).
250              
251              
252             =head2 C
253              
254             Instance method, takes no arguments.
255              
256             Delete the group from the group table. After a call to this method, the object
257             should be considered unusable.
258              
259              
260             =head2 C
261              
262             Instance method, takes no arguments.
263              
264             Return a list of usernames (NOT user objects) who are members of this group.
265             Implementations may return a list of user objects as long as they have
266             implemented the overload behavior described above.
267              
268             Future releases may require this method to return a list of objects. Alternate
269             implementations are encouraged to return objects.
270              
271              
272             =head2 C
273              
274             Instance method, takes a list of @users arguments. The users may be user
275             objects, usernames, or a combination of the two.
276              
277             Remove a relationship between the group and user such that the user is no
278             longer returned in the C.
279              
280             Return void (currently always returns true).
281              
282              
283             =head1 SUPPORT
284              
285             Support for this module and all the modules of the CBF is via the mailing list.
286             The list is used for general support on the use of the CBF, announcements, bug
287             reports, patches, suggestions for improvements or new features. The API to the
288             CBF is stable, but if you use the CBF in a production environment, it's
289             probably a good idea to keep a watch on the list.
290              
291             You can join the CBF mailing list at this url:
292              
293             L
294              
295              
296             =head1 AUTHOR
297              
298             Vincent Veselosky
299              
300              
301             =head1 COPYRIGHT AND LICENSE
302              
303             Copyright 2004 by Vincent Veselosky
304              
305             This library is free software; you can redistribute it and/or modify
306             it under the same terms as Perl itself.
307              
308              
309              
310             =cut
311              
312             "Copyright 2004 Vincent Veselosky [[http://control-escape.com]]";
313             # vim:ft=perl:expandtab:ts=3:sw=3: