silverstripe-framework/Security/PermissionRole.php

108 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2016-06-23 01:37:22 +02:00
namespace SilverStripe\Security;
use SilverStripe\ORM\DataObject;
2016-06-23 01:37:22 +02:00
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\ManyManyList;
/**
* A PermissionRole represents a collection of permission codes that can be applied to groups.
2014-08-15 08:53:05 +02:00
*
* Because permission codes are very granular, this lets website administrators create more
* business-oriented units of access control - Roles - and assign those to groups.
2014-08-15 08:53:05 +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).
2014-08-15 08:53:05 +02:00
*
* @property string Title
* @property string OnlyAdminCanApply
*
* @method HasManyList Codes() List of PermissionRoleCode objects
* @method ManyManyList Groups() List of Group objects
*/
class PermissionRole extends DataObject {
private static $db = array(
"Title" => "Varchar",
"OnlyAdminCanApply" => "Boolean"
);
2014-08-15 08:53:05 +02:00
private static $has_many = array(
2016-06-23 01:37:22 +02:00
"Codes" => "SilverStripe\\Security\\PermissionRoleCode",
);
2014-08-15 08:53:05 +02:00
private static $belongs_many_many = array(
2016-06-23 01:37:22 +02:00
"Groups" => "SilverStripe\\Security\\Group",
);
2014-08-15 08:53:05 +02:00
2016-06-23 01:37:22 +02:00
private static $table_name = "PermissionRole";
private static $default_sort = '"Title"';
2014-08-15 08:53:05 +02:00
private static $singular_name = 'Role';
private static $plural_name = 'Roles';
2014-08-15 08:53:05 +02:00
public function getCMSFields() {
$fields = parent::getCMSFields();
2014-08-15 08:53:05 +02:00
$fields->removeFieldFromTab('Root', 'Codes');
$fields->removeFieldFromTab('Root', 'Groups');
2014-08-15 08:53:05 +02:00
$fields->addFieldToTab(
2014-08-15 08:53:05 +02:00
'Root.Main',
$permissionField = new PermissionCheckboxSetField(
'Codes',
2016-06-23 01:37:22 +02:00
Permission::singleton()->i18n_plural_name(),
'SilverStripe\\Security\\PermissionRoleCode',
'RoleID'
)
);
$permissionField->setHiddenPermissions(
2016-06-23 01:37:22 +02:00
Permission::config()->hidden_permissions
);
2014-08-15 08:53:05 +02:00
return $fields;
}
2014-08-15 08:53:05 +02:00
public function onAfterDelete() {
parent::onAfterDelete();
2014-08-15 08:53:05 +02:00
// Delete associated permission codes
$codes = $this->Codes();
foreach ( $codes as $code ) {
$code->delete();
}
}
public function fieldLabels($includerelations = true) {
$labels = parent::fieldLabels($includerelations);
$labels['Title'] = _t('PermissionRole.Title', 'Title');
$labels['OnlyAdminCanApply'] = _t(
2014-08-15 08:53:05 +02:00
'PermissionRole.OnlyAdminCanApply',
'Only admin can apply',
'Checkbox to limit which user can apply this role'
);
2014-08-15 08:53:05 +02:00
return $labels;
}
public function canView($member = null) {
return Permission::check('APPLY_ROLES', 'any', $member);
}
public function canCreate($member = null, $context = array()) {
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);
}
}