[Rt-commit] rt branch, 4.0/reset-rt-at-a-glance, created. rt-4.0.19-66-g5ebef04

Jim Brandt jbrandt at bestpractical.com
Fri Apr 4 11:35:44 EDT 2014


The branch, 4.0/reset-rt-at-a-glance has been created
        at  5ebef047eb784aa923bcf9fcf4750180621754f4 (commit)

- Log -----------------------------------------------------------------
commit 2898cadebedd3651e99fbdf3910f5fdba91dcf38
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Thu Jun 21 13:11:39 2012 -0400

    Add a method to remove a user preference

diff --git a/lib/RT/User.pm b/lib/RT/User.pm
index 95fe9a2..20ce784 100644
--- a/lib/RT/User.pm
+++ b/lib/RT/User.pm
@@ -1390,6 +1390,28 @@ sub SetPreferences {
     }
 }
 
+=head2 DeletePreferences NAME/OBJ VALUE
+
+Delete user preferences associated with given object or name.
+
+=cut
+
+sub DeletePreferences {
+    my $self = shift;
+    my $name = _PrefName( shift );
+
+    return (0, $self->loc("No permission to set preferences"))
+        unless $self->CurrentUserCanModify('Preferences');
+
+    my $attr = RT::Attribute->new( $self->CurrentUser );
+    $attr->LoadByNameAndObject( Object => $self, Name => $name );
+    if ( $attr->Id ) {
+        return $attr->Delete;
+    }
+
+    return (0, $self->loc("Preferences were not found"));
+}
+
 =head2 Stylesheet
 
 Returns a list of valid stylesheets take from preferences.

commit 7c6e2af5e2f5292a19fb3d1fe583bbdb2c3ee22f
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Thu Jun 21 13:38:03 2012 -0400

    Add a "Reset" button to configuration of homepage portlets
    
    This allows the user to reset to the search's defaults.

diff --git a/share/html/Prefs/Search.html b/share/html/Prefs/Search.html
index bb52a62..3f2c404 100644
--- a/share/html/Prefs/Search.html
+++ b/share/html/Prefs/Search.html
@@ -68,6 +68,14 @@
 
 </form>
 
+<&|/Widgets/TitleBox, title => loc("Reset") &>
+<form method="post" name="ResetSearchOptions" action="Search.html">
+<input type="hidden" name="Reset" value="1" />
+<input type="hidden" name="name" value="<%$ARGS{name}%>" class="hidden" />
+<input type="submit" class="button" name="ResetSearchOptions" value="<% loc('Reset to default') %>">
+</form>
+</&>
+
 <%INIT>
 my @actions;
 my $title = loc("Customize").' ';
@@ -81,6 +89,13 @@ Abort('No search specified')
 
 my $search = $class->new ($session{'CurrentUser'});
 $search->LoadById ($id);
+
+# If we are resetting prefs, do so before attempting to load them
+if ($ARGS{'Reset'}) {
+    my ($ok, $msg) = $session{'CurrentUser'}->UserObj->DeletePreferences($ARGS{name});
+    push @actions, $ok ? loc('Preferences reset.') : $msg;
+}
+
 $title .= loc (RT::SavedSearch->EscapeDescription($search->Description), loc ('"N"'));
 my $user = $session{'CurrentUser'}->UserObj;
 my $SearchArg = $user->Preferences($search, $search->Content);

commit ad921e40bc03b9e2390c2645218409c71bb3b745
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Thu Jun 21 13:58:32 2012 -0400

    Remove HomepageSettings preference on "reset" to pick up global defaults
    
    Ensure that the global settings for "RT at a Glance" are taken when a
    user resets their preferences.  Previously, the setting was simply set
    to an empty hash, which meant that the global defaults were not applied.
    
    This causes the reset button to revert current session to the current
    state of the global configuration, which is thereafter cached for the
    duration of the session in $session{'my_rt_portlets'}.

diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index a595ccf..7083dea 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -97,8 +97,12 @@ if ( $ARGS{'UpdateSummaryRows'} ) {
 $ARGS{'SummaryRows'} ||= $user->Preferences('SummaryRows', RT->Config->Get('DefaultSummaryRows'));
 
 if ($ARGS{Reset}) {
-    my ($ok, $msg) = $user->SetPreferences('HomepageSettings', {});
-    push @results, $ok ? loc('Preferences saved.') : $msg;
+    for my $pref_name ('HomepageSettings', 'SummaryRows') {
+        next unless $user->Preferences($pref_name);
+        my ($ok, $msg) = $user->DeletePreferences($pref_name);
+        push @results, $msg unless $ok;
+    }
+    push @results, loc('Preferences saved.') unless @results;
     delete $session{'my_rt_portlets'};
 }
 

commit 9582e25ab2a680229e548a16c00cb4ec0fe932be
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Thu Jun 21 14:10:19 2012 -0400

    Remove the session cache of the "RT at a Glance" portlet lists
    
    The initial commit of "RT at a Glance" configurability (8b8e90d) added a
    caching layer in the session, rather than hit the Attribute for the
    preferences on each request.  Remove this caching, in order to ensure
    that users who have not overridden the preference always get the most
    recent global configuration.  This comes at the cost of an extra 1-2
    Attribute queries on the front page, but as these are well-indexed, this
    negligable performance impact seems worth it to obey the principle of
    least surprise when the admin updates the global configuration.

diff --git a/share/html/Elements/MyRT b/share/html/Elements/MyRT
index c59ec1c..84db949 100644
--- a/share/html/Elements/MyRT
+++ b/share/html/Elements/MyRT
@@ -67,12 +67,10 @@
 my %allowed_components = map {$_ => 1} @{RT->Config->Get('HomepageComponents')};
 
 my $user = $session{'CurrentUser'}->UserObj;
-$Portlets ||= $session{'my_rt_portlets'};
+$Portlets ||= $user->Preferences('HomepageSettings');
 unless ( $Portlets ) {
-    my ($default_portlets) = RT::System->new($session{'CurrentUser'})->Attributes->Named('HomepageSettings');
-    $Portlets = $session{'my_rt_portlets'} = $user->Preferences(
-        HomepageSettings => $default_portlets? $default_portlets->Content: {},
-    );
+    my ($defaults) = RT::System->new($session{'CurrentUser'})->Attributes->Named('HomepageSettings');
+    $Portlets = $defaults ? $defaults->Content : {};
 }
 
 $m->callback( CallbackName => 'MassagePortlets', Portlets => $Portlets );
diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index 7083dea..288df0b 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -103,16 +103,13 @@ if ($ARGS{Reset}) {
         push @results, $msg unless $ok;
     }
     push @results, loc('Preferences saved.') unless @results;
-    delete $session{'my_rt_portlets'};
 }
 
