This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/server/routes/admin.js

50 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-10-31 13:33:59 -07:00
import { Router } from 'express';
import SQL from 'sql-template-strings';
2020-11-02 12:12:15 -08:00
import avatar from '../avatar';
2020-11-02 12:45:45 -08:00
import {config as socialLoginConfig} from "../social";
import {now} from "../../src/helpers";
2020-10-31 13:33:59 -07:00
const router = Router();
router.get('/admin/users', async (req, res) => {
if (!req.admin) {
return res.status(401).json({error: 'Unauthorised'});
}
const users = await req.db.all(SQL`
2020-11-02 12:12:15 -08:00
SELECT u.id, u.username, u.email, u.roles, u.avatarSource, p.locale
2020-10-31 13:33:59 -07:00
FROM users u
LEFT JOIN profiles p ON p.userId = u.id
ORDER BY u.id DESC
`);
2020-11-02 12:45:45 -08:00
const authenticators = await req.db.all(SQL`
SELECT userId, type FROM authenticators
WHERE type IN (`.append(Object.keys(socialLoginConfig).map(k => `'${k}'`).join(',')).append(SQL`)
AND (validUntil IS NULL OR validUntil > ${now()})
`));
2020-10-31 13:33:59 -07:00
const groupedUsers = {};
for (let user of users) {
if (groupedUsers[user.id] === undefined) {
groupedUsers[user.id] = {
...user,
locale: undefined,
profiles: user.locale ? [user.locale] : [],
2020-11-02 12:12:15 -08:00
avatar: await avatar(req.db, user),
2020-11-02 12:45:45 -08:00
socialConnections: [],
2020-10-31 13:33:59 -07:00
}
} else {
groupedUsers[user.id].profiles.push(user.locale);
}
}
2020-11-02 12:45:45 -08:00
for (let auth of authenticators) {
groupedUsers[auth.userId].socialConnections.push(auth.type);
}
2020-10-31 13:33:59 -07:00
return res.json(groupedUsers);
});
export default router;