这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions server/endpoints/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ function adminEndpoints(app) {
try {
const { id } = request.params;
const updates = reqBody(request);
const user = await User.get({ id: Number(id) });

// Check to make sure with this update that includes a role change to
// something other than admin that we still have at least one admin left.
if (
updates.hasOwnProperty("role") && // has admin prop to change
updates.role !== "admin" && // and we are changing to non-admin
user.role === "admin" // and they currently are an admin
) {
const adminCount = await User.count({ role: "admin" });
if (adminCount - 1 <= 0) {
response
.status(200)
.json({
success: false,
error:
"No system admins will remain if you do this. Update failed.",
});
return;
}
}

const { success, error } = await User.update(id, updates);
response.status(200).json({ success, error });
} catch (e) {
Expand Down
22 changes: 22 additions & 0 deletions server/endpoints/api/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,28 @@ function apiAdminEndpoints(app) {

const { id } = request.params;
const updates = reqBody(request);
const user = await User.get({ id: Number(id) });

// Check to make sure with this update that includes a role change to
// something other than admin that we still have at least one admin left.
if (
updates.hasOwnProperty("role") && // has admin prop to change
updates.role !== "admin" && // and we are changing to non-admin
user.role === "admin" // and they currently are an admin
) {
const adminCount = await User.count({ role: "admin" });
if (adminCount - 1 <= 0) {
response
.status(200)
.json({
success: false,
error:
"No system admins will remain if you do this. Update failed.",
});
return;
}
}

const { success, error } = await User.update(id, updates);
response.status(200).json({ success, error });
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const User = {

update: async function (userId, updates = {}) {
try {
const updatedUser = await prisma.users.update({
await prisma.users.update({
where: { id: parseInt(userId) },
data: updates,
});
Expand Down