agent virtualization, take one (stuff from "inactive" changeset snuck into cust_main...
[freeside.git] / FS / FS / access_user.pm
1 package FS::access_user;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::m2m_Common;
7 use FS::access_usergroup;
8
9 @ISA = qw( FS::m2m_Common FS::Record );
10
11 =head1 NAME
12
13 FS::access_user - Object methods for access_user records
14
15 =head1 SYNOPSIS
16
17   use FS::access_user;
18
19   $record = new FS::access_user \%hash;
20   $record = new FS::access_user { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::access_user object represents an internal access user.  FS::access_user inherits from
33 FS::Record.  The following fields are currently supported:
34
35 =over 4
36
37 =item usernum - primary key
38
39 =item username - 
40
41 =item _password - 
42
43 =item last -
44
45 =item first -
46
47 =back
48
49 =head1 METHODS
50
51 =over 4
52
53 =item new HASHREF
54
55 Creates a new internal access user.  To add the user to the database, see L<"insert">.
56
57 Note that this stores the hash reference, not a distinct copy of the hash it
58 points to.  You can ask the object for a copy with the I<hash> method.
59
60 =cut
61
62 # the new method can be inherited from FS::Record, if a table method is defined
63
64 sub table { 'access_user'; }
65
66 =item insert
67
68 Adds this record to the database.  If there is an error, returns the error,
69 otherwise returns false.
70
71 =cut
72
73 # the insert method can be inherited from FS::Record
74
75 =item delete
76
77 Delete this record from the database.
78
79 =cut
80
81 # the delete method can be inherited from FS::Record
82
83 =item replace OLD_RECORD
84
85 Replaces the OLD_RECORD with this one in the database.  If there is an error,
86 returns the error, otherwise returns false.
87
88 =cut
89
90 # the replace method can be inherited from FS::Record
91
92 =item check
93
94 Checks all fields to make sure this is a valid internal access user.  If there is
95 an error, returns the error, otherwise returns false.  Called by the insert
96 and replace methods.
97
98 =cut
99
100 # the check method should currently be supplied - FS::Record contains some
101 # data checking routines
102
103 sub check {
104   my $self = shift;
105
106   my $error = 
107     $self->ut_numbern('usernum')
108     || $self->ut_text('username')
109     || $self->ut_text('_password')
110     || $self->ut_text('last')
111     || $self->ut_text('first')
112   ;
113   return $error if $error;
114
115   $self->SUPER::check;
116 }
117
118 =item name
119
120 Returns a name string for this user: "Last, First".
121
122 =cut
123
124 sub name {
125   my $self = shift;
126   $self->get('last'). ', '. $self->first;
127 }
128
129 =item access_usergroup
130
131 =cut
132
133 sub access_usergroup {
134   my $self = shift;
135   qsearch( 'access_usergroup', { 'usernum' => $self->usernum } );
136 }
137
138 #=item access_groups
139 #
140 #=cut
141 #
142 #sub access_groups {
143 #
144 #}
145 #
146 #=item access_groupnames
147 #
148 #=cut
149 #
150 #sub access_groupnames {
151 #
152 #}
153
154 =item agentnums 
155
156 Returns a list of agentnums this user can view (via group membership).
157
158 =cut
159
160 sub agentnums {
161   my $self = shift;
162   my $sth = dbh->prepare(
163     "SELECT DISTINCT agentnum FROM access_usergroup
164                               JOIN access_groupagent USING ( groupnum )
165        WHERE usernum = ?"
166   ) or die dbh->errstr;
167   $sth->execute($self->usernum) or die $sth->errstr;
168   map { $_->[0] } @{ $sth->fetchall_arrayref };
169 }
170
171 =item agentnums_href
172
173 Returns a hashref of agentnums this user can view.
174
175 =cut
176
177 sub agentnums_href {
178   my $self = shift;
179   { map { $_ => 1 } $self->agentnums };
180 }
181
182 =item agentnums_sql
183
184 Returns an sql fragement to select only agentnums this user can view.
185
186 =cut
187
188 sub agentnums_sql {
189   my $self = shift;
190   '( '.
191     join( ' OR ', map "agentnum = $_", $self->agentnums ).
192   ' )';
193 }
194
195 =back
196
197 =head1 BUGS
198
199 =head1 SEE ALSO
200
201 L<FS::Record>, schema.html from the base documentation.
202
203 =cut
204
205 1;
206