1 package FS::access_user;
4 use base qw( FS::m2m_Common FS::option_Common );
5 use vars qw( $DEBUG $me $conf );
9 use FS::Record qw( qsearch qsearchs dbh );
10 use FS::access_user_pref;
11 use FS::access_usergroup;
16 $me = '[FS::access_user]';
20 FS::access_user - Object methods for access_user records
26 $record = new FS::access_user \%hash;
27 $record = new FS::access_user { 'column' => 'value' };
29 $error = $record->insert;
31 $error = $new_record->replace($old_record);
33 $error = $record->delete;
35 $error = $record->check;
39 An FS::access_user object represents an internal access user. FS::access_user
40 inherits from FS::Record. The following fields are currently supported:
44 =item usernum - primary key
54 =item disabled - empty or 'Y'
64 Creates a new internal access user. To add the user to the database, see L<"insert">.
66 Note that this stores the hash reference, not a distinct copy of the hash it
67 points to. You can ask the object for a copy with the I<hash> method.
71 # the new method can be inherited from FS::Record, if a table method is defined
73 sub table { 'access_user'; }
75 sub _option_table { 'access_user_pref'; }
76 sub _option_namecol { 'prefname'; }
77 sub _option_valuecol { 'prefvalue'; }
81 Adds this record to the database. If there is an error, returns the error,
82 otherwise returns false.
89 my $error = $self->check;
90 return $error if $error;
92 local $SIG{HUP} = 'IGNORE';
93 local $SIG{INT} = 'IGNORE';
94 local $SIG{QUIT} = 'IGNORE';
95 local $SIG{TERM} = 'IGNORE';
96 local $SIG{TSTP} = 'IGNORE';
97 local $SIG{PIPE} = 'IGNORE';
99 my $oldAutoCommit = $FS::UID::AutoCommit;
100 local $FS::UID::AutoCommit = 0;
104 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
108 $error = $self->SUPER::insert(@_);
111 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
114 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
122 Delete this record from the database.
129 local $SIG{HUP} = 'IGNORE';
130 local $SIG{INT} = 'IGNORE';
131 local $SIG{QUIT} = 'IGNORE';
132 local $SIG{TERM} = 'IGNORE';
133 local $SIG{TSTP} = 'IGNORE';
134 local $SIG{PIPE} = 'IGNORE';
136 my $oldAutoCommit = $FS::UID::AutoCommit;
137 local $FS::UID::AutoCommit = 0;
140 my $error = $self->SUPER::delete(@_);
143 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
146 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
152 =item replace OLD_RECORD
154 Replaces the OLD_RECORD with this one in the database. If there is an error,
155 returns the error, otherwise returns false.
162 my $old = ( ref($_[0]) eq ref($new) )
166 local $SIG{HUP} = 'IGNORE';
167 local $SIG{INT} = 'IGNORE';
168 local $SIG{QUIT} = 'IGNORE';
169 local $SIG{TERM} = 'IGNORE';
170 local $SIG{TSTP} = 'IGNORE';
171 local $SIG{PIPE} = 'IGNORE';
173 my $oldAutoCommit = $FS::UID::AutoCommit;
174 local $FS::UID::AutoCommit = 0;
177 return "Must change password when enabling this account"
178 if $old->disabled && !$new->disabled
179 && ( $new->_password =~ /changeme/i
180 || $new->_password eq 'notyet'
183 my $error = $new->SUPER::replace($old, @_);
186 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
189 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
197 Checks all fields to make sure this is a valid internal access user. If there is
198 an error, returns the error, otherwise returns false. Called by the insert
203 # the check method should currently be supplied - FS::Record contains some
204 # data checking routines
210 $self->ut_numbern('usernum')
211 || $self->ut_alpha_lower('username')
212 || $self->ut_text('_password')
213 || $self->ut_text('last')
214 || $self->ut_text('first')
215 || $self->ut_foreign_keyn('user_custnum', 'cust_main', 'custnum')
216 || $self->ut_enum('disabled', [ '', 'Y' ] )
218 return $error if $error;
225 Returns a name string for this user: "Last, First".
231 return $self->username
232 if $self->get('last') eq 'Lastname' && $self->first eq 'Firstname';
233 return $self->get('last'). ', '. $self->first;
238 Returns the FS::cust_main object (see L<FS::cust_main>), if any, for this
245 qsearchs( 'cust_main', { 'custnum' => $self->user_custnum } );
248 =item access_usergroup
250 Returns links to the the groups this user is a part of, as FS::access_usergroup
251 objects (see L<FS::access_usergroup>).
255 sub access_usergroup {
257 qsearch( 'access_usergroup', { 'usernum' => $self->usernum } );
268 #=item access_groupnames
272 #sub access_groupnames {
278 Returns a list of agentnums this user can view (via group membership).
284 my $sth = dbh->prepare(
285 "SELECT DISTINCT agentnum FROM access_usergroup
286 JOIN access_groupagent USING ( groupnum )
288 ) or die dbh->errstr;
289 $sth->execute($self->usernum) or die $sth->errstr;
290 map { $_->[0] } @{ $sth->fetchall_arrayref };
295 Returns a hashref of agentnums this user can view.
301 scalar( { map { $_ => 1 } $self->agentnums } );
304 =item agentnums_sql [ HASHREF | OPTION => VALUE ... ]
306 Returns an sql fragement to select only agentnums this user can view.
308 Options are passed as a hashref or a list. Available options are:
314 The frament will also allow the selection of null agentnums.
318 The fragment will also allow the selection of null agentnums if the current
319 user has the provided access right
323 Optional table name in which agentnum is being checked. Sometimes required to
324 resolve 'column reference "agentnum" is ambiguous' errors.
328 All agents will be viewable if the current user has the provided access right.
329 Defaults to 'View customers of all agents'.
337 my %opt = ref($_[0]) ? %{$_[0]} : @_;
339 my $agentnum = $opt{'table'} ? $opt{'table'}.'.agentnum' : 'agentnum';
343 my $viewall_right = $opt{'viewall_right'} || 'View customers of all agents';
344 if ( $self->access_right($viewall_right) ) {
345 push @or, "$agentnum IS NOT NULL";
347 push @or, "$agentnum IN (". join(',', $self->agentnums). ')';
350 push @or, "$agentnum IS NULL"
352 || ( $opt{'null_right'} && $self->access_right($opt{'null_right'}) );
354 return ' 1 = 0 ' unless scalar(@or);
355 '( '. join( ' OR ', @or ). ' )';
361 Returns true if the user can view the specified agent.
366 my( $self, $agentnum ) = @_;
367 my $sth = dbh->prepare(
368 "SELECT COUNT(*) FROM access_usergroup
369 JOIN access_groupagent USING ( groupnum )
370 WHERE usernum = ? AND agentnum = ?"
371 ) or die dbh->errstr;
372 $sth->execute($self->usernum, $agentnum) or die $sth->errstr;
373 $sth->fetchrow_arrayref->[0];
376 =item agents [ HASHREF | OPTION => VALUE ... ]
378 Returns the list of agents this user can view (via group membership), as
379 FS::agent objects. Accepts the same options as the agentnums_sql method.
387 'hashref' => { disabled=>'' },
388 'extra_sql' => ' AND '. $self->agentnums_sql(@_),
392 =item access_right RIGHTNAME | LISTREF
394 Given a right name or a list reference of right names, returns true if this
395 user has this right, or, for a list, one of the rights (currently via group
396 membership, eventually also via user overrides).
401 my( $self, $rightname ) = @_;
403 $rightname = [ $rightname ] unless ref($rightname);
405 warn "$me access_right called on ". join(', ', @$rightname). "\n"
408 #some caching of ACL requests for low-hanging fruit perf improvement
409 #since we get a new $CurrentUser object each page view there shouldn't be any
410 #issues with stickiness
411 if ( $self->{_ACLcache} ) {
413 unless ( grep !exists($self->{_ACLcache}{$_}), @$rightname ) {
414 warn "$me ACL cache hit for ". join(', ', @$rightname). "\n"
416 return grep $self->{_ACLcache}{$_}, @$rightname
419 warn "$me ACL cache miss for ". join(', ', @$rightname). "\n"
424 warn "initializing ACL cache\n"
426 $self->{_ACLcache} = {};
430 my $has_right = ' rightname IN ('. join(',', map '?', @$rightname ). ') ';
432 my $sth = dbh->prepare("
433 SELECT groupnum FROM access_usergroup
434 LEFT JOIN access_group USING ( groupnum )
435 LEFT JOIN access_right
436 ON ( access_group.groupnum = access_right.rightobjnum )
438 AND righttype = 'FS::access_group'
441 ") or die dbh->errstr;
442 $sth->execute($self->usernum, @$rightname) or die $sth->errstr;
443 my $row = $sth->fetchrow_arrayref;
445 my $return = $row ? $row->[0] : '';
447 #just caching the single-rightname hits should be enough of a win for now
448 if ( scalar(@$rightname) == 1 ) {
449 $self->{_ACLcache}{${$rightname}[0]} = $return;
456 =item default_customer_view
458 Returns the default customer view for this user, from the
459 "default_customer_view" user preference, the "cust_main-default_view" config,
460 or the hardcoded default, "basics" (formerly "jumbo" prior to 3.0).
464 sub default_customer_view {
467 $self->option('default_customer_view')
468 || $conf->config('cust_main-default_view')
469 || 'basics'; #s/jumbo/basics/ starting with 3.0
473 =item spreadsheet_format [ OVERRIDE ]
475 Returns a hashref of this user's Excel spreadsheet download settings:
476 'extension' (xls or xlsx), 'class' (Spreadsheet::WriteExcel or
477 Excel::Writer::XLSX), and 'mime_type'. If OVERRIDE is 'XLS' or 'XLSX',
478 use that instead of the user's setting.
482 # is there a better place to put this?
486 class => 'Spreadsheet::WriteExcel',
487 mime_type => 'application/vnd.ms-excel',
490 extension => '.xlsx',
491 class => 'Excel::Writer::XLSX',
492 mime_type => # it's on wikipedia, it must be true
493 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
497 sub spreadsheet_format {
499 my $override = shift;
502 || $self->option('spreadsheet_format')
503 || $conf->config('spreadsheet_format')
511 Returns true if this user has the name of a known system account. These
512 users cannot log into the web interface and can't have passwords set.
518 return grep { $_ eq $self->username } ( qw(
528 =item change_password NEW_PASSWORD
532 sub change_password {
533 #my( $self, $password ) = @_;
534 #FS::Auth->auth_class->change_password( $self, $password );
535 FS::Auth->auth_class->change_password( @_ );
538 =item change_password_fields NEW_PASSWORD
542 sub change_password_fields {
543 #my( $self, $password ) = @_;
544 #FS::Auth->auth_class->change_password_fields( $self, $password );
545 FS::Auth->auth_class->change_password_fields( @_ );
554 L<FS::Record>, schema.html from the base documentation.