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