summaryrefslogtreecommitdiffstats
path: root/docs/permissions.md
blob: 7d32dc61a027292794d751039675388555e2a7ba (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
# Permissions

The system supports the concept of *permissions*, a common part of a role-based access control system. In the Gatekeeper
system the permissions have these properties:

- id
- name
- description
- created date
- updated date

## Creating a permission

When creating a permission, you need to specify a name and description value. The `name` must be unique:

```php
<?php
$perm = array(
	'name' => 'perm1',
	'description' => 'Permission #1'
);
if (Gatekeeper::createPermission($perm) === true) {
	echo 'Permission created successfully!';
}
?>
```

You can also set an expiration date on your permissions using the `expire` property:

```php
<?php
$perm = [
	'name' => 'perm1',
	'description' => 'Permission #1',
	'expire' => strtotime('+1 day')
];
?>
```

These values are stored as Unix timestamps on the permission records themselves. This will cause the permission to exire, **not** the permission to no longer be allowed for a user (that's in the user-to-permission relationship). You can also check to see if a permission is expired with the `isExpired` method:

```php
<?php
$permission = Gatekeeper::findPermissionById(1);
if ($permission->isExpired() === true) {
	echo 'Oh noes, the permission expired!';
}
?>
```

You can also update the expiration time directly when you have a permission object in hand:

```php
<?php
$permission = Gatekeeper::findPermissionById(1);
$permission->expire = strtotime('+1 month');
$permission->save();
?>
```

## Adding Child Permissions

Much like groups, permissions also support the concept of children. Adding a permission as a child to a parent is easy and can be done in one of two ways:

```
<?php
$permission = Gatekeeper::findPermissionById(1);

// You can either add via ID
$permission->addChild(1);
// or with another model instance
$permission2 = Gatekeeper::findPermissionById(2);
$permission->addChild($permission);
?>
```

## Removing Child Permissions

You can also remove child permissions in a similar way:

```
<?php
$permission1 = Gatekeeper::findPermissionById(1);

// You can either remove via ID
$permission1->removeChild(2);
// Or by model instance
$permission2 = Gatekeeper::findPermissionById(1);
$permission1->removeChild($permission2);
?>
```

## Finding child permissions

If you want to find the permissions that are children of the current instance, you can use the `children` property:

```
<?php
$permission1 = Gatekeeper::findPermissionById(1);

$childPermissions = $permission1->children;
?>
```

This will return an array of permission objects representing the children.