backend/src/api/passenger/policies/add-only-self.ts (view raw)
1import { errors } from "@strapi/utils";
2
3export default async (policyContext) => {
4 const user = policyContext.state.user;
5 const inputUserId = policyContext.args?.data?.user;
6
7 if (inputUserId) {
8 if (user && `${user.id}` !== inputUserId) {
9 const event = await strapi.entityService.findOne(
10 "api::event.event",
11 policyContext.args.data.event
12 );
13 const administrators = event.administrators?.split(/, ?/) || [];
14 const isEventAdmin = [...administrators, event.email].includes(
15 user.email
16 );
17 if (!isEventAdmin)
18 throw new errors.UnauthorizedError("Can't add another linked user");
19 else policyContext.args.data.isAdmin = true;
20 } else if (!user)
21 throw new errors.UnauthorizedError("Can't add linked user as anonymous");
22 }
23};