blob: 7aa60cb5b3f2f11d3d4c2ce90829316a991102a4 (
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# Groups
Groups are represented as objects in the Gatekeeper system with the following properties:
- description
- name
- id
- created
- updated
- users (relational)
Gatekeeper also supports hierarchical groups (see below).
## Creating a Group
Making a new group is as easy as making a new user. One thing to note, the *group name* must be **unique**:
```php
<?php
$attrs = array(
'name' => 'group1',
'description' => 'Group #1'
);
Gatekeeper::createGroup($attrs);
?>
```
## Getting Group Users
Much like you can easily get the groups the user belongs to, you can also get the members of a group. This will return a collection of user objects:
```php
<?php
$users = Gatekeeper::findGroupById(1)->users;
foreach ($users as $user) {
echo 'Username: '.$user->username."\n";
}
?>
```
## Adding a user to a group
You can add a user to a group by giving the `addUser` method one of two kinds of inputs:
```php
<?php
// Either a user ID
Gatekeeper::findGroupById(1)->addUser(1);
// Or a user model, it will extract the ID
$user = new UserModel();
Gatekeeper::findGroupById(1)->addUser($user);
?>
```
## Removing a user from a group
You can remove a user from a group in much the same way, either by an ID or a User model instance with the `removeUser` method:
```
<?php
// Either a user ID
Gatekeeper::findGroupById(1)->removeUser(1);
// Or a user model, it will extract the ID
$user = new UserModel();
Gatekeeper::findGroupById(1)->removeUser($user);
?>
```
## Checking to see if a user is in a group
You can use the `inGroup` method to check and see if a user ID is in a group:
```php
<?php
$userId = 1;
if (Gatekeeper::findGroupById(1)->inGroup($userId) === true) {
echo 'User is in the group!';
}
?>
```
## Adding a permission to a group
You can add permissions to groups too. These are related to the groups, not the users directly, so if you get the permissions for a user, these will not show in the list.
```php
<?php
$permId = 1;
Gatekeeper::findGroupById(1)->addPermission($permId);
// Or you can use a PermissionModel object
$permission = Gatekeeper::findPermissionById(1);
Gatekeeper::findGroupById(1)->addPermission($permission);
?>
```
## Removing a permission from a group
A permission can be removed from a group in the same way a user can, just with the `removePermission` method:
```
<?php
$permId = 1;
Gatekeeper::findGroupById(1)->removePermission($permId);
// Or you can use a PermissionModel object
$permission = Gatekeeper::findPermissionById(1);
Gatekeeper::findGroupById(1)->removePermission($permission);
?>
```
## Getting the permissions associated with the group
Since groups can have permissions attached, you can fetch those through the `permissions` property much in the same way you can for users:
```php
<?php
$permissions = Gatekeeper::findGroupById(1)->permissions;
foreach ($permissions as $permission) {
echo 'Description: '.$permission->description."\n";
}
?>
```
## Hierarchical Groups
Groups can also be added as children of other groups to help make categorizing easier. They can either be added by ID or model instance:
```php
<?php
$group = Gatekeeper::findGroupById(1);
$group->addChild(2);
// or
$childGroup = Gatekeeper::findGroupById(2);
$group->addChild($childGroup);
?>
```
You can find the child groups using the `children` property from a group instance:
```
<?php
$children = Gatekeeper::findGroupById(1)->children;
?>
```
You can also remove child groups similarly:
```
<?php
$group = Gatekeeper::findGroupById(1);
// You can remove either by ID
$group->removeChild(2);
// or by model instance
$group2 = Gatekeeper::findGroupById(2);
$group->removeChild($group2);
?>
```
|