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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
# Users
We'll start with the **User** handling. Gatekeeper makes it simple to manage users and perform the usual CRUD (create, read update, delete) operations on their data.
Users are represented as objects in the code with the following properties:
- username
- password
- email
- firstName
- lastName
- status
- id
- resetCode
- resetCodeTimeout
- groups
- created
- updated
- groups (relational)
- permissions (relational)
- loginAttempts (relational)
You can access this data on a populated user object as you would any other object properties:
```php
<?php
echo 'Full name: '.$user->firstName.' '.$user->lastName."\n";
?>
```
## Getting All Users
You can use the `findUsers` method on the `Gatekeeper` class to get a list (returnes a `UserCollection`) of the current users:
```php
$users = Gatekeeper::findUsers();
// You can then slice it up how you need, like getting the first three
$shortUserList = $users->slice(1, 3);
```
## Creating Users
To create a user, you only need to provide the user details to the `register` method:
```php
<?php
$credentials = array(
'username' => 'ccornutt',
'password' => 'test1',
'email' => 'ccornutt@phpdeveloper.org',
'first_name' => 'Chris',
'last_name' => 'Cornutt'
);
Gatekeeper::register($credentials);
?>
```
The return value from the `register` call is a *boolean* indicating the pass/fail status of the registration.
Addiitonally, you can also link the user to permissions at create time:
```php
<?php
$credentials = array(
'username' => 'ccornutt',
'password' => 'test1',
'email' => 'ccornutt@phpdeveloper.org',
'first_name' => 'Chris',
'last_name' => 'Cornutt'
);
// Use can use permission names
$credentials['permissions'] = array('perm1', 'perm2');
// or use IDs
$credentials['permissions'] = array(1, 2);
Gatekeeper::register($credentials);
?>
```
**NOTE:** The permissions by the name/id you use must exist *before* the user, otherwise the link is not created.
You can also create groups the same way:
```php
<?php
$credentials = array(
'username' => 'ccornutt',
'password' => 'test1',
'email' => 'ccornutt@phpdeveloper.org',
'first_name' => 'Chris',
'last_name' => 'Cornutt'
);
// Use can use permission names
$credentials['groups'] = array('group1', 'group2');
// or use IDs
$credentials['groups'] = array(1, 2);
Gatekeeper::register($credentials);
?>
```
## Removing users
Deleteing user records can be done with the `deleteUserById` method:
```php
<?php
if (Gatekeeper::deleteUserById(1) === true) {
echo "User removed successfully!";
}
// Or, if you already have the User model object
Gatekeeper::deleteUser($user);
?>
```
## Activating/Deactivating Users
You can mark a user as active or inactive in the system easily. Inactive users will not be able to log in using the `authenticate` method. Changing the user status is easy:
```php
<?php
// Change the user status to active
Gatekeeper::findUserById(1)->activate();
// Change the user status to inactive
Gatekeeper::findUserById($userId)->deactivate();
?>
```
## Get User Groups
You can use the `groups` relational property to find the groups the user is a member of. It will return an iterable collection
you can use like any other array of data:
```php
<?php
$groups = Gatekeeper::findUserById($userId)->groups;
foreach($groups as $group) {
echo 'Group name: '.$group->name."\n";
}
?>
```
## See if a user is in a group
You can check to see if a user is in a group with the `inGroup` method:
```php
<?php
$groupId = 1;
if (Gatekeeper::findUserById(1)->inGroup($groupId) === true) {
echo 'The user is in the group!';
}
?>
```
## Adding a user to a group
You can add a user to a group by using the group ID:
```php
<?php
$groupId = 1;
if (Gatekeeper::findUserById($userId)->addGroup($groupId) === true) {
echo "User added successfullly!";
}
?>
```
## Revoking access to a group
You can also remove a user from a group by revoking their access:
```php
<?php
$groupId = 1;
if (Gatekeeper::findUserById($userId)->revokeGroup($groupId) === true) {
echo "User removed from group successfully!";
}
?>
```
## Checking to see if a user has a permission
You can check the user's immediate permissions (not the ones on groups they belong to) with the `hasPermission` method:
```php
<?php
$permissionId = 1;
if (Gatekeeper::findUserById($userId)->hasPermission($permissionId) === true) {
echo "They've got it!";
}
?>
```
You'll need to have the `id` value for the permission you want to check and provide that as the parameter in the call.
## Get a list of user permissions
You can use the `permissions` property to get the full set of user permissions. These are the permissions **directly assigned** to the user, not to any groups they may be a part of:
```php
<?php
$permissions = Gatekeeper::findUserById(1)->permissions;
foreach ($permissions as $perm) {
echo $perm->description."\n";
}
?>
```
## Giving a user a permission
You can assign a permission **directly** to a user (not through a group) with the `addPermission` method:
```php
<?php
$userId = 1;
$permissionId = 1;
if (Gatekeeper::findUserById($userId)->addPermission($permissionId) === true) {
echo 'Permission added!';
}
?>
```
You can also provide an optional second parameter with an expiration time if you only want to allow the user the permission for a limited about of time. This parameter should be in the form of a Unix timestamp:
```php
<?php
Gatekeeper::findUserById(1)->addPermission($permissionId, strtotime('+1 day'));
?>
```
When fetching a user's permission list (like with `$user->permissions`) it will only return the non-expired or permanent permissions.
## Revoking a permission
You can remove a permission from a user by revoking it:
```
<?php
$userId = 1;
$permissionId = 1;
if (Gatekeeper::findUserById($userId)->revokePermission($permissionId) === true) {
echo 'Permission revoked!';
}
?>
```
## Using "grant"
There's also a method on the User object that can be used to grant a user access to multiple permissions and groups all at the same time: `grant`. Here's an example:
```php
<?php
Gatekeeper::findUserById(1)->grant(array(
'permissions' => array(1, 3),
'groups' => array(1)
));
?>
```
You can either specify a `permissions` and `groups` values as an array of IDs or you can feed in objects...or a mix of both:
```php
<?php
$perm1 = Gatekeeper::findPermissionById(1);
$group1 = Gatekeeper::findGroupById(1);
Gatekeeper::findUserById(1)->grant(array(
'permissions' => array($perm1, 3),
'groups' => array($group1)
));
?>
```
## Check if a user is currently banned (throttling)
If the user login has had too many failed attempts, they'll be marked as "banned" in the system. You can find a user's ban status with the `isBanned` check:
```php
<?php
if (Gatekeeper::findUserById(1)->isBanned() === true) {
echo "User is banned!";
}
?>
```
## Get full user throttle information
You can also get the full throttling information for a user using the `throttle` property:
```php
<?php
$throttle = Gatekeeper::findUserById(1)->throttle;
// This gives you properties like:
$throttle->attempts;
$throttle->status;
$throttle->lastAttempt;
$throttle->statusChange;
?>
```
## Get the number of login attempts
You can also get information about the number of times a login has been attempted for a user (valid or invalid) with the `loginAttempts` property:
```php
<?php
$attempts = Gatekeeper::findUserById(1)->loginAttempts;
echo "The user has tried to log in ".$attempts." times.";
?>
```
|