fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-07-12 11:00 am

[dw-free] change modify_caps from LJ function to user method

[commit: http://hg.dwscoalition.org/dw-free/rev/29c9f30d4ad6]

http://bugs.dwscoalition.org/show_bug.cgi?id=2756

Refactoring.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/DW/Pay.pm
  • cgi-bin/LJ/User.pm
  • htdocs/admin/capedit.bml
  • htdocs/misc/beta.bml
--------------------------------------------------------------------------------
diff -r 20062fc96a44 -r 29c9f30d4ad6 cgi-bin/DW/Pay.pm
--- a/cgi-bin/DW/Pay.pm	Mon Jul 12 18:38:07 2010 +0800
+++ b/cgi-bin/DW/Pay.pm	Mon Jul 12 19:06:28 2010 +0800
@@ -710,7 +710,7 @@ sub sync_caps {
     if ( ! $ps || ( ! $ps->{permanent} && $ps->{expiresin} < 0 ) ) {
         # reset back to the default, and turn off all other bits; then set the
         # email count to defined-but-0
-        LJ::modify_caps( $u, [ $default ], [ grep { $_ != $default } @bits ] );
+        $u->modify_caps( [ $default ], [ grep { $_ != $default } @bits ] );
         DW::Pay::update_paid_status( $u, lastemail => 0 );
 
     } else {
@@ -722,7 +722,7 @@ sub sync_caps {
 
         # simply modify it to use the typeid specified, as typeids are bits... but
         # turn off any other bits
-        LJ::modify_caps( $u, [ $ps->{typeid} ], [ grep { $_ != $ps->{typeid} } @bits ] );
+        $u->modify_caps( [ $ps->{typeid} ], [ grep { $_ != $ps->{typeid} } @bits ] );
         DW::Pay::update_paid_status( $u, lastemail => undef );
     }
 
diff -r 20062fc96a44 -r 29c9f30d4ad6 cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm	Mon Jul 12 18:38:07 2010 +0800
+++ b/cgi-bin/LJ/User.pm	Mon Jul 12 19:06:28 2010 +0800
@@ -1860,7 +1860,7 @@ sub add_to_class {
         LJ::Hooks::run_hooks('add_to_class', $u, $class);
     }
 
-    return LJ::modify_caps($u, [$bit], []);
+    return $u->modify_caps( [$bit], [] );
 }
 
 
@@ -2495,6 +2495,79 @@ sub large_journal_icon {
 }
 
 
+# des: Given a list of caps to add and caps to remove, updates a user's caps.
+# args: cap_add, cap_del, res
+# des-cap_add: arrayref of bit numbers to turn on
+# des-cap_del: arrayref of bit numbers to turn off
+# des-res: hashref returned from 'modify_caps' hook
+# returns: updated u object, retrieved from $dbh, then 'caps' key modified
+#          otherwise, returns 0 unless all hooks run properly.
+sub modify_caps {
+    my ( $argu, $cap_add, $cap_del, $res ) = @_;
+    my $userid = LJ::want_userid( $argu );
+    return undef unless $userid;
+
+    $cap_add ||= [];
+    $cap_del ||= [];
+    my %cap_add_mod = ();
+    my %cap_del_mod = ();
+
+    # convert capnames to bit numbers
+    if ( LJ::Hooks::are_hooks( "get_cap_bit" ) ) {
+        foreach my $bit ( @$cap_add, @$cap_del ) {
+            next if $bit =~ /^\d+$/;
+
+            # bit is a magical reference into the array
+            $bit = LJ::Hooks::run_hook( "get_cap_bit", $bit );
+        }
+    }
+
+    # get a u object directly from the db
+    my $u = LJ::load_userid( $userid, "force" );
+
+    # add new caps
+    my $newcaps = int( $u->{caps} );
+    foreach ( @$cap_add ) {
+        my $cap = 1 << $_;
+
+        # about to turn bit on, is currently off?
+        $cap_add_mod{$_} = 1 unless $newcaps & $cap;
+        $newcaps |= $cap;
+    }
+
+    # remove deleted caps
+    foreach ( @$cap_del ) {
+        my $cap = 1 << $_;
+
+        # about to turn bit off, is it currently on?
+        $cap_del_mod{$_} = 1 if $newcaps & $cap;
+        $newcaps &= ~$cap;
+    }
+
+    # run hooks for modified bits
+    if ( LJ::Hooks::are_hooks( "modify_caps" ) ) {
+        $res = LJ::Hooks::run_hook( "modify_caps",
+             { u => $u,
+               newcaps => $newcaps,
+               oldcaps => $u->{caps},
+               cap_on_req  => { map { $_ => 1 } @$cap_add },
+               cap_off_req => { map { $_ => 1 } @$cap_del },
+               cap_on_mod  => \%cap_add_mod,
+               cap_off_mod => \%cap_del_mod } );
+
+        # hook should return a status code
+        return undef unless defined $res;
+    }
+
+    # update user row
+    return 0 unless LJ::update_user( $u, { caps => $newcaps } );
+
+    $u->{caps} = $newcaps;
+    $argu->{caps} = $newcaps;
+    return $u;
+}
+
+
 sub opt_logcommentips {
     my $u = shift;
 
@@ -2701,7 +2774,7 @@ sub remove_from_class {
         LJ::Hooks::run_hooks('remove_from_class', $u, $class);
     }
 
-    return LJ::modify_caps($u, [], [$bit]);
+    return $u->modify_caps( [], [$bit] );
 }
 
 
@@ -6152,7 +6225,6 @@ use Carp;
 ###  4. Login, Session, and Rename Functions
 ###  5. Database and Memcache Functions
 ###  6. What the App Shows to Users
-###  7. Userprops, Caps, and Displaying Content to Others
 ###  8. Formatting Content Shown to Users
 ###  9. Logging and Recording Actions
 ###  12. Comment-Related Functions
@@ -7412,89 +7484,6 @@ sub ljuser
 
 
 ########################################################################
-###  7. Userprops, Caps, and Displaying Content to Others
-
-=head2 Userprops, Caps, and Displaying Content to Others (LJ)
-=cut
-
-# <LJFUNC>
-# name: LJ::modify_caps
-# des: Given a list of caps to add and caps to remove, updates a user's caps.
-# args: uuid, cap_add, cap_del, res
-# des-cap_add: arrayref of bit numbers to turn on
-# des-cap_del: arrayref of bit numbers to turn off
-# des-res: hashref returned from 'modify_caps' hook
-# returns: updated u object, retrieved from $dbh, then 'caps' key modified
-#          otherwise, returns 0 unless all hooks run properly.
-# </LJFUNC>
-sub modify_caps {
-    my ($argu, $cap_add, $cap_del, $res) = @_;
-    my $userid = LJ::want_userid($argu);
-    return undef unless $userid;
-
-    $cap_add ||= [];
-    $cap_del ||= [];
-    my %cap_add_mod = ();
-    my %cap_del_mod = ();
-
-    # convert capnames to bit numbers
-    if (LJ::Hooks::are_hooks("get_cap_bit")) {
-        foreach my $bit (@$cap_add, @$cap_del) {
-            next if $bit =~ /^\d+$/;
-
-            # bit is a magical reference into the array
-            $bit = LJ::Hooks::run_hook("get_cap_bit", $bit);
-        }
-    }
-
-    # get a u object directly from the db
-    my $u = LJ::load_userid($userid, "force");
-
-    # add new caps
-    my $newcaps = int($u->{'caps'});
-    foreach (@$cap_add) {
-        my $cap = 1 << $_;
-
-        # about to turn bit on, is currently off?
-        $cap_add_mod{$_} = 1 unless $newcaps & $cap;
-        $newcaps |= $cap;
-    }
-
-    # remove deleted caps
-    foreach (@$cap_del) {
-        my $cap = 1 << $_;
-
-        # about to turn bit off, is it currently on?
-        $cap_del_mod{$_} = 1 if $newcaps & $cap;
-        $newcaps &= ~$cap;
-    }
-
-    # run hooks for modified bits
-    if (LJ::Hooks::are_hooks("modify_caps")) {
-        $res = LJ::Hooks::run_hook("modify_caps",
-                            { 'u' => $u,
-                              'newcaps' => $newcaps,
-                              'oldcaps' => $u->{'caps'},
-                              'cap_on_req'  => { map { $_ => 1 } @$cap_add },
-                              'cap_off_req' => { map { $_ => 1 } @$cap_del },
-                              'cap_on_mod'  => \%cap_add_mod,
-                              'cap_off_mod' => \%cap_del_mod,
-                          });
-
-        # hook should return a status code
-        return undef unless defined $res;
-    }
-
-    # update user row
-    return 0 unless LJ::update_user($u, { 'caps' => $newcaps });
-
-    $u->{caps} = $newcaps;
-    $argu->{caps} = $newcaps if ref $argu; # FIXME: temp hack
-    return $u;
-}
-
-
-########################################################################
 ###  8. Formatting Content Shown to Users
 
 =head2 Formatting Content Shown to Users (LJ)
diff -r 20062fc96a44 -r 29c9f30d4ad6 htdocs/admin/capedit.bml
--- a/htdocs/admin/capedit.bml	Mon Jul 12 18:38:07 2010 +0800
+++ b/htdocs/admin/capedit.bml	Mon Jul 12 19:06:28 2010 +0800
@@ -76,7 +76,7 @@ _c?>
      LJ::statushistory_add($u->{'userid'}, $remote->{'userid'},
                            "capedit", "add: $add_txt, del: $del_txt\n");
 
-     LJ::modify_caps($u, \@cap_add, \@cap_del)
+     $u->modify_caps( \@cap_add, \@cap_del )
          or return"<b>Error:</b> Unable to modify caps.";
 
      # $u->{caps} is now updated in memory for later in this request
diff -r 20062fc96a44 -r 29c9f30d4ad6 htdocs/misc/beta.bml
--- a/htdocs/misc/beta.bml	Mon Jul 12 18:38:07 2010 +0800
+++ b/htdocs/misc/beta.bml	Mon Jul 12 19:06:28 2010 +0800
@@ -52,7 +52,7 @@ body<=
             }->{$t}
                 or return "Invalid caps.";
 
-            LJ::modify_caps( $u, @$caps );
+            $u->modify_caps( @$caps );
 
             return BML::redirect( "$LJ::SITEROOT/misc/beta?authas=" . $u->user );
         }
--------------------------------------------------------------------------------