Issues (2882)

src/Security/PermissionRole.php (8 issues)

1
<?php
2
3
namespace SilverStripe\Security;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\HasManyList;
7
use SilverStripe\ORM\ManyManyList;
8
9
/**
10
 * A PermissionRole represents a collection of permission codes that can be applied to groups.
11
 *
12
 * Because permission codes are very granular, this lets website administrators create more
13
 * business-oriented units of access control - Roles - and assign those to groups.
14
 *
15
 * If the <b>OnlyAdminCanApply</b> property is set to TRUE, the role can only be assigned
16
 * to new groups by a user with ADMIN privileges. This is a simple way to prevent users
17
 * with access to {@link SecurityAdmin} (but no ADMIN privileges) to get themselves ADMIN access
18
 * (which might be implied by certain roles).
19
 *
20
 * @property string Title
21
 * @property string OnlyAdminCanApply
22
 *
23
 * @method HasManyList Codes() List of PermissionRoleCode objects
24
 * @method ManyManyList Groups() List of Group objects
25
 */
26
class PermissionRole extends DataObject
27
{
28
    private static $db = array(
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
29
        "Title" => "Varchar",
30
        "OnlyAdminCanApply" => "Boolean"
31
    );
32
33
    private static $has_many = array(
0 ignored issues
show
The private property $has_many is not used, and could be removed.
Loading history...
34
        "Codes" => "SilverStripe\\Security\\PermissionRoleCode",
35
    );
36
37
    private static $belongs_many_many = array(
0 ignored issues
show
The private property $belongs_many_many is not used, and could be removed.
Loading history...
38
        "Groups" => "SilverStripe\\Security\\Group",
39
    );
40
41
    private static $table_name = "PermissionRole";
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
42
43
    private static $default_sort = '"Title"';
0 ignored issues
show
The private property $default_sort is not used, and could be removed.
Loading history...
44
45
    private static $singular_name = 'Role';
0 ignored issues
show
The private property $singular_name is not used, and could be removed.
Loading history...
46
47
    private static $plural_name = 'Roles';
0 ignored issues
show
The private property $plural_name is not used, and could be removed.
Loading history...
48
49
    public function getCMSFields()
50
    {
51
        $fields = parent::getCMSFields();
52
53
        $fields->removeFieldFromTab('Root', 'Codes');
54
        $fields->removeFieldFromTab('Root', 'Groups');
55
56
        $fields->addFieldToTab(
57
            'Root.Main',
58
            $permissionField = new PermissionCheckboxSetField(
59
                'Codes',
60
                Permission::singleton()->i18n_plural_name(),
61
                'SilverStripe\\Security\\PermissionRoleCode',
62
                'RoleID'
63
            )
64
        );
65
        $permissionField->setHiddenPermissions(
66
            Permission::config()->hidden_permissions
0 ignored issues
show
Bug Best Practice introduced by
The property hidden_permissions does not exist on SilverStripe\Core\Config\Config_ForClass. Since you implemented __get, consider adding a @property annotation.
Loading history...
67
        );
68
69
        return $fields;
70
    }
71
72
    public function onAfterDelete()
73
    {
74
        parent::onAfterDelete();
75
76
        // Delete associated permission codes
77
        $codes = $this->Codes();
78
        foreach ($codes as $code) {
79
            $code->delete();
80
        }
81
    }
82
83
    public function fieldLabels($includerelations = true)
84
    {
85
        $labels = parent::fieldLabels($includerelations);
86
        $labels['Title'] = _t('SilverStripe\\Security\\PermissionRole.Title', 'Title');
87
        $labels['OnlyAdminCanApply'] = _t(
88
            'SilverStripe\\Security\\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
    {
98
        return Permission::check('APPLY_ROLES', 'any', $member);
99
    }
100
101
    public function canCreate($member = null, $context = array())
102
    {
103
        return Permission::check('APPLY_ROLES', 'any', $member);
104
    }
105
106
    public function canEdit($member = null)
107
    {
108
        return Permission::check('APPLY_ROLES', 'any', $member);
109
    }
110
111
    public function canDelete($member = null)
112
    {
113
        return Permission::check('APPLY_ROLES', 'any', $member);
114
    }
115
}
116