import rt 3.8.10
[freeside.git] / rt / lib / RT / System.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2011 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 use base qw/RT::Record/;
73
74 use RT::ACL;
75
76 # System rights are rights granted to the whole system
77 # XXX TODO Can't localize these outside of having an object around.
78 our $RIGHTS = {
79     SuperUser              => 'Do anything and everything',           # loc_pair
80     AdminAllPersonalGroups =>
81       "Create, delete and modify the members of any user's personal groups", # loc_pair
82     AdminOwnPersonalGroups =>
83       'Create, delete and modify the members of personal groups',     # loc_pair
84     AdminUsers     => 'Create, delete and modify users',              # loc_pair
85     ModifySelf     => "Modify one's own RT account",                  # loc_pair
86     DelegateRights =>
87       "Delegate specific rights which have been granted to you.",     # loc_pair
88     ShowConfigTab => "show Configuration tab",     # loc_pair
89     ShowApprovalsTab => "show Approvals tab",     # loc_pair
90     LoadSavedSearch => "allow loading of saved searches",     # loc_pair
91     CreateSavedSearch => "allow creation of saved searches",      # loc_pair
92 };
93
94 # Tell RT::ACE that this sort of object can get acls granted
95 $RT::ACE::OBJECT_TYPES{'RT::System'} = 1;
96
97 foreach my $right ( keys %{$RIGHTS} ) {
98     $RT::ACE::LOWERCASERIGHTNAMES{ lc $right } = $right;
99 }
100
101
102 =head2 AvailableRights
103
104 Returns a hash of available rights for this object.
105 The keys are the right names and the values are a
106 description of what the rights do.
107
108 This method as well returns rights of other RT objects,
109 like L<RT::Queue> or L<RT::Group>. To allow users to apply
110 those rights globally.
111
112 =cut
113
114
115 use RT::CustomField;
116 use RT::Queue;
117 use RT::Group; 
118 sub AvailableRights {
119     my $self = shift;
120
121     my $queue = RT::Queue->new($RT::SystemUser);
122     my $group = RT::Group->new($RT::SystemUser);
123     my $cf    = RT::CustomField->new($RT::SystemUser);
124
125     my $qr = $queue->AvailableRights();
126     my $gr = $group->AvailableRights();
127     my $cr = $cf->AvailableRights();
128
129     # Build a merged list of all system wide rights, queue rights and group rights.
130     my %rights = (%{$RIGHTS}, %{$gr}, %{$qr}, %{$cr});
131
132     return(\%rights);
133 }
134
135 =head2 AddRights C<RIGHT>, C<DESCRIPTION> [, ...]
136
137 Adds the given rights to the list of possible rights.  This method
138 should be called during server startup, not at runtime.
139
140 =cut
141
142 sub AddRights {
143     my $self = shift if ref $_[0] or $_[0] eq __PACKAGE__;
144     my %new = @_;
145     $RIGHTS = { %$RIGHTS, %new };
146     %RT::ACE::LOWERCASERIGHTNAMES = ( %RT::ACE::LOWERCASERIGHTNAMES,
147                                       map { lc($_) => $_ } keys %new);
148 }
149
150 sub _Init {
151     my $self = shift;
152     $self->SUPER::_Init (@_) if @_ && $_[0];
153 }
154
155 =head2 id
156
157 Returns RT::System's id. It's 1. 
158
159 =cut
160
161 *Id = \&id;
162 sub id { return 1 }
163
164 =head2 Load
165
166 Since this object is pretending to be an RT::Record, we need a load method.
167 It does nothing
168
169 =cut
170
171 sub Load    { return 1 }
172 sub Name    { return 'RT System' }
173 sub __Set   { return 0 }
174 sub __Value { return 0 }
175 sub Create  { return 0 }
176 sub Delete  { return 0 }
177
178 sub SubjectTag {
179     my $self = shift;
180     my $queue = shift;
181
182     my $map = $self->FirstAttribute('BrandedSubjectTag');
183     $map = $map->Content if $map;
184     return $queue ? undef : () unless $map;
185
186     return $map->{ $queue->id } if $queue;
187
188     my %seen = ();
189     return grep !$seen{lc $_}++, values %$map;
190 }
191
192 RT::Base->_ImportOverlays();
193
194 1;