1# BEGIN BPS TAGGED BLOCK {{{
2#
3# COPYRIGHT:
4#
5# This software is Copyright (c) 1996-2021 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
49use strict;
50use warnings;
51
52package RT::ObjectCustomRole;
53use base 'RT::Record::AddAndSort';
54
55use RT::CustomRole;
56use RT::ObjectCustomRoles;
57
58=head1 NAME
59
60RT::ObjectCustomRole - record representing addition of a custom role to a queue
61
62=head1 DESCRIPTION
63
64This record is created if you want to add a custom role to a queue.
65
66Inherits methods from L<RT::Record::AddAndSort>.
67
68For most operations it's better to use methods in L<RT::CustomRole>.
69
70=head1 METHODS
71
72=head2 Table
73
74Returns table name for records of this class.
75
76=cut
77
78sub Table {'ObjectCustomRoles'}
79
80=head2 ObjectCollectionClass
81
82Returns class name of collection of records custom roles can be added to.
83Now it's only L<RT::Queue>, so 'RT::Queues' is returned.
84
85=cut
86
87sub ObjectCollectionClass {'RT::Queues'}
88
89=head2 CustomRoleObj
90
91Returns the L<RT::CustomRole> object with the id returned by L</CustomRole>
92
93=cut
94
95sub CustomRoleObj {
96    my $self = shift;
97    my $id = shift || $self->CustomRole;
98    my $obj = RT::CustomRole->new( $self->CurrentUser );
99    $obj->Load( $id );
100    return $obj;
101}
102
103=head2 QueueObj
104
105Returns the L<RT::Queue> object which this ObjectCustomRole is added to
106
107=cut
108
109sub QueueObj {
110    my $self = shift;
111    my $queue = RT::Queue->new($self->CurrentUser);
112    $queue->Load($self->ObjectId);
113    return $queue;
114}
115
116=head2 Add
117
118Adds the custom role to the queue and creates (or re-enables) that queue's role
119group.
120
121=cut
122
123sub Add {
124    my $self = shift;
125
126    $RT::Handle->BeginTransaction;
127
128    my ($ok, $msg) = $self->SUPER::Add(@_);
129    unless ($ok) {
130        $RT::Handle->Rollback;
131        $RT::Logger->error("Couldn't add ObjectCustomRole: $msg");
132        return(undef);
133    }
134
135    my $queue = $self->QueueObj;
136    my $role = $self->CustomRoleObj;
137
138    # see if we already have this role group (which can happen if you
139    # add a role to a queue, remove it, then add it back in)
140    my $existing = RT::Group->new($self->CurrentUser);
141    $existing->LoadRoleGroup(
142        Name   => $role->GroupType,
143        Object => $queue,
144    );
145
146    if ($existing->Id) {
147        RT::Principal->InvalidateACLCache();
148    }
149    else {
150        my $group = RT::Group->new($self->CurrentUser);
151        my ($ok, $msg) = $group->CreateRoleGroup(
152            Name   => $role->GroupType,
153            Object => $queue,
154        );
155
156        unless ($ok) {
157            $RT::Handle->Rollback;
158            $RT::Logger->error("Couldn't create a role group: $msg");
159            return(undef);
160        }
161    }
162
163    $RT::Handle->Commit;
164
165    return ($ok, $msg);
166}
167
168
169=head2 Delete
170
171Removes the custom role from the queue and disables that queue's role group.
172
173=cut
174
175sub Delete {
176    my $self = shift;
177
178    # remove the ObjectCustomRole record
179    my ($ok, $msg) = $self->SUPER::Delete(@_);
180    unless ($ok) {
181        $RT::Logger->error("Couldn't add ObjectCustomRole: $msg");
182        return(undef);
183    }
184
185    RT::Principal->InvalidateACLCache();
186
187    return ($ok, $msg);
188}
189
190sub FindDependencies {
191    my $self = shift;
192    my ($walker, $deps) = @_;
193
194    $self->SUPER::FindDependencies($walker, $deps);
195
196    $deps->Add( out => $self->CustomRoleObj );
197    $deps->Add( out => $self->QueueObj );
198}
199
200sub Serialize {
201    my $self = shift;
202    my %args = (@_);
203    my %store = $self->SUPER::Serialize(@_);
204
205    if ($store{ObjectId}) {
206        my $obj = RT::Queue->new( RT->SystemUser );
207        $obj->Load( $store{ObjectId} );
208        $store{ObjectId} = \($obj->UID);
209    }
210    return %store;
211}
212
213=head2 id
214
215Returns the current value of id.
216(In the database, id is stored as int(11).)
217
218
219=cut
220
221
222=head2 CustomRole
223
224Returns the current value of CustomRole.
225(In the database, CustomRole is stored as int(11).)
226
227=head2 SetCustomRole VALUE
228
229
230Set CustomRole to VALUE.
231Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
232(In the database, CustomRole will be stored as a int(11).)
233
234=cut
235
236=head2 ObjectId
237
238Returns the current value of ObjectId.
239(In the database, ObjectId is stored as int(11).)
240
241
242
243=head2 SetObjectId VALUE
244
245
246Set ObjectId to VALUE.
247Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
248(In the database, ObjectId will be stored as a int(11).)
249
250
251=cut
252
253
254=head2 SortOrder
255
256Returns the current value of SortOrder.
257(In the database, SortOrder is stored as int(11).)
258
259
260
261=head2 SetSortOrder VALUE
262
263
264Set SortOrder to VALUE.
265Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
266(In the database, SortOrder will be stored as a int(11).)
267
268
269=cut
270
271
272=head2 Creator
273
274Returns the current value of Creator.
275(In the database, Creator is stored as int(11).)
276
277
278=cut
279
280
281=head2 Created
282
283Returns the current value of Created.
284(In the database, Created is stored as datetime.)
285
286
287=cut
288
289
290=head2 LastUpdatedBy
291
292Returns the current value of LastUpdatedBy.
293(In the database, LastUpdatedBy is stored as int(11).)
294
295
296=cut
297
298
299=head2 LastUpdated
300
301Returns the current value of LastUpdated.
302(In the database, LastUpdated is stored as datetime.)
303
304
305=cut
306
307
308
309sub _CoreAccessible {
310    {
311
312        id =>
313                {read => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
314        CustomRole =>
315                {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
316        ObjectId =>
317                {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
318        SortOrder =>
319                {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
320        Creator =>
321                {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
322        Created =>
323                {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
324        LastUpdatedBy =>
325                {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
326        LastUpdated =>
327                {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
328
329 }
330};
331
332RT::Base->_ImportOverlays();
333
3341;
335