| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210 |
- import { ErrorV3 } from '@growi/core/dist/models';
- import { userHomepagePath } from '@growi/core/dist/utils/page-path-utils';
- import ExternalUserGroupRelation from '~/features/external-user-group/server/models/external-user-group-relation';
- import { SupportedAction } from '~/interfaces/activity';
- import Activity from '~/server/models/activity';
- import ExternalAccount from '~/server/models/external-account';
- import UserGroupRelation from '~/server/models/user-group-relation';
- import { configManager } from '~/server/service/config-manager';
- import { deleteCompletelyUserHomeBySystem } from '~/server/service/page/delete-completely-user-home-by-system';
- import loggerFactory from '~/utils/logger';
- import { generateAddActivityMiddleware } from '../../middlewares/add-activity';
- import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
- const logger = loggerFactory('growi:routes:apiv3:users');
- const path = require('path');
- const express = require('express');
- const router = express.Router();
- const { body, query } = require('express-validator');
- const { isEmail } = require('validator');
- const { serializePageSecurely } = require('../../models/serializers/page-serializer');
- const { serializeUserSecurely } = require('../../models/serializers/user-serializer');
- const PAGE_ITEMS = 50;
- const validator = {};
- /**
- * @swagger
- * tags:
- * name: Users
- */
- /**
- * @swagger
- *
- * components:
- * schemas:
- * User:
- * description: User
- * type: object
- * properties:
- * _id:
- * type: string
- * description: user ID
- * example: 5ae5fccfc5577b0004dbd8ab
- * lang:
- * type: string
- * description: language
- * example: 'en_US'
- * status:
- * type: integer
- * description: status
- * example: 0
- * admin:
- * type: boolean
- * description: whether the admin
- * example: false
- * email:
- * type: string
- * description: E-Mail address
- * example: alice@aaa.aaa
- * username:
- * type: string
- * description: username
- * example: alice
- * name:
- * type: string
- * description: full name
- * example: Alice
- * createdAt:
- * type: string
- * description: date created at
- * example: 2010-01-01T00:00:00.000Z
- */
- module.exports = (crowi) => {
- const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
- const loginRequired = require('../../middlewares/login-required')(crowi, true);
- const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
- const adminRequired = require('../../middlewares/admin-required')(crowi);
- const addActivity = generateAddActivityMiddleware(crowi);
- const activityEvent = crowi.event('activity');
- const {
- User,
- Page,
- } = crowi.models;
- const statusNo = {
- registered: User.STATUS_REGISTERED,
- active: User.STATUS_ACTIVE,
- suspended: User.STATUS_SUSPENDED,
- invited: User.STATUS_INVITED,
- };
- validator.statusList = [
- query('selectedStatusList').if(value => value != null).custom((value, { req }) => {
- const { user } = req;
- if (user != null && user.admin) {
- return value;
- }
- throw new Error('the param \'selectedStatusList\' is not allowed to use by the users except administrators');
- }),
- // validate sortOrder : asc or desc
- query('sortOrder').isIn(['asc', 'desc']),
- // validate sort : what column you will sort
- query('sort').isIn(['id', 'status', 'username', 'name', 'email', 'createdAt', 'lastLoginAt']),
- query('page').isInt({ min: 1 }),
- query('forceIncludeAttributes').toArray().custom((value, { req }) => {
- // only the admin user can specify forceIncludeAttributes
- if (value.length === 0) {
- return true;
- }
- return req.user.admin;
- }),
- ];
- validator.recentCreatedByUser = [
- query('limit').if(value => value != null).isInt({ max: 300 }).withMessage('You should set less than 300 or not to set limit.'),
- ];
- validator.usernames = [
- query('q').isString().withMessage('q is required'),
- query('offset').optional().isInt().withMessage('offset must be a number'),
- query('limit').optional().isInt({ max: 20 }).withMessage('You should set less than 20 or not to set limit.'),
- query('options').optional().isString().withMessage('options must be string'),
- ];
- // express middleware
- const certifyUserOperationOtherThenYourOwn = (req, res, next) => {
- const { id } = req.params;
- if (req.user._id.toString() === id) {
- const msg = 'This API is not available for your own users';
- logger.error(msg);
- return res.apiv3Err(new ErrorV3(msg), 400);
- }
- next();
- };
- const sendEmailByUserList = async(userList) => {
- const { appService, mailService } = crowi;
- const appTitle = appService.getAppTitle();
- const locale = configManager.getConfig('crowi', 'app:globalLang');
- const failedToSendEmailList = [];
- for (const user of userList) {
- try {
- // eslint-disable-next-line no-await-in-loop
- await mailService.send({
- to: user.email,
- subject: `Invitation to ${appTitle}`,
- template: path.join(crowi.localeDir, `${locale}/admin/userInvitation.ejs`),
- vars: {
- email: user.email,
- password: user.password,
- url: crowi.appService.getSiteUrl(),
- appTitle,
- },
- });
- // eslint-disable-next-line no-await-in-loop
- await User.updateIsInvitationEmailSended(user.user.id);
- }
- catch (err) {
- logger.error(err);
- failedToSendEmailList.push({
- email: user.email,
- reason: err.message,
- });
- }
- }
- return { failedToSendEmailList };
- };
- const sendEmailByUser = async(user) => {
- const { appService, mailService } = crowi;
- const appTitle = appService.getAppTitle();
- const locale = configManager.getConfig('crowi', 'app:globalLang');
- await mailService.send({
- to: user.email,
- subject: `New password for ${appTitle}`,
- template: path.join(crowi.localeDir, `${locale}/admin/userResetPassword.ejs`),
- vars: {
- email: user.email,
- password: user.password,
- url: crowi.appService.getSiteUrl(),
- appTitle,
- },
- });
- };
- /**
- * @swagger
- *
- * paths:
- * /users:
- * get:
- * tags: [Users]
- * operationId: listUsers
- * summary: /users
- * description: Select selected columns from users order by asc or desc
- * parameters:
- * - name: page
- * in: query
- * description: page number
- * schema:
- * type: number
- * - name: selectedStatusList
- * in: query
- * description: status list
- * schema:
- * type: string
- * - name: searchText
- * in: query
- * description: For incremental search value from input box
- * schema:
- * type: string
- * - name: sortOrder
- * in: query
- * description: asc or desc
- * schema:
- * type: string
- * - name: sort
- * in: query
- * description: sorting column
- * schema:
- * type: string
- * responses:
- * 200:
- * description: users are fetched
- * content:
- * application/json:
- * schema:
- * properties:
- * paginateResult:
- * $ref: '#/components/schemas/PaginateResult'
- */
- router.get('/', accessTokenParser, loginRequired, validator.statusList, apiV3FormValidator, async(req, res) => {
- const page = parseInt(req.query.page) || 1;
- // status
- const { forceIncludeAttributes } = req.query;
- const selectedStatusList = req.query.selectedStatusList || ['active'];
- const statusNoList = (selectedStatusList.includes('all')) ? Object.values(statusNo) : selectedStatusList.map(element => statusNo[element]);
- // Search from input
- const searchText = req.query.searchText || '';
- const searchWord = new RegExp(`${searchText}`);
- // Sort
- const { sort, sortOrder } = req.query;
- const sortOutput = {
- [sort]: (sortOrder === 'desc') ? -1 : 1,
- };
- // For more information about the external specification of the User API, see here (https://dev.growi.org/5fd7466a31d89500488248e3)
- const orConditions = [
- { name: { $in: searchWord } },
- { username: { $in: searchWord } },
- ];
- const query = {
- $and: [
- { status: { $in: statusNoList } },
- {
- $or: orConditions,
- },
- ],
- };
- try {
- if (req.user != null) {
- orConditions.push(
- {
- $and: [
- { isEmailPublished: true },
- { email: { $in: searchWord } },
- ],
- },
- );
- }
- if (forceIncludeAttributes.includes('email')) {
- orConditions.push({ email: { $in: searchWord } });
- }
- const paginateResult = await User.paginate(
- query,
- {
- sort: sortOutput,
- page,
- limit: PAGE_ITEMS,
- },
- );
- paginateResult.docs = paginateResult.docs.map((doc) => {
- // return email only when specified by query
- const { email } = doc;
- const user = serializeUserSecurely(doc);
- if (forceIncludeAttributes.includes('email')) {
- user.email = email;
- }
- return user;
- });
- return res.apiv3({ paginateResult });
- }
- 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'), 500);
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /{id}/recent:
- * get:
- * tags: [Users]
- * operationId: recent created page of user id
- * summary: /usersIdReacent
- * parameters:
- * - name: id
- * in: path
- * required: true
- * description: id of user
- * schema:
- * type: string
- * responses:
- * 200:
- * description: users recent created pages are fetched
- * content:
- * application/json:
- * schema:
- * properties:
- * paginateResult:
- * $ref: '#/components/schemas/PaginateResult'
- */
- router.get('/:id/recent', accessTokenParser, loginRequired, validator.recentCreatedByUser, apiV3FormValidator, async(req, res) => {
- const { id } = req.params;
- let user;
- try {
- user = await User.findById(id);
- }
- catch (err) {
- const msg = 'Error occurred in find user';
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(msg, 'retrieve-recent-created-pages-failed'), 500);
- }
- if (user == null) {
- return res.apiv3Err(new ErrorV3('find-user-is-not-found'));
- }
- const limit = parseInt(req.query.limit) || await configManager.getConfig('crowi', 'customize:showPageLimitationM') || 30;
- const page = req.query.page;
- const offset = (page - 1) * limit;
- const queryOptions = { offset, limit };
- try {
- const result = await Page.findListByCreator(user, req.user, queryOptions);
- result.pages = result.pages.map(page => serializePageSecurely(page));
- return res.apiv3(result);
- }
- catch (err) {
- const msg = 'Error occurred in retrieve recent created pages for user';
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(msg, 'retrieve-recent-created-pages-failed'), 500);
- }
- });
- validator.inviteEmail = [
- // isEmail prevents line breaks, so use isString
- body('shapedEmailList').custom((value) => {
- const array = value.filter((value) => { return isEmail(value) });
- if (array.length === 0) {
- throw new Error('At least one valid email address is required');
- }
- return array;
- }),
- ];
- /**
- * @swagger
- *
- * paths:
- * /users/invite:
- * post:
- * tags: [Users]
- * operationId: inviteUser
- * summary: /users/invite
- * description: Create new users and send Emails
- * parameters:
- * - name: shapedEmailList
- * in: query
- * description: Invitation emailList
- * schema:
- * type: object
- * - name: sendEmail
- * in: query
- * description: Whether to send mail
- * schema:
- * type: boolean
- * responses:
- * 200:
- * description: Inviting user success
- * content:
- * application/json:
- * schema:
- * properties:
- * createdUserList:
- * type: object
- * description: Users successfully created
- * existingEmailList:
- * type: object
- * description: Users email that already exists
- * failedEmailList:
- * type: object
- * description: Users email that failed to create or send email
- */
- router.post('/invite', loginRequiredStrictly, adminRequired, addActivity, validator.inviteEmail, apiV3FormValidator, async(req, res) => {
- // Delete duplicate email addresses
- const emailList = Array.from(new Set(req.body.shapedEmailList));
- let failedEmailList = [];
- // Create users
- const createUser = await User.createUsersByEmailList(emailList);
- if (createUser.failedToCreateUserEmailList.length > 0) {
- failedEmailList = failedEmailList.concat(createUser.failedToCreateUserEmailList);
- }
- // Send email
- if (req.body.sendEmail) {
- const sendEmail = await sendEmailByUserList(createUser.createdUserList);
- if (sendEmail.failedToSendEmailList.length > 0) {
- failedEmailList = failedEmailList.concat(sendEmail.failedToSendEmailList);
- }
- }
- const parameters = { action: SupportedAction.ACTION_ADMIN_USERS_INVITE };
- activityEvent.emit('update', res.locals.activity._id, parameters);
- return res.apiv3({
- createdUserList: createUser.createdUserList,
- existingEmailList: createUser.existingEmailList,
- failedEmailList,
- }, 201);
- });
- /**
- * @swagger
- *
- * paths:
- * /users/{id}/grant-admin:
- * put:
- * tags: [Users]
- * operationId: grantAdminUser
- * summary: /users/{id}/grant-admin
- * description: Grant user admin
- * parameters:
- * - name: id
- * in: path
- * required: true
- * description: id of user for admin
- * schema:
- * type: string
- * responses:
- * 200:
- * description: Grant user admin success
- * content:
- * application/json:
- * schema:
- * properties:
- * userData:
- * type: object
- * description: data of admin user
- */
- router.put('/:id/grant-admin', loginRequiredStrictly, adminRequired, addActivity, async(req, res) => {
- const { id } = req.params;
- try {
- const userData = await User.findById(id);
- await userData.grantAdmin();
- const serializedUserData = serializeUserSecurely(userData);
- activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_USERS_GRANT_ADMIN });
- return res.apiv3({ userData: serializedUserData });
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/{id}/revoke-admin:
- * put:
- * tags: [Users]
- * operationId: revokeAdminUser
- * summary: /users/{id}/revoke-admin
- * description: Revoke user admin
- * parameters:
- * - name: id
- * in: path
- * required: true
- * description: id of user for revoking admin
- * schema:
- * type: string
- * responses:
- * 200:
- * description: Revoke user admin success
- * content:
- * application/json:
- * schema:
- * properties:
- * userData:
- * type: object
- * description: data of revoked admin user
- */
- router.put('/:id/revoke-admin', loginRequiredStrictly, adminRequired, certifyUserOperationOtherThenYourOwn, addActivity, async(req, res) => {
- const { id } = req.params;
- try {
- const userData = await User.findById(id);
- await userData.revokeAdmin();
- const serializedUserData = serializeUserSecurely(userData);
- activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_USERS_REVOKE_ADMIN });
- return res.apiv3({ userData: serializedUserData });
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/{id}/grant-read-only:
- * put:
- * tags: [Users]
- * operationId: ReadOnly
- * summary: /users/{id}/grant-read-only
- * description: Grant user read only access
- * parameters:
- * - name: id
- * in: path
- * required: true
- * description: id of user for read only access
- * schema:
- * type: string
- * responses:
- * 200:
- * description: Grant user read only access success
- * content:
- * application/json:
- * schema:
- * properties:
- * userData:
- * type: object
- * description: data of read only
- */
- router.put('/:id/grant-read-only', loginRequiredStrictly, adminRequired, addActivity, async(req, res) => {
- const { id } = req.params;
- try {
- const userData = await User.findById(id);
- if (userData == null) {
- return res.apiv3Err(new ErrorV3('User not found'), 404);
- }
- await userData.grantReadOnly();
- const serializedUserData = serializeUserSecurely(userData);
- activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_USERS_GRANT_READ_ONLY });
- return res.apiv3({ userData: serializedUserData });
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/{id}/revoke-read-only:
- * put:
- * tags: [Users]
- * operationId: revokeReadOnly
- * summary: /users/{id}/revoke-read-only
- * description: Revoke user read only access
- * parameters:
- * - name: id
- * in: path
- * required: true
- * description: id of user for removing read only access
- * schema:
- * type: string
- * responses:
- * 200:
- * description: Revoke user read only access success
- * content:
- * application/json:
- * schema:
- * properties:
- * userData:
- * type: object
- * description: data of revoke read only
- */
- router.put('/:id/revoke-read-only', loginRequiredStrictly, adminRequired, addActivity, async(req, res) => {
- const { id } = req.params;
- try {
- const userData = await User.findById(id);
- if (userData == null) {
- return res.apiv3Err(new ErrorV3('User not found'), 404);
- }
- await userData.revokeReadOnly();
- const serializedUserData = serializeUserSecurely(userData);
- activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_USERS_REVOKE_READ_ONLY });
- return res.apiv3({ userData: serializedUserData });
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/{id}/activate:
- * put:
- * tags: [Users]
- * operationId: activateUser
- * summary: /users/{id}/activate
- * 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, addActivity, 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();
- const serializedUserData = serializeUserSecurely(userData);
- activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_USERS_ACTIVATE });
- return res.apiv3({ userData: serializedUserData });
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/{id}/deactivate:
- * put:
- * tags: [Users]
- * operationId: deactivateUser
- * summary: /users/{id}/deactivate
- * 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, certifyUserOperationOtherThenYourOwn, addActivity, async(req, res) => {
- const { id } = req.params;
- try {
- const userData = await User.findById(id);
- await userData.statusSuspend();
- const serializedUserData = serializeUserSecurely(userData);
- activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_USERS_DEACTIVATE });
- return res.apiv3({ userData: serializedUserData });
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/{id}/remove:
- * delete:
- * tags: [Users]
- * operationId: removeUser
- * summary: /users/{id}/remove
- * description: Delete user
- * parameters:
- * - name: id
- * in: path
- * required: true
- * description: id of delete user
- * schema:
- * type: string
- * responses:
- * 200:
- * description: Deleting user success
- * content:
- * application/json:
- * schema:
- * properties:
- * user:
- * type: object
- * description: data of deleted user
- */
- router.delete('/:id/remove', loginRequiredStrictly, adminRequired, certifyUserOperationOtherThenYourOwn, addActivity, async(req, res) => {
- const { id } = req.params;
- const isUsersHomepageDeletionEnabled = configManager.getConfig('crowi', 'security:user-homepage-deletion:isEnabled');
- const isForceDeleteUserHomepageOnUserDeletion = configManager.getConfig('crowi', 'security:user-homepage-deletion:isForceDeleteUserHomepageOnUserDeletion');
- try {
- const user = await User.findById(id);
- // !! DO NOT MOVE homepagePath FROM THIS POSITION !! -- 05.31.2023
- // catch username before delete user because username will be change to deleted_at_*
- const homepagePath = userHomepagePath(user);
- await UserGroupRelation.remove({ relatedUser: user });
- await ExternalUserGroupRelation.remove({ relatedUser: user });
- await user.statusDelete();
- await ExternalAccount.remove({ user });
- const serializedUser = serializeUserSecurely(user);
- activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_USERS_REMOVE });
- if (isUsersHomepageDeletionEnabled && isForceDeleteUserHomepageOnUserDeletion) {
- deleteCompletelyUserHomeBySystem(homepagePath, crowi.pageService);
- }
- return res.apiv3({ user: serializedUser });
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/external-accounts:
- * get:
- * tags: [Users]
- * operationId: listExternalAccountsUsers
- * summary: /users/external-accounts
- * description: Get external-account
- * responses:
- * 200:
- * description: external-account are fetched
- * content:
- * application/json:
- * schema:
- * properties:
- * paginateResult:
- * $ref: '#/components/schemas/PaginateResult'
- */
- router.get('/external-accounts/', loginRequiredStrictly, adminRequired, async(req, res) => {
- const page = parseInt(req.query.page) || 1;
- try {
- const paginateResult = await ExternalAccount.findAllWithPagination({ page });
- return res.apiv3({ paginateResult });
- }
- catch (err) {
- const msg = 'Error occurred in fetching external-account list ';
- logger.error(msg, err);
- return res.apiv3Err(new ErrorV3(msg + err.message, 'external-account-list-fetch-failed'), 500);
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/external-accounts/{id}/remove:
- * delete:
- * tags: [Users]
- * operationId: removeExternalAccountUser
- * summary: /users/external-accounts/{id}/remove
- * description: Delete ExternalAccount
- * parameters:
- * - name: id
- * in: path
- * required: true
- * description: id of ExternalAccount
- * schema:
- * type: string
- * responses:
- * 200:
- * description: External Account is removed
- * content:
- * application/json:
- * schema:
- * properties:
- * externalAccount:
- * type: object
- * description: A result of `ExtenralAccount.findByIdAndRemove`
- */
- router.delete('/external-accounts/:id/remove', loginRequiredStrictly, adminRequired, apiV3FormValidator, async(req, res) => {
- const { id } = req.params;
- try {
- const externalAccount = await ExternalAccount.findByIdAndRemove(id);
- return res.apiv3({ externalAccount });
- }
- catch (err) {
- const msg = 'Error occurred in deleting a external account ';
- logger.error(msg, err);
- return res.apiv3Err(new ErrorV3(msg + err.message, 'extenral-account-delete-failed'));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/update.imageUrlCache:
- * put:
- * tags: [Users]
- * operationId: update.imageUrlCache
- * summary: /users/update.imageUrlCache
- * description: update imageUrlCache
- * parameters:
- * - name: userIds
- * in: query
- * description: user id list
- * schema:
- * type: string
- * responses:
- * 200:
- * description: success creating imageUrlCached
- * content:
- * application/json:
- * schema:
- * properties:
- * userData:
- * type: object
- * description: users updated with imageUrlCached
- */
- router.put('/update.imageUrlCache', loginRequiredStrictly, adminRequired, async(req, res) => {
- try {
- const userIds = req.body.userIds;
- const users = await User.find({ _id: { $in: userIds }, imageUrlCached: null });
- const requests = await Promise.all(users.map(async(user) => {
- return {
- updateOne: {
- filter: { _id: user._id },
- update: { $set: { imageUrlCached: await user.generateImageUrlCached() } },
- },
- };
- }));
- if (requests.length > 0) {
- await User.bulkWrite(requests);
- }
- return res.apiv3({});
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/reset-password:
- * put:
- * tags: [Users]
- * operationId: resetPassword
- * summary: /users/reset-password
- * description: update imageUrlCache
- * requestBody:
- * content:
- * application/json:
- * schema:
- * properties:
- * newPassword:
- * type: string
- * user:
- * type: string
- * description: user id for reset password
- * responses:
- * 200:
- * description: success reset password
- */
- router.put('/reset-password', loginRequiredStrictly, adminRequired, addActivity, async(req, res) => {
- const { id } = req.body;
- try {
- const [newPassword, user] = await Promise.all([
- await User.resetPasswordByRandomString(id),
- await User.findById(id)]);
- activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_USERS_PASSWORD_RESET });
- return res.apiv3({ newPassword, user });
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/reset-password-email:
- * put:
- * tags: [Users]
- * operationId: resetPasswordEmail
- * summary: /users/reset-password-email
- * description: send new password email
- * requestBody:
- * content:
- * application/json:
- * schema:
- * properties:
- * newPassword:
- * type: string
- * user:
- * type: string
- * description: user id for send new password email
- * responses:
- * 200:
- * description: success send new password email
- */
- router.put('/reset-password-email', loginRequiredStrictly, adminRequired, addActivity, async(req, res) => {
- const { id } = req.body;
- try {
- const user = await User.findById(id);
- if (user == null) {
- throw new Error('User not found');
- }
- const userInfo = {
- email: user.email,
- password: req.body.newPassword,
- };
- await sendEmailByUser(userInfo);
- return res.apiv3();
- }
- catch (err) {
- const msg = err.message;
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(msg));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/send-invitation-email:
- * put:
- * tags: [Users]
- * operationId: sendInvitationEmail
- * summary: /users/send-invitation-email
- * description: send invitation email
- * requestBody:
- * content:
- * application/json:
- * schema:
- * properties:
- * id:
- * type: string
- * description: user id for send invitation email
- * responses:
- * 200:
- * description: success send invitation email
- * content:
- * application/json:
- * schema:
- * properties:
- * failedToSendEmail:
- * type: object
- * description: email and reasons for email sending failure
- */
- router.put('/send-invitation-email', loginRequiredStrictly, adminRequired, addActivity, async(req, res) => {
- const { id } = req.body;
- try {
- const user = await User.findById(id);
- const newPassword = await User.resetPasswordByRandomString(id);
- const userList = [{
- email: user.email,
- password: newPassword,
- user: { id },
- }];
- const sendEmail = await sendEmailByUserList(userList);
- // return null if absent
- activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_USERS_SEND_INVITATION_EMAIL });
- return res.apiv3({ failedToSendEmail: sendEmail.failedToSendEmailList[0] });
- }
- catch (err) {
- logger.error('Error', err);
- return res.apiv3Err(new ErrorV3(err));
- }
- });
- /**
- * @swagger
- *
- * paths:
- * /users/list:
- * get:
- * tags: [Users]
- * summary: /users/list
- * operationId: getUsersList
- * description: Get list of users
- * parameters:
- * - in: query
- * name: userIds
- * schema:
- * type: string
- * description: user IDs
- * example: 5e06fcc7516d64004dbf4da6,5e098d53baa2ac004e7d24ad
- * responses:
- * 200:
- * description: Succeeded to get list of users.
- * content:
- * application/json:
- * schema:
- * properties:
- * users:
- * type: array
- * items:
- * $ref: '#/components/schemas/User'
- * description: user list
- * 403:
- * $ref: '#/components/responses/403'
- * 500:
- * $ref: '#/components/responses/500'
- */
- router.get('/list', accessTokenParser, loginRequired, async(req, res) => {
- const userIds = req.query.userIds || null;
- let userFetcher;
- if (userIds !== null && userIds.split(',').length > 0) {
- userFetcher = User.findUsersByIds(userIds.split(','));
- }
- else {
- userFetcher = User.findAllUsers();
- }
- const data = {};
- try {
- const users = await userFetcher;
- data.users = users.map((user) => {
- // omit email
- if (user.isEmailPublished !== true) { // compare to 'true' because Crowi original data doesn't have 'isEmailPublished'
- user.email = undefined;
- }
- return user.toObject({ virtuals: true });
- });
- }
- catch (err) {
- return res.apiv3Err(new ErrorV3(err));
- }
- return res.apiv3(data);
- });
- router.get('/usernames', accessTokenParser, loginRequired, validator.usernames, apiV3FormValidator, async(req, res) => {
- const q = req.query.q;
- const offset = +req.query.offset || 0;
- const limit = +req.query.limit || 10;
- try {
- const options = JSON.parse(req.query.options || '{}');
- const data = {};
- if (options.isIncludeActiveUser == null || options.isIncludeActiveUser) {
- const activeUserData = await User.findUserByUsernameRegexWithTotalCount(q, [User.STATUS_ACTIVE], { offset, limit });
- const activeUsernames = activeUserData.users.map(user => user.username);
- Object.assign(data, { activeUser: { usernames: activeUsernames, totalCount: activeUserData.totalCount } });
- }
- if (options.isIncludeInactiveUser) {
- const inactiveUserStates = [User.STATUS_REGISTERED, User.STATUS_SUSPENDED, User.STATUS_INVITED];
- const inactiveUserData = await User.findUserByUsernameRegexWithTotalCount(q, inactiveUserStates, { offset, limit });
- const inactiveUsernames = inactiveUserData.users.map(user => user.username);
- Object.assign(data, { inactiveUser: { usernames: inactiveUsernames, totalCount: inactiveUserData.totalCount } });
- }
- if (options.isIncludeActivitySnapshotUser && req.user.admin) {
- const activitySnapshotUserData = await Activity.findSnapshotUsernamesByUsernameRegexWithTotalCount(q, { offset, limit });
- Object.assign(data, { activitySnapshotUser: activitySnapshotUserData });
- }
- // eslint-disable-next-line max-len
- const canIncludeMixedUsernames = (options.isIncludeMixedUsernames && req.user.admin) || (options.isIncludeMixedUsernames && !options.isIncludeActivitySnapshotUser);
- if (canIncludeMixedUsernames) {
- const allUsernames = [...data.activeUser?.usernames || [], ...data.inactiveUser?.usernames || [], ...data?.activitySnapshotUser?.usernames || []];
- const distinctUsernames = Array.from(new Set(allUsernames));
- Object.assign(data, { mixedUsernames: distinctUsernames });
- }
- return res.apiv3(data);
- }
- catch (err) {
- logger.error('Failed to get usernames', err);
- return res.apiv3Err(err);
- }
- });
- return router;
- };
|