-
Notifications
You must be signed in to change notification settings - Fork 611
/
UserGroup.php
138 lines (123 loc) · 3.16 KB
/
UserGroup.php
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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\models;
use Craft;
use craft\base\Model;
use craft\records\UserGroup as UserGroupRecord;
use craft\validators\HandleValidator;
use craft\validators\UniqueValidator;
/**
* UserGroup model class.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class UserGroup extends Model
{
/**
* @var int|null ID
*/
public ?int $id = null;
/**
* @var string|null Name
*/
public ?string $name = null;
/**
* @var string|null Handle
*/
public ?string $handle = null;
/**
* @var string|null Description
* @since 3.5.0
*/
public ?string $description = null;
/**
* @var string|null UID
*/
public ?string $uid = null;
/**
* @inheritdoc
*/
public function attributeLabels(): array
{
return [
'handle' => Craft::t('app', 'Handle'),
'name' => Craft::t('app', 'Name'),
];
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['id'], 'number', 'integerOnly' => true];
$rules[] = [['name', 'handle'], 'required'];
$rules[] = [['name', 'handle'], 'string', 'max' => 255];
$rules[] = [
['handle'],
HandleValidator::class,
'reservedWords' => [
'admins',
'all',
'credentialed',
'dateCreated',
'dateUpdated',
'id',
'inactive',
'new',
'title',
'uid',
],
];
$rules[] = [['name', 'handle'], UniqueValidator::class, 'targetClass' => UserGroupRecord::class];
return $rules;
}
/**
* Use the translated group name as the string representation.
*
* @return string
*/
public function __toString(): string
{
return Craft::t('site', $this->name) ?: static::class;
}
/**
* Returns whether the group has permission to perform a given action.
*
* @param string $permission
* @return bool
*/
public function can(string $permission): bool
{
if ($this->id) {
return Craft::$app->getUserPermissions()->doesGroupHavePermission($this->id, $permission);
}
return false;
}
/**
* Returns the user group’s config.
*
* @param bool $withPermissions Whether permissions should be included
* @return array
* @since 3.5.0
*/
public function getConfig(bool $withPermissions = true): array
{
$config = [
'name' => $this->name,
'handle' => $this->handle,
'description' => $this->description,
];
if ($withPermissions && $this->id) {
$permissions = Craft::$app->getUserPermissions()->getPermissionsByGroupId($this->id);
sort($permissions);
$config['permissions'] = $permissions;
}
return $config;
}
}