-unless (exists $session{'my_rt_portlets'}) {
-    my ($default_portlets) = RT::System->new($session{'CurrentUser'})->Attributes->Named('HomepageSettings');
-    my $portlets = $default_portlets ? $default_portlets->Content : {};
-
-    $session{'my_rt_portlets'} = $user->Preferences('HomepageSettings', $portlets);
+my $portlets = $user->Preferences('HomepageSettings');
+unless ($portlets) {
+    my ($defaults) = RT::System->new($session{'CurrentUser'})->Attributes->Named('HomepageSettings');
+    $portlets = $defaults ? $defaults->Content : {};
 }
-my $portlets = $session{'my_rt_portlets'};
 
 my %seen;
 my @items = map ["component-$_", loc($_)], grep !$seen{$_}++, @{RT->Config->Get('HomepageComponents')};
@@ -161,7 +158,6 @@ my @panes = $m->comp(
         my ( $conf, $pane ) = @_;
         my ($ok, $msg) = $user->SetPreferences( 'HomepageSettings', $conf );
         push @results, $ok ? loc('Preferences saved for [_1].', $pane) : $msg;
-        delete $session{'my_rt_portlets'};
     }
 );
 

commit 5ebef047eb784aa923bcf9fcf4750180621754f4
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Fri Apr 4 11:30:31 2014 -0400

    Tests for user preferences

diff --git a/t/api/user-prefs.t b/t/api/user-prefs.t
new file mode 100644
index 0000000..a4aa49f
--- /dev/null
+++ b/t/api/user-prefs.t
@@ -0,0 +1,59 @@
+
+use strict;
+use warnings;
+use RT;
+use RT::Test tests => undef;
+
+use_ok( 'RT::User' );
+
+my $create_user = RT::User->new(RT->SystemUser);
+isa_ok($create_user, 'RT::User');
+my ($ret, $msg) = $create_user->Create(Name => 'CreateTest1'.$$,
+    EmailAddress => $$.'create-test-1 at example.com');
+ok ($ret, "Creating user CreateTest1 - " . $msg );
+
+# Create object to operate as the test user
+my $user1 = RT::User->new($create_user);
+($ret, $msg) = $user1->Load($create_user->Id);
+ok ($ret, "Loaded the new user $msg");
+
+diag "Set a search preference";
+my $prefs = {
+     'Order' => 'DESC|ASC|ASC|ASC',
+     'OrderBy' => 'Due',
+     'Format' => '\'<a href="__WebPath__/Ticket/Display.html?id=__id__">__id__</a>/TITLE:#\',
+\'<a href="__WebPath__/Ticket/Display.html?id=__id__">__Subject__</a>/TITLE:Subject\',
+\'__Priority__\',
+\'__QueueName__\',
+\'__ExtendedStatus__\',
+\'__Due__\'',
+     'RowsPerPage' => '50'
+};
+
+ok (!$user1->HasRight( Right => 'ModifySelf', Object => $RT::System), "Can't ModifySelf");
+($ret, $msg) = $user1->SetPreferences("SearchDisplay", $prefs);
+ok( !$ret, "No permission to set preferences");
+ok (($ret, $msg) = $create_user->PrincipalObj->GrantRight( Right => 'ModifySelf'),
+    "Granted ModifySelf");
+($ret, $msg) = $user1->SetPreferences("SearchDisplay", $prefs);
+ok( $ret, "Search preference set");
+
+diag "Fetch preference";
+ok (my $saved_prefs = $user1->Preferences("SearchDisplay"), "Fetched prefs");
+is ($prefs->{OrderBy}, 'Due', "Prefs look ok");
+
+diag "Delete prefs";
+ok (($ret, $msg) = $create_user->PrincipalObj->RevokeRight( Right => 'ModifySelf'),
+    "Revoked ModifySelf");
+($ret, $msg) = $user1->DeletePreferences("SearchDisplay");
+ok( !$ret, "No permission to delete preferences");
+ok (($ret, $msg) = $create_user->PrincipalObj->GrantRight( Right => 'ModifySelf'),
+    "Granted ModifySelf");
+($ret, $msg) = $user1->DeletePreferences("SearchDisplay");
+ok( $ret, "Search preference deleted");
+
+$saved_prefs = $user1->Preferences("SearchDisplay");
+ok (!$saved_prefs, "No saved preferences returned");
+
+done_testing;
+

-----------------------------------------------------------------------


More information about the rt-commit mailing list