Skip to content

Commit 53e2e30

Browse files
authored
Merge pull request #266 from codeigniter4/variadic-sync-2
2 parents e41b531 + 9c5e58a commit 53e2e30

3 files changed

Lines changed: 51 additions & 5 deletions

File tree

docs/quickstart.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ NOTE: The examples assume that you have run the setup script and that you have c
2020
- [Assign Permissions to a Group](#assign-permissions-to-a-group)
2121
- [Assign Permissions to a User](#assign-permissions-to-a-user)
2222
- [Check If a User Has Permission](#check-if-a-user-has-permission)
23+
- [Adding A Group To A User](#adding-a-group-to-a-user)
24+
- [Removing A Group From A User](#removing-a-group-from-a-user)
25+
- [Checking If User Belongs To A Group](#checking-if-user-belongs-to-a-group)
2326
- [Managing Users](#managing-users)
2427
- [Creating Users](#creating-users)
2528
- [Deleting Users](#deleting-users)
@@ -193,7 +196,7 @@ This will add all new permissions. You can also sync permissions so that the use
193196
```php
194197
$user = auth()->user();
195198

196-
$user->syncPermissions(['users.create', 'beta.access']);
199+
$user->syncPermissions('users.create', 'beta.access');
197200
```
198201

199202
## Check If a User Has Permission
@@ -208,7 +211,50 @@ if (! auth()->user()->can('users.create')) {
208211

209212
Note: The example above can also be done through a [controller filter](https://codeigniter.com/user_guide/incoming/filters.html) if you want to apply it to multiple pages of your site.
210213

214+
### Adding A Group To A User
211215

216+
Groups are assigned to a user via the `addGroup` method. You can pass multiple groups in and they will all be assigned to the user.
217+
218+
```php
219+
$user = auth()->user();
220+
$user->addGroup('admin', 'beta');
221+
```
222+
223+
This will add all new groups. You can also sync groups so that the user ONLY belongs to the groups directly assigned to them. Any not in the provided list are removed from the user.
224+
225+
```php
226+
$user = auth()->user();
227+
$user->syncGroups('admin', 'beta');
228+
```
229+
230+
### Removing A Group From A User
231+
232+
Groups are removed from a user via the `removeGroup` method. Multiple groups may be removed at once by passing all of their names into the method.
233+
234+
```php
235+
$user = auth()->user();
236+
$user->removeGroup('admin', 'beta');
237+
```
238+
239+
### Checking If User Belongs To A Group
240+
241+
You can check if a user belongs to a group with the `inGroup` method.
242+
243+
```php
244+
$user = auth()->user();
245+
if ($user->inGroup('admin')) {
246+
// do something
247+
}
248+
```
249+
250+
You can pass more than one group to the method and it will return `true` if the user belongs to any of the specified groups.
251+
252+
```php
253+
$user = auth()->user();
254+
if ($user->inGroup('admin', 'beta')) {
255+
// do something
256+
}
257+
```
212258

213259

214260

src/Authorization/Traits/Authorizable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function removeGroup(string ...$groups): self
8080
*
8181
* @return $this
8282
*/
83-
public function syncGroups(array $groups): self
83+
public function syncGroups(string ...$groups): self
8484
{
8585
$this->populateGroups();
8686

@@ -189,7 +189,7 @@ public function removePermission(string ...$permissions): self
189189
*
190190
* @return $this
191191
*/
192-
public function syncPermissions(array $permissions): self
192+
public function syncPermissions(string ...$permissions): self
193193
{
194194
$this->populatePermissions();
195195

tests/Authorization/AuthorizableTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function testSyncGroups(): void
142142
'created_at' => Time::now()->toDateTimeString(),
143143
]);
144144

145-
$this->user->syncGroups(['admin', 'beta']);
145+
$this->user->syncGroups('admin', 'beta');
146146
$this->assertSame(['admin', 'beta'], $this->user->getGroups());
147147
$this->seeInDatabase('auth_groups_users', [
148148
'user_id' => $this->user->id,
@@ -263,7 +263,7 @@ public function testSyncPermissions(): void
263263
'created_at' => Time::now()->toDateTimeString(),
264264
]);
265265

266-
$this->user->syncPermissions(['admin.access', 'beta.access']);
266+
$this->user->syncPermissions('admin.access', 'beta.access');
267267
$this->assertSame(['admin.access', 'beta.access'], $this->user->getPermissions());
268268
$this->seeInDatabase('auth_permissions_users', [
269269
'user_id' => $this->user->id,

0 commit comments

Comments
 (0)