HasPermissionTrait::getAllPermissions()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * CakePHP permission handling library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\CakePermission\Model\Entity;
7
8
trait HasPermissionTrait
9
{
10
    /**
11
     * Checks whether the role or user has the permission with the name or array of permission names
12
     * @param string|array $name
13
     * @return bool
14
     */
15
    public function hasPermission($name)
16
    {
17
        if (is_array($name)) {
18
            $hasPermission = true;
19
            foreach ($name as $permissionName) {
20
                $hasPermission = $hasPermission && $this->hasPermission($permissionName);
21
                if (!$hasPermission) {
22
                    break;
23
                }
24
            }
25
            return $hasPermission;
26
        } else {
27
            $permissions = $this->getAllPermissions();
28
            $hasPermission = false;
29
            foreach ($permissions as $permission) {
30
                if ($permission->get('name') == $name) {
31
                    $hasPermission = true;
32
                    break;
33
                }
34
            }
35
            return $hasPermission;
36
        }
37
    }
38
39
    /**
40
     * Checks whether the role or user has any of permissions in the specified permissions
41
     * @param array $names
42
     * @return bool
43
     */
44
    public function hasAnyPermission(array $names)
45
    {
46
        $hasPermission = false;
47
        foreach ($names as $permissionName) {
48
            if ($hasPermission = $this->hasPermission($permissionName)) {
49
                break;
50
            }
51
        }
52
        return $hasPermission;
53
    }
54
55
    /**
56
     * Gets all permissions
57
     * @return Permission[]
58
     */
59
    abstract public function getAllPermissions();
60
}