2009-10-16 00:27:56 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A PermissionRole represents a collection of permission codes that can be applied to groups.
|
|
|
|
*
|
|
|
|
* Because permission codes are very granular, this lets website administrators create more
|
|
|
|
* business-oriented units of access control - Roles - and assign those to groups.
|
2010-04-23 02:25:47 +02:00
|
|
|
*
|
2010-05-11 23:20:13 +02:00
|
|
|
* If the <b>OnlyAdminCanApply</b> property is set to TRUE, the role can only be assigned
|
|
|
|
* to new groups by a user with ADMIN privileges. This is a simple way to prevent users
|
|
|
|
* with access to {@link SecurityAdmin} (but no ADMIN privileges) to get themselves ADMIN access
|
|
|
|
* (which might be implied by certain roles).
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-04-23 02:25:47 +02:00
|
|
|
* @subpackage security
|
2014-01-26 04:17:17 +01:00
|
|
|
*
|
|
|
|
* @property string Title
|
|
|
|
* @property string OnlyAdminCanApply
|
|
|
|
*
|
|
|
|
* @method HasManyList Codes() List of PermissionRoleCode objects
|
|
|
|
* @method ManyManyList Groups() List of Group objects
|
2009-10-16 00:27:56 +02:00
|
|
|
*/
|
|
|
|
class PermissionRole extends DataObject {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-10-16 00:27:56 +02:00
|
|
|
"Title" => "Varchar",
|
2009-10-29 23:07:44 +01:00
|
|
|
"OnlyAdminCanApply" => "Boolean"
|
2009-10-16 00:27:56 +02:00
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2009-10-16 00:27:56 +02:00
|
|
|
"Codes" => "PermissionRoleCode",
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = array(
|
2009-10-16 00:27:56 +02:00
|
|
|
"Groups" => "Group",
|
|
|
|
);
|
2009-10-16 00:28:11 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_sort = '"Title"';
|
2009-10-16 00:28:11 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $singular_name = 'Role';
|
2010-03-10 03:23:41 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $plural_name = 'Roles';
|
2010-03-10 03:23:41 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCMSFields() {
|
2012-04-13 15:46:47 +02:00
|
|
|
$fields = parent::getCMSFields();
|
2009-10-16 00:28:11 +02:00
|
|
|
|
|
|
|
$fields->removeFieldFromTab('Root', 'Codes');
|
|
|
|
$fields->removeFieldFromTab('Root', 'Groups');
|
|
|
|
|
2010-03-10 03:23:41 +01:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
$permissionField = new PermissionCheckboxSetField(
|
|
|
|
'Codes',
|
|
|
|
singleton('Permission')->i18n_plural_name(),
|
|
|
|
'PermissionRoleCode',
|
|
|
|
'RoleID'
|
|
|
|
)
|
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
$permissionField->setHiddenPermissions(
|
|
|
|
Config::inst()->get('Permission', 'hidden_permissions')
|
|
|
|
);
|
2009-10-16 00:28:11 +02:00
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
2009-12-10 04:44:35 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onAfterDelete() {
|
2009-12-10 04:44:35 +01:00
|
|
|
parent::onAfterDelete();
|
|
|
|
|
|
|
|
// Delete associated permission codes
|
|
|
|
$codes = $this->Codes();
|
|
|
|
foreach ( $codes as $code ) {
|
|
|
|
$code->delete();
|
|
|
|
}
|
|
|
|
}
|
2012-09-11 13:53:09 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function fieldLabels($includerelations = true) {
|
2012-09-11 13:53:09 +02:00
|
|
|
$labels = parent::fieldLabels($includerelations);
|
|
|
|
$labels['Title'] = _t('PermissionRole.Title', 'Title');
|
|
|
|
$labels['OnlyAdminCanApply'] = _t(
|
|
|
|
'PermissionRole.OnlyAdminCanApply',
|
|
|
|
'Only admin can apply',
|
|
|
|
'Checkbox to limit which user can apply this role'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $labels;
|
|
|
|
}
|
2013-08-30 13:59:38 +02:00
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return Permission::check('APPLY_ROLES', 'any', $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canCreate($member = null) {
|
|
|
|
return Permission::check('APPLY_ROLES', 'any', $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canEdit($member = null) {
|
|
|
|
return Permission::check('APPLY_ROLES', 'any', $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canDelete($member = null) {
|
|
|
|
return Permission::check('APPLY_ROLES', 'any', $member);
|
|
|
|
}
|
2009-10-16 00:28:11 +02:00
|
|
|
}
|