Skip to content

Commit 78fc1da

Browse files
committed
feat: add student manager and student viewer roles for RBAC
1 parent 1098e2c commit 78fc1da

5 files changed

Lines changed: 27 additions & 75 deletions

File tree

strapi/src/api/admin-assignment/content-types/admin-assignment/schema.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

strapi/src/api/menu-item/content-types/menu-item/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"type": "relation",
6262
"relation": "manyToOne",
6363
"target": "api::school.school",
64-
"inversedBy": "menu_items"
64+
"inversedBy": "menuItems"
6565
}
6666
}
6767
}

strapi/src/api/school/content-types/school/schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@
4949
"instagram": {
5050
"type": "string"
5151
},
52-
"menu_items": {
52+
"menuItems": {
5353
"type": "relation",
5454
"relation": "oneToMany",
5555
"target": "api::menu-item.menu-item",
5656
"mappedBy": "school"
57+
},
58+
"managers": {
59+
"type": "relation",
60+
"relation": "oneToMany",
61+
"target": "admin::user"
5762
}
5863
}
5964
}

strapi/src/index.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@ export default {
33
async bootstrap({ strapi }) {
44
const getAssignedSchoolIds = async (user) => {
55
if (!user) return [];
6-
const assignment = await strapi
7-
.documents("api::admin-assignment.admin-assignment")
8-
.findFirst({
9-
filters: { user: { id: user.id } },
10-
populate: ["schools"],
6+
7+
const schools = await strapi
8+
.documents("api::school.school")
9+
.findMany({
10+
filters: {
11+
managers: {
12+
id: user.id,
13+
},
14+
},
1115
fields: ["id"],
1216
});
1317

14-
const schools = assignment.schools;
18+
console.log(schools)
19+
1520
return schools.map((school) => school.id);
1621
};
1722

18-
const getMenuItemIds = async (schoolId: string): Promise<number[]> => {
23+
const getMenuItemIds = async (schoolIds: string[]): Promise<number[]> => {
1924
const itemIds = await strapi
2025
.documents("api::menu-item.menu-item")
2126
.findMany({
22-
filters: { school: { id: schoolId } },
27+
filters: { school: { id: { $in: schoolIds } } },
2328
populate: ["school"],
2429
fields: ["id"],
2530
});
@@ -29,24 +34,22 @@ export default {
2934

3035
await strapi.admin.services.permission.conditionProvider.registerMany([
3136
{
32-
displayName: "Schools assigned to current admin",
33-
name: "schools-assigned-to-admin",
37+
displayName: "Manager Permissions for School",
38+
name: "school-for-manager",
3439
async handler(user) {
3540
const schoolIds = await getAssignedSchoolIds(user);
3641
if (schoolIds.length === 0) return false;
3742
return { id: { $in: schoolIds } };
3843
},
3944
},
4045
{
41-
displayName: "Menu items for admin's schools",
42-
name: "menu-items-for-admin-schools",
46+
displayName: "Manager Permissions for Menu Items",
47+
name: "menu-items-for-manager",
4348
async handler(user) {
4449
const schoolIds = await getAssignedSchoolIds(user);
4550
if (schoolIds.length === 0) return false;
4651

47-
const itemIds = (await Promise.all(
48-
schoolIds.map(async schoolId => await getMenuItemIds(schoolId))
49-
)).flat();
52+
const itemIds = await getMenuItemIds(schoolIds);
5053

5154
return { id: { $in: itemIds } };
5255
},

strapi/types/generated/contentTypes.d.ts

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -430,38 +430,6 @@ export interface AdminUser extends Struct.CollectionTypeSchema {
430430
};
431431
}
432432

433-
export interface ApiAdminAssignmentAdminAssignment
434-
extends Struct.CollectionTypeSchema {
435-
collectionName: 'admin_assignments';
436-
info: {
437-
displayName: 'Admin Assignment';
438-
pluralName: 'admin-assignments';
439-
singularName: 'admin-assignment';
440-
};
441-
options: {
442-
draftAndPublish: false;
443-
};
444-
attributes: {
445-
createdAt: Schema.Attribute.DateTime;
446-
createdBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> &
447-
Schema.Attribute.Private;
448-
locale: Schema.Attribute.String & Schema.Attribute.Private;
449-
localizations: Schema.Attribute.Relation<
450-
'oneToMany',
451-
'api::admin-assignment.admin-assignment'
452-
> &
453-
Schema.Attribute.Private;
454-
publishedAt: Schema.Attribute.DateTime;
455-
schools: Schema.Attribute.Relation<'manyToMany', 'api::school.school'> &
456-
Schema.Attribute.Required;
457-
updatedAt: Schema.Attribute.DateTime;
458-
updatedBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> &
459-
Schema.Attribute.Private;
460-
user: Schema.Attribute.Relation<'manyToOne', 'admin::user'> &
461-
Schema.Attribute.Required;
462-
};
463-
}
464-
465433
export interface ApiCartCart extends Struct.CollectionTypeSchema {
466434
collectionName: 'carts';
467435
info: {
@@ -624,7 +592,8 @@ export interface ApiSchoolSchool extends Struct.CollectionTypeSchema {
624592
'api::school.school'
625593
> &
626594
Schema.Attribute.Private;
627-
menu_items: Schema.Attribute.Relation<
595+
managers: Schema.Attribute.Relation<'oneToMany', 'admin::user'>;
596+
menuItems: Schema.Attribute.Relation<
628597
'oneToMany',
629598
'api::menu-item.menu-item'
630599
>;
@@ -1187,7 +1156,6 @@ declare module '@strapi/strapi' {
11871156
'admin::transfer-token': AdminTransferToken;
11881157
'admin::transfer-token-permission': AdminTransferTokenPermission;
11891158
'admin::user': AdminUser;
1190-
'api::admin-assignment.admin-assignment': ApiAdminAssignmentAdminAssignment;
11911159
'api::cart.cart': ApiCartCart;
11921160
'api::global.global': ApiGlobalGlobal;
11931161
'api::menu-item.menu-item': ApiMenuItemMenuItem;

0 commit comments

Comments
 (0)