]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Initial user role controls on profile pages, for owner to add/remove administrator...
authorBrion Vibber <brion@pobox.com>
Wed, 3 Mar 2010 23:43:49 +0000 (15:43 -0800)
committerBrion Vibber <brion@pobox.com>
Wed, 3 Mar 2010 23:43:49 +0000 (15:43 -0800)
Buttons need to be themed.

actions/grantrole.php [new file with mode: 0644]
actions/revokerole.php [new file with mode: 0644]
classes/Profile.php
classes/Profile_role.php
lib/grantroleform.php [new file with mode: 0644]
lib/revokeroleform.php [new file with mode: 0644]
lib/right.php
lib/router.php
lib/userprofile.php

diff --git a/actions/grantrole.php b/actions/grantrole.php
new file mode 100644 (file)
index 0000000..cd6bd4d
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Action class to sandbox an abusive user
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Action
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>
+ * @copyright 2009 StatusNet, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link      http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+    exit(1);
+}
+
+/**
+ * Sandbox a user.
+ *
+ * @category Action
+ * @package  StatusNet
+ * @author   Evan Prodromou <evan@status.net>
+ * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
+ * @link     http://status.net/
+ */
+
+class GrantRoleAction extends ProfileFormAction
+{
+    /**
+     * Check parameters
+     *
+     * @param array $args action arguments (URL, GET, POST)
+     *
+     * @return boolean success flag
+     */
+
+    function prepare($args)
+    {
+        if (!parent::prepare($args)) {
+            return false;
+        }
+        
+        $this->role = $this->arg('role');
+        if (!Profile_role::isValid($this->role)) {
+            $this->clientError(_("Invalid role."));
+            return false;
+        }
+        if (!Profile_role::isSettable($this->role)) {
+            $this->clientError(_("This role is reserved and cannot be set."));
+            return false;
+        }
+
+        $cur = common_current_user();
+
+        assert(!empty($cur)); // checked by parent
+
+        if (!$cur->hasRight(Right::GRANTROLE)) {
+            $this->clientError(_("You cannot grant user roles on this site."));
+            return false;
+        }
+
+        assert(!empty($this->profile)); // checked by parent
+
+        if ($this->profile->hasRole($this->role)) {
+            $this->clientError(_("User already has this role."));
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Sandbox a user.
+     *
+     * @return void
+     */
+
+    function handlePost()
+    {
+        $this->profile->grantRole($this->role);
+    }
+}
diff --git a/actions/revokerole.php b/actions/revokerole.php
new file mode 100644 (file)
index 0000000..b78c1c2
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Action class to sandbox an abusive user
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Action
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>
+ * @copyright 2009 StatusNet, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link      http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+    exit(1);
+}
+
+/**
+ * Sandbox a user.
+ *
+ * @category Action
+ * @package  StatusNet
+ * @author   Evan Prodromou <evan@status.net>
+ * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
+ * @link     http://status.net/
+ */
+
+class RevokeRoleAction extends ProfileFormAction
+{
+    /**
+     * Check parameters
+     *
+     * @param array $args action arguments (URL, GET, POST)
+     *
+     * @return boolean success flag
+     */
+
+    function prepare($args)
+    {
+        if (!parent::prepare($args)) {
+            return false;
+        }
+        
+        $this->role = $this->arg('role');
+        if (!Profile_role::isValid($this->role)) {
+            $this->clientError(_("Invalid role."));
+            return false;
+        }
+        if (!Profile_role::isSettable($this->role)) {
+            $this->clientError(_("This role is reserved and cannot be set."));
+            return false;
+        }
+
+        $cur = common_current_user();
+
+        assert(!empty($cur)); // checked by parent
+
+        if (!$cur->hasRight(Right::REVOKEROLE)) {
+            $this->clientError(_("You cannot revoke user roles on this site."));
+            return false;
+        }
+
+        assert(!empty($this->profile)); // checked by parent
+
+        if (!$this->profile->hasRole($this->role)) {
+            $this->clientError(_("User doesn't have this role."));
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Sandbox a user.
+     *
+     * @return void
+     */
+
+    function handlePost()
+    {
+        $this->profile->revokeRole($this->role);
+    }
+}
index 9c2fa7a0c5664ca49d73f66cdc74db5178a3e886..0322c935886028a09858653be49bd528fef71f8d 100644 (file)
@@ -743,6 +743,10 @@ class Profile extends Memcached_DataObject
             case Right::CONFIGURESITE:
                 $result = $this->hasRole(Profile_role::ADMINISTRATOR);
                 break;
+            case Right::GRANTROLE:
+            case Right::REVOKEROLE:
+                $result = $this->hasRole(Profile_role::OWNER);
+                break;
             case Right::NEWNOTICE:
             case Right::NEWMESSAGE:
             case Right::SUBSCRIBE:
index bf2c453ed0bf719f9cc00d5ff679c5cac1f6d708..d0a0b31f0f86b6662efc8efeec85d72940b6803e 100644 (file)
@@ -53,4 +53,21 @@ class Profile_role extends Memcached_DataObject
     const ADMINISTRATOR = 'administrator';
     const SANDBOXED     = 'sandboxed';
     const SILENCED      = 'silenced';
+
+    public static function isValid($role)
+    {
+        // @fixme could probably pull this from class constants
+        $known = array(self::OWNER,
+                       self::MODERATOR,
+                       self::ADMINISTRATOR,
+                       self::SANDBOXED,
+                       self::SILENCED);
+        return in_array($role, $known);
+    }
+
+    public static function isSettable($role)
+    {
+        $allowedRoles = array('administrator', 'moderator');
+        return self::isValid($role) && in_array($role, $allowedRoles);
+    }
 }
diff --git a/lib/grantroleform.php b/lib/grantroleform.php
new file mode 100644 (file)
index 0000000..b5f9527
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Form for granting a role
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Form
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>, Brion Vibber <brion@status.net>
+ * @copyright 2009-2010 StatusNet, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link      http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+    exit(1);
+}
+
+/**
+ * Form for sandboxing a user
+ *
+ * @category Form
+ * @package  StatusNet
+ * @author   Evan Prodromou <evan@status.net>
+ * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link     http://status.net/
+ *
+ * @see      UnSandboxForm
+ */
+
+class GrantRoleForm extends ProfileActionForm
+{
+    function __construct($role, $label, $writer, $profile, $r2args)
+    {
+        parent::__construct($writer, $profile, $r2args);
+        $this->role = $role;
+        $this->label = $label;
+    }
+
+    /**
+     * Action this form provides
+     *
+     * @return string Name of the action, lowercased.
+     */
+
+    function target()
+    {
+        return 'grantrole';
+    }
+
+    /**
+     * Title of the form
+     *
+     * @return string Title of the form, internationalized
+     */
+
+    function title()
+    {
+        return $this->label;
+    }
+
+    function formData()
+    {
+        parent::formData();
+        $this->out->hidden('role', $this->role);
+    }
+
+    /**
+     * Description of the form
+     *
+     * @return string description of the form, internationalized
+     */
+
+    function description()
+    {
+        return sprintf(_('Grant this user the "%s" role'), $this->label);
+    }
+}
diff --git a/lib/revokeroleform.php b/lib/revokeroleform.php
new file mode 100644 (file)
index 0000000..ec24b99
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Form for revoking a role
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Form
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>, Brion Vibber <brion@status.net>
+ * @copyright 2009-2010 StatusNet, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link      http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+    exit(1);
+}
+
+/**
+ * Form for sandboxing a user
+ *
+ * @category Form
+ * @package  StatusNet
+ * @author   Evan Prodromou <evan@status.net>
+ * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link     http://status.net/
+ *
+ * @see      UnSandboxForm
+ */
+
+class RevokeRoleForm extends ProfileActionForm
+{
+    function __construct($role, $label, $writer, $profile, $r2args)
+    {
+        parent::__construct($writer, $profile, $r2args);
+        $this->role = $role;
+        $this->label = $label;
+    }
+
+    /**
+     * Action this form provides
+     *
+     * @return string Name of the action, lowercased.
+     */
+
+    function target()
+    {
+        return 'revokerole';
+    }
+
+    /**
+     * Title of the form
+     *
+     * @return string Title of the form, internationalized
+     */
+
+    function title()
+    {
+        return $this->label;
+    }
+
+    function formData()
+    {
+        parent::formData();
+        $this->out->hidden('role', $this->role);
+    }
+
+    /**
+     * Description of the form
+     *
+     * @return string description of the form, internationalized
+     */
+
+    function description()
+    {
+        return sprintf(_('Revoke the "%s" role from this user'), $this->label);
+    }
+}
index 4e9c5a918dbe02b2e2795c63167dd9a99d7db8e7..deb451fde90c96c5f9cf5b355b3bec11dbc43528 100644 (file)
@@ -58,5 +58,7 @@ class Right
     const EMAILONSUBSCRIBE   = 'emailonsubscribe';
     const EMAILONFAVE        = 'emailonfave';
     const MAKEGROUPADMIN     = 'makegroupadmin';
