user rights

This commit is contained in:
Evan Prodromou 2009-09-15 15:28:11 -04:00
parent a8d1b7e9c2
commit 6c069312e2
3 changed files with 135 additions and 0 deletions

View File

@ -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;
}
}
}

50
lib/right.php Normal file
View File

@ -0,0 +1,50 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Class for user rights
*
* 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 Authorization
* @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') && !defined('LACONICA')) {
exit(1);
}
/**
* class for rights
*
* Mostly for holding the rights constants
*
* @category Authorization
* @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/
*/
class Right
{
const deleteOthersNotice = 'deleteothersnotice';
}

59
tests/UserRightsTest.php Normal file
View File

@ -0,0 +1,59 @@
<?php
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
print "This script must be run from the command line\n";
exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('STATUSNET', true);
require_once INSTALLDIR . '/lib/common.php';
class UserRightsTest extends PHPUnit_Framework_TestCase
{
protected $user = null;
function setUp()
{
$this->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));
}
}