|
|
@@ -32,6 +32,39 @@ module.exports = (crowi) => {
|
|
|
|
|
|
const { ApiV3FormValidator } = crowi.middlewares;
|
|
|
|
|
|
+ /**
|
|
|
+ * @swagger
|
|
|
+ *
|
|
|
+ * paths:
|
|
|
+ * /_api/v3/users:
|
|
|
+ * get:
|
|
|
+ * tags: [Users]
|
|
|
+ * description: Get users
|
|
|
+ * responses:
|
|
|
+ * 200:
|
|
|
+ * description: users are fetched
|
|
|
+ * content:
|
|
|
+ * application/json:
|
|
|
+ * schema:
|
|
|
+ * properties:
|
|
|
+ * users:
|
|
|
+ * type: object
|
|
|
+ * description: a result of `Users.find`
|
|
|
+ */
|
|
|
+ router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
|
|
|
+ try {
|
|
|
+ const page = parseInt(req.query.page) || 1;
|
|
|
+ const result = await User.findUsersWithPagination({ page });
|
|
|
+ const { docs: users, total: totalUsers, limit: pagingLimit } = result;
|
|
|
+ return res.apiv3({ users, totalUsers, pagingLimit });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ const msg = 'Error occurred in fetching user group list';
|
|
|
+ logger.error('Error', err);
|
|
|
+ return res.apiv3Err(new ErrorV3(msg, 'user-group-list-fetch-failed'));
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
validator.inviteEmail = [
|
|
|
// isEmail prevents line breaks, so use isString
|
|
|
body('shapedEmailList').custom((value) => {
|
|
|
@@ -86,6 +119,170 @@ module.exports = (crowi) => {
|
|
|
return res.apiv3Err(new ErrorV3(err));
|
|
|
}
|
|
|
});
|
|
|
+ /**
|
|
|
+ * @swagger
|
|
|
+ *
|
|
|
+ * paths:
|
|
|
+ * /_api/v3/users/{id}/giveAdmin:
|
|
|
+ * put:
|
|
|
+ * tags: [Users]
|
|
|
+ * description: Give user admin
|
|
|
+ * parameters:
|
|
|
+ * - name: id
|
|
|
+ * in: path
|
|
|
+ * required: true
|
|
|
+ * description: id of user for admin
|
|
|
+ * schema:
|
|
|
+ * type: string
|
|
|
+ * responses:
|
|
|
+ * 200:
|
|
|
+ * description: Give user admin success
|
|
|
+ * content:
|
|
|
+ * application/json:
|
|
|
+ * schema:
|
|
|
+ * properties:
|
|
|
+ * userData:
|
|
|
+ * type: object
|
|
|
+ * description: data of admin user
|
|
|
+ */
|
|
|
+ router.put('/:id/giveAdmin', loginRequiredStrictly, adminRequired, csrf, async(req, res) => {
|
|
|
+ const { id } = req.params;
|
|
|
+
|
|
|
+ try {
|
|
|
+ const userData = await User.findById(id);
|
|
|
+ await userData.makeAdmin();
|
|
|
+ return res.apiv3({ userData });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error('Error', err);
|
|
|
+ return res.apiv3Err(new ErrorV3(err));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ /**
|
|
|
+ * @swagger
|
|
|
+ *
|
|
|
+ * paths:
|
|
|
+ * /_api/v3/users/{id}/removeAdmin:
|
|
|
+ * put:
|
|
|
+ * tags: [Users]
|
|
|
+ * description: Remove user admin
|
|
|
+ * parameters:
|
|
|
+ * - name: id
|
|
|
+ * in: path
|
|
|
+ * required: true
|
|
|
+ * description: id of user for removing admin
|
|
|
+ * schema:
|
|
|
+ * type: string
|
|
|
+ * responses:
|
|
|
+ * 200:
|
|
|
+ * description: Remove user admin success
|
|
|
+ * content:
|
|
|
+ * application/json:
|
|
|
+ * schema:
|
|
|
+ * properties:
|
|
|
+ * userData:
|
|
|
+ * type: object
|
|
|
+ * description: data of removed admin user
|
|
|
+ */
|
|
|
+ router.put('/:id/removeAdmin', loginRequiredStrictly, adminRequired, csrf, async(req, res) => {
|
|
|
+ const { id } = req.params;
|
|
|
+
|
|
|
+ try {
|
|
|
+ const userData = await User.findById(id);
|
|
|
+ await userData.removeFromAdmin();
|
|
|
+ return res.apiv3({ userData });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error('Error', err);
|
|
|
+ return res.apiv3Err(new ErrorV3(err));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ /**
|
|
|
+ * @swagger
|
|
|
+ *
|
|
|
+ * paths:
|
|
|
+ * /_api/v3/users/{id}/activate:
|
|
|
+ * put:
|
|
|
+ * tags: [Users]
|
|
|
+ * description: Activate user
|
|
|
+ * parameters:
|
|
|
+ * - name: id
|
|
|
+ * in: path
|
|
|
+ * required: true
|
|
|
+ * description: id of activate user
|
|
|
+ * schema:
|
|
|
+ * type: string
|
|
|
+ * responses:
|
|
|
+ * 200:
|
|
|
+ * description: Activationg user success
|
|
|
+ * content:
|
|
|
+ * application/json:
|
|
|
+ * schema:
|
|
|
+ * properties:
|
|
|
+ * userData:
|
|
|
+ * type: object
|
|
|
+ * description: data of activate user
|
|
|
+ */
|
|
|
+ router.put('/:id/activate', loginRequiredStrictly, adminRequired, csrf, async(req, res) => {
|
|
|
+ // check user upper limit
|
|
|
+ const isUserCountExceedsUpperLimit = await User.isUserCountExceedsUpperLimit();
|
|
|
+ if (isUserCountExceedsUpperLimit) {
|
|
|
+ const msg = 'Unable to activate because user has reached limit';
|
|
|
+ logger.error('Error', msg);
|
|
|
+ return res.apiv3Err(new ErrorV3(msg));
|
|
|
+ }
|
|
|
+
|
|
|
+ const { id } = req.params;
|
|
|
+
|
|
|
+ try {
|
|
|
+ const userData = await User.findById(id);
|
|
|
+ await userData.statusActivate();
|
|
|
+ return res.apiv3({ userData });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error('Error', err);
|
|
|
+ return res.apiv3Err(new ErrorV3(err));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ /**
|
|
|
+ * @swagger
|
|
|
+ *
|
|
|
+ * paths:
|
|
|
+ * /_api/v3/users/{id}/deactivate:
|
|
|
+ * put:
|
|
|
+ * tags: [Users]
|
|
|
+ * description: Deactivate user
|
|
|
+ * parameters:
|
|
|
+ * - name: id
|
|
|
+ * in: path
|
|
|
+ * required: true
|
|
|
+ * description: id of deactivate user
|
|
|
+ * schema:
|
|
|
+ * type: string
|
|
|
+ * responses:
|
|
|
+ * 200:
|
|
|
+ * description: Deactivationg user success
|
|
|
+ * content:
|
|
|
+ * application/json:
|
|
|
+ * schema:
|
|
|
+ * properties:
|
|
|
+ * userData:
|
|
|
+ * type: object
|
|
|
+ * description: data of deactivate user
|
|
|
+ */
|
|
|
+ router.put('/:id/deactivate', loginRequiredStrictly, adminRequired, csrf, async(req, res) => {
|
|
|
+ const { id } = req.params;
|
|
|
+
|
|
|
+ try {
|
|
|
+ const userData = await User.findById(id);
|
|
|
+ await userData.statusSuspend();
|
|
|
+ return res.apiv3({ userData });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error('Error', err);
|
|
|
+ return res.apiv3Err(new ErrorV3(err));
|
|
|
+ }
|
|
|
+ });
|
|
|
/**
|
|
|
* @swagger
|
|
|
*
|