Completed
Push — master ( a8b6d8...ab125f )
by Ryan
02:01
created

RoleModel::setPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Anomaly\UsersModule\Role;
2
3
use Anomaly\Streams\Platform\Model\Users\UsersRolesEntryModel;
4
use Anomaly\UsersModule\Role\Contract\RoleInterface;
5
use Anomaly\UsersModule\User\UserCollection;
6
7
/**
8
 * Class RoleModel
9
 *
10
 * @link          http://anomaly.is/streams-platform
11
 * @author        AnomalyLabs, Inc. <[email protected]>
12
 * @author        Ryan Thompson <[email protected]>
13
 * @package       Anomaly\UsersModule\RoleInterface
14
 */
15
class RoleModel extends UsersRolesEntryModel implements RoleInterface
16
{
17
18
    /**
19
     * The cache minutes.
20
     *
21
     * @var int
22
     */
23
    protected $ttl = 99999;
24
25
    /**
26
     * Eager loaded relations.
27
     *
28
     * @var array
29
     */
30
    protected $with = [
31
        'translations'
32
    ];
33
34
    /**
35
     * Get the role slug.
36
     *
37
     * @return string
38
     */
39
    public function getSlug()
40
    {
41
        return $this->slug;
42
    }
43
44
    /**
45
     * Get the role name.
46
     *
47
     * @return string
48
     */
49
    public function getName()
50
    {
51
        return $this->name;
52
    }
53
54
    /**
55
     * Get the role's permissions.
56
     *
57
     * @return array
58
     */
59
    public function getPermissions()
60
    {
61
        return $this->permissions;
62
    }
63
64
    /**
65
     * Return if a role as access to a the permission.
66
     *
67
     * @param string $permission
68
     * @return mixed
69
     */
70
    public function hasPermission($permission)
71
    {
72
        if ($this->getSlug() == 'admin') {
73
            return true;
74
        }
75
76
        if (!$this->getPermissions()) {
77
            return false;
78
        }
79
80
        if (in_array($permission, $this->getPermissions())) {
81
            return true;
82
        }
83
84
        return false;
85
    }
86
87
    /**
88
     * Get the related users.
89
     *
90
     * @return UserCollection
91
     */
92
    public function getUsers()
93
    {
94
        return $this->users;
95
    }
96
97
    /**
98
     * Return the users relation.
99
     *
100
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
101
     */
102
    public function users()
103
    {
104
        return $this->belongsToMany(
105
            'Anomaly\UsersModule\User\UserModel',
106
            'users_users_roles',
107
            'related_id',
108
            'entry_id'
109
        );
110
    }
111
}
112