From 6c069312e2911d3b2fe54d051354f579fde7bb63 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 15 Sep 2009 15:28:11 -0400 Subject: [PATCH] user rights --- classes/User.php | 26 ++++++++++++++++++ lib/right.php | 50 ++++++++++++++++++++++++++++++++++ tests/UserRightsTest.php | 59 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 lib/right.php create mode 100644 tests/UserRightsTest.php diff --git a/classes/User.php b/classes/User.php index 5e74c7fde4..bea81af4d2 100644 --- a/classes/User.php +++ b/classes/User.php @@ -711,4 +711,30 @@ class User extends Memcached_DataObject return true; } + + /** + * Does this user have the right to do X? + * + * With our role-based authorization, this is merely a lookup for whether the user + * has a particular role. The implementation currently uses a switch statement + * to determine if the user has the pre-defined role to exercise the right. Future + * implementations may allow per-site roles, and different mappings of roles to rights. + * + * @param $right string Name of the right, usually a constant in class Right + * @return boolean whether the user has the right in question + */ + + function hasRight($right) + { + switch ($right) + { + case Right::deleteOthersNotice: + return $this->hasRole('moderator'); + break; + default: + $result = false; + Event::handle('UserRightsCheck', array($this, &$result)); + return $result; + } + } } diff --git a/lib/right.php b/lib/right.php new file mode 100644 index 0000000000..4e0096d46a --- /dev/null +++ b/lib/right.php @@ -0,0 +1,50 @@ +. + * + * @category Authorization + * @package StatusNet + * @author Evan Prodromou + * @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') && !defined('LACONICA')) { + exit(1); +} + +/** + * class for rights + * + * Mostly for holding the rights constants + * + * @category Authorization + * @package StatusNet + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +class Right +{ + const deleteOthersNotice = 'deleteothersnotice'; +} + diff --git a/tests/UserRightsTest.php b/tests/UserRightsTest.php new file mode 100644 index 0000000000..6544ee53d7 --- /dev/null +++ b/tests/UserRightsTest.php @@ -0,0 +1,59 @@ +user = User::register(array('nickname' => 'userrightstestuser')); + } + + function tearDown() + { + $profile = $this->user->getProfile(); + $this->user->delete(); + $profile->delete(); + } + + function testInvalidRole() + { + $this->assertFalse($this->user->hasRole('invalidrole')); + } + + function standardRoles() + { + return array('admin', 'moderator'); + } + + /** + * @dataProvider standardRoles + * + */ + + function testUngrantedRole($role) + { + $this->assertFalse($this->user->hasRole($role)); + } + + /** + * @dataProvider standardRoles + * + */ + + function testGrantedRole($role) + { + $this->user->grantRole($role); + $this->assertFalse($this->user->hasRole($role)); + } +} \ No newline at end of file