agent-virtualize advertising sources
[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   
110   #awful kludge to skip setting htpasswd for fs_* users
111   return '' if $self->username =~ /^fs_/;
112
113   unshift @_, '-c' unless -e $htpasswd_file;
114   if ( 
115        system('htpasswd', '-b', @_,
116                           $htpasswd_file,
117                           $self->username,
118                           $self->_password,
119              ) == 0
120      )
121   {
122     return '';
123   } else {
124     return 'htpasswd exited unsucessfully';
125   }
126 }
127
128
129 =item delete
130
131 Delete this record from the database.
132
133 =cut
134
135 sub delete {
136   my $self = shift;
137
138   local $SIG{HUP} = 'IGNORE';
139   local $SIG{INT} = 'IGNORE';
140   local $SIG{QUIT} = 'IGNORE';
141   local $SIG{TERM} = 'IGNORE';
142   local $SIG{TSTP} = 'IGNORE';
143   local $SIG{PIPE} = 'IGNORE';
144
145   my $oldAutoCommit = $FS::UID::AutoCommit;
146   local $FS::UID::AutoCommit = 0;
147   my $dbh = dbh;
148
149   my $error =
150        $self->SUPER::delete(@_)
151     || $self->htpasswd_kludge('-D')
152   ;
153
154   if ( $error ) {
155     $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
156     return $error;
157   } else {
158     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
159     '';
160   }
161
162 }
163
164 =item replace OLD_RECORD
165
166 Replaces the OLD_RECORD with this one in the database.  If there is an error,
167 returns the error, otherwise returns false.
168
169 =cut
170
171 sub replace {
172   my($new, $old) = ( shift, shift );
173
174   local $SIG{HUP} = 'IGNORE';
175   local $SIG{INT} = 'IGNORE';
176   local $SIG{QUIT} = 'IGNORE';
177   local $SIG{TERM} = 'IGNORE';
178   local $SIG{TSTP} = 'IGNORE';
179   local $SIG{PIPE} = 'IGNORE';
180
181   my $oldAutoCommit = $FS::UID::AutoCommit;
182   local $FS::UID::AutoCommit = 0;
183   my $dbh = dbh;
184
185   my $error =
186        $new->SUPER::replace($old, @_)
187     || $new->htpasswd_kludge()
188   ;
189
190   if ( $error ) {
191     $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
192     return $error;
193   } else {
194     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
195     '';
196   }
197
198 }
199
200 =item check
201
202 Checks all fields to make sure this is a valid internal access user.  If there is
203 an error, returns the error, otherwise returns false.  Called by the insert
204 and replace methods.
205
206 =cut
207
208 # the check method should currently be supplied - FS::Record contains some
209 # data checking routines
210
211 sub check {
212   my $self = shift;
213
214   my $error = 
215     $self->ut_numbern('usernum')
216     || $self->ut_text('username')
217     || $self->ut_text('_password')
218     || $self->ut_text('last')
219     || $self->ut_text('first')
220     || $self->ut_enum('disabled', [ '', 'Y' ] )
221   ;
222   return $error if $error;
223
224   $self->SUPER::check;
225 }
226
227 =item name
228
229 Returns a name string for this user: "Last, First".
230
231 =cut
232
233 sub name {
234   my $self = shift;
235   $self->get('last'). ', '. $self->first;
236 }
237
238 =item access_usergroup
239
240 =cut
241
242 sub access_usergroup {
243   my $self = shift;
244   qsearch( 'access_usergroup', { 'usernum' => $self->usernum } );
245 }
246
247 #=item access_groups
248 #
249 #=cut
250 #
251 #sub access_groups {
252 #
253 #}
254 #
255 #=item access_groupnames
256 #
257 #=cut
258 #
259 #sub access_groupnames {
260 #
261 #}
262
263 =item agentnums 
264
265 Returns a list of agentnums this user can view (via group membership).
266
267 =cut
268
269 sub agentnums {
270   my $self = shift;
271   my $sth = dbh->prepare(
272     "SELECT DISTINCT agentnum FROM access_usergroup
273                               JOIN access_groupagent USING ( groupnum )
274        WHERE usernum = ?"
275   ) or die dbh->errstr;
276   $sth->execute($self->usernum) or die $sth->errstr;
277   map { $_->[0] } @{ $sth->fetchall_arrayref };
278 }
279
280 =item agentnums_href
281
282 Returns a hashref of agentnums this user can view.
283
284 =cut
285
286 sub agentnums_href {
287   my $self = shift;
288   { map { $_ => 1 } $self->agentnums };
289 }
290
291 =item agentnums_sql
292
293 Returns an sql fragement to select only agentnums this user can view.
294
295 =cut
296
297 sub agentnums_sql {
298   my $self = shift;
299   '( '.
300     join( ' OR ', map "agentnum = $_", $self->agentnums ).
301   ' )';
302 }
303
304 =item agentnum
305
306 Returns true if the user can view the specified agent.
307
308 =cut
309
310 sub agentnum {
311   my( $self, $agentnum ) = @_;
312   my $sth = dbh->prepare(
313     "SELECT COUNT(*) FROM access_usergroup
314                      JOIN access_groupagent USING ( groupnum )
315        WHERE usernum = ? AND agentnum = ?"
316   ) or die dbh->errstr;
317   $sth->execute($self->usernum, $agentnum) or die $sth->errstr;
318   $sth->fetchrow_arrayref->[0];
319 }
320
321
322 =item access_right
323
324 Given a right name, returns true if this user has this right (currently via
325 group membership, eventually also via user overrides).
326
327 =cut
328
329 sub access_right {
330   my( $self, $rightname ) = @_;
331   my $sth = dbh->prepare("
332     SELECT groupnum FROM access_usergroup
333                     LEFT JOIN access_group USING ( groupnum )
334                     LEFT JOIN access_right
335                          ON ( access_group.groupnum = access_right.rightobjnum )
336       WHERE usernum = ?
337         AND righttype = 'FS::access_group'
338         AND rightname = ?
339   ") or die dbh->errstr;
340   $sth->execute($self->usernum, $rightname) or die $sth->errstr;
341   my $row = $sth->fetchrow_arrayref;
342   $row ? $row->[0] : '';
343 }
344
345 =back
346
347 =head1 BUGS
348
349 =head1 SEE ALSO
350
351 L<FS::Record>, schema.html from the base documentation.
352
353 =cut
354
355 1;
356