[Rt-commit] rt branch, 4.0/delete-user-attributes-on-reset, created. rt-4.0.4-285-g66033a9

Jason May jasonmay at bestpractical.com
Wed Feb 15 19:20:33 EST 2012


The branch, 4.0/delete-user-attributes-on-reset has been created
        at  66033a9d04439418216f63ec136091de5356a491 (commit)

- Log -----------------------------------------------------------------
commit c7ab02fc86cd0b8cc50930f014333bd4c394e419
Author: Jason May <jasonmay at bestpractical.com>
Date:   Thu Feb 9 18:34:58 2012 -0500

    Provide the option to reset saved search adjustments
    
    This provides a way for users to reset their preferences at the
    saved-search level, which causes the preferences to default to the
    system saved search settings.

diff --git a/share/html/Prefs/Search.html b/share/html/Prefs/Search.html
index fdd9c17..82c431f 100644
--- a/share/html/Prefs/Search.html
+++ b/share/html/Prefs/Search.html
@@ -68,6 +68,14 @@
 
 </form>
 
+<&|/Widgets/TitleBox, title => loc("Reset adjustments") &>
+<form method="post" 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" value="<% loc('Reset to default') %>">
+</form>
+</&>
+
 <%INIT>
 my @actions;
 my $title = loc("Customize").' ';
@@ -81,6 +89,26 @@ 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 $pref = $class->new($session{'CurrentUser'});
+    my ($pref_ok) = $pref->LoadByNameAndObject(
+        Object => $session{'CurrentUser'}->UserObj,
+        Name   => 'Pref-'.$ARGS{'name'},
+    );
+
+    my ($ok, $msg);
+    if ($pref_ok) {
+        ($ok, $msg) = $pref->Delete;
+    }
+    else {
+        # The pref doesn't exist yet, but say it was reset anyway
+        $ok = 1;
+    }
+    push @actions, $ok ? loc('Preferences saved.') : $msg;
+}
+
 $title .= loc ($search->Description, loc ('"N"'));
 my $user = $session{'CurrentUser'}->UserObj;
 my $SearchArg = $user->Preferences($search, $search->Content);

commit accb0e8c599eda615dd77e2bb35995219d18ee44
Author: Jason May <jasonmay at bestpractical.com>
Date:   Wed Dec 7 12:24:38 2011 -0500

    Delete the user's Pref-HomepageSettings attribute on Reset
    
    If the user's Pref-HomepageSettings is there but not empty, it won't
    respect any new changes made to the global homepage settings.
    
    However, the fix is still not visible due to referring to session cache
    checks. The preferences are checked in the session before the database,
    so users will still not see homepage changes.

diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index 55e5cc9..db9d912 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -97,7 +97,9 @@ if ( $ARGS{'UpdateSummaryRows'} ) {
 $ARGS{'SummaryRows'} ||= $user->Preferences('SummaryRows', RT->Config->Get('DefaultSummaryRows'));
 
 if ($ARGS{Reset}) {
-    my ($ok, $msg) = $user->SetPreferences('HomepageSettings', {});
+    my $attr = RT::Attribute->new($session{CurrentUser});
+    $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-HomepageSettings' );
+    my ($ok, $msg) = $attr->Delete;
     push @results, $ok ? loc('Preferences saved.') : $msg;
     delete $session{'my_rt_portlets'};
 }

commit 1d1afe81d516e1c8965f2f7728631e82a739f517
Author: Jason May <jasonmay at bestpractical.com>
Date:   Wed Dec 7 19:18:31 2011 -0500

    Stop caching the default system prefs in the session
    
    This lets users blow away their preferences and track the system
    defaults instead of getting a snapshot of the defaults upon reset.

diff --git a/share/html/Elements/MyRT b/share/html/Elements/MyRT
index 2447fed..3d4417a 100644
--- a/share/html/Elements/MyRT
+++ b/share/html/Elements/MyRT
@@ -70,7 +70,7 @@ my $user = $session{'CurrentUser'}->UserObj;
 $Portlets ||= $session{'my_rt_portlets'};
 unless ( $Portlets ) {
     my ($default_portlets) = RT::System->new($session{'CurrentUser'})->Attributes->Named('HomepageSettings');
-    $Portlets = $session{'my_rt_portlets'} = $user->Preferences(
+    $Portlets = $user->Preferences(
         HomepageSettings => $default_portlets? $default_portlets->Content: {},
     );
 }
diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index db9d912..77c1562 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -104,12 +104,15 @@ if ($ARGS{Reset}) {
     delete $session{'my_rt_portlets'};
 }
 
-unless (exists $session{'my_rt_portlets'}) {
+my $portlets;
+if (exists $session{'my_rt_portlets'}) {
+    $portlets = $session{'my_rt_portlets'};
+}
+else {
     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);
+    $default_portlets = $default_portlets ? $default_portlets->Content : {};
+    $portlets = $user->Preferences('HomepageSettings', $default_portlets);
 }
-my $portlets = $session{'my_rt_portlets'};
 
 my %seen;
 my @items = map ["component-$_", $_], grep !$seen{$_}++, @{RT->Config->Get('HomepageComponents')};

commit 63de0adc2446ced5a0f088b2f85e003c47aa25a2
Author: Jason May <jasonmay at bestpractical.com>
Date:   Mon Dec 12 15:47:05 2011 -0500

    Delete the Pref-SummaryRows attribute to reset everything on MyRT.html

diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index 77c1562..9d9ceb3 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -99,8 +99,15 @@ $ARGS{'SummaryRows'} ||= $user->Preferences('SummaryRows', RT->Config->Get('Defa
 if ($ARGS{Reset}) {
     my $attr = RT::Attribute->new($session{CurrentUser});
     $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-HomepageSettings' );
-    my ($ok, $msg) = $attr->Delete;
-    push @results, $ok ? loc('Preferences saved.') : $msg;
+    my ($hp_ok, $msg) = $attr->Delete;
+    push @results, $msg unless $hp_ok;
+
+    $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-SummaryRows' );
+    (my $sr_ok, $msg) = $attr->Delete;
+    push @results, $msg unless $sr_ok;
+
+    push @results, loc('Preferences saved.') if $hp_ok and $sr_ok;
+
     delete $session{'my_rt_portlets'};
 }
 

commit 2289cf272c621316f2ebfb7f116cf513ee12f67e
Author: Jason May <jasonmay at bestpractical.com>
Date:   Fri Feb 10 15:22:46 2012 -0500

    Add tests for resetting preferences

diff --git a/share/html/Prefs/Search.html b/share/html/Prefs/Search.html
index 82c431f..a060e4d 100644
--- a/share/html/Prefs/Search.html
+++ b/share/html/Prefs/Search.html
@@ -69,10 +69,10 @@
 </form>
 
 <&|/Widgets/TitleBox, title => loc("Reset adjustments") &>
-<form method="post" action="Search.html">
+<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" value="<% loc('Reset to default') %>">
+<input type="submit" class="button" name="ResetSearchOptions" value="<% loc('Reset to default') %>">
 </form>
 </&>
 
diff --git a/t/web/reset_prefs.t b/t/web/reset_prefs.t
new file mode 100644
index 0000000..9ad777f
--- /dev/null
+++ b/t/web/reset_prefs.t
@@ -0,0 +1,57 @@
+#!/usr/bin/perl -w
+use strict;
+
+use RT::Test tests => undef;
+my ($baseurl, $m) = RT::Test->started_ok;
+$m->login;
+my $url = $m->rt_base_url;
+
+#TODO Test resetting RT at a glance
+
+# find a saved search
+my $search;
+{
+    my $system = RT::System->new(RT->SystemUser);
+    while (my $attribute = $system->Attributes->Next) {
+        if ($attribute->Name =~ /^Search - /) {
+            $search = $attribute;
+            last;
+        }
+    }
+}
+
+my $search_name = 'RT::Attribute-'.$search->id;
+my $pref_name = 'Pref-'.$search_name;
+my $uri = URI->new($url.'Prefs/Search.html');
+$uri->query_form(
+    OrderBy => 'Priority|Due', # something different from the default
+    name    => $search_name,
+);
+
+$m->get($uri);
+
+require RT::User;
+my $user = RT::User->new(RT->SystemUser);
+$user->Load('root');
+
+require RT::Attribute;
+my $pref = RT::Attribute->new($user);
+
+my ($pref_created, $pref_exists);
+($pref_exists) = $pref->LoadByNameAndObject(Name => $pref_name, Object => $user);
+ok(!$pref_exists, 'Preference does not exist yet');
+
+$m->form_name('BuildQuery');
+$m->click_button(name => 'Save');
+
+($pref_created) = $pref->LoadByNameAndObject(Name => $pref_name, Object => $user);
+ok($pref_created, 'Preference was successfully created upon saving');
+
+$m->form_name('ResetSearchOptions');
+$m->click_button(name => 'ResetSearchOptions');
+
+$m->form_name('BuildQuery');
+isnt($m->value('OrderBy', 2), 'Due', 'Custom prefs were cleared');
+
+undef $m;
+done_testing();

commit d3f06fc8f499b3eae67175e36c673569c1ab626c
Author: Jason May <jasonmay at bestpractical.com>
Date:   Mon Feb 13 18:59:25 2012 -0500

    Extract pref deletion logic into a RT::User method for reuse

diff --git a/lib/RT/User.pm b/lib/RT/User.pm
index 8a82377..77cb86b 100644
--- a/lib/RT/User.pm
+++ b/lib/RT/User.pm
@@ -1323,6 +1323,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.
diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index 9d9ceb3..af66d2b 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -97,16 +97,22 @@ if ( $ARGS{'UpdateSummaryRows'} ) {
 $ARGS{'SummaryRows'} ||= $user->Preferences('SummaryRows', RT->Config->Get('DefaultSummaryRows'));
 
 if ($ARGS{Reset}) {
+    my $user = $session{CurrentUser}->UserObj;
     my $attr = RT::Attribute->new($session{CurrentUser});
-    $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-HomepageSettings' );
-    my ($hp_ok, $msg) = $attr->Delete;
-    push @results, $msg unless $hp_ok;
 
-    $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-SummaryRows' );
-    (my $sr_ok, $msg) = $attr->Delete;
-    push @results, $msg unless $sr_ok;
+    my $reset_ok = 1;
+    for my $pref_name ('HomepageSettings', 'SummaryRows') {
+        my $prefs = $user->Preferences($pref_name);
+        if ($prefs) {
+            my ($ok, $msg) = $user->DeletePreferences($pref_name);
+            if (!$ok)  {
+                push @results, $msg unless $ok;
+                $reset_ok = 0;
+            }
+        }
+    }
 
-    push @results, loc('Preferences saved.') if $hp_ok and $sr_ok;
+    push @results, loc('Preferences saved.') if $reset_ok;
 
     delete $session{'my_rt_portlets'};
 }
diff --git a/share/html/Prefs/Search.html b/share/html/Prefs/Search.html
index a060e4d..01ef876 100644
--- a/share/html/Prefs/Search.html
+++ b/share/html/Prefs/Search.html
@@ -92,20 +92,7 @@ $search->LoadById ($id);
 
 # If we are resetting prefs, do so before attempting to load them
 if ($ARGS{'Reset'}) {
-    my $pref = $class->new($session{'CurrentUser'});
-    my ($pref_ok) = $pref->LoadByNameAndObject(
-        Object => $session{'CurrentUser'}->UserObj,
-        Name   => 'Pref-'.$ARGS{'name'},
-    );
-
-    my ($ok, $msg);
-    if ($pref_ok) {
-        ($ok, $msg) = $pref->Delete;
-    }
-    else {
-        # The pref doesn't exist yet, but say it was reset anyway
-        $ok = 1;
-    }
+    my ($ok, $msg) = $session{'CurrentUser'}->UserObj->DeletePreferences($ARGS{name});
     push @actions, $ok ? loc('Preferences saved.') : $msg;
 }
 

commit 66033a9d04439418216f63ec136091de5356a491
Author: Jason May <jasonmay at bestpractical.com>
Date:   Wed Feb 15 19:19:58 2012 -0500

    Add tests for resetting RT at a Glance prefs

diff --git a/t/web/reset_prefs.t b/t/web/reset_prefs.t
index 9ad777f..0e29597 100644
--- a/t/web/reset_prefs.t
+++ b/t/web/reset_prefs.t
@@ -53,5 +53,57 @@ $m->click_button(name => 'ResetSearchOptions');
 $m->form_name('BuildQuery');
 isnt($m->value('OrderBy', 2), 'Due', 'Custom prefs were cleared');
 
+$m->get($url.'Prefs/MyRT.html');
+
+diag("Verifying Dashboards are not a default");
+{
+    lacks_dashboards_ok('body-Selected', "'Dashboards' is not a default pref");
+}
+{ undef $m; done_testing(); exit } # XXX
+
+diag("Adding a component to body prefs");
+{
+    $m->form_name('SelectionBox-body');
+    $m->field('body-Available' => 'component-Dashboards');
+    $m->click_button(name => 'Add');
+    has_dashboards_ok('body-Selected', 'Dashboards are now in the prefs');
+}
+
+diag("Resetting the body prefs");
+{
+    $m->submit_form(fields => {Reset => 1});
+
+    lacks_dashboard_ok('body-Selected', 'Dashboards are no longer in the prefs');
+}
+
 undef $m;
 done_testing();
+
+sub _has_dashboards {
+    my $input_name = shift;
+    $m->form_name('SelectionBox-body');
+
+    # Load the potential value of each input in the select
+    my @selected_values = grep { defined }
+                          map { $_->possible_values }
+                          $m->current_form->find_input($input_name);
+
+    my $has_dashboards = grep { $_ eq 'component-Dashboards' } @selected_values;
+    return $has_dashboards;
+}
+
+sub lacks_dashboards_ok {
+    my ($input_name, $message) = @_;
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+    my $has_dashboards = _has_dashboards($input_name);
+    ok(!$has_dashboards, $message);
+}
+
+sub has_dashboards_ok {
+    my ($input_name, $message) = @_;
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+    my $has_dashboards = _has_dashboards($input_name);
+    ok($has_dashboards, $message);
+}

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


More information about the Rt-commit mailing list