rt 4.0.23
[freeside.git] / rt / lib / RT / System.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 =head1 NAME 
50
51 RT::System
52
53 =head1 DESCRIPTION
54
55 RT::System is a simple global object used as a focal point for things
56 that are system-wide.
57
58 It works sort of like an RT::Record, except it's really a single object that has
59 an id of "1" when instantiated.
60
61 This gets used by the ACL system so that you can have rights for the scope "RT::System"
62
63 In the future, there will probably be other API goodness encapsulated here.
64
65 =cut
66
67
68 package RT::System;
69
70 use strict;
71 use warnings;
72
73 use base qw/RT::Record/;
74
75 use RT::ACL;
76
77 # System rights are rights granted to the whole system
78 # XXX TODO Can't localize these outside of having an object around.
79 our $RIGHTS = {
80     SuperUser              => 'Do anything and everything',           # loc_pair
81     AdminUsers     => 'Create, modify and delete users',              # loc_pair
82     ModifySelf     => "Modify one's own RT account",                  # loc_pair
83     ShowConfigTab => "Show Configuration tab",     # loc_pair
84     ShowApprovalsTab => "Show Approvals tab",     # loc_pair
85     ShowGlobalTemplates => "Show global templates",     # loc_pair
86     LoadSavedSearch => "Allow loading of saved searches",     # loc_pair
87     CreateSavedSearch => "Allow creation of saved searches",      # loc_pair
88     ExecuteCode => "Allow writing Perl code in templates, scrips, etc", # loc_pair
89 };
90
91 our $RIGHT_CATEGORIES = {
92     SuperUser              => 'Admin',
93     AdminUsers             => 'Admin',
94     ModifySelf             => 'Staff',
95     ShowConfigTab          => 'Admin',
96     ShowApprovalsTab       => 'Admin',
97     ShowGlobalTemplates    => 'Staff',
98     LoadSavedSearch        => 'General',
99     CreateSavedSearch      => 'General',
100     ExecuteCode            => 'Admin',
101 };
102
103 # Tell RT::ACE that this sort of object can get acls granted
104 $RT::ACE::OBJECT_TYPES{'RT::System'} = 1;
105
106 __PACKAGE__->AddRights(%$RIGHTS);
107 __PACKAGE__->AddRightCategories(%$RIGHT_CATEGORIES);
108
109 =head2 AvailableRights
110
111 Returns a hash of available rights for this object.
112 The keys are the right names and the values are a
113 description of what the rights do.
114
115 This method as well returns rights of other RT objects,
116 like L<RT::Queue> or L<RT::Group>. To allow users to apply
117 those rights globally.
118
119 =cut
120
121
122 use RT::CustomField;
123 use RT::Queue;
124 use RT::Group; 
125 use RT::Class;
126 sub AvailableRights {
127     my $self = shift;
128
129     my $queue = RT::Queue->new(RT->SystemUser);
130     my $group = RT::Group->new(RT->SystemUser);
131     my $cf    = RT::CustomField->new(RT->SystemUser);
132     my $class = RT::Class->new(RT->SystemUser);
133
134     my $qr = $queue->AvailableRights();
135     my $gr = $group->AvailableRights();
136     my $cr = $cf->AvailableRights();
137     my $clr = $class->AvailableRights();
138
139     # Build a merged list of all system wide rights, queue rights and group rights.
140     my %rights = (%{$RIGHTS}, %{$gr}, %{$qr}, %{$cr}, %{$clr});
141     delete $rights{ExecuteCode} if RT->Config->Get('DisallowExecuteCode');
142
143     return(\%rights);
144 }
145
146 =head2 RightCategories
147
148 Returns a hashref where the keys are rights for this type of object and the
149 values are the category (General, Staff, Admin) the right falls into.
150
151 =cut
152
153 sub RightCategories {
154     my $self = shift;
155
156     my $queue = RT::Queue->new(RT->SystemUser);
157     my $group = RT::Group->new(RT->SystemUser);
158     my $cf    = RT::CustomField->new(RT->SystemUser);
159     my $class = RT::Class->new(RT->SystemUser);
160
161     my $qr = $queue->RightCategories();
162     my $gr = $group->RightCategories();
163     my $cr = $cf->RightCategories();
164     my $clr = $class->RightCategories();
165
166     # Build a merged list of all system wide rights, queue rights and group rights.
167     my %rights = (%{$RIGHT_CATEGORIES}, %{$gr}, %{$qr}, %{$cr}, %{$clr});
168
169     return(\%rights);
170 }
171
172 =head2 AddRights C<RIGHT>, C<DESCRIPTION> [, ...]
173
174 Adds the given rights to the list of possible rights.  This method
175 should be called during server startup, not at runtime.
176
177 =cut
178
179 sub AddRights {
180     my $self = shift if ref $_[0] or $_[0] eq __PACKAGE__;
181     my %new = @_;
182     $RIGHTS = { %$RIGHTS, %new };
183     %RT::ACE::LOWERCASERIGHTNAMES = ( %RT::ACE::LOWERCASERIGHTNAMES,
184                                       map { lc($_) => $_ } keys %new);
185 }
186
187 =head2 AddRightCategories C<RIGHT>, C<CATEGORY> [, ...]
188
189 Adds the given right and category pairs to the list of right categories.  This
190 method should be called during server startup, not at runtime.
191
192 =cut
193
194 sub AddRightCategories {
195     my $self = shift if ref $_[0] or $_[0] eq __PACKAGE__;
196     my %new = @_;
197     $RIGHT_CATEGORIES = { %$RIGHT_CATEGORIES, %new };
198 }
199
200 sub _Init {
201     my $self = shift;
202     $self->SUPER::_Init (@_) if @_ && $_[0];
203 }
204
205 =head2 id
206
207 Returns RT::System's id. It's 1. 
208
209 =cut
210
211 *Id = \&id;
212 sub id { return 1 }
213
214 =head2 Load
215
216 Since this object is pretending to be an RT::Record, we need a load method.
217 It does nothing
218
219 =cut
220
221 sub Load    { return 1 }
222 sub Name    { return 'RT System' }
223 sub __Set   { return 0 }
224 sub __Value { return 0 }
225 sub Create  { return 0 }
226 sub Delete  { return 0 }
227
228 sub SubjectTag {
229     my $self = shift;
230     my $queue = shift;
231
232     use Carp;
233     confess "SubjectTag called on $self with $queue" if $queue;
234
235     return $queue->SubjectTag if $queue;
236
237     my $queues = RT::Queues->new( $self->CurrentUser );
238     $queues->Limit( FIELD => 'SubjectTag', OPERATOR => 'IS NOT', VALUE => 'NULL' );
239     return $queues->DistinctFieldValues('SubjectTag');
240 }
241
242 =head2 QueueCacheNeedsUpdate ( 1 )
243
244 Attribute to decide when SelectQueue needs to flush the list of queues
245 and retrieve new ones.  Set when queues are created, enabled/disabled
246 and on certain acl changes.  Should also better understand group management.
247
248 If passed a true value, will update the attribute to be the current time.
249
250 =cut
251
252 sub QueueCacheNeedsUpdate {
253     my $self = shift;
254     my $update = shift;
255
256     if ($update) {
257         return $self->SetAttribute(Name => 'QueueCacheNeedsUpdate', Content => time);
258     } else {
259         my $cache = $self->FirstAttribute('QueueCacheNeedsUpdate');
260         return (defined $cache ? $cache->Content : 0 );
261     }
262 }
263
264 RT::Base->_ImportOverlays();
265
266 1;