OpenAuth_server/src/api/admin/permission.ts

112 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-11-06 19:48:50 +00:00
import { Request, Router } from "express";
import { GetUserMiddleware } from "../middlewares/user";
import RequestError, { HttpStatusCode } from "../../helper/request_error";
import promiseMiddleware from "../../helper/promiseMiddleware";
import Permission from "../../models/permissions";
import verify, { Types } from "../middlewares/verify";
import Client from "../../models/client";
import { ObjectID } from "bson";
2018-11-06 19:48:50 +00:00
const PermissionRoute: Router = Router();
PermissionRoute.route("/")
2019-03-14 22:10:39 +00:00
/**
* @api {get} /admin/permission
* @apiName AdminGetPermissions
2020-03-18 13:41:22 +00:00
*
2019-03-14 22:10:39 +00:00
* @apiParam client Optionally filter by client _id
2020-03-18 13:41:22 +00:00
*
2019-03-14 22:10:39 +00:00
* @apiGroup admin_permission
* @apiPermission admin
2020-03-18 13:41:22 +00:00
*
2019-03-14 22:10:39 +00:00
* @apiSuccess {Object[]} permissions
* @apiSuccess {String} permissions._id The ID
* @apiSuccess {String} permissions.name Permission name
* @apiSuccess {String} permissions.description A description, that makes it clear to the user, what this Permission allows to do
* @apiSuccess {String} permissions.client The ID of the owning client
*/
2020-03-18 13:41:22 +00:00
.get(
promiseMiddleware(async (req, res) => {
let query = {};
if (req.query.client) {
query = { client: new ObjectID(req.query.client) };
}
let permissions = await Permission.find(query);
res.json(permissions);
})
)
2019-03-14 22:10:39 +00:00
/**
* @api {post} /admin/permission
* @apiName AdminAddPermission
2020-03-18 13:41:22 +00:00
*
2019-03-14 22:10:39 +00:00
* @apiParam client The ID of the owning client
* @apiParam name Permission name
* @apiParam description A description, that makes it clear to the user, what this Permission allows to do
2020-03-18 13:41:22 +00:00
*
2019-03-14 22:10:39 +00:00
* @apiGroup admin_permission
* @apiPermission admin
2020-03-18 13:41:22 +00:00
*
2019-03-14 22:10:39 +00:00
* @apiSuccess {Object[]} permissions
* @apiSuccess {String} permissions._id The ID
* @apiSuccess {String} permissions.name Permission name
* @apiSuccess {String} permissions.description A description, that makes it clear to the user, what this Permission allows to do
* @apiSuccess {String} permissions.client The ID of the owning client
2020-03-18 13:41:22 +00:00
* @apiSuccess {String} permissions.grant_type The type of the permission. "user" | "client" granted
2019-03-14 22:10:39 +00:00
*/
2020-03-18 13:41:22 +00:00
.post(
verify(
{
client: {
2020-08-07 14:16:39 +00:00
type: Types.STRING,
2020-03-18 13:41:22 +00:00
},
name: {
2020-08-07 14:16:39 +00:00
type: Types.STRING,
2020-03-18 13:41:22 +00:00
},
description: {
2020-08-07 14:16:39 +00:00
type: Types.STRING,
2020-03-18 13:41:22 +00:00
},
type: {
type: Types.ENUM,
2020-08-07 14:16:39 +00:00
values: ["user", "client"],
},
2020-03-18 13:41:22 +00:00
},
true
),
promiseMiddleware(async (req, res) => {
let client = await Client.findById(req.body.client);
if (!client) {
throw new RequestError(
"Client not found",
HttpStatusCode.BAD_REQUEST
);
}
let permission = Permission.new({
description: req.body.description,
name: req.body.name,
client: client._id,
2020-08-07 14:16:39 +00:00
grant_type: req.body.type,
2020-03-18 13:41:22 +00:00
});
await Permission.save(permission);
res.json(permission);
})
)
2019-03-14 22:10:39 +00:00
/**
* @api {delete} /admin/permission
* @apiName AdminDeletePermission
2020-03-18 13:41:22 +00:00
*
2019-03-14 22:10:39 +00:00
* @apiParam id The permission ID
2020-03-18 13:41:22 +00:00
*
2019-03-14 22:10:39 +00:00
* @apiGroup admin_permission
* @apiPermission admin
2020-03-18 13:41:22 +00:00
*
2019-03-14 22:10:39 +00:00
* @apiSuccess {Boolean} success
*/
2020-03-18 13:41:22 +00:00
.delete(
promiseMiddleware(async (req, res) => {
let { id } = req.query;
await Permission.delete(id);
res.json({ success: true });
})
);
2018-11-06 19:48:50 +00:00
2020-03-18 13:41:22 +00:00
export default PermissionRoute;