summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrion Vibber <brion@pobox.com>2010-03-03 15:43:49 -0800
committerBrion Vibber <brion@pobox.com>2010-03-03 15:43:49 -0800
commit4a2511139eaafcbe93a2e720e0c6f170ecb00d77 (patch)
tree1b9abf6fcd9db6a4d785871352dd7eb20ae35b82
parent06db00d303bda500eec578b490c12d2bf389c853 (diff)
Initial user role controls on profile pages, for owner to add/remove administrator and moderator options.
Buttons need to be themed.
-rw-r--r--actions/grantrole.php99
-rw-r--r--actions/revokerole.php99
-rw-r--r--classes/Profile.php4
-rw-r--r--classes/Profile_role.php17
-rw-r--r--lib/grantroleform.php93
-rw-r--r--lib/revokeroleform.php93
-rw-r--r--lib/right.php2
-rw-r--r--lib/router.php1
-rw-r--r--lib/userprofile.php26
9 files changed, 434 insertions, 0 deletions
diff --git a/actions/grantrole.php b/actions/grantrole.php
new file mode 100644
index 000000000..cd6bd4d79
--- /dev/null
+++ b/actions/grantrole.php
@@ -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
index 000000000..b78c1c25a
--- /dev/null
+++ b/actions/revokerole.php
@@ -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);
+ }
+}
diff --git a/classes/Profile.php b/classes/Profile.php
index 9c2fa7a0c..0322c9358 100644
--- a/classes/Profile.php
+++ b/classes/Profile.php
@@ -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:
diff --git a/classes/Profile_role.php b/classes/Profile_role.php
index bf2c453ed..d0a0b31f0 100644
--- a/classes/Profile_role.php
+++ b/classes/Profile_role.php
@@ -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
index 000000000..b5f952746
--- /dev/null
+++ b/lib/grantroleform.php
@@ -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
index 000000000..ec24b9910
--- /dev/null
+++ b/lib/revokeroleform.php
@@ -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);
+ }
+}
diff --git a/lib/right.php b/lib/right.php
index 4e9c5a918..deb451fde 100644
--- a/lib/right.php
+++ b/lib/right.php
@@ -58,5 +58,7 @@ class Right
const EMAILONSUBSCRIBE = 'emailonsubscribe';
const EMAILONFAVE = 'emailonfave';
const MAKEGROUPADMIN = 'makegroupadmin';
+ const GRANTROLE = 'grantrole';
+ const REVOKEROLE = 'revokerole';
}
diff --git a/lib/router.php b/lib/router.php
index 7e8e22a7d..15f88c959 100644
--- a/lib/router.php
+++ b/lib/router.php
@@ -98,6 +98,7 @@ class Router
'groupblock', 'groupunblock',
'sandbox', 'unsandbox',
'silence', 'unsilence',
+ 'grantrole', 'revokerole',
'repeat',
'deleteuser',
'geocode',
diff --git a/lib/userprofile.php b/lib/userprofile.php
index 43dfd05be..8464c2446 100644
--- a/lib/userprofile.php
+++ b/lib/userprofile.php
@@ -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',