-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: admin users can update some user details (#371)
- Loading branch information
1 parent
7c8dfbc
commit bf2abb0
Showing
8 changed files
with
166 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
directus/extensions/directus-extension-mcc/src/user-admin/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {defineEndpoint} from "@directus/extensions-sdk"; | ||
import {update} from "./update"; | ||
|
||
export default defineEndpoint((router, {services, database}) => { | ||
router.post("/update-user", async (req: any, res: any) => { | ||
return await update(req, res, services, database); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
directus/extensions/directus-extension-mcc/src/user-admin/update.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {AdminAccountability, userHasRole} from "../utils"; | ||
|
||
export async function update(req: any, res: any, services: any, database: any) { | ||
const { | ||
UsersService | ||
} = services; | ||
|
||
try { | ||
const userToUpdate = req.body; | ||
const userId = req.accountability.user; | ||
|
||
if (!userToUpdate) { | ||
return res.status(400).send("Missing user"); | ||
} | ||
|
||
const userHasPermission = await userHasRole(req, services, database, userId, ["Administrator", "Committee"]); | ||
|
||
if (!userHasPermission) { | ||
return res.status(401).send("You are not allowed to update user details"); | ||
} | ||
|
||
const userService = new UsersService({ | ||
knex: database, | ||
schema: req.schema, | ||
accountability: AdminAccountability | ||
}); | ||
|
||
const existingUser = await userService.readOne(userToUpdate.id); | ||
if (!existingUser) { | ||
return res.status(400).send("Cannot update user that does not exist"); | ||
} | ||
|
||
const canUpdateUser = !(await userHasRole(req, services, database, userToUpdate.id, ["Administrator", "Unverified", "Junior"])); | ||
|
||
if (!canUpdateUser) { | ||
return res.status(401).send("This user cannot be updated"); | ||
} | ||
|
||
await userService.updateOne(userToUpdate.id, userToUpdate); | ||
return res.status(200).send("User updated successfully"); | ||
} catch (err: any) { | ||
onsole.error("Error updating user from admin area", err); | ||
return res.status(500).send("Error updating user from admin area"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export const AdminAccountability = { | ||
admin: true | ||
}; | ||
|
||
export async function userHasRole(req: any, services: any, database: any, userId: string, roles: string[]) { | ||
console.log("checking is user has role", userId, roles); | ||
const { | ||
UsersService | ||
} = services; | ||
|
||
const userService = new UsersService({ | ||
knex: database, | ||
schema: req.schema, | ||
accountability: AdminAccountability | ||
}); | ||
|
||
const user = await userService.readOne(userId, { | ||
fields: ["role.name"] | ||
}); | ||
|
||
console.log("found user", user); | ||
const includes = roles.map(r => r.toLowerCase()).includes(user.role.name.toLowerCase()); | ||
|
||
console.log("result", includes); | ||
return includes; | ||
} | ||
|
||
export async function isUserLeader(req: any, services: any, database: any, eventId: string, userId: string) { | ||
console.log("checking if user is leader"); | ||
const { | ||
ItemsService | ||
} = services; | ||
|
||
const eventLeadersService = new ItemsService("events_directus_users", { | ||
knex: database, | ||
schema: req.schema, | ||
accountability: AdminAccountability | ||
}); | ||
|
||
const leaders = await eventLeadersService.readByQuery({ | ||
fields: ["*", "directus_users_id.first_name", "directus_users_id.last_name", "directus_users_id.avatar", "directus_users_id.id"], | ||
filter: { | ||
events_id: { | ||
_eq: eventId | ||
} | ||
} | ||
}); | ||
|
||
console.log("leaders", leaders); | ||
|
||
if (!leaders || leaders.length === 0) { | ||
return false; | ||
} | ||
|
||
console.log("got leaders", leaders); | ||
|
||
const userIsLeader = leaders.find((x: any) => x.directus_users_id.id === userId); | ||
console.log("result", !!userIsLeader); | ||
return !!userIsLeader; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters