You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The primary goal of CASL is to provide a simple way to declare user abilities in application. In the same time application may or may not have some capabilities depending on hardware, user's culture, subscription plan, etc. Lets see how these cases can be covered by CASL.
And later in UI check the capability with help of `ability.can('read', 'wifi')`. You can mix these definitions with your permission logic if you need. In this way, you have 2 levels of permission: device level (whether such capability exists at all) and role/subscription level (whether a user has the right to do this). But be careful with the order of rules. See [Combining Abilities][combining-abilities] for details.
33
33
34
-
The better way would be to create 2 instances of `Ability`: 1 for hardware capabilities another for permission based system and then use them together:
34
+
The better way would be to create 2 functions which defines:
35
+
* hardware capabilities
36
+
* and another for permission based system
37
+
38
+
Hardware should forbid user to do allowed action if it doesn't support that action. That means hardware permissions need to be defined using `cannot` DSL function
cannot('manage', 'Wifi').because('Device does not support Wifi')
40
46
}
41
47
42
-
can(....)
43
-
....
44
-
})
48
+
cannot(...)
49
+
50
+
return rules
51
+
}
52
+
```
53
+
54
+
User permissions can be defined as usually:
55
+
56
+
```js
57
+
functionuserPermissions(user) {
58
+
const { can, rules } =AbilityBuilder.extract()
45
59
46
-
constuserAbility=AbilityBuilder.define(can=> {
47
-
if (user.isAdmin) {
60
+
if (user.role==='admin') {
48
61
can('manage', 'all')
49
62
} else {
50
63
can('read', 'all')
51
64
}
52
-
})
53
65
54
-
if (hardwareAbility.can('manage', 'Wifi') &&userAbility.can('manage', 'Wifi')) {
55
-
// show wifi settings page
66
+
return rules
56
67
}
57
68
```
58
69
70
+
Now, we can merge them and create `Ability` instance. As hardware capabilities should override role permissions, it should be added at the end (rules order matters, see [Combining Abilities][combining-abilities] for details)
71
+
72
+
```js
73
+
// `capabilities` is an object, represents hardware capabilities, usually provided by hardware API
74
+
// `user` is an object, represents information about user details, usually provided by server API
75
+
76
+
construles=userPermissions(user)
77
+
.concat(hardwarePermissions(capabilities))
78
+
79
+
constability=newAbility(rules)
80
+
81
+
ability.can('manage', 'Wifi')
82
+
```
83
+
59
84
## Feature flags
60
85
61
86
Also you can use CASL to define feature flags and run [A/B testing](https://en.wikipedia.org/wiki/A/B_testing) for new features.
0 commit comments