summaryrefslogtreecommitdiff
path: root/src/Models/Auth/Permission.php
blob: aa05f53e6f5854d0ce1690db0e013bb0d3913b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

namespace Engelsystem\Models\Auth;

use Carbon\Carbon;
use Engelsystem\Models\BaseModel;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;

/**
 * @property integer                             $id
 * @property string                              $name
 * @property string|null                         $description
 * @property Carbon                              $created_at
 * @property Carbon                              $updated_at
 *
 * @property-read QueryBuilder|Collection|Role[] $roles
 * @property-read QueryBuilder|Collection|User[] $users
 *
 * @method static QueryBuilder|Permission              whereId($value)
 * @method static QueryBuilder|Collection|Permission[] whereName($value)
 * @method static QueryBuilder|Collection|Permission[] whereDescription($value)
 * @method static QueryBuilder|Collection|Permission[] whereCreatedAt($value)
 * @method static QueryBuilder|Collection|Permission[] whereUpdatedAt($value)
 */
class Permission extends BaseModel
{
    /** @var bool enable timestamps */
    public $timestamps = true;

    /** The attributes that are mass assignable */
    protected $fillable = [
        'name',
        'description',
    ];

    /**
     * The roles that have this permission
     *
     * @return BelongsToMany
     */
    public function roles()
    {
        return $this
            ->belongsToMany(Role::class)
            ->withTimestamps();
    }

    /**
     * The users that that have the permission
     *
     * @return HasManyDeep
     */
    public function users()
    {
        return $this->hasManyDeepFromRelations($this->roles(), (new Role)->users());
    }
}