Issues (2882)

src/Security/PermissionRoleCode.php (4 issues)

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(
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
18
        "Code" => "Varchar",
19
    );
20
21
    private static $has_one = array(
0 ignored issues
show
The private property $has_one is not used, and could be removed.
Loading history...
22
        "Role" => PermissionRole::class,
23
    );
24
25
    private static $table_name = "PermissionRoleCode";
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property privileged_permissions does not exist on SilverStripe\Core\Config\Config_ForClass. Since you implemented __get, consider adding a @property annotation.
Loading history...
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