1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2015 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;
75 use base qw/RT::SharedSetting/;
78 RT::System::AddRights(
79 SubscribeDashboard => 'Subscribe to dashboards', #loc_pair
81 SeeDashboard => 'View system dashboards', #loc_pair
82 CreateDashboard => 'Create system dashboards', #loc_pair
83 ModifyDashboard => 'Modify system dashboards', #loc_pair
84 DeleteDashboard => 'Delete system dashboards', #loc_pair
86 SeeOwnDashboard => 'View personal dashboards', #loc_pair
87 CreateOwnDashboard => 'Create personal dashboards', #loc_pair
88 ModifyOwnDashboard => 'Modify personal dashboards', #loc_pair
89 DeleteOwnDashboard => 'Delete personal dashboards', #loc_pair
92 RT::System::AddRightCategories(
93 SubscribeDashboard => 'Staff',
95 SeeDashboard => 'General',
96 CreateDashboard => 'Admin',
97 ModifyDashboard => 'Admin',
98 DeleteDashboard => 'Admin',
100 SeeOwnDashboard => 'Staff',
101 CreateOwnDashboard => 'Staff',
102 ModifyOwnDashboard => 'Staff',
103 DeleteOwnDashboard => 'Staff',
108 An object of this class is called "dashboard"
112 sub ObjectName { "dashboard" } # loc
119 return $object->AddAttribute(
120 'Name' => 'Dashboard',
121 'Description' => $args->{'Name'},
122 'Content' => {Panes => $args->{'Panes'}},
126 sub UpdateAttribute {
130 my ($status, $msg) = (1, undef);
131 if (defined $args->{'Panes'}) {
132 ($status, $msg) = $self->{'Attribute'}->SetSubValues(
133 Panes => $args->{'Panes'},
137 if ($status && $args->{'Name'}) {
138 ($status, $msg) = $self->{'Attribute'}->SetDescription($args->{'Name'})
139 unless $self->Name eq $args->{'Name'};
142 if ($status && $args->{'Privacy'}) {
143 my ($new_obj_type, $new_obj_id) = split /-/, $args->{'Privacy'};
144 my ($obj_type, $obj_id) = split /-/, $self->Privacy;
146 my $attr = $self->{'Attribute'};
147 if ($new_obj_type ne $obj_type) {
148 ($status, $msg) = $attr->SetObjectType($new_obj_type);
150 if ($status && $new_obj_id != $obj_id ) {
151 ($status, $msg) = $attr->SetObjectId($new_obj_id);
153 $self->{'Privacy'} = $args->{'Privacy'} if $status;
156 return ($status, $msg);
159 =head2 PostLoadValidate
161 Ensure that the ID corresponds to an actual dashboard object, since it's all
162 attributes under the hood.
166 sub PostLoadValidate {
168 return (0, "Invalid object type") unless $self->{'Attribute'}->Name eq 'Dashboard';
174 Returns a hashref of pane name to portlets
180 return unless ref($self->{'Attribute'}) eq 'RT::Attribute';
181 return $self->{'Attribute'}->SubValue('Panes') || {};
186 Returns the list of this dashboard's portlets, each a hashref with key
187 C<portlet_type> being C<search> or C<component>.
193 return map { @$_ } values %{ $self->Panes };
198 Returns a list of loaded sub-dashboards
205 my $search = RT::Dashboard->new($self->CurrentUser);
206 $search->LoadById($_->{id});
208 } grep { $_->{portlet_type} eq 'dashboard' } $self->Portlets;
213 Returns a list of loaded saved searches
220 my $search = RT::SavedSearch->new($self->CurrentUser);
221 $search->Load($_->{privacy}, $_->{id});
223 } grep { $_->{portlet_type} eq 'search' } $self->Portlets;
226 =head2 ShowSearchName Portlet
228 Returns an array for one saved search, suitable for passing to
229 /Elements/ShowSearch.
237 if ($portlet->{privacy} eq 'RT::System') {
238 return Name => $portlet->{description};
241 return SavedSearch => join('-', $portlet->{privacy}, 'SavedSearch', $portlet->{id});
244 =head2 PossibleHiddenSearches
246 This will return a list of saved searches that are potentially not visible by
247 all users for whom the dashboard is visible. You may pass in a privacy to
248 use instead of the dashboard's privacy.
252 sub PossibleHiddenSearches {
254 my $privacy = shift || $self->Privacy;
256 return grep { !$_->IsVisibleTo($privacy) } $self->Searches, $self->Dashboards;
259 # _PrivacyObjects: returns a list of objects that can be used to load
260 # dashboards from. You probably want to use the wrapper methods like
261 # ObjectsForLoading, ObjectsForCreating, etc.
263 sub _PrivacyObjects {
268 my $CurrentUser = $self->CurrentUser;
269 push @objects, $CurrentUser->UserObj;
271 my $groups = RT::Groups->new($CurrentUser);
272 $groups->LimitToUserDefinedGroups;
273 $groups->WithCurrentUser;
274 push @objects, @{ $groups->ItemsArrayRef };
276 push @objects, RT::System->new($CurrentUser);
283 sub _CurrentUserCan {
285 my $privacy = shift || $self->Privacy;
288 if (!defined($privacy)) {
289 $RT::Logger->debug("No privacy provided to $self->_CurrentUserCan");
293 my $object = $self->_GetObject($privacy);
294 return 0 unless $object;
298 if ($object->isa('RT::User')) { $level = 'Own' }
299 elsif ($object->isa('RT::Group')) { $level = 'Group' }
300 elsif ($object->isa('RT::System')) { $level = '' }
302 $RT::Logger->error("Unknown object $object from privacy $privacy");
306 # users are mildly special-cased, since we actually have to check that
307 # the user is operating on himself
308 if ($object->isa('RT::User')) {
309 return 0 unless $object->Id == $self->CurrentUser->Id;
312 my $right = $args{FullRight}
313 || join('', $args{Right}, $level, 'Dashboard');
315 # all rights, except group rights, are global
316 $object = $RT::System unless $object->isa('RT::Group');
318 return $self->CurrentUser->HasRight(
324 sub CurrentUserCanSee {
328 $self->_CurrentUserCan($privacy, Right => 'See');
331 sub CurrentUserCanCreate {
335 $self->_CurrentUserCan($privacy, Right => 'Create');
338 sub CurrentUserCanModify {
342 $self->_CurrentUserCan($privacy, Right => 'Modify');
345 sub CurrentUserCanDelete {
349 $self->_CurrentUserCan($privacy, Right => 'Delete');
352 sub CurrentUserCanSubscribe {
356 $self->_CurrentUserCan($privacy, FullRight => 'SubscribeDashboard');
361 Returns the L<RT::Attribute> representing the current user's subscription
362 to this dashboard if there is one; otherwise, returns C<undef>.
369 # no subscription to unloaded dashboards
370 return unless $self->id;
372 for my $sub ($self->CurrentUser->UserObj->Attributes->Named('Subscription')) {
373 return $sub if $sub->SubValue('DashboardId') == $self->id;
379 sub ObjectsForLoading {
382 IncludeSuperuserGroups => 1,
387 # If you've been granted the SeeOwnDashboard global right (which you
388 # could have by way of global user right or global group right), you
389 # get to see your own dashboards
390 my $CurrentUser = $self->CurrentUser;
391 push @objects, $CurrentUser->UserObj
392 if $CurrentUser->HasRight(Object => $RT::System, Right => 'SeeOwnDashboard');
394 # Find groups for which: (a) you are a member of the group, and (b)
395 # you have been granted SeeGroupDashboard on (by any means), and (c)
396 # have at least one dashboard
397 my $groups = RT::Groups->new($CurrentUser);
398 $groups->LimitToUserDefinedGroups;
399 $groups->ForWhichCurrentUserHasRight(
400 Right => 'SeeGroupDashboard',
401 IncludeSuperusers => $args{IncludeSuperuserGroups},
403 $groups->WithCurrentUser;
404 my $attrs = $groups->Join(
407 TABLE2 => 'Attributes',
408 FIELD2 => 'ObjectId',
412 FIELD => 'ObjectType',
413 VALUE => 'RT::Group',
418 VALUE => 'Dashboard',
420 push @objects, @{ $groups->ItemsArrayRef };
422 # Finally, if you have been granted the SeeDashboard right (which
423 # you could have by way of global user right or global group right),
424 # you can see system dashboards.
425 push @objects, RT::System->new($CurrentUser)
426 if $CurrentUser->HasRight(Object => $RT::System, Right => 'SeeDashboard');
431 sub CurrentUserCanCreateAny {
435 my $CurrentUser = $self->CurrentUser;
437 if $CurrentUser->HasRight(Object => $RT::System, Right => 'CreateOwnDashboard');
439 my $groups = RT::Groups->new($CurrentUser);
440 $groups->LimitToUserDefinedGroups;
441 $groups->ForWhichCurrentUserHasRight(
442 Right => 'CreateGroupDashboard',
443 IncludeSuperusers => 1,
445 return 1 if $groups->Count;
448 if $CurrentUser->HasRight(Object => $RT::System, Right => 'CreateDashboard');
455 Deletes the dashboard and related subscriptions.
456 Returns a tuple of status and message, where status is true upon success.
463 my ( $status, $msg ) = $self->SUPER::Delete(@_);
465 # delete all the subscriptions
466 my $subscriptions = RT::Attributes->new( RT->SystemUser );
467 $subscriptions->Limit(
469 VALUE => 'Subscription',
471 $subscriptions->Limit(
472 FIELD => 'Description',
473 VALUE => "Subscription to dashboard $id",
475 while ( my $subscription = $subscriptions->Next ) {
476 $subscription->Delete();
480 return ( $status, $msg );
483 RT::Base->_ImportOverlays();