@faryzal2020/v-perms 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,262 @@
1
+ /**
2
+ * Base adapter interface that defines the contract for database adapters
3
+ * All methods must be implemented by concrete adapters
4
+ */
5
+ class BaseAdapter {
6
+ // ==================== User Operations ====================
7
+
8
+ /**
9
+ * Get all roles assigned to a user
10
+ * @param {string} userId
11
+ * @returns {Promise<Array>}
12
+ */
13
+ async getUserRoles(userId) {
14
+ throw new Error('getUserRoles must be implemented');
15
+ }
16
+
17
+ /**
18
+ * Assign a role to a user
19
+ * @param {string} userId
20
+ * @param {string} roleId
21
+ * @returns {Promise<Object>}
22
+ */
23
+ async assignRoleToUser(userId, roleId) {
24
+ throw new Error('assignRoleToUser must be implemented');
25
+ }
26
+
27
+ /**
28
+ * Remove a role from a user
29
+ * @param {string} userId
30
+ * @param {string} roleId
31
+ * @returns {Promise<boolean>}
32
+ */
33
+ async removeRoleFromUser(userId, roleId) {
34
+ throw new Error('removeRoleFromUser must be implemented');
35
+ }
36
+
37
+ /**
38
+ * Check if a user has a specific role
39
+ * @param {string} userId
40
+ * @param {string} roleId
41
+ * @returns {Promise<boolean>}
42
+ */
43
+ async userHasRole(userId, roleId) {
44
+ throw new Error('userHasRole must be implemented');
45
+ }
46
+
47
+ // ==================== Role Operations ====================
48
+
49
+ /**
50
+ * Create a new role
51
+ * @param {Object} data - Role data { name, description?, priority?, isDefault? }
52
+ * @returns {Promise<Object>}
53
+ */
54
+ async createRole(data) {
55
+ throw new Error('createRole must be implemented');
56
+ }
57
+
58
+ /**
59
+ * Get a role by ID
60
+ * @param {string} roleId
61
+ * @returns {Promise<Object|null>}
62
+ */
63
+ async getRole(roleId) {
64
+ throw new Error('getRole must be implemented');
65
+ }
66
+
67
+ /**
68
+ * Get a role by name
69
+ * @param {string} name
70
+ * @returns {Promise<Object|null>}
71
+ */
72
+ async getRoleByName(name) {
73
+ throw new Error('getRoleByName must be implemented');
74
+ }
75
+
76
+ /**
77
+ * Update a role
78
+ * @param {string} roleId
79
+ * @param {Object} data - Fields to update
80
+ * @returns {Promise<Object>}
81
+ */
82
+ async updateRole(roleId, data) {
83
+ throw new Error('updateRole must be implemented');
84
+ }
85
+
86
+ /**
87
+ * Delete a role
88
+ * @param {string} roleId
89
+ * @returns {Promise<boolean>}
90
+ */
91
+ async deleteRole(roleId) {
92
+ throw new Error('deleteRole must be implemented');
93
+ }
94
+
95
+ /**
96
+ * Get all permissions assigned to a role
97
+ * @param {string} roleId
98
+ * @returns {Promise<Array>}
99
+ */
100
+ async getRolePermissions(roleId) {
101
+ throw new Error('getRolePermissions must be implemented');
102
+ }
103
+
104
+ /**
105
+ * Get roles that this role inherits from
106
+ * @param {string} roleId
107
+ * @returns {Promise<Array>}
108
+ */
109
+ async getRoleInheritance(roleId) {
110
+ throw new Error('getRoleInheritance must be implemented');
111
+ }
112
+
113
+ /**
114
+ * Set role inheritance
115
+ * @param {string} roleId - Role that will inherit
116
+ * @param {string} inheritsFromId - Role to inherit from
117
+ * @param {number} priority
118
+ * @returns {Promise<Object>}
119
+ */
120
+ async setRoleInheritance(roleId, inheritsFromId, priority) {
121
+ throw new Error('setRoleInheritance must be implemented');
122
+ }
123
+
124
+ /**
125
+ * Remove role inheritance
126
+ * @param {string} roleId
127
+ * @param {string} inheritsFromId
128
+ * @returns {Promise<boolean>}
129
+ */
130
+ async removeRoleInheritance(roleId, inheritsFromId) {
131
+ throw new Error('removeRoleInheritance must be implemented');
132
+ }
133
+
134
+ // ==================== Permission Operations ====================
135
+
136
+ /**
137
+ * Create a new permission
138
+ * @param {Object} data - { key, description?, category? }
139
+ * @returns {Promise<Object>}
140
+ */
141
+ async createPermission(data) {
142
+ throw new Error('createPermission must be implemented');
143
+ }
144
+
145
+ /**
146
+ * Get a permission by key
147
+ * @param {string} permissionKey
148
+ * @returns {Promise<Object|null>}
149
+ */
150
+ async getPermission(permissionKey) {
151
+ throw new Error('getPermission must be implemented');
152
+ }
153
+
154
+ /**
155
+ * Get a permission by ID
156
+ * @param {string} permissionId
157
+ * @returns {Promise<Object|null>}
158
+ */
159
+ async getPermissionById(permissionId) {
160
+ throw new Error('getPermissionById must be implemented');
161
+ }
162
+
163
+ /**
164
+ * Delete a permission by key
165
+ * @param {string} permissionKey
166
+ * @returns {Promise<boolean>}
167
+ */
168
+ async deletePermission(permissionKey) {
169
+ throw new Error('deletePermission must be implemented');
170
+ }
171
+
172
+ /**
173
+ * Assign a permission to a role
174
+ * @param {string} permissionKey
175
+ * @param {string} roleId
176
+ * @param {boolean} granted
177
+ * @returns {Promise<Object>}
178
+ */
179
+ async assignPermissionToRole(permissionKey, roleId, granted) {
180
+ throw new Error('assignPermissionToRole must be implemented');
181
+ }
182
+
183
+ /**
184
+ * Remove a permission from a role
185
+ * @param {string} permissionKey
186
+ * @param {string} roleId
187
+ * @returns {Promise<boolean>}
188
+ */
189
+ async removePermissionFromRole(permissionKey, roleId) {
190
+ throw new Error('removePermissionFromRole must be implemented');
191
+ }
192
+
193
+ /**
194
+ * Assign a permission to a user (override)
195
+ * @param {string} permissionKey
196
+ * @param {string} userId
197
+ * @param {boolean} granted
198
+ * @returns {Promise<Object>}
199
+ */
200
+ async assignPermissionToUser(permissionKey, userId, granted) {
201
+ throw new Error('assignPermissionToUser must be implemented');
202
+ }
203
+
204
+ /**
205
+ * Remove a permission from a user
206
+ * @param {string} permissionKey
207
+ * @param {string} userId
208
+ * @returns {Promise<boolean>}
209
+ */
210
+ async removePermissionFromUser(permissionKey, userId) {
211
+ throw new Error('removePermissionFromUser must be implemented');
212
+ }
213
+
214
+ /**
215
+ * Get all direct permissions assigned to a user
216
+ * @param {string} userId
217
+ * @returns {Promise<Array>}
218
+ */
219
+ async getUserDirectPermissions(userId) {
220
+ throw new Error('getUserDirectPermissions must be implemented');
221
+ }
222
+
223
+ /**
224
+ * Get specific role permission assignment
225
+ * @param {string} roleId
226
+ * @param {string} permissionKey
227
+ * @returns {Promise<Object|null>} - { granted: boolean } or null
228
+ */
229
+ async getRolePermission(roleId, permissionKey) {
230
+ throw new Error('getRolePermission must be implemented');
231
+ }
232
+
233
+ /**
234
+ * Get specific user permission assignment
235
+ * @param {string} userId
236
+ * @param {string} permissionKey
237
+ * @returns {Promise<Object|null>} - { granted: boolean } or null
238
+ */
239
+ async getUserPermission(userId, permissionKey) {
240
+ throw new Error('getUserPermission must be implemented');
241
+ }
242
+
243
+ // ==================== Listing Operations ====================
244
+
245
+ /**
246
+ * List all permissions
247
+ * @returns {Promise<Array>}
248
+ */
249
+ async listAllPermissions() {
250
+ throw new Error('listAllPermissions must be implemented');
251
+ }
252
+
253
+ /**
254
+ * List all roles
255
+ * @returns {Promise<Array>}
256
+ */
257
+ async listAllRoles() {
258
+ throw new Error('listAllRoles must be implemented');
259
+ }
260
+ }
261
+
262
+ export default BaseAdapter;