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 {
110 #awful kludge to skip setting htpasswd for fs_* users
111 return '' if $self->username =~ /^fs_/;
113 unshift @_, '-c' unless -e $htpasswd_file;
115 system('htpasswd', '-b', @_,
124 return 'htpasswd exited unsucessfully';
131 Delete this record from the database.
138 local $SIG{HUP} = 'IGNORE';
139 local $SIG{INT} = 'IGNORE';
140 local $SIG{QUIT} = 'IGNORE';
141 local $SIG{TERM} = 'IGNORE';
142 local $SIG{TSTP} = 'IGNORE';
143 local $SIG{PIPE} = 'IGNORE';
145 my $oldAutoCommit = $FS::UID::AutoCommit;
146 local $FS::UID::AutoCommit = 0;
150 $self->SUPER::delete(@_)
151 || $self->htpasswd_kludge('-D')
155 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
158 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
164 =item replace OLD_RECORD
166 Replaces the OLD_RECORD with this one in the database. If there is an error,
167 returns the error, otherwise returns false.
172 my($new, $old) = ( shift, shift );
174 local $SIG{HUP} = 'IGNORE';
175 local $SIG{INT} = 'IGNORE';
176 local $SIG{QUIT} = 'IGNORE';
177 local $SIG{TERM} = 'IGNORE';
178 local $SIG{TSTP} = 'IGNORE';
179 local $SIG{PIPE} = 'IGNORE';
181 my $oldAutoCommit = $FS::UID::AutoCommit;
182 local $FS::UID::AutoCommit = 0;
186 $new->SUPER::replace($old, @_)
187 || $new->htpasswd_kludge()
191 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
194 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
202 Checks all fields to make sure this is a valid internal access user. If there is
203 an error, returns the error, otherwise returns false. Called by the insert
208 # the check method should currently be supplied - FS::Record contains some
209 # data checking routines
215 $self->ut_numbern('usernum')
216 || $self->ut_text('username')
217 || $self->ut_text('_password')
218 || $self->ut_text('last')
219 || $self->ut_text('first')
220 || $self->ut_enum('disabled', [ '', 'Y' ] )
222 return $error if $error;
229 Returns a name string for this user: "Last, First".
235 $self->get('last'). ', '. $self->first;
238 =item access_usergroup
242 sub access_usergroup {
244 qsearch( 'access_usergroup', { 'usernum' => $self->usernum } );
255 #=item access_groupnames
259 #sub access_groupnames {
265 Returns a list of agentnums this user can view (via group membership).
271 my $sth = dbh->prepare(
272 "SELECT DISTINCT agentnum FROM access_usergroup
273 JOIN access_groupagent USING ( groupnum )
275 ) or die dbh->errstr;
276 $sth->execute($self->usernum) or die $sth->errstr;
277 map { $_->[0] } @{ $sth->fetchall_arrayref };
282 Returns a hashref of agentnums this user can view.
288 { map { $_ => 1 } $self->agentnums };
293 Returns an sql fragement to select only agentnums this user can view.
300 join( ' OR ', map "agentnum = $_", $self->agentnums ).
306 Returns true if the user can view the specified agent.
311 my( $self, $agentnum ) = @_;
312 my $sth = dbh->prepare(
313 "SELECT COUNT(*) FROM access_usergroup
314 JOIN access_groupagent USING ( groupnum )
315 WHERE usernum = ? AND agentnum = ?"
316 ) or die dbh->errstr;
317 $sth->execute($self->usernum, $agentnum) or die $sth->errstr;
318 $sth->fetchrow_arrayref->[0];
324 Given a right name, returns true if this user has this right (currently via
325 group membership, eventually also via user overrides).
330 my( $self, $rightname ) = @_;
331 my $sth = dbh->prepare("
332 SELECT groupnum FROM access_usergroup
333 LEFT JOIN access_group USING ( groupnum )
334 LEFT JOIN access_right
335 ON ( access_group.groupnum = access_right.rightobjnum )
337 AND righttype = 'FS::access_group'
339 ") or die dbh->errstr;
340 $sth->execute($self->usernum, $rightname) or die $sth->errstr;
341 my $row = $sth->fetchrow_arrayref;
342 $row ? $row->[0] : '';
351 L<FS::Record>, schema.html from the base documentation.