Completed
Push — master ( 20efb0...a2cc06 )
by Hamish
29s
created

PermissionRole::fieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace SilverStripe\Security;
4
5
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\HasManyList;
8
use SilverStripe\ORM\ManyManyList;
9
10
/**
11
 * A PermissionRole represents a collection of permission codes that can be applied to groups.
12
 *
13
 * Because permission codes are very granular, this lets website administrators create more
14
 * business-oriented units of access control - Roles - and assign those to groups.
15
 *
16
 * If the <b>OnlyAdminCanApply</b> property is set to TRUE, the role can only be assigned
17
 * to new groups by a user with ADMIN privileges. This is a simple way to prevent users
18
 * with access to {@link SecurityAdmin} (but no ADMIN privileges) to get themselves ADMIN access
19
 * (which might be implied by certain roles).
20
 *
21
 * @package framework
22
 * @subpackage security
23
 *
24
 * @property string Title
25
 * @property string OnlyAdminCanApply
26
 *
27
 * @method HasManyList Codes() List of PermissionRoleCode objects
28
 * @method ManyManyList Groups() List of Group objects
29
 */
30
class PermissionRole extends DataObject {
31
	private static $db = array(
32
		"Title" => "Varchar",
33
		"OnlyAdminCanApply" => "Boolean"
34
	);
35
36
	private static $has_many = array(
37
		"Codes" => "SilverStripe\\Security\\PermissionRoleCode",
38
	);
39
40
	private static $belongs_many_many = array(
41
		"Groups" => "SilverStripe\\Security\\Group",
42
	);
43
44
	private static $table_name = "PermissionRole";
45
46
	private static $default_sort = '"Title"';
47
48
	private static $singular_name = 'Role';
49
50
	private static $plural_name = 'Roles';
51
52
	public function getCMSFields() {
53
		$fields = parent::getCMSFields();
54
55
		$fields->removeFieldFromTab('Root', 'Codes');
56
		$fields->removeFieldFromTab('Root', 'Groups');
57
58
		$fields->addFieldToTab(
59
			'Root.Main',
60
			$permissionField = new PermissionCheckboxSetField(
61
				'Codes',
62
				Permission::singleton()->i18n_plural_name(),
63
				'SilverStripe\\Security\\PermissionRoleCode',
64
				'RoleID'
65
			)
66
		);
67
		$permissionField->setHiddenPermissions(
68
			Permission::config()->hidden_permissions
69
		);
70
71
		return $fields;
72
	}
73
74
	public function onAfterDelete() {
75
		parent::onAfterDelete();
76
77
		// Delete associated permission codes
78
		$codes = $this->Codes();
79
		foreach ( $codes as $code ) {
80
			$code->delete();
81
		}
82
	}
83
84
	public function fieldLabels($includerelations = true) {
85
		$labels = parent::fieldLabels($includerelations);
86
		$labels['Title'] = _t('PermissionRole.Title', 'Title');
87
		$labels['OnlyAdminCanApply'] = _t(
88
			'PermissionRole.OnlyAdminCanApply',
89
			'Only admin can apply',
90
			'Checkbox to limit which user can apply this role'
91
		);
92
93
		return $labels;
94
	}
95
96
	public function canView($member = null) {
97
		return Permission::check('APPLY_ROLES', 'any', $member);
98
	}
99
100
	public function canCreate($member = null, $context = array()) {
101
		return Permission::check('APPLY_ROLES', 'any', $member);
102
	}
103
104
	public function canEdit($member = null) {
105
		return Permission::check('APPLY_ROLES', 'any', $member);
106
	}
107
108
	public function canDelete($member = null) {
109
		return Permission::check('APPLY_ROLES', 'any', $member);
110
	}
111
}
112