1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace z1haze\Acl\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use z1haze\Acl\Contracts\AclLevelInterface; |
7
|
|
|
use z1haze\Acl\Traits\UserAndLevel; |
8
|
|
|
|
9
|
|
|
class Level extends Model implements AclLevelInterface |
10
|
|
|
{ |
11
|
|
|
use UserAndLevel; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The attributes that should be cast to native types. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $casts = ['id' => 'integer', 'rank' => 'integer']; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The attributes that aren't mass assignable. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $guarded = ['id', 'created_id', 'updated_at']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* LEVEL |
29
|
|
|
* A Level has many users |
30
|
|
|
* |
31
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
32
|
|
|
*/ |
33
|
2 |
|
public function users() |
34
|
|
|
{ |
35
|
2 |
|
return $this->hasMany(config('laravel-acl.user')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* LEVEL |
40
|
|
|
* A level has many permissions |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
43
|
|
|
*/ |
44
|
15 |
|
public function permissions() |
45
|
|
|
{ |
46
|
15 |
|
$model = config('laravel-acl.permission', Permission::class); |
47
|
|
|
|
48
|
15 |
|
return $this->hasMany($model); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/* ------------------------------------------------------------------------------------------------ |
53
|
|
|
| Other Functions |
54
|
|
|
| ------------------------------------------------------------------------------------------------ |
55
|
|
|
*/ |
56
|
|
|
/** |
57
|
|
|
* Level constructor. |
58
|
|
|
* Sets the table name from the config |
59
|
|
|
* |
60
|
|
|
* @param array $attributes |
61
|
|
|
*/ |
62
|
55 |
|
public function __construct(array $attributes = []) |
63
|
|
|
{ |
64
|
55 |
|
parent::__construct($attributes); |
65
|
|
|
|
66
|
55 |
|
$this->table = config('laravel-acl.tables.level'); |
67
|
55 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Handle model events |
71
|
|
|
*/ |
72
|
55 |
|
public static function boot() |
73
|
|
|
{ |
74
|
55 |
|
parent::boot(); |
75
|
|
|
|
76
|
55 |
|
static::deleting(function($level) { |
77
|
1 |
|
$level->permissions()->where('level_id', $level->id)->update(['level_id' => null]); |
78
|
1 |
|
foreach ($level->users as $user) { |
79
|
|
|
$user->level()->dissociate()->save(); |
80
|
|
|
} |
81
|
55 |
|
}); |
82
|
55 |
|
} |
83
|
|
|
} |
84
|
|
|
|