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
50 =item disabled - empty or 'Y'
60 Creates a new internal access user. To add the user to the database, see L<"insert">.
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.
67 # the new method can be inherited from FS::Record, if a table method is defined
69 sub table { 'access_user'; }
73 Adds this record to the database. If there is an error, returns the error,
74 otherwise returns false.
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';
88 my $oldAutoCommit = $FS::UID::AutoCommit;
89 local $FS::UID::AutoCommit = 0;
93 $self->SUPER::insert(@_)
94 || $self->htpasswd_kludge()
98 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
101 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
107 sub htpasswd_kludge {
109 unshift @_, '-c' unless -e $htpasswd_file;
111 system('htpasswd', '-b', @_,
120 return 'htpasswd exited unsucessfully';
127 Delete this record from the database.
134 local $SIG{HUP} = 'IGNORE';
135 local $SIG{INT} = 'IGNORE';
136 local $SIG{QUIT} = 'IGNORE';
137 local $SIG{TERM} = 'IGNORE';
138 local $SIG{TSTP} = 'IGNORE';
139 local $SIG{PIPE} = 'IGNORE';
141 my $oldAutoCommit = $FS::UID::AutoCommit;
142 local $FS::UID::AutoCommit = 0;
146 $self->SUPER::delete(@_)
147 || $self->htpasswd_kludge('-D')
151 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
154 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
160 =item replace OLD_RECORD
162 Replaces the OLD_RECORD with this one in the database. If there is an error,
163 returns the error, otherwise returns false.
168 my($new, $old) = ( shift, shift );
170 local $SIG{HUP} = 'IGNORE';
171 local $SIG{INT} = 'IGNORE';
172 local $SIG{QUIT} = 'IGNORE';
173 local $SIG{TERM} = 'IGNORE';
174 local $SIG{TSTP} = 'IGNORE';
175 local $SIG{PIPE} = 'IGNORE';
177 my $oldAutoCommit = $FS::UID::AutoCommit;
178 local $FS::UID::AutoCommit = 0;
182 $new->SUPER::replace($old, @_)
183 || $new->htpasswd_kludge()
187 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
190 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
198 Checks all fields to make sure this is a valid internal access user. If there is
199 an error, returns the error, otherwise returns false. Called by the insert
204 # the check method should currently be supplied - FS::Record contains some
205 # data checking routines
211 $self->ut_numbern('usernum')
212 || $self->ut_text('username')
213 || $self->ut_text('_password')
214 || $self->ut_text('last')
215 || $self->ut_text('first')
216 || $self->ut_enum('disabled', [ '', 'Y' ] )
218 return $error if $error;
225 Returns a name string for this user: "Last, First".
231 $self->get('last'). ', '. $self->first;
234 =item access_usergroup
238 sub access_usergroup {
240 qsearch( 'access_usergroup', { 'usernum' => $self->usernum } );
251 #=item access_groupnames
255 #sub access_groupnames {
261 Returns a list of agentnums this user can view (via group membership).
267 my $sth = dbh->prepare(
268 "SELECT DISTINCT agentnum FROM access_usergroup
269 JOIN access_groupagent USING ( groupnum )
271 ) or die dbh->errstr;
272 $sth->execute($self->usernum) or die $sth->errstr;
273 map { $_->[0] } @{ $sth->fetchall_arrayref };
278 Returns a hashref of agentnums this user can view.
284 { map { $_ => 1 } $self->agentnums };
289 Returns an sql fragement to select only agentnums this user can view.
296 join( ' OR ', map "agentnum = $_", $self->agentnums ).
302 Given a right name, returns true if this user has this right (currently via
303 group membership, eventually also via user overrides).
308 my( $self, $rightname ) = @_;
309 my $sth = dbh->prepare("
310 SELECT groupnum FROM access_usergroup
311 LEFT JOIN access_group USING ( groupnum )
312 LEFT JOIN access_right
313 ON ( access_group.groupnum = access_right.rightobjnum )
315 AND righttype = 'FS::access_group'
317 ") or die dbh->errstr;
318 $sth->execute($self->usernum, $rightname) or die $sth->errstr;
319 my $row = $sth->fetchrow_arrayref;
320 $row ? $row->[0] : '';
329 L<FS::Record>, schema.html from the base documentation.