1 package FS::access_user;
4 use vars qw( @ISA $DEBUG $me $conf $htpasswd_file );
7 use FS::Record qw( qsearch qsearchs dbh );
10 use FS::access_user_pref;
11 use FS::access_usergroup;
15 @ISA = qw( FS::m2m_Common FS::option_Common FS::Record );
16 #@ISA = qw( FS::m2m_Common FS::option_Common );
19 $me = '[FS::access_user]';
21 #kludge htpasswd for now (i hope this bootstraps okay)
22 FS::UID->install_callback( sub {
24 $htpasswd_file = $conf->base_dir. '/htpasswd';
29 FS::access_user - Object methods for access_user records
35 $record = new FS::access_user \%hash;
36 $record = new FS::access_user { 'column' => 'value' };
38 $error = $record->insert;
40 $error = $new_record->replace($old_record);
42 $error = $record->delete;
44 $error = $record->check;
48 An FS::access_user object represents an internal access user. FS::access_user
49 inherits from FS::Record. The following fields are currently supported:
53 =item usernum - primary key
63 =item disabled - empty or 'Y'
73 Creates a new internal access user. To add the user to the database, see L<"insert">.
75 Note that this stores the hash reference, not a distinct copy of the hash it
76 points to. You can ask the object for a copy with the I<hash> method.
80 # the new method can be inherited from FS::Record, if a table method is defined
82 sub table { 'access_user'; }
84 sub _option_table { 'access_user_pref'; }
85 sub _option_namecol { 'prefname'; }
86 sub _option_valuecol { 'prefvalue'; }
90 Adds this record to the database. If there is an error, returns the error,
91 otherwise returns false.
98 my $error = $self->check;
99 return $error if $error;
101 local $SIG{HUP} = 'IGNORE';
102 local $SIG{INT} = 'IGNORE';
103 local $SIG{QUIT} = 'IGNORE';
104 local $SIG{TERM} = 'IGNORE';
105 local $SIG{TSTP} = 'IGNORE';
106 local $SIG{PIPE} = 'IGNORE';
108 my $oldAutoCommit = $FS::UID::AutoCommit;
109 local $FS::UID::AutoCommit = 0;
112 $error = $self->htpasswd_kludge();
114 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
118 $error = $self->SUPER::insert(@_);
121 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
123 #make sure it isn't a dup username? or you could nuke people's passwords
124 #blah. really just should do our own login w/cookies
125 #and auth out of the db in the first place
126 #my $hterror = $self->htpasswd_kludge('-D');
127 #$error .= " - additionally received error cleaning up htpasswd file: $hterror"
131 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
137 sub htpasswd_kludge {
140 #awful kludge to skip setting htpasswd for fs_* users
141 return '' if $self->username =~ /^fs_/;
143 unshift @_, '-c' unless -e $htpasswd_file;
145 system('htpasswd', '-b', @_,
154 return 'htpasswd exited unsucessfully';
160 Delete this record from the database.
167 local $SIG{HUP} = 'IGNORE';
168 local $SIG{INT} = 'IGNORE';
169 local $SIG{QUIT} = 'IGNORE';
170 local $SIG{TERM} = 'IGNORE';
171 local $SIG{TSTP} = 'IGNORE';
172 local $SIG{PIPE} = 'IGNORE';
174 my $oldAutoCommit = $FS::UID::AutoCommit;
175 local $FS::UID::AutoCommit = 0;
179 $self->SUPER::delete(@_)
180 || $self->htpasswd_kludge('-D')
184 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
187 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
193 =item replace OLD_RECORD
195 Replaces the OLD_RECORD with this one in the database. If there is an error,
196 returns the error, otherwise returns false.
203 my $old = ( ref($_[0]) eq ref($new) )
207 local $SIG{HUP} = 'IGNORE';
208 local $SIG{INT} = 'IGNORE';
209 local $SIG{QUIT} = 'IGNORE';
210 local $SIG{TERM} = 'IGNORE';
211 local $SIG{TSTP} = 'IGNORE';
212 local $SIG{PIPE} = 'IGNORE';
214 my $oldAutoCommit = $FS::UID::AutoCommit;
215 local $FS::UID::AutoCommit = 0;
218 if ( $new->_password ne $old->_password ) {
219 my $error = $new->htpasswd_kludge();
221 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
224 } elsif ( $old->disabled && !$new->disabled
225 && $new->_password =~ /changeme/i ) {
226 return "Must change password when enabling this account";
229 my $error = $new->SUPER::replace($old, @_);
232 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
235 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
243 Checks all fields to make sure this is a valid internal access user. If there is
244 an error, returns the error, otherwise returns false. Called by the insert
249 # the check method should currently be supplied - FS::Record contains some
250 # data checking routines
256 $self->ut_numbern('usernum')
257 || $self->ut_alpha_lower('username')
258 || $self->ut_text('_password')
259 || $self->ut_text('last')
260 || $self->ut_text('first')
261 || $self->ut_foreign_keyn('user_custnum', 'cust_main', 'custnum')
262 || $self->ut_enum('disabled', [ '', 'Y' ] )
264 return $error if $error;
271 Returns a name string for this user: "Last, First".
277 $self->get('last'). ', '. $self->first;
282 Returns the FS::cust_main object (see L<FS::cust_main>), if any, for this
289 qsearchs( 'cust_main', { 'custnum' => $self->user_custnum } );
292 =item access_usergroup
294 Returns links to the the groups this user is a part of, as FS::access_usergroup
295 objects (see L<FS::access_usergroup>).
299 sub access_usergroup {
301 qsearch( 'access_usergroup', { 'usernum' => $self->usernum } );
312 #=item access_groupnames
316 #sub access_groupnames {
322 Returns a list of agentnums this user can view (via group membership).
328 my $sth = dbh->prepare(
329 "SELECT DISTINCT agentnum FROM access_usergroup
330 JOIN access_groupagent USING ( groupnum )
332 ) or die dbh->errstr;
333 $sth->execute($self->usernum) or die $sth->errstr;
334 map { $_->[0] } @{ $sth->fetchall_arrayref };
339 Returns a hashref of agentnums this user can view.
345 scalar( { map { $_ => 1 } $self->agentnums } );
348 =item agentnums_sql [ HASHREF | OPTION => VALUE ... ]
350 Returns an sql fragement to select only agentnums this user can view.
352 Options are passed as a hashref or a list. Available options are:
358 The frament will also allow the selection of null agentnums.
362 The fragment will also allow the selection of null agentnums if the current
363 user has the provided access right
367 Optional table name in which agentnum is being checked. Sometimes required to
368 resolve 'column reference "agentnum" is ambiguous' errors.
376 my %opt = ref($_[0]) ? %{$_[0]} : @_;
378 my $agentnum = $opt{'table'} ? $opt{'table'}.'.agentnum' : 'agentnum';
380 # my @agentnums = map { "$agentnum = $_" } $self->agentnums;
382 push @agentnums, "$agentnum IN (". join(',', $self->agentnums). ')';
384 push @agentnums, "$agentnum IS NULL"
386 || ( $opt{'null_right'} && $self->access_right($opt{'null_right'}) );
388 return ' 1 = 0 ' unless scalar(@agentnums);
389 '( '. join( ' OR ', @agentnums ). ' )';
395 Returns true if the user can view the specified agent.
400 my( $self, $agentnum ) = @_;
401 my $sth = dbh->prepare(
402 "SELECT COUNT(*) FROM access_usergroup
403 JOIN access_groupagent USING ( groupnum )
404 WHERE usernum = ? AND agentnum = ?"
405 ) or die dbh->errstr;
406 $sth->execute($self->usernum, $agentnum) or die $sth->errstr;
407 $sth->fetchrow_arrayref->[0];
412 Returns the list of agents this user can view (via group membership), as
421 'hashref' => { disabled=>'' },
422 'extra_sql' => ' AND '. $self->agentnums_sql,
426 =item access_right RIGHTNAME | LISTREF
428 Given a right name or a list reference of right names, returns true if this
429 user has this right, or, for a list, one of the rights (currently via group
430 membership, eventually also via user overrides).
435 my( $self, $rightname ) = @_;
437 $rightname = [ $rightname ] unless ref($rightname);
439 warn "$me access_right called on ". join(', ', @$rightname). "\n"
442 #some caching of ACL requests for low-hanging fruit perf improvement
443 #since we get a new $CurrentUser object each page view there shouldn't be any
444 #issues with stickiness
445 if ( $self->{_ACLcache} ) {
447 unless ( grep !exists($self->{_ACLcache}{$_}), @$rightname ) {
448 warn "$me ACL cache hit for ". join(', ', @$rightname). "\n"
450 return grep $self->{_ACLcache}{$_}, @$rightname
453 warn "$me ACL cache miss for ". join(', ', @$rightname). "\n"
458 warn "initializing ACL cache\n"
460 $self->{_ACLcache} = {};
464 my $has_right = ' rightname IN ('. join(',', map '?', @$rightname ). ') ';
466 my $sth = dbh->prepare("
467 SELECT groupnum FROM access_usergroup
468 LEFT JOIN access_group USING ( groupnum )
469 LEFT JOIN access_right
470 ON ( access_group.groupnum = access_right.rightobjnum )
472 AND righttype = 'FS::access_group'
475 ") or die dbh->errstr;
476 $sth->execute($self->usernum, @$rightname) or die $sth->errstr;
477 my $row = $sth->fetchrow_arrayref;
479 my $return = $row ? $row->[0] : '';
481 #just caching the single-rightname hits should be enough of a win for now
482 if ( scalar(@$rightname) == 1 ) {
483 $self->{_ACLcache}{${$rightname}[0]} = $return;
490 =item default_customer_view
492 Returns the default customer view for this user, from the
493 "default_customer_view" user preference, the "cust_main-default_view" config,
494 or the hardcoded default, "jumbo" (may change to "basics" in the near future).
498 sub default_customer_view {
501 $self->option('default_customer_view')
502 || $conf->config('cust_main-default_view')
503 || 'jumbo'; #'basics' in 1.9.1?
513 L<FS::Record>, schema.html from the base documentation.