+    const GRANTROLE          = 'grantrole';
+    const REVOKEROLE         = 'revokerole';
 }
 
index 7e8e22a7dba0985c89a33f72439044c01d3e7994..15f88c959d104c25e5b16bbc61491ad20ff228a4 100644 (file)
@@ -98,6 +98,7 @@ class Router
                           'groupblock', 'groupunblock',
                           'sandbox', 'unsandbox',
                           'silence', 'unsilence',
+                          'grantrole', 'revokerole',
                           'repeat',
                           'deleteuser',
                           'geocode',
index 43dfd05be5988e0d912af9f28124b5fa8b56da70..8464c2446499556a8fd8bb7812121f2f32f8e32d 100644 (file)
@@ -346,6 +346,16 @@ class UserProfile extends Widget
                             $this->out->elementEnd('ul');
                             $this->out->elementEnd('li');
                         }
+                        
+                        if ($cur->hasRight(Right::GRANTROLE)) {
+                            $this->out->elementStart('li', 'entity_role');
+                            $this->out->element('p', null, _('User role'));
+                            $this->out->elementStart('ul');
+                            $this->roleButton('administrator', _m('role', 'Administrator'));
+                            $this->roleButton('moderator', _m('role', 'Moderator'));
+                            $this->out->elementEnd('ul');
+                            $this->out->elementEnd('li');
+                        }
                     }
                 }
 
@@ -359,6 +369,22 @@ class UserProfile extends Widget
         }
     }
 
+    function roleButton($role, $label)
+    {
+        list($action, $r2args) = $this->out->returnToArgs();
+        $r2args['action'] = $action;
+
+        $this->out->elementStart('li', "entity_role_$role");
+        if ($this->user->hasRole($role)) {
+            $rf = new RevokeRoleForm($role, $label, $this->out, $this->profile, $r2args);
+            $rf->show();
+        } else {
+            $rf = new GrantRoleForm($role, $label, $this->out, $this->profile, $r2args);
+            $rf->show();
+        }
+        $this->out->elementEnd('li');
+    }
+
     function showRemoteSubscribeLink()
     {
         $url = common_local_url('remotesubscribe',