1 package FS::access_user;
4 use vars qw( @ISA $htpasswd_file );
5 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::access_usergroup;
10 @ISA = qw( FS::m2m_Common FS::Record );
12 #kludge htpasswd for now
13 $htpasswd_file = '/usr/local/etc/freeside/htpasswd';
17 FS::access_user - Object methods for access_user records
23 $record = new FS::access_user \%hash;
24 $record = new FS::access_user { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::access_user object represents an internal access user. FS::access_user inherits from
37 FS::Record. The following fields are currently supported:
41 =item usernum - primary key
51 =item disabled - empty or 'Y'
61 Creates a new internal access user. To add the user to the database, see L<"insert">.
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to. You can ask the object for a copy with the I<hash> method.
68 # the new method can be inherited from FS::Record, if a table method is defined
70 sub table { 'access_user'; }
74 Adds this record to the database. If there is an error, returns the error,
75 otherwise returns false.
82 local $SIG{HUP} = 'IGNORE';
83 local $SIG{INT} = 'IGNORE';
84 local $SIG{QUIT} = 'IGNORE';
85 local $SIG{TERM} = 'IGNORE';
86 local $SIG{TSTP} = 'IGNORE';
87 local $SIG{PIPE} = 'IGNORE';
89 my $oldAutoCommit = $FS::UID::AutoCommit;
90 local $FS::UID::AutoCommit = 0;
94 $self->SUPER::insert(@_)
95 || $self->htpasswd_kludge()
99 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
102 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
108 sub htpasswd_kludge {
111 #awful kludge to skip setting htpasswd for fs_* users
112 return '' if $self->username =~ /^fs_/;
114 unshift @_, '-c' unless -e $htpasswd_file;
116 system('htpasswd', '-b', @_,
125 return 'htpasswd exited unsucessfully';
132 Delete this record from the database.
139 local $SIG{HUP} = 'IGNORE';
140 local $SIG{INT} = 'IGNORE';
141 local $SIG{QUIT} = 'IGNORE';
142 local $SIG{TERM} = 'IGNORE';
143 local $SIG{TSTP} = 'IGNORE';
144 local $SIG{PIPE} = 'IGNORE';
146 my $oldAutoCommit = $FS::UID::AutoCommit;
147 local $FS::UID::AutoCommit = 0;
151 $self->SUPER::delete(@_)
152 || $self->htpasswd_kludge('-D')
156 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
159 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
165 =item replace OLD_RECORD
167 Replaces the OLD_RECORD with this one in the database. If there is an error,
168 returns the error, otherwise returns false.
173 my($new, $old) = ( shift, shift );
175 local $SIG{HUP} = 'IGNORE';
176 local $SIG{INT} = 'IGNORE';
177 local $SIG{QUIT} = 'IGNORE';
178 local $SIG{TERM} = 'IGNORE';
179 local $SIG{TSTP} = 'IGNORE';
180 local $SIG{PIPE} = 'IGNORE';
182 my $oldAutoCommit = $FS::UID::AutoCommit;
183 local $FS::UID::AutoCommit = 0;
187 $new->SUPER::replace($old, @_)
188 || $new->htpasswd_kludge()
192 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
195 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
203 Checks all fields to make sure this is a valid internal access user. If there is
204 an error, returns the error, otherwise returns false. Called by the insert
209 # the check method should currently be supplied - FS::Record contains some
210 # data checking routines
216 $self->ut_numbern('usernum')
217 || $self->ut_text('username')
218 || $self->ut_text('_password')
219 || $self->ut_text('last')
220 || $self->ut_text('first')
221 || $self->ut_enum('disabled', [ '', 'Y' ] )
223 return $error if $error;
230 Returns a name string for this user: "Last, First".
236 $self->get('last'). ', '. $self->first;
239 =item access_usergroup
243 sub access_usergroup {
245 qsearch( 'access_usergroup', { 'usernum' => $self->usernum } );
256 #=item access_groupnames
260 #sub access_groupnames {
266 Returns a list of agentnums this user can view (via group membership).
272 my $sth = dbh->prepare(
273 "SELECT DISTINCT agentnum FROM access_usergroup
274 JOIN access_groupagent USING ( groupnum )
276 ) or die dbh->errstr;
277 $sth->execute($self->usernum) or die $sth->errstr;
278 map { $_->[0] } @{ $sth->fetchall_arrayref };
283 Returns a hashref of agentnums this user can view.
289 { map { $_ => 1 } $self->agentnums };
294 Returns an sql fragement to select only agentnums this user can view.
301 my @agentnums = $self->agentnums;
302 return ' 1 = 0 ' unless scalar(@agentnums);
305 join( ' OR ', map "agentnum = $_", @agentnums ).
311 Returns true if the user can view the specified agent.
316 my( $self, $agentnum ) = @_;
317 my $sth = dbh->prepare(
318 "SELECT COUNT(*) FROM access_usergroup
319 JOIN access_groupagent USING ( groupnum )
320 WHERE usernum = ? AND agentnum = ?"
321 ) or die dbh->errstr;
322 $sth->execute($self->usernum, $agentnum) or die $sth->errstr;
323 $sth->fetchrow_arrayref->[0];
328 Returns the list of agents this user can view (via group membership), as
337 'hashref' => { disabled=>'' },
338 'extra_sql' => ' AND '. $self->agentnums_sql,
344 Given a right name, returns true if this user has this right (currently via
345 group membership, eventually also via user overrides).
350 my( $self, $rightname ) = @_;
351 my $sth = dbh->prepare("
352 SELECT groupnum FROM access_usergroup
353 LEFT JOIN access_group USING ( groupnum )
354 LEFT JOIN access_right
355 ON ( access_group.groupnum = access_right.rightobjnum )
357 AND righttype = 'FS::access_group'
359 ") or die dbh->errstr;
360 $sth->execute($self->usernum, $rightname) or die $sth->errstr;
361 my $row = $sth->fetchrow_arrayref;
362 $row ? $row->[0] : '';
371 L<FS::Record>, schema.html from the base documentation.