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;
9 @ISA = qw( FS::m2m_Common FS::Record );
11 #kludge htpasswd for now
12 $htpasswd_file = '/usr/local/etc/freeside/htpasswd';
16 FS::access_user - Object methods for access_user records
22 $record = new FS::access_user \%hash;
23 $record = new FS::access_user { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
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:
40 =item usernum - primary key
58 Creates a new internal access user. To add the user to the database, see L<"insert">.
60 Note that this stores the hash reference, not a distinct copy of the hash it
61 points to. You can ask the object for a copy with the I<hash> method.
65 # the new method can be inherited from FS::Record, if a table method is defined
67 sub table { 'access_user'; }
71 Adds this record to the database. If there is an error, returns the error,
72 otherwise returns false.
79 local $SIG{HUP} = 'IGNORE';
80 local $SIG{INT} = 'IGNORE';
81 local $SIG{QUIT} = 'IGNORE';
82 local $SIG{TERM} = 'IGNORE';
83 local $SIG{TSTP} = 'IGNORE';
84 local $SIG{PIPE} = 'IGNORE';
86 my $oldAutoCommit = $FS::UID::AutoCommit;
87 local $FS::UID::AutoCommit = 0;
91 $self->SUPER::insert(@_)
92 || $self->htpasswd_kludge()
96 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
99 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
105 sub htpasswd_kludge {
108 system('htpasswd', '-b', @_,
117 return 'htpasswd exited unsucessfully';
124 Delete this record from the database.
131 local $SIG{HUP} = 'IGNORE';
132 local $SIG{INT} = 'IGNORE';
133 local $SIG{QUIT} = 'IGNORE';
134 local $SIG{TERM} = 'IGNORE';
135 local $SIG{TSTP} = 'IGNORE';
136 local $SIG{PIPE} = 'IGNORE';
138 my $oldAutoCommit = $FS::UID::AutoCommit;
139 local $FS::UID::AutoCommit = 0;
143 $self->SUPER::delete(@_)
144 || $self->htpasswd_kludge('-D')
148 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
151 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
157 =item replace OLD_RECORD
159 Replaces the OLD_RECORD with this one in the database. If there is an error,
160 returns the error, otherwise returns false.
165 my($new, $old) = ( shift, shift );
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 $new->SUPER::replace($old, @_)
180 || $new->htpasswd_kludge()
184 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
187 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
195 Checks all fields to make sure this is a valid internal access user. If there is
196 an error, returns the error, otherwise returns false. Called by the insert
201 # the check method should currently be supplied - FS::Record contains some
202 # data checking routines
208 $self->ut_numbern('usernum')
209 || $self->ut_text('username')
210 || $self->ut_text('_password')
211 || $self->ut_text('last')
212 || $self->ut_text('first')
214 return $error if $error;
221 Returns a name string for this user: "Last, First".
227 $self->get('last'). ', '. $self->first;
230 =item access_usergroup
234 sub access_usergroup {
236 qsearch( 'access_usergroup', { 'usernum' => $self->usernum } );
247 #=item access_groupnames
251 #sub access_groupnames {
257 Returns a list of agentnums this user can view (via group membership).
263 my $sth = dbh->prepare(
264 "SELECT DISTINCT agentnum FROM access_usergroup
265 JOIN access_groupagent USING ( groupnum )
267 ) or die dbh->errstr;
268 $sth->execute($self->usernum) or die $sth->errstr;
269 map { $_->[0] } @{ $sth->fetchall_arrayref };
274 Returns a hashref of agentnums this user can view.
280 { map { $_ => 1 } $self->agentnums };
285 Returns an sql fragement to select only agentnums this user can view.
292 join( ' OR ', map "agentnum = $_", $self->agentnums ).
298 Given a right name, returns true if this user has this right (currently via
299 group membership, eventually also via user overrides).
304 my( $self, $rightname ) = @_;
305 my $sth = dbh->prepare("
306 SELECT groupnum FROM access_usergroup
307 LEFT JOIN access_group USING ( groupnum )
308 LEFT JOIN access_right
309 ON ( access_group.groupnum = access_right.rightobjnum )
311 AND righttype = 'FS::access_group'
313 ") or die dbh->errstr;
314 $sth->execute($self->usernum, $rightname) or die $sth->errstr;
315 my $row = $sth->fetchrow_arrayref;
316 $row ? $row->[0] : '';
325 L<FS::Record>, schema.html from the base documentation.