@holoyan/adonisjs-permissions 0.5.0 → 0.5.2
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.
|
@@ -288,6 +288,16 @@ export default class PermissionsService extends BaseService {
|
|
|
288
288
|
const newPermissions = (await this.permissionClassName.createMany(createManyData));
|
|
289
289
|
newPermissions.map((i) => permissionIds.push(i.id));
|
|
290
290
|
}
|
|
291
|
+
// first check if there are assigned or not
|
|
292
|
+
const alreadyAssigned = await this.modelPermissionQuery
|
|
293
|
+
.whereIn('id', permissionIds)
|
|
294
|
+
.where('model_type', modelType)
|
|
295
|
+
.where('model_id', modelId)
|
|
296
|
+
.select('id');
|
|
297
|
+
const alreadyAssignedIds = alreadyAssigned.map((item) => item.id);
|
|
298
|
+
permissionIds = permissionIds.filter((item) => {
|
|
299
|
+
return !alreadyAssignedIds.includes(item);
|
|
300
|
+
});
|
|
291
301
|
let modelPermissionMany = permissionIds.map((i) => ({
|
|
292
302
|
modelType: modelType,
|
|
293
303
|
modelId: modelId,
|
|
@@ -36,12 +36,14 @@ export default class RolesService extends BaseService {
|
|
|
36
36
|
}
|
|
37
37
|
modelRolesQuery(modelType, modelId) {
|
|
38
38
|
return this.roleQuery
|
|
39
|
-
.
|
|
39
|
+
.leftJoin(this.modelRoleTable + ' as mr', 'mr.role_id', '=', this.roleTable + '.id')
|
|
40
40
|
.where('mr.model_type', modelType)
|
|
41
41
|
.where('mr.model_id', modelId);
|
|
42
42
|
}
|
|
43
43
|
all(modelType, modelId) {
|
|
44
|
-
return this.modelRolesQuery(modelType, modelId)
|
|
44
|
+
return this.modelRolesQuery(modelType, modelId)
|
|
45
|
+
.distinct(this.roleTable + '.id')
|
|
46
|
+
.select(this.roleTable + '.*');
|
|
45
47
|
}
|
|
46
48
|
has(modelType, modelId, role) {
|
|
47
49
|
return this.hasAll(modelType, modelId, [role]);
|
|
@@ -74,29 +76,30 @@ export default class RolesService extends BaseService {
|
|
|
74
76
|
// @ts-ignore
|
|
75
77
|
return +r[0].$extras.total > 0;
|
|
76
78
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (!r.length) {
|
|
80
|
-
throw new Error('Role not found');
|
|
81
|
-
}
|
|
82
|
-
await this.modelRoleClassName.create({
|
|
83
|
-
modelType,
|
|
84
|
-
modelId,
|
|
85
|
-
roleId: r[0].id,
|
|
86
|
-
});
|
|
87
|
-
return true;
|
|
79
|
+
assign(role, modelType, modelId) {
|
|
80
|
+
return this.assignAll([role], modelType, modelId);
|
|
88
81
|
}
|
|
89
82
|
async assignAll(roles, modelType, modelId) {
|
|
90
83
|
const rs = await this.extractRoleModel(roles);
|
|
91
84
|
if (!rs.length) {
|
|
92
85
|
throw new Error('One or many roles not found');
|
|
93
86
|
}
|
|
87
|
+
let roleIds = rs.map((role) => role.id);
|
|
88
|
+
const modelRoles = await this.modelRoleQuery
|
|
89
|
+
.whereIn('role_id', roleIds)
|
|
90
|
+
.where('model_type', modelType)
|
|
91
|
+
.where('model_id', modelId)
|
|
92
|
+
.select('id');
|
|
93
|
+
const modelRoleIds = modelRoles.map((modelRole) => modelRole.id);
|
|
94
|
+
roleIds = roleIds.filter((roleId) => {
|
|
95
|
+
return !modelRoleIds.includes(roleId);
|
|
96
|
+
});
|
|
94
97
|
const data = [];
|
|
95
|
-
for (const
|
|
98
|
+
for (const id of roleIds) {
|
|
96
99
|
data.push({
|
|
97
100
|
modelType,
|
|
98
101
|
modelId,
|
|
99
|
-
roleId:
|
|
102
|
+
roleId: id,
|
|
100
103
|
});
|
|
101
104
|
}
|
|
102
105
|
await this.modelRoleClassName.createMany(data);
|