-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathGroup.php
More file actions
executable file
·144 lines (121 loc) · 3.65 KB
/
Group.php
File metadata and controls
executable file
·144 lines (121 loc) · 3.65 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace App\Models;
use App\Models\Traits\Searchable;
use App\Presenters\GroupPresenter;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Gate;
use Watson\Validating\ValidatingTrait;
class Group extends SnipeModel
{
use HasFactory;
use Presentable;
protected $table = 'permission_groups';
public $rules = [
'name' => 'required|max:255|unique',
];
protected $fillable = [
'name',
'permissions',
'notes',
];
/**
* Whether the model should inject it's identifier to the unique
* validation rules before attempting validation. If this property
* is not set in the model it will default to true.
*
* @var bool
*/
protected $injectUniqueIdentifier = true;
protected $presenter = GroupPresenter::class;
use Searchable;
use ValidatingTrait;
/**
* The attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableAttributes = ['name', 'created_at', 'notes'];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [];
public function isDeletable()
{
return Gate::allows('delete', $this)
&& (($this->users_count ?? $this->users()->count()) === 0);
}
/**
* Establishes the groups -> users relationship
*
* @author A. Gianotto <snipe@snipe.net>
*
* @since [v1.0]
*
* @return Relation
*/
public function users()
{
return $this->belongsToMany(User::class, 'users_groups');
}
/* this is just a shim for SCIM to work */
public function members()
{
return $this->users();
}
/**
* Get the user that created the group
*
* @author A. Gianotto <snipe@snipe.net>
*
* @since [v6.3.0]
*
* @return Relation
*/
public function adminuser()
{
return $this->belongsTo(User::class, 'created_by')->withTrashed();
}
/**
* Decode JSON permissions into array
*
* @author A. Gianotto <snipe@snipe.net>
*
* @since [v1.0]
*
* @return array | \stdClass
*/
public function decodePermissions()
{
// If the permissions are an array, convert it to JSON
if (is_array($this->permissions)) {
$this->permissions = json_encode($this->permissions);
}
$permissions = json_decode($this->permissions ?? '{}', JSON_OBJECT_AS_ARRAY);
// Otherwise, loop through the permissions and cast the values as integers
if ((is_array($permissions)) && ($permissions)) {
foreach ($permissions as $permission => $value) {
if (! is_int($permission)) {
$permissions[$permission] = (int) $value;
} else {
\Log::info('Weird data here - skipping it');
unset($permissions[$permission]);
}
}
return $permissions ?: new \stdClass;
}
return new \stdClass;
}
/**
* -----------------------------------------------
* BEGIN QUERY SCOPES
* -----------------------------------------------
**/
public function scopeOrderByCreatedBy($query, $order)
{
return $query->leftJoin('users as admin_sort', 'permission_groups.created_by', '=', 'admin_sort.id')->select('permission_groups.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
}
}