1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Security; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\Security\PermissionRole; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A PermissionRoleCode represents a single permission code assigned to a {@link PermissionRole}. |
10
|
|
|
* |
11
|
|
|
* @property string Code |
12
|
|
|
* @property int RoleID |
13
|
|
|
* @method PermissionRole Role() |
14
|
|
|
*/ |
15
|
|
|
class PermissionRoleCode extends DataObject |
16
|
|
|
{ |
17
|
|
|
private static $db = array( |
|
|
|
|
18
|
|
|
"Code" => "Varchar", |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
private static $has_one = array( |
|
|
|
|
22
|
|
|
"Role" => PermissionRole::class, |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
private static $table_name = "PermissionRoleCode"; |
|
|
|
|
26
|
|
|
|
27
|
|
|
public function validate() |
28
|
|
|
{ |
29
|
|
|
$result = parent::validate(); |
30
|
|
|
|
31
|
|
|
// Check that new code doesn't increase privileges, unless an admin is editing. |
32
|
|
|
$privilegedCodes = Permission::config()->privileged_permissions; |
|
|
|
|
33
|
|
|
if ($this->Code |
34
|
|
|
&& in_array($this->Code, $privilegedCodes) |
35
|
|
|
&& !Permission::check('ADMIN') |
36
|
|
|
) { |
37
|
|
|
$result->addError( |
38
|
|
|
_t( |
39
|
|
|
__CLASS__ . '.PermsError', |
40
|
|
|
'Can\'t assign code "{code}" with privileged permissions (requires ADMIN access)', |
41
|
|
|
['code' => $this->Code] |
42
|
|
|
) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function canCreate($member = null, $context = array()) |
50
|
|
|
{ |
51
|
|
|
return Permission::check('APPLY_ROLES', 'any', $member); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function canEdit($member = null) |
55
|
|
|
{ |
56
|
|
|
return Permission::check('APPLY_ROLES', 'any', $member); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function canDelete($member = null) |
60
|
|
|
{ |
61
|
|
|
return Permission::check('APPLY_ROLES', 'any', $member); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|