@@ -54,28 +54,109 @@ Then you can publish the package resources (if needed) by doing:
54
54
python craft package:publish masonite-permission
55
55
```
56
56
57
+ Finally, extend ` User ` model class with ` HasRoles ` .
58
+
59
+ ``` python
60
+ from masoniteorm.models import Model
61
+ from masoniteorm.scopes import SoftDeletesMixin
62
+ from masonite.authentication import Authenticates
63
+ from masonite_permission.models.has_roles import HasRoles
64
+
65
+ class User (Model , SoftDeletesMixin , Authenticates , HasRoles ):
66
+ """ User Model."""
67
+
68
+ __fillable__ = [" name" , " email" , " password" ]
69
+ __hidden__ = [" password" ]
70
+ __auth__ = " email"
71
+ ```
72
+
57
73
## Usage
58
74
75
+ ** Role**
76
+ Methods that can be used in role model object:
77
+
59
78
``` python
60
- """ Permission Syncing """
61
- permissions = Permission.all()
62
- role.sync_permissions(permissions) # sync permissions to role
63
- role.sync_permissions([]) # remove all permissions from role
79
+ """ collection can be synced """
80
+ permission_collection = Permission.all()
81
+ role = Role.find(1 )
82
+
83
+ role.sync_permissions(permission_collection)
84
+
85
+ """ id will also work """
86
+ permission_ids = [1 ,2 ,3 ,4 ,5 ]
87
+ role.sync_permissions(permission_ids)
88
+
89
+ """ clear related data """
90
+ role.sync_permissions([])
91
+
64
92
65
93
""" Attach/Detatch Permission """
66
94
permission = Permission.first()
67
95
role.attach_permission(permission) # add permission to role, ignores if permission already exists
68
96
role.detach_permission(permission) # remove permission from role, ignores if permission doesn't exist
69
97
70
- """ Role Syncing """
71
- roles = Role.all()
72
- permission.sync_roles(roles) # sync roles to role
73
- permissioin.sync_roles([]) # remove all roles from role
98
+ """ this can also be done """
99
+ role.attach_permission(1 ) # passing permission id instead of object, ignores if permission already exists
100
+ role.detatch_permission(1 ) # passing permission id instead of object, ignores if permission doesn't exist
101
+ ```
102
+
103
+ ** Permission**
104
+ Methods that can be used in permission model object:
105
+
106
+ ``` python
107
+ """ collection can be synced """
108
+ roles_collection = Role.all()
109
+ permission = Permission.find(1 )
110
+
111
+ permission.sync_roles(role_collection)
112
+
113
+ """ id will also work """
114
+ role_ids = [1 ,2 ,3 ,4 ,5 ]
115
+ permission.sync_roles(role_ids)
116
+
117
+ """ clear related data """
118
+ permissioin.sync_roles([])
119
+
74
120
75
121
""" Attach/Detatch Role """
76
122
role = Role.first()
77
123
permission.attach_role(role) # add role to permission, ignores if role already exists
78
124
permission.detach_role(role) # remove role from permission, ignores if role doesn't exist
125
+
126
+ """ this can also be done """
127
+ permission.attach_role(1 ) # passing role id instead of object, ignores if role already exists
128
+ permission.detatch_role(1 ) # passing role id instead of object, ignores if role doesn't exist
129
+ ```
130
+
131
+ ** User**
132
+ Methods that can be used in user model object:
133
+
134
+ ``` python
135
+ user = User.first()
136
+
137
+ # Add/Remove single role
138
+ role = Role.first()
139
+
140
+ user.attach_role(role) # or you can pass role id
141
+ user.detatch_role(role) # or you can pass role id
142
+
143
+ # if you want to add multiple roles
144
+ roles = Role.all()
145
+
146
+ user.sync_roles(roles) # or you can also pass list of ids...
147
+
148
+ # remove all roles from user at once
149
+ user.sync_roles([])
150
+
151
+ # check if user has role
152
+ user.has_role(" role-slug" ) # returns boolean
153
+
154
+ # check if user has any of the roles
155
+ user.has_any_role([" role-1" , " role-2" ]) # returns boolean
156
+
157
+ # check if user has all of the roles
158
+ user.has_all_roles([" role-1" , " role-2" ]) # returns boolean
159
+
79
160
```
80
161
81
162
## Contributing
@@ -89,3 +170,7 @@ Please read the [Contributing Documentation](CONTRIBUTING.md) here.
89
170
## License
90
171
91
172
Masonite Permission is open-sourced software licensed under the [ MIT license] ( LICENSE ) .
173
+
174
+ ```
175
+
176
+ ```
0 commit comments