add internal user disable-ing
[freeside.git] / FS / FS / access_user.pm
1 package FS::access_user;
2
3 use strict;
4 use vars qw( @ISA $htpasswd_file );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::m2m_Common;
7 use FS::access_usergroup;
8
9 @ISA = qw( FS::m2m_Common FS::Record );
10
11 #kludge htpasswd for now
12 $htpasswd_file = '/usr/local/etc/freeside/htpasswd';
13
14 =head1 NAME
15
16 FS::access_user - Object methods for access_user records
17
18 =head1 SYNOPSIS
19
20   use FS::access_user;
21
22   $record = new FS::access_user \%hash;
23   $record = new FS::access_user { 'column' => 'value' };
24
25   $error = $record->insert;
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33 =head1 DESCRIPTION
34
35 An FS::access_user object represents an internal access user.  FS::access_user inherits from
36 FS::Record.  The following fields are currently supported:
37
38 =over 4
39
40 =item usernum - primary key
41
42 =item username - 
43
44 =item _password - 
45
46 =item last -
47
48 =item first -
49
50 =item disabled - empty or 'Y'
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new internal access user.  To add the user to the database, see L<"insert">.
61
62 Note that this stores the hash reference, not a distinct copy of the hash it
63 points to.  You can ask the object for a copy with the I<hash> method.
64
65 =cut
66
67 # the new method can be inherited from FS::Record, if a table method is defined
68
69 sub table { 'access_user'; }
70
71 =item insert
72
73 Adds this record to the database.  If there is an error, returns the error,
74 otherwise returns false.
75
76 =cut
77
78 sub insert {
79   my $self = shift;
80
81   local $SIG{HUP} = 'IGNORE';
82   local $SIG{INT} = 'IGNORE';
83   local $SIG{QUIT} = 'IGNORE';
84   local $SIG{TERM} = 'IGNORE';
85   local $SIG{TSTP} = 'IGNORE';
86   local $SIG{PIPE} = 'IGNORE';
87
88   my $oldAutoCommit = $FS::UID::AutoCommit;
89   local $FS::UID::AutoCommit = 0;
90   my $dbh = dbh;
91
92   my $error =
93        $self->SUPER::insert(@_)
94     || $self->htpasswd_kludge()
95   ;
96
97   if ( $error ) {
98     $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
99     return $error;
100   } else {
101     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
102     '';
103   }
104
105 }
106
107 sub htpasswd_kludge {
108   my $self = shift;
109   unshift @_, '-c' unless -e $htpasswd_file;
110   if ( 
111        system('htpasswd', '-b', @_,
112                           $htpasswd_file,
113                           $self->username,
114                           $self->_password,
115              ) == 0
116      )
117   {
118     return '';
119   } else {
120     return 'htpasswd exited unsucessfully';
121   }
122 }
123
124
125 =item delete
126
127 Delete this record from the database.
128
129 =cut
130
131 sub delete {
132   my $self = shift;
133
134   local $SIG{HUP} = 'IGNORE';
135   local $SIG{INT} = 'IGNORE';
136   local $SIG{QUIT} = 'IGNORE';
137   local $SIG{TERM} = 'IGNORE';
138   local $SIG{TSTP} = 'IGNORE';
139   local $SIG{PIPE} = 'IGNORE';
140
141   my $oldAutoCommit = $FS::UID::AutoCommit;
142   local $FS::UID::AutoCommit = 0;
143   my $dbh = dbh;
144
145   my $error =
146        $self->SUPER::delete(@_)
147     || $self->htpasswd_kludge('-D')
148   ;
149
150   if ( $error ) {
151     $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
152     return $error;
153   } else {
154     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
155     '';
156   }
157
158 }
159
160 =item replace OLD_RECORD
161
162 Replaces the OLD_RECORD with this one in the database.  If there is an error,
163 returns the error, otherwise returns false.
164
165 =cut
166
167 sub replace {
168   my($new, $old) = ( shift, shift );
169
170   local $SIG{HUP} = 'IGNORE';
171   local $SIG{INT} = 'IGNORE';
172   local $SIG{QUIT} = 'IGNORE';
173   local $SIG{TERM} = 'IGNORE';
174   local $SIG{TSTP} = 'IGNORE';
175   local $SIG{PIPE} = 'IGNORE';
176
177   my $oldAutoCommit = $FS::UID::AutoCommit;
178   local $FS::UID::AutoCommit = 0;
179   my $dbh = dbh;
180
181   my $error =
182        $new->SUPER::replace($old, @_)
183     || $new->htpasswd_kludge()
184   ;
185
186   if ( $error ) {
187     $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
188     return $error;
189   } else {
190     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
191     '';
192   }
193
194 }
195
196 =item check
197
198 Checks all fields to make sure this is a valid internal access user.  If there is
199 an error, returns the error, otherwise returns false.  Called by the insert
200 and replace methods.
201
202 =cut
203
204 # the check method should currently be supplied - FS::Record contains some
205 # data checking routines
206
207 sub check {
208   my $self = shift;
209
210   my $error = 
211     $self->ut_numbern('usernum')
212     || $self->ut_text('username')
213     || $self->ut_text('_password')
214     || $self->ut_text('last')
215     || $self->ut_text('first')
216     || $self->ut_enum('disabled', [ '', 'Y' ] )
217   ;
218   return $error if $error;
219
220   $self->SUPER::check;
221 }
222
223 =item name
224
225 Returns a name string for this user: "Last, First".
226
227 =cut
228
229 sub name {
230   my $self = shift;
231   $self->get('last'). ', '. $self->first;
232 }
233
234 =item access_usergroup
235
236 =cut
237
238 sub access_usergroup {
239   my $self = shift;
240   qsearch( 'access_usergroup', { 'usernum' => $self->usernum } );
241 }
242
243 #=item access_groups
244 #
245 #=cut
246 #
247 #sub access_groups {
248 #
249 #}
250 #
251 #=item access_groupnames
252 #
253 #=cut
254 #
255 #sub access_groupnames {
256 #
257 #}
258
259 =item agentnums 
260
261 Returns a list of agentnums this user can view (via group membership).
262
263 =cut
264
265 sub agentnums {
266   my $self = shift;
267   my $sth = dbh->prepare(
268     "SELECT DISTINCT agentnum FROM access_usergroup
269                               JOIN access_groupagent USING ( groupnum )
270        WHERE usernum = ?"
271   ) or die dbh->errstr;
272   $sth->execute($self->usernum) or die $sth->errstr;
273   map { $_->[0] } @{ $sth->fetchall_arrayref };
274 }
275
276 =item agentnums_href
277
278 Returns a hashref of agentnums this user can view.
279
280 =cut
281
282 sub agentnums_href {
283   my $self = shift;
284   { map { $_ => 1 } $self->agentnums };
285 }
286
287 =item agentnums_sql
288
289 Returns an sql fragement to select only agentnums this user can view.
290
291 =cut
292
293 sub agentnums_sql {
294   my $self = shift;
295   '( '.
296     join( ' OR ', map "agentnum = $_", $self->agentnums ).
297   ' )';
298 }
299
300 =item access_right
301
302 Given a right name, returns true if this user has this right (currently via
303 group membership, eventually also via user overrides).
304
305 =cut
306
307 sub access_right {
308   my( $self, $rightname ) = @_;
309   my $sth = dbh->prepare("
310     SELECT groupnum FROM access_usergroup
311                     LEFT JOIN access_group USING ( groupnum )
312                     LEFT JOIN access_right
313                          ON ( access_group.groupnum = access_right.rightobjnum )
314       WHERE usernum = ?
315         AND righttype = 'FS::access_group'
316         AND rightname = ?
317   ") or die dbh->errstr;
318   $sth->execute($self->usernum, $rightname) or die $sth->errstr;
319   my $row = $sth->fetchrow_arrayref;
320   $row ? $row->[0] : '';
321 }
322
323 =back
324
325 =head1 BUGS
326
327 =head1 SEE ALSO
328
329 L<FS::Record>, schema.html from the base documentation.
330
331 =cut
332
333 1;
334