1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
51 RT::Dashboard - an API for saving and retrieving dashboards
59 Dashboard is an object that can belong to either an RT::User or an
60 RT::Group. It consists of an ID, a name, and a number of
61 saved searches and portlets.
68 package RT::Dashboard;
73 use base qw/RT::SharedSetting/;
78 'RT::System'->AddRight( Staff => SubscribeDashboard => 'Subscribe to dashboards'); # loc
80 'RT::System'->AddRight( General => SeeDashboard => 'View system dashboards'); # loc
81 'RT::System'->AddRight( Admin => CreateDashboard => 'Create system dashboards'); # loc
82 'RT::System'->AddRight( Admin => ModifyDashboard => 'Modify system dashboards'); # loc
83 'RT::System'->AddRight( Admin => DeleteDashboard => 'Delete system dashboards'); # loc
85 'RT::System'->AddRight( Staff => SeeOwnDashboard => 'View personal dashboards'); # loc
86 'RT::System'->AddRight( Staff => CreateOwnDashboard => 'Create personal dashboards'); # loc
87 'RT::System'->AddRight( Staff => ModifyOwnDashboard => 'Modify personal dashboards'); # loc
88 'RT::System'->AddRight( Staff => DeleteOwnDashboard => 'Delete personal dashboards'); # loc
93 An object of this class is called "dashboard"
97 sub ObjectName { "dashboard" } # loc
104 return $object->AddAttribute(
105 'Name' => 'Dashboard',
106 'Description' => $args->{'Name'},
107 'Content' => {Panes => $args->{'Panes'}},
111 sub UpdateAttribute {
115 my ($status, $msg) = (1, undef);
116 if (defined $args->{'Panes'}) {
117 ($status, $msg) = $self->{'Attribute'}->SetSubValues(
118 Panes => $args->{'Panes'},
122 if ($status && $args->{'Name'}) {
123 ($status, $msg) = $self->{'Attribute'}->SetDescription($args->{'Name'})
124 unless $self->Name eq $args->{'Name'};
127 if ($status && $args->{'Privacy'}) {
128 my ($new_obj_type, $new_obj_id) = split /-/, $args->{'Privacy'};
129 my ($obj_type, $obj_id) = split /-/, $self->Privacy;
131 my $attr = $self->{'Attribute'};
132 if ($new_obj_type ne $obj_type) {
133 ($status, $msg) = $attr->SetObjectType($new_obj_type);
135 if ($status && $new_obj_id != $obj_id ) {
136 ($status, $msg) = $attr->SetObjectId($new_obj_id);
138 $self->{'Privacy'} = $args->{'Privacy'} if $status;
141 return ($status, $msg);
144 =head2 PostLoadValidate
146 Ensure that the ID corresponds to an actual dashboard object, since it's all
147 attributes under the hood.
151 sub PostLoadValidate {
153 return (0, "Invalid object type") unless $self->{'Attribute'}->Name eq 'Dashboard';
159 Returns a hashref of pane name to portlets
165 return unless ref($self->{'Attribute'}) eq 'RT::Attribute';
166 return $self->{'Attribute'}->SubValue('Panes') || {};
171 Returns the list of this dashboard's portlets, each a hashref with key
172 C<portlet_type> being C<search> or C<component>.
178 return map { @$_ } values %{ $self->Panes };
183 Returns a list of loaded sub-dashboards
190 my $search = RT::Dashboard->new($self->CurrentUser);
191 $search->LoadById($_->{id});
193 } grep { $_->{portlet_type} eq 'dashboard' } $self->Portlets;
198 Returns a list of loaded saved searches
205 my $search = RT::SavedSearch->new($self->CurrentUser);
206 $search->Load($_->{privacy}, $_->{id});
208 } grep { $_->{portlet_type} eq 'search' } $self->Portlets;
211 =head2 ShowSearchName Portlet
213 Returns an array for one saved search, suitable for passing to
214 /Elements/ShowSearch.
222 if ($portlet->{privacy} eq 'RT::System') {
223 return Name => $portlet->{description};
226 return SavedSearch => join('-', $portlet->{privacy}, 'SavedSearch', $portlet->{id});
229 =head2 PossibleHiddenSearches
231 This will return a list of saved searches that are potentially not visible by
232 all users for whom the dashboard is visible. You may pass in a privacy to
233 use instead of the dashboard's privacy.
237 sub PossibleHiddenSearches {
239 my $privacy = shift || $self->Privacy;
241 return grep { !$_->IsVisibleTo($privacy) } $self->Searches, $self->Dashboards;
244 # _PrivacyObjects: returns a list of objects that can be used to load
245 # dashboards from. You probably want to use the wrapper methods like
246 # ObjectsForLoading, ObjectsForCreating, etc.
248 sub _PrivacyObjects {
253 my $CurrentUser = $self->CurrentUser;
254 push @objects, $CurrentUser->UserObj;
256 my $groups = RT::Groups->new($CurrentUser);
257 $groups->LimitToUserDefinedGroups;
258 $groups->WithCurrentUser;
259 push @objects, @{ $groups->ItemsArrayRef };
261 push @objects, RT::System->new($CurrentUser);
268 sub _CurrentUserCan {
270 my $privacy = shift || $self->Privacy;
273 if (!defined($privacy)) {
274 $RT::Logger->debug("No privacy provided to $self->_CurrentUserCan");
278 my $object = $self->_GetObject($privacy);
279 return 0 unless $object;
283 if ($object->isa('RT::User')) { $level = 'Own' }
284 elsif ($object->isa('RT::Group')) { $level = 'Group' }
285 elsif ($object->isa('RT::System')) { $level = '' }
287 $RT::Logger->error("Unknown object $object from privacy $privacy");
291 # users are mildly special-cased, since we actually have to check that
292 # the user is operating on himself
293 if ($object->isa('RT::User')) {
294 return 0 unless $object->Id == $self->CurrentUser->Id;
297 my $right = $args{FullRight}
298 || join('', $args{Right}, $level, 'Dashboard');
300 # all rights, except group rights, are global
301 $object = $RT::System unless $object->isa('RT::Group');
303 return $self->CurrentUser->HasRight(
309 sub CurrentUserCanSee {
313 $self->_CurrentUserCan($privacy, Right => 'See');
316 sub CurrentUserCanCreate {
320 $self->_CurrentUserCan($privacy, Right => 'Create');
323 sub CurrentUserCanModify {
327 $self->_CurrentUserCan($privacy, Right => 'Modify');
330 sub CurrentUserCanDelete {
334 $self->_CurrentUserCan($privacy, Right => 'Delete');
337 sub CurrentUserCanSubscribe {
341 $self->_CurrentUserCan($privacy, FullRight => 'SubscribeDashboard');
346 Returns the L<RT::Attribute> representing the current user's subscription
347 to this dashboard if there is one; otherwise, returns C<undef>.
354 # no subscription to unloaded dashboards
355 return unless $self->id;
357 for my $sub ($self->CurrentUser->UserObj->Attributes->Named('Subscription')) {
358 return $sub if $sub->SubValue('DashboardId') == $self->id;
364 sub ObjectsForLoading {
367 IncludeSuperuserGroups => 1,
372 # If you've been granted the SeeOwnDashboard global right (which you
373 # could have by way of global user right or global group right), you
374 # get to see your own dashboards
375 my $CurrentUser = $self->CurrentUser;
376 push @objects, $CurrentUser->UserObj
377 if $CurrentUser->HasRight(Object => $RT::System, Right => 'SeeOwnDashboard');
379 # Find groups for which: (a) you are a member of the group, and (b)
380 # you have been granted SeeGroupDashboard on (by any means), and (c)
381 # have at least one dashboard
382 my $groups = RT::Groups->new($CurrentUser);
383 $groups->LimitToUserDefinedGroups;
384 $groups->ForWhichCurrentUserHasRight(
385 Right => 'SeeGroupDashboard',
386 IncludeSuperusers => $args{IncludeSuperuserGroups},
388 $groups->WithCurrentUser;
389 my $attrs = $groups->Join(
392 TABLE2 => 'Attributes',
393 FIELD2 => 'ObjectId',
397 FIELD => 'ObjectType',
398 VALUE => 'RT::Group',
403 VALUE => 'Dashboard',
405 push @objects, @{ $groups->ItemsArrayRef };
407 # Finally, if you have been granted the SeeDashboard right (which
408 # you could have by way of global user right or global group right),
409 # you can see system dashboards.
410 push @objects, RT::System->new($CurrentUser)
411 if $CurrentUser->HasRight(Object => $RT::System, Right => 'SeeDashboard');
416 sub CurrentUserCanCreateAny {
420 my $CurrentUser = $self->CurrentUser;
422 if $CurrentUser->HasRight(Object => $RT::System, Right => 'CreateOwnDashboard');
424 my $groups = RT::Groups->new($CurrentUser);
425 $groups->LimitToUserDefinedGroups;
426 $groups->ForWhichCurrentUserHasRight(
427 Right => 'CreateGroupDashboard',
428 IncludeSuperusers => 1,
430 return 1 if $groups->Count;
433 if $CurrentUser->HasRight(Object => $RT::System, Right => 'CreateDashboard');
440 Deletes the dashboard and related subscriptions.
441 Returns a tuple of status and message, where status is true upon success.
448 my ( $status, $msg ) = $self->SUPER::Delete(@_);
450 # delete all the subscriptions
451 my $subscriptions = RT::Attributes->new( RT->SystemUser );
452 $subscriptions->Limit(
454 VALUE => 'Subscription',
456 $subscriptions->Limit(
457 FIELD => 'Description',
458 VALUE => "Subscription to dashboard $id",
460 while ( my $subscription = $subscriptions->Next ) {
461 $subscription->Delete();
465 return ( $status, $msg );
468 RT::Base->_ImportOverlays();