summaryrefslogtreecommitdiff
path: root/rt/lib/RT/Users.pm
blob: f4a97268ceffec1b562b0def706789488dc1600a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

# $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Users.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
# (c) 1996-1999 Jesse Vincent <jesse@fsck.com>
# This software is redistributable under the terms of the GNU GPL

=head1 NAME

  RT::Users - Collection of RT::User objects

=head1 SYNOPSIS

  use RT::Users;


=head1 DESCRIPTION


=head1 METHODS

=begin testing

ok(require RT::TestHarness);
ok(require RT::Users);

=end testing

=cut

package RT::Users;
use RT::EasySearch;
@ISA = qw(RT::EasySearch);

# {{{ sub _Init 
sub _Init  {
  my $self = shift;
  $self->{'table'} = "Users";
  $self->{'primary_key'} = "id";

  # By default, order by name
  $self->OrderBy( ALIAS => 'main',
		  FIELD => 'Name',
		  ORDER => 'ASC');

  return ($self->SUPER::_Init(@_));
  
}
# }}}

# {{{ sub _DoSearch 

=head2 _DoSearch

  A subclass of DBIx::SearchBuilder::_DoSearch that makes sure that _Disabled rows never get seen unless
we're explicitly trying to see them.

=cut

sub _DoSearch {
    my $self = shift;
    
    #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
    unless($self->{'find_disabled_rows'}) {
	$self->LimitToEnabled();
    }
    
    return($self->SUPER::_DoSearch(@_));
    
}

# }}}

# {{{ sub NewItem 

sub NewItem  {
  my $self = shift;

  use RT::User;
  my $item = new RT::User($self->CurrentUser);
  return($item);
}
# }}}

# {{{ LimitToEmail
=head2 LimitToEmail

Takes one argument. an email address. limits the returned set to
that email address

=cut

sub LimitToEmail {
    my $self = shift;
    my $addr = shift;
    $self->Limit(FIELD => 'EmailAddress', VALUE => "$addr");
}

# }}}

# {{{ MemberOfGroup

=head2 MemberOfGroup

takes one argument, a group id number. Limits the returned set
to members of a given group

=cut

sub MemberOfGroup {
    my $self = shift;
    my $group = shift;
    
    return ("No group specified") if (!defined $group);

    my $groupalias = $self->NewAlias('GroupMembers');

    $self->Join( ALIAS1 => 'main', FIELD1 => 'id', 
		 ALIAS2 => "$groupalias", FIELD2 => 'Name');
    
    $self->Limit (ALIAS => "$groupalias",
		  FIELD => 'GroupId',
		  VALUE => "$group",
		  OPERATOR => "="
		 );
}

# }}}

# {{{ LimitToPrivileged

=head2 LimitToPrivileged

Limits to users who can be made members of ACLs and groups

=cut

sub LimitToPrivileged {
    my $self = shift;
    $self->Limit( FIELD => 'Privileged',
                  OPERATOR => '=',
                  VALUE => '1');
}

# }}}



# {{{ LimitToSystem

=head2 LimitToSystem

Limits to users who can be granted rights, but who should
never have their rights modified by a user or be made members of groups.

=cut

sub LimitToSystem {
    my $self = shift;
    $self->Limit( FIELD => 'Privileged',
                  OPERATOR => '=',
                  VALUE => '2');
}

# }}}

# {{{ HasQueueRight

=head2 HasQueueRight

Takes a queue id as its first argument.  Queue Id "0" is treated by RT as "applies to all queues"
Takes a specific right as an optional second argument

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.



This should not be used as an ACL check, but only for obtaining lists of
users with explicit rights in a given queue.

=cut

sub HasQueueRight {
    my $self = shift;
    my $queue = shift;
    my $right;
    
    $right = shift if (@_);


    my $acl_alias  = $self->NewAlias('ACL');
    $self->Join( ALIAS1 => 'main',  FIELD1 => 'id',
		 ALIAS2 => $acl_alias, FIELD2 => 'PrincipalId');
    $self->Limit (ALIAS => $acl_alias,
		 FIELD => 'PrincipalType',
		 OPERATOR => '=',
		 VALUE => 'User');


    $self->Limit(ALIAS => $acl_alias,
		 FIELD => 'RightAppliesTo',
		 OPERATOR => '=',
		 VALUE => "$queue");


    $self->Limit(ALIAS => $acl_alias,
		 FIELD => 'RightScope',
		 OPERATOR => '=',
		 ENTRYAGGREGATOR => 'OR',
		 VALUE => 'Queue');


    $self->Limit(ALIAS => $acl_alias,
		 FIELD => 'RightScope',
		 OPERATOR => '=',
		 ENTRYAGGREGATOR => 'OR',
		 VALUE => 'Ticket');


    #TODO: is this being initialized properly if the right isn't there?
    if (defined ($right)) {
	
	$self->Limit(ALIAS => $acl_alias,
		     FIELD => 'RightName',
		     OPERATOR => '=',
		     VALUE => "$right");
	
	
       };


}



# }}}

# {{{ HasSystemRight

=head2 HasSystemRight

Takes one optional argument:
   The name of a System level right.

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

=cut

sub HasSystemRight {
    my $self = shift;
    my $right = shift if (@_);
       my $acl_alias  = $self->NewAlias('ACL');


    $self->Join( ALIAS1 => 'main',  FIELD1 => 'id',
		 ALIAS2 => $acl_alias, FIELD2 => 'PrincipalId');
    $self->Limit (ALIAS => $acl_alias,
		 FIELD => 'PrincipalType',
		 OPERATOR => '=',
		 VALUE => 'User');

    $self->Limit(ALIAS => $acl_alias,
		 FIELD => 'RightScope',
		 OPERATOR => '=',
		 VALUE => 'System');


    #TODO: is this being initialized properly if the right isn't there?
    if (defined ($right)) {
	$self->Limit(ALIAS => $acl_alias,
		     FIELD => 'RightName',
		     OPERATOR => '=',
		     VALUE => "$right");
	
       }

    
}

# }}}

1;