2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Represents a permission assigned to a group.
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage security
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Permission extends DataObject {
|
2007-09-16 16:32:54 +02:00
|
|
|
|
2007-09-16 16:58:27 +02:00
|
|
|
// the (1) after Type specifies the DB default value which is needed for
|
|
|
|
// upgrades from older SilverStripe versions
|
2007-07-19 12:40:28 +02:00
|
|
|
static $db = array(
|
|
|
|
"Code" => "Varchar",
|
2007-09-14 19:44:33 +02:00
|
|
|
"Arg" => "Int",
|
2007-09-16 16:58:27 +02:00
|
|
|
"Type" => "Int(1)"
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
|
|
|
static $has_one = array(
|
2007-09-16 16:58:27 +02:00
|
|
|
"Group" => "Group"
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
|
|
|
static $indexes = array(
|
2007-09-16 16:58:27 +02:00
|
|
|
"Code" => true
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2007-09-16 16:58:27 +02:00
|
|
|
static $defaults = array(
|
|
|
|
"Type" => 1
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the value to use for the "Type" field if a permission should be
|
|
|
|
* granted.
|
|
|
|
*/
|
|
|
|
const GRANT_PERMISSION = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the value to use for the "Type" field if a permission should be
|
|
|
|
* denied.
|
|
|
|
*/
|
|
|
|
const DENY_PERMISSION = -1;
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2007-09-16 04:15:16 +02:00
|
|
|
/**
|
2007-09-16 16:58:27 +02:00
|
|
|
* This is the value to use for the "Type" field if a permission should be
|
|
|
|
* inherited.
|
|
|
|
*/
|
|
|
|
const INHERIT_PERMISSION = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to globally disable "strict" checking, which means a permission
|
|
|
|
* will be granted if the key does not exist at all.
|
|
|
|
*
|
|
|
|
* @var bool
|
2007-09-16 04:15:16 +02:00
|
|
|
*/
|
|
|
|
static $declared_permissions = null;
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Linear list of declared permissions in the system.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2007-09-16 04:15:16 +02:00
|
|
|
protected static $declared_permissions_list = null;
|
2007-09-16 16:32:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var $strict_checking Boolean Method to globally disable "strict" checking,
|
|
|
|
* which means a permission will be granted if the key does not exist at all.
|
|
|
|
*/
|
|
|
|
static $strict_checking = true;
|
|
|
|
|
2007-09-16 04:15:16 +02:00
|
|
|
/**
|
|
|
|
* If this setting is set, then permissions can imply other permissions
|
2007-09-16 16:32:54 +02:00
|
|
|
*
|
|
|
|
* @var bool
|
2007-09-16 04:15:16 +02:00
|
|
|
*/
|
|
|
|
static $implied_permissions = false;
|
2007-09-16 16:32:54 +02:00
|
|
|
|
2007-09-16 04:15:16 +02:00
|
|
|
/**
|
2007-09-16 16:32:54 +02:00
|
|
|
* Set to false to prevent the 'ADMIN' permission from implying all
|
|
|
|
* permissions in the system
|
|
|
|
*
|
|
|
|
* @var bool
|
2007-09-16 04:15:16 +02:00
|
|
|
*/
|
|
|
|
static $admin_implies_all = true;
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check that the current member has the given permission
|
|
|
|
*
|
2007-09-16 16:32:54 +02:00
|
|
|
* @param string $code Code of the permission to check
|
|
|
|
* @param string $arg Optional argument (e.g. a permissions for a specific
|
|
|
|
* page)
|
2008-09-22 18:04:22 +02:00
|
|
|
* @param int|Member $member Optional member instance or ID. If set to NULL, the permssion
|
2007-09-16 16:32:54 +02:00
|
|
|
* will be checked for the current user
|
|
|
|
* @param bool $strict Use "strict" checking (which means a permission
|
|
|
|
* will be granted if the key does not exist at all)?
|
|
|
|
* @return int|bool The ID of the permission record if the permission
|
2007-09-16 16:40:43 +02:00
|
|
|
* exists; FALSE otherwise. If "strict" checking is
|
2007-09-16 16:32:54 +02:00
|
|
|
* disabled, TRUE will be returned if the permission does
|
|
|
|
* not exist at all.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-09-22 18:04:22 +02:00
|
|
|
public static function check($code, $arg = "any", $member = null, $strict = true) {
|
|
|
|
if(!$member) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!Member::currentUser()) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-22 18:04:22 +02:00
|
|
|
$member = Member::currentUser();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2008-09-22 18:04:22 +02:00
|
|
|
return self::checkMember($member, $code, $arg, $strict);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2007-09-16 16:32:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check that the given member has the given permission
|
2008-08-09 08:53:26 +02:00
|
|
|
* @param int|Member memberID The ID of the member to check. Leave blank for the current member.
|
|
|
|
* Alternatively you can use a member object.
|
2008-04-09 13:16:32 +02:00
|
|
|
* @param string|array $code Code of the permission to check
|
2007-09-16 16:32:54 +02:00
|
|
|
* @param string $arg Optional argument (e.g. a permissions for a specific
|
|
|
|
* page)
|
|
|
|
* @param bool $strict Use "strict" checking (which means a permission
|
|
|
|
* will be granted if the key does not exist at all)?
|
|
|
|
* @return int|bool The ID of the permission record if the permission
|
2007-09-16 16:40:43 +02:00
|
|
|
* exists; FALSE otherwise. If "strict" checking is
|
2007-09-16 16:32:54 +02:00
|
|
|
* disabled, TRUE will be returned if the permission does
|
|
|
|
* not exist at all.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-08-09 08:53:26 +02:00
|
|
|
public static function checkMember($member, $code, $arg = "any", $strict = true) {
|
2007-09-16 04:15:16 +02:00
|
|
|
$perms_list = self::get_declared_permissions_list();
|
2008-08-09 08:53:26 +02:00
|
|
|
$memberID = (is_object($member)) ? $member->ID : $member;
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
if(self::$declared_permissions && is_array($perms_list) &&
|
|
|
|
!in_array($code, $perms_list)) {
|
|
|
|
//user_error("Permission '$code' has not been declared. Use " .
|
|
|
|
// "Permission::declare_permissions() to add this permission",
|
|
|
|
// E_USER_WARNING);
|
2007-09-16 04:15:16 +02:00
|
|
|
}
|
2007-09-16 16:32:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupList = self::groupList($memberID);
|
|
|
|
if($groupList) {
|
|
|
|
$groupCSV = implode(", ", $groupList);
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Arg component
|
|
|
|
switch($arg) {
|
2007-09-16 16:32:54 +02:00
|
|
|
case "any":
|
|
|
|
$argClause = "";
|
|
|
|
break;
|
|
|
|
case "all":
|
|
|
|
$argClause = " AND Arg = -1";
|
|
|
|
break;
|
2007-09-14 19:44:33 +02:00
|
|
|
default:
|
2007-09-16 16:32:54 +02:00
|
|
|
if(is_numeric($arg)) {
|
|
|
|
$argClause = "AND Arg IN (-1, $arg) ";
|
|
|
|
} else {
|
2008-08-09 08:53:26 +02:00
|
|
|
user_error("Permission::checkMember: bad arg '$arg'",
|
2007-09-16 16:32:54 +02:00
|
|
|
E_USER_ERROR);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-31 02:28:30 +02:00
|
|
|
if(is_array($code)) $SQL_codeList = "'" . implode("', '", Convert::raw2sql($code)) . "'";
|
|
|
|
else $SQL_codeList = "'" . Convert::raw2sql($code) . "'";
|
|
|
|
|
2008-04-09 13:16:32 +02:00
|
|
|
$SQL_code = Convert::raw2sql($code);
|
|
|
|
|
2007-09-16 16:32:54 +02:00
|
|
|
$adminFilter = (self::$admin_implies_all)
|
|
|
|
? ",'ADMIN'"
|
|
|
|
: '';
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Raw SQL for efficiency
|
2007-09-16 16:40:43 +02:00
|
|
|
$permission = DB::query("
|
2007-09-16 16:58:27 +02:00
|
|
|
SELECT ID
|
|
|
|
FROM Permission
|
|
|
|
WHERE (
|
|
|
|
Code IN ($SQL_codeList $adminFilter)
|
|
|
|
AND Type = " . self::GRANT_PERMISSION . "
|
|
|
|
AND GroupID IN ($groupCSV)
|
|
|
|
$argClause
|
|
|
|
)
|
|
|
|
")->value();
|
2008-04-26 08:35:03 +02:00
|
|
|
|
2007-09-16 16:40:43 +02:00
|
|
|
if($permission)
|
|
|
|
return $permission;
|
|
|
|
|
|
|
|
|
|
|
|
// Strict checking disabled?
|
|
|
|
if(!self::$strict_checking || !$strict) {
|
2007-09-16 16:58:27 +02:00
|
|
|
$hasPermission = DB::query("
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM Permission
|
|
|
|
WHERE (
|
2008-04-09 13:16:32 +02:00
|
|
|
(Code IN '$SQL_code')'
|
2007-09-16 16:58:27 +02:00
|
|
|
AND (Type = " . self::GRANT_PERMISSION . ")
|
|
|
|
)
|
|
|
|
")->value();
|
|
|
|
if(!$hasPermission) {
|
2007-09-16 16:40:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:44:33 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the list of groups that the given member belongs to.
|
2007-09-16 16:32:54 +02:00
|
|
|
*
|
|
|
|
* Call without an argument to get the groups that the current member
|
|
|
|
* belongs to. In this case, the results will be session-cached.
|
|
|
|
*
|
|
|
|
* @param int $memberID The ID of the member. Leave blank for the current
|
|
|
|
* member.
|
|
|
|
* @return array Returns a list of group IDs to which the member belongs
|
|
|
|
* to or NULL.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public static function groupList($memberID = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Default to current member, with session-caching
|
|
|
|
if(!$memberID) {
|
|
|
|
$member = Member::currentUser();
|
2007-09-16 16:32:54 +02:00
|
|
|
if($member && isset($_SESSION['Permission_groupList'][$member->ID]))
|
|
|
|
return $_SESSION['Permission_groupList'][$member->ID];
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$member = DataObject::get_by_id("Member", $memberID);
|
|
|
|
}
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($member) {
|
2007-09-16 16:32:54 +02:00
|
|
|
// Build a list of the IDs of the groups. Most of the heavy lifting
|
|
|
|
// is done by Member::Groups
|
|
|
|
// NOTE: This isn't effecient; but it's called once per session so
|
|
|
|
// it's a low priority to fix.
|
2007-07-19 12:40:28 +02:00
|
|
|
$groups = $member->Groups();
|
2007-09-15 01:12:22 +02:00
|
|
|
$groupList = array();
|
|
|
|
|
|
|
|
if($groups) {
|
|
|
|
foreach($groups as $group)
|
|
|
|
$groupList[] = $group->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Session caching
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$memberID) {
|
|
|
|
$_SESSION['Permission_groupList'][$member->ID] = $groupList;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset($groupList) ? $groupList : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 16:32:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Grant the given permission code/arg to the given group
|
2007-09-16 16:32:54 +02:00
|
|
|
*
|
|
|
|
* @param int $groupID The ID of the group
|
|
|
|
* @param string $code The permission code
|
|
|
|
* @param string Optional: The permission argument (e.g. a page ID).
|
|
|
|
* @returns Permission Returns the new permission object.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public static function grant($groupID, $code, $arg = "any") {
|
2007-07-19 12:40:28 +02:00
|
|
|
$perm = new Permission();
|
|
|
|
$perm->GroupID = $groupID;
|
|
|
|
$perm->Code = $code;
|
2007-09-16 16:58:27 +02:00
|
|
|
$perm->Type = self::GRANT_PERMISSION;
|
|
|
|
|
|
|
|
// Arg component
|
|
|
|
switch($arg) {
|
|
|
|
case "any":
|
|
|
|
break;
|
|
|
|
case "all":
|
|
|
|
$perm->Arg = -1;
|
|
|
|
default:
|
|
|
|
if(is_numeric($arg)) {
|
|
|
|
$perm->Arg = $arg;
|
|
|
|
} else {
|
|
|
|
use_error("Permission::checkMember: bad arg '$arg'",
|
|
|
|
E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$perm->write();
|
|
|
|
return $perm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deny the given permission code/arg to the given group
|
|
|
|
*
|
|
|
|
* @param int $groupID The ID of the group
|
|
|
|
* @param string $code The permission code
|
|
|
|
* @param string Optional: The permission argument (e.g. a page ID).
|
|
|
|
* @returns Permission Returns the new permission object.
|
|
|
|
*/
|
|
|
|
public static function deny($groupID, $code, $arg = "any") {
|
|
|
|
$perm = new Permission();
|
|
|
|
$perm->GroupID = $groupID;
|
|
|
|
$perm->Code = $code;
|
|
|
|
$perm->Type = self::DENY_PERMISSION;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Arg component
|
|
|
|
switch($arg) {
|
2007-09-16 16:32:54 +02:00
|
|
|
case "any":
|
|
|
|
break;
|
|
|
|
case "all":
|
|
|
|
$perm->Arg = -1;
|
|
|
|
default:
|
|
|
|
if(is_numeric($arg)) {
|
|
|
|
$perm->Arg = $arg;
|
|
|
|
} else {
|
|
|
|
use_error("Permission::checkMember: bad arg '$arg'",
|
|
|
|
E_USER_ERROR);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$perm->write();
|
|
|
|
return $perm;
|
|
|
|
}
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add default records to database.
|
|
|
|
*
|
|
|
|
* This function is called whenever the database is built, after the
|
|
|
|
* database tables have all been created.
|
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public function requireDefaultRecords() {
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::requireDefaultRecords();
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Add default content if blank
|
2008-10-17 00:46:48 +02:00
|
|
|
if(!DB::query("SELECT ID FROM Permission")->value() && array_key_exists('CanCMSAdmin', DB::fieldList('Group'))) {
|
2007-09-16 16:32:54 +02:00
|
|
|
$admins = DB::query("SELECT ID FROM `Group` WHERE CanCMSAdmin = 1")
|
|
|
|
->column();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($admins)) {
|
2007-09-16 16:32:54 +02:00
|
|
|
foreach($admins as $admin)
|
|
|
|
Permission::grant($admin, "ADMIN");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2007-09-16 16:32:54 +02:00
|
|
|
$authors = DB::query("SELECT ID FROM `Group` WHERE CanCMS = 1")
|
|
|
|
->column();
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($authors)) {
|
|
|
|
foreach($authors as $author) {
|
|
|
|
Permission::grant($author, "CMS_ACCESS_CMSMain");
|
|
|
|
Permission::grant($author, "CMS_ACCESS_AssetAdmin");
|
|
|
|
Permission::grant($author, "CMS_ACCESS_NewsletterAdmin");
|
|
|
|
Permission::grant($author, "CMS_ACCESS_ReportAdmin");
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 19:44:33 +02:00
|
|
|
|
2007-09-16 16:32:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns all members for a specific permission.
|
|
|
|
*
|
2007-08-28 03:53:36 +02:00
|
|
|
* @param $code String|array Either a single permission code, or a list of permission codes
|
2007-09-16 16:32:54 +02:00
|
|
|
* @return DataObjectSet Returns a set of member that have the specified
|
|
|
|
* permission.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public static function get_members_by_permission($code) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDs = array();
|
2007-08-28 03:53:36 +02:00
|
|
|
|
2007-09-16 16:58:27 +02:00
|
|
|
$SQL_codeList = (is_array($code)) ? implode("','", Convert::raw2sql($code)) : Convert::raw2sql($code);
|
|
|
|
|
|
|
|
$SQL_filter = "Permission.Code IN ('" . $SQL_codeList . "') " .
|
|
|
|
"AND Permission.Type = " . self::GRANT_PERMISSION;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$toplevelGroups = DataObject::get(
|
|
|
|
'Group',
|
2007-08-28 03:53:36 +02:00
|
|
|
$SQL_filter, // filter
|
2007-07-19 12:40:28 +02:00
|
|
|
null, // limit
|
2007-09-16 16:58:27 +02:00
|
|
|
"LEFT JOIN `Permission` ON `Group`.`ID` = `Permission`.`GroupID`"
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2007-09-16 16:32:54 +02:00
|
|
|
if(!$toplevelGroups)
|
|
|
|
return false;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($toplevelGroups as $group) {
|
|
|
|
$familyIDs = $group->collateFamilyIDs();
|
|
|
|
if(is_array($familyIDs)) {
|
|
|
|
$groupIDs = array_merge($groupIDs, array_values($familyIDs));
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
if(!count($groupIDs))
|
|
|
|
return false;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$members = DataObject::get(
|
|
|
|
Object::getCustomClass('Member'),
|
|
|
|
$_filter = "`Group`.ID IN (" . implode(",",$groupIDs) . ")",
|
|
|
|
$_sort = "",
|
|
|
|
$_join = "LEFT JOIN `Group_Members` ON `Member`.`ID` = `Group_Members`.`MemberID` " .
|
|
|
|
"LEFT JOIN `Group` ON `Group_Members`.`GroupID` = `Group`.`ID` "
|
|
|
|
);
|
|
|
|
return $members;
|
|
|
|
}
|
2007-09-04 05:55:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the groups that have one of the given permission codes
|
|
|
|
* @param $codes array|string Either a single permission code, or an array of permission codes
|
|
|
|
* @return DataObjectSet The matching group objects
|
|
|
|
*/
|
|
|
|
static function get_groups_by_permission($codes) {
|
|
|
|
if(!is_array($codes)) $codes = array($codes);
|
|
|
|
|
|
|
|
$SQLa_codes = Convert::raw2sql($codes);
|
|
|
|
$SQL_codes = join("','", $SQLa_codes);
|
|
|
|
|
|
|
|
return DataObject::get(
|
|
|
|
'Group',
|
|
|
|
"Permission.Code IN ('$SQL_codes')",
|
|
|
|
"",
|
|
|
|
"LEFT JOIN Permission ON Group.ID = Permission.GroupID"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of all available permission codes
|
|
|
|
*
|
|
|
|
* @param bool|string $blankItemText Text for permission with the empty
|
|
|
|
* code (""). If set to TRUE it will be
|
|
|
|
* set to "(select)"; if set to NULL or
|
|
|
|
* FALSE the empty permission is not
|
|
|
|
* included in the list.
|
|
|
|
* @return array Returns an array of all available permission codes. The
|
|
|
|
* array indicies are the permission codes as used in
|
|
|
|
* {@link Permission::check()}. The value is a description
|
|
|
|
* suitable for using in an interface.
|
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public static function get_codes($blankItemText = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$classes = ClassInfo::implementorsOf('PermissionProvider');
|
2007-09-15 23:41:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$allCodes = array();
|
2007-09-16 16:32:54 +02:00
|
|
|
if($blankItemText){
|
|
|
|
$allCodes[''] = ($blankItemText === true)
|
|
|
|
? '(select)'
|
|
|
|
: $blankItemText;
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
$allCodes['ADMIN'] = _t('Permission.FULLADMINRIGHTS', 'Full administrative rights');
|
2007-09-15 23:41:54 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
if($classes) foreach($classes as $class) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$SNG = singleton($class);
|
2008-10-09 20:16:13 +02:00
|
|
|
if($SNG instanceof TestOnly) continue;
|
2007-07-19 12:40:28 +02:00
|
|
|
$someCodes = $SNG->providePermissions();
|
|
|
|
if($someCodes) foreach($someCodes as $k => $v) {
|
|
|
|
$allCodes[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 16:32:54 +02:00
|
|
|
$otherPerms = DB::query("SELECT DISTINCT Code From Permission")
|
|
|
|
->column();
|
2008-02-25 03:10:37 +01:00
|
|
|
if($otherPerms) foreach($otherPerms as $otherPerm) {
|
2007-09-16 16:32:54 +02:00
|
|
|
if(!array_key_exists($otherPerm, $allCodes))
|
|
|
|
$allCodes[$otherPerm] = $otherPerm;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-31 02:28:30 +02:00
|
|
|
asort($allCodes);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $allCodes;
|
|
|
|
}
|
|
|
|
|
2007-09-04 05:55:02 +02:00
|
|
|
/*
|
2007-07-19 12:40:28 +02:00
|
|
|
* Controller action to list the codes available
|
2007-09-16 16:32:54 +02:00
|
|
|
*
|
|
|
|
* @see Permission::get_codes()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public function listcodes() {
|
2007-09-16 16:32:54 +02:00
|
|
|
if(!Permission::check('ADMIN'))
|
|
|
|
Security::permissionFailure();
|
2007-09-15 23:41:54 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
echo '<h1>'._t('Permission.PERMSDEFINED', 'The following permission codes are defined').'</h1>';
|
2007-07-19 12:40:28 +02:00
|
|
|
$codes = self::get_codes();
|
|
|
|
echo "<pre>";
|
|
|
|
print_r($codes);
|
|
|
|
}
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
|
2007-09-16 04:15:16 +02:00
|
|
|
/**
|
2007-09-16 16:32:54 +02:00
|
|
|
* Declare an array of permissions for the system.
|
|
|
|
*
|
|
|
|
* Permissions can be grouped by nesting arrays. Scalar values are always
|
|
|
|
* treated as permissions.
|
|
|
|
*
|
|
|
|
* @param array $permArray A (possibly nested) array of permissions to
|
|
|
|
* declare for the system.
|
2007-09-16 04:15:16 +02:00
|
|
|
*/
|
|
|
|
static function declare_permissions($permArray) {
|
2007-09-16 16:32:54 +02:00
|
|
|
if(is_array(self::$declared_permissions)) {
|
|
|
|
self::$declared_permissions =
|
|
|
|
array_merge_recursive(self::$declared_permissions, $permArray);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self::$declared_permissions = $permArray;
|
|
|
|
}
|
2007-09-16 04:15:16 +02:00
|
|
|
}
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
|
2007-09-16 04:15:16 +02:00
|
|
|
/**
|
|
|
|
* Get a linear list of the permissions in the system.
|
2007-09-16 16:32:54 +02:00
|
|
|
*
|
|
|
|
* @return array Linear list of declared permissions in the system.
|
2007-09-16 04:15:16 +02:00
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public static function get_declared_permissions_list() {
|
2007-09-16 16:32:54 +02:00
|
|
|
if(!self::$declared_permissions)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if(self::$declared_permissions_list)
|
|
|
|
return self::$declared_permissions_list;
|
|
|
|
|
2007-09-16 04:15:16 +02:00
|
|
|
self::$declared_permissions_list = array();
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
self::traverse_declared_permissions(self::$declared_permissions,
|
|
|
|
self::$declared_permissions_list);
|
|
|
|
|
2007-09-16 04:15:16 +02:00
|
|
|
return self::$declared_permissions_list;
|
|
|
|
}
|
2007-09-16 16:32:54 +02:00
|
|
|
|
2008-08-11 04:25:44 +02:00
|
|
|
/**
|
|
|
|
* Look up the human-readable title for the permission as defined by <code>Permission::declare_permissions</code>
|
|
|
|
*
|
|
|
|
* @param $perm Permission code
|
|
|
|
* @return Label for the given permission, or the permission itself if the label doesn't exist
|
|
|
|
*/
|
|
|
|
public static function get_label_for_permission($perm) {
|
|
|
|
$list = self::get_declared_permissions_list();
|
|
|
|
if(array_key_exists($perm, $list)) return $list[$perm];
|
|
|
|
return $perm;
|
|
|
|
}
|
2007-09-16 16:32:54 +02:00
|
|
|
|
2007-09-16 04:15:16 +02:00
|
|
|
/**
|
2007-09-16 16:32:54 +02:00
|
|
|
* Recursively traverse the nested list of declared permissions and create
|
|
|
|
* a linear list.
|
|
|
|
*
|
|
|
|
* @param aeeay $declared Nested structure of permissions.
|
|
|
|
* @param $list List of permissions in the structure. The result will be
|
|
|
|
* written to this array.
|
2007-09-16 04:15:16 +02:00
|
|
|
*/
|
2007-09-16 16:32:54 +02:00
|
|
|
protected static function traverse_declared_permissions($declared,
|
|
|
|
&$list) {
|
|
|
|
if(!is_array($declared))
|
|
|
|
return;
|
|
|
|
|
2007-09-16 04:15:16 +02:00
|
|
|
foreach($declared as $perm => $value) {
|
|
|
|
if($value instanceof Permission_Group) {
|
|
|
|
$list[] = $value->getName();
|
2007-09-16 16:22:26 +02:00
|
|
|
self::traverse_declared_permissions($value->getPermissions(), $list);
|
2007-09-16 16:32:54 +02:00
|
|
|
} else {
|
|
|
|
$list[$perm] = $value;
|
|
|
|
}
|
2007-09-16 04:15:16 +02:00
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Permission_Group class
|
|
|
|
*
|
|
|
|
* This class is used to group permissions together for showing on an
|
|
|
|
* interface.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage security
|
2007-09-16 16:32:54 +02:00
|
|
|
*/
|
2007-09-16 04:15:16 +02:00
|
|
|
class Permission_Group {
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the permission group (can be used as label in an interface)
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-09-16 04:15:16 +02:00
|
|
|
protected $name;
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Associative array of permissions in this permission group. The array
|
|
|
|
* indicies are the permission codes as used in
|
|
|
|
* {@link Permission::check()}. The value is suitable for using in an
|
|
|
|
* interface.
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-09-16 04:15:16 +02:00
|
|
|
protected $permissions = array();
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $name Text that could be used as label used in an
|
|
|
|
* interface
|
|
|
|
* @param array $permissions Associative array of permissions in this
|
|
|
|
* permission group. The array indicies are the
|
|
|
|
* permission codes as used in
|
|
|
|
* {@link Permission::check()}. The value is
|
|
|
|
* suitable for using in an interface.
|
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public function __construct($name, $permissions) {
|
2007-09-16 04:15:16 +02:00
|
|
|
$this->name = $name;
|
|
|
|
$this->permissions = $permissions;
|
|
|
|
}
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the permission group
|
|
|
|
*
|
|
|
|
* @return string Name (label) of the permission group
|
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public function getName() {
|
2007-09-16 04:15:16 +02:00
|
|
|
return $this->name;
|
|
|
|
}
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get permissions
|
|
|
|
*
|
|
|
|
* @return array Associative array of permissions in this permission
|
|
|
|
* group. The array indicies are the permission codes as
|
|
|
|
* used in {@link Permission::check()}. The value is
|
|
|
|
* suitable for using in an interface.
|
|
|
|
*/
|
2007-09-16 16:42:40 +02:00
|
|
|
public function getPermissions() {
|
2007-09-16 04:15:16 +02:00
|
|
|
return $this->permissions;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 16:32:54 +02:00
|
|
|
|
|
|
|
|
2008-10-17 00:46:48 +02:00
|
|
|
?>
|