retropilot-server/src/server/controllers/admin.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

import orm from '../../models/index.model';
2021-08-14 16:11:32 -06:00
// TODO move everythijng away from this dumb intertwined style
2022-01-08 17:35:40 -07:00
// eslint-disable-next-line no-unused-vars
2022-01-12 08:02:30 -07:00
import devices from './devices';
import authentication from './authentication';
async function isCurrentUserAdmin(hardFail, req) {
2022-01-07 18:35:55 -07:00
const account = await authentication.getAuthenticatedAccount(req);
if (!account) return { isAdmin: false, account };
if (account.admin !== 1) {
return { isAdmin: false, account };
}
2021-08-14 16:11:32 -06:00
2022-01-07 18:35:55 -07:00
return { isAdmin: true, account };
2021-08-14 16:11:32 -06:00
}
async function banAccount(ban, userId) {
2022-01-08 17:35:40 -07:00
if (!userId || !ban) {
return { success: false, status: 400, data: { bad_data: true } };
}
2022-01-07 18:35:55 -07:00
let cleanBan;
if (ban === 'true' || ban === 'false') {
cleanBan = ban === 'true';
2022-01-08 13:43:57 -07:00
} else {
2022-01-07 18:35:55 -07:00
return { success: false, status: 400, data: { bad_data: true } };
}
2022-01-08 17:35:40 -07:00
await orm.models.accounts.update(
2022-01-07 18:35:55 -07:00
{ banned: cleanBan ? 1 : 0 },
2022-01-08 13:43:57 -07:00
{ where: { id: userId } },
2022-01-07 18:35:55 -07:00
);
2022-01-08 17:35:40 -07:00
const verify = await orm.models.accounts.findOne({ where: { id: userId } });
2022-01-07 18:35:55 -07:00
if (verify.dataValues && verify.dataValues.banned === cleanBan ? 1 : 0) {
return { success: true, status: 200, data: { banned: ban } };
}
return { success: false, status: 500, data: { banned: false } };
2021-08-14 16:11:32 -06:00
}
2022-01-12 08:02:30 -07:00
export default {
2022-01-07 18:35:55 -07:00
banAccount,
2022-01-08 13:43:57 -07:00
isCurrentUserAdmin,
2022-01-07 18:35:55 -07:00
};