import rt 2.0.14
[freeside.git] / rt / lib / RT / Users.pm
1
2 # $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Users.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
3 # (c) 1996-1999 Jesse Vincent <jesse@fsck.com>
4 # This software is redistributable under the terms of the GNU GPL
5
6 =head1 NAME
7
8   RT::Users - Collection of RT::User objects
9
10 =head1 SYNOPSIS
11
12   use RT::Users;
13
14
15 =head1 DESCRIPTION
16
17
18 =head1 METHODS
19
20 =begin testing
21
22 ok(require RT::TestHarness);
23 ok(require RT::Users);
24
25 =end testing
26
27 =cut
28
29 package RT::Users;
30 use RT::EasySearch;
31 @ISA = qw(RT::EasySearch);
32
33 # {{{ sub _Init 
34 sub _Init  {
35   my $self = shift;
36   $self->{'table'} = "Users";
37   $self->{'primary_key'} = "id";
38
39   # By default, order by name
40   $self->OrderBy( ALIAS => 'main',
41                   FIELD => 'Name',
42                   ORDER => 'ASC');
43
44   return ($self->SUPER::_Init(@_));
45   
46 }
47 # }}}
48
49 # {{{ sub _DoSearch 
50
51 =head2 _DoSearch
52
53   A subclass of DBIx::SearchBuilder::_DoSearch that makes sure that _Disabled rows never get seen unless
54 we're explicitly trying to see them.
55
56 =cut
57
58 sub _DoSearch {
59     my $self = shift;
60     
61     #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
62     unless($self->{'find_disabled_rows'}) {
63         $self->LimitToEnabled();
64     }
65     
66     return($self->SUPER::_DoSearch(@_));
67     
68 }
69
70 # }}}
71
72 # {{{ sub NewItem 
73
74 sub NewItem  {
75   my $self = shift;
76
77   use RT::User;
78   my $item = new RT::User($self->CurrentUser);
79   return($item);
80 }
81 # }}}
82
83 # {{{ LimitToEmail
84 =head2 LimitToEmail
85
86 Takes one argument. an email address. limits the returned set to
87 that email address
88
89 =cut
90
91 sub LimitToEmail {
92     my $self = shift;
93     my $addr = shift;
94     $self->Limit(FIELD => 'EmailAddress', VALUE => "$addr");
95 }
96
97 # }}}
98
99 # {{{ MemberOfGroup
100
101 =head2 MemberOfGroup
102
103 takes one argument, a group id number. Limits the returned set
104 to members of a given group
105
106 =cut
107
108 sub MemberOfGroup {
109     my $self = shift;
110     my $group = shift;
111     
112     return ("No group specified") if (!defined $group);
113
114     my $groupalias = $self->NewAlias('GroupMembers');
115
116     $self->Join( ALIAS1 => 'main', FIELD1 => 'id', 
117                  ALIAS2 => "$groupalias", FIELD2 => 'Name');
118     
119     $self->Limit (ALIAS => "$groupalias",
120                   FIELD => 'GroupId',
121                   VALUE => "$group",
122                   OPERATOR => "="
123                  );
124 }
125
126 # }}}
127
128 # {{{ LimitToPrivileged
129
130 =head2 LimitToPrivileged
131
132 Limits to users who can be made members of ACLs and groups
133
134 =cut
135
136 sub LimitToPrivileged {
137     my $self = shift;
138     $self->Limit( FIELD => 'Privileged',
139                   OPERATOR => '=',
140                   VALUE => '1');
141 }
142
143 # }}}
144
145
146
147 # {{{ LimitToSystem
148
149 =head2 LimitToSystem
150
151 Limits to users who can be granted rights, but who should
152 never have their rights modified by a user or be made members of groups.
153
154 =cut
155
156 sub LimitToSystem {
157     my $self = shift;
158     $self->Limit( FIELD => 'Privileged',
159                   OPERATOR => '=',
160                   VALUE => '2');
161 }
162
163 # }}}
164
165 # {{{ HasQueueRight
166
167 =head2 HasQueueRight
168
169 Takes a queue id as its first argument.  Queue Id "0" is treated by RT as "applies to all queues"
170 Takes a specific right as an optional second argument
171
172 Limits the returned set to users who have rights in the queue specified, personally.  If the optional second argument is supplied, limits to users who have been explicitly granted that right.
173
174
175
176 This should not be used as an ACL check, but only for obtaining lists of
177 users with explicit rights in a given queue.
178
179 =cut
180
181 sub HasQueueRight {
182     my $self = shift;
183     my $queue = shift;
184     my $right;
185     
186     $right = shift if (@_);
187
188
189     my $acl_alias  = $self->NewAlias('ACL');
190     $self->Join( ALIAS1 => 'main',  FIELD1 => 'id',
191                  ALIAS2 => $acl_alias, FIELD2 => 'PrincipalId');
192     $self->Limit (ALIAS => $acl_alias,
193                  FIELD => 'PrincipalType',
194                  OPERATOR => '=',
195                  VALUE => 'User');
196
197
198     $self->Limit(ALIAS => $acl_alias,
199                  FIELD => 'RightAppliesTo',
200                  OPERATOR => '=',
201                  VALUE => "$queue");
202
203
204     $self->Limit(ALIAS => $acl_alias,
205                  FIELD => 'RightScope',
206                  OPERATOR => '=',
207                  ENTRYAGGREGATOR => 'OR',
208                  VALUE => 'Queue');
209
210
211     $self->Limit(ALIAS => $acl_alias,
212                  FIELD => 'RightScope',
213                  OPERATOR => '=',
214                  ENTRYAGGREGATOR => 'OR',
215                  VALUE => 'Ticket');
216
217
218     #TODO: is this being initialized properly if the right isn't there?
219     if (defined ($right)) {
220         
221         $self->Limit(ALIAS => $acl_alias,
222                      FIELD => 'RightName',
223                      OPERATOR => '=',
224                      VALUE => "$right");
225         
226         
227        };
228
229
230 }
231
232
233
234 # }}}
235
236 # {{{ HasSystemRight
237
238 =head2 HasSystemRight
239
240 Takes one optional argument:
241    The name of a System level right.
242
243 Limits the returned set to users who have been granted system rights, personally.  If the optional argument is passed in, limits to users who have been granted the explicit right listed.   Please see the note attached to LimitToQueueRights
244
245 =cut
246
247 sub HasSystemRight {
248     my $self = shift;
249     my $right = shift if (@_);
250        my $acl_alias  = $self->NewAlias('ACL');
251
252
253     $self->Join( ALIAS1 => 'main',  FIELD1 => 'id',
254                  ALIAS2 => $acl_alias, FIELD2 => 'PrincipalId');
255     $self->Limit (ALIAS => $acl_alias,
256                  FIELD => 'PrincipalType',
257                  OPERATOR => '=',
258                  VALUE => 'User');
259
260     $self->Limit(ALIAS => $acl_alias,
261                  FIELD => 'RightScope',
262                  OPERATOR => '=',
263                  VALUE => 'System');
264
265
266     #TODO: is this being initialized properly if the right isn't there?
267     if (defined ($right)) {
268         $self->Limit(ALIAS => $acl_alias,
269                      FIELD => 'RightName',
270                      OPERATOR => '=',
271                      VALUE => "$right");
272         
273        }
274
275     
276 }
277
278 # }}}
279
280 1;
281