2019-08-09 17:11:24 +03:00
|
|
|
const Promise = require('bluebird');
|
2021-05-03 19:29:44 +03:00
|
|
|
const i18n = require('../../../shared/i18n');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const permissionsService = require('../../services/permissions');
|
2021-01-15 19:34:06 +03:00
|
|
|
const dbBackup = require('../../data/db/backup');
|
2021-06-23 15:54:28 +03:00
|
|
|
const auth = require('../../services/auth');
|
|
|
|
const apiMail = require('./index').mail;
|
|
|
|
const apiSettings = require('./index').settings;
|
2021-01-15 19:34:06 +03:00
|
|
|
const UsersService = require('../../services/users');
|
2021-06-23 15:54:28 +03:00
|
|
|
const userService = new UsersService({dbBackup, models, auth, apiMail, apiSettings});
|
2019-08-09 17:11:24 +03:00
|
|
|
const ALLOWED_INCLUDES = ['count.posts', 'permissions', 'roles', 'roles.permissions'];
|
|
|
|
const UNSAFE_ATTRS = ['status', 'roles'];
|
|
|
|
|
2020-10-26 19:27:12 +03:00
|
|
|
function permissionOnlySelf(frame) {
|
|
|
|
const targetId = getTargetId(frame);
|
|
|
|
const userId = frame.user.id;
|
|
|
|
if (targetId !== userId) {
|
|
|
|
return Promise.reject(new errors.NoPermissionError({message: i18n.t('errors.permissions.noPermissionToAction')}));
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTargetId(frame) {
|
|
|
|
return frame.options.id === 'me' ? frame.user.id : frame.options.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchOrCreatePersonalToken(userId) {
|
|
|
|
const token = await models.ApiKey.findOne({user_id: userId}, {});
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
const newToken = await models.ApiKey.add({user_id: userId, type: 'admin'});
|
|
|
|
return newToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'users',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields',
|
|
|
|
'limit',
|
|
|
|
'order',
|
|
|
|
'page',
|
|
|
|
'debug'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.User.findPage(frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields',
|
|
|
|
'debug'
|
|
|
|
],
|
|
|
|
data: [
|
|
|
|
'id',
|
|
|
|
'slug',
|
|
|
|
'email',
|
|
|
|
'role'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.User.findOne(frame.data, frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.users.userNotFound')
|
2019-08-09 17:11:24 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: {
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'id',
|
|
|
|
'include'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.User.edit(frame.data.users[0], frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.users.userNotFound')
|
2019-08-09 17:11:24 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.wasChanged()) {
|
|
|
|
this.headers.cacheInvalidate = true;
|
|
|
|
} else {
|
|
|
|
this.headers.cacheInvalidate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
2019-12-16 10:23:46 +03:00
|
|
|
async query(frame) {
|
2021-01-15 19:34:06 +03:00
|
|
|
return userService.destroyUser(frame.options).catch((err) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NoPermissionError({
|
2019-08-09 17:11:24 +03:00
|
|
|
err: err
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
changePassword: {
|
|
|
|
validation: {
|
|
|
|
docName: 'password',
|
|
|
|
data: {
|
|
|
|
newPassword: {required: true},
|
|
|
|
ne2Password: {required: true},
|
|
|
|
user_id: {required: true}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
docName: 'user',
|
|
|
|
method: 'edit',
|
|
|
|
identifier(frame) {
|
|
|
|
return frame.data.password[0].user_id;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
query(frame) {
|
2020-03-05 13:22:32 +03:00
|
|
|
frame.options.skipSessionID = frame.original.session.id;
|
2019-08-09 17:11:24 +03:00
|
|
|
return models.User.changePassword(frame.data.password[0], frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
transferOwnership: {
|
|
|
|
permissions(frame) {
|
|
|
|
return models.Role.findOne({name: 'Owner'})
|
|
|
|
.then((ownerRole) => {
|
|
|
|
return permissionsService.canThis(frame.options.context).assign.role(ownerRole);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.User.transferOwnership(frame.data.owner[0], frame.options);
|
|
|
|
}
|
2020-10-26 19:27:12 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
readToken: {
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: permissionOnlySelf,
|
|
|
|
query(frame) {
|
|
|
|
const targetId = getTargetId(frame);
|
|
|
|
return fetchOrCreatePersonalToken(targetId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
regenerateToken: {
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: permissionOnlySelf,
|
|
|
|
query(frame) {
|
|
|
|
const targetId = getTargetId(frame);
|
|
|
|
return fetchOrCreatePersonalToken(targetId).then((model) => {
|
|
|
|
return models.ApiKey.refreshSecret(model.toJSON(), Object.assign({}, {id: model.id}));
|
|
|
|
});
|
|
|
|
}
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
};
|