2019-12-16 14:34:26 +03:00
|
|
|
const path = require('path');
|
2019-08-09 17:11:24 +03:00
|
|
|
const Promise = require('bluebird');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const errors = require('@tryghost/errors');
|
2019-12-16 11:29:24 +03:00
|
|
|
const dbBackup = require('../../data/db/backup');
|
2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const permissionsService = require('../../services/permissions');
|
|
|
|
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) {
|
2019-12-16 14:34:26 +03:00
|
|
|
const backupPath = await dbBackup.backup();
|
|
|
|
const parsedFileName = path.parse(backupPath);
|
|
|
|
const filename = `${parsedFileName.name}${parsedFileName.ext}`;
|
2019-12-16 10:23:46 +03:00
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
return models.Base.transaction((t) => {
|
|
|
|
frame.options.transacting = t;
|
|
|
|
|
2020-04-07 09:20:56 +03:00
|
|
|
return models.Post.destroyByAuthor(frame.options)
|
2020-11-16 17:50:59 +03:00
|
|
|
.then(() => {
|
|
|
|
return models.ApiKey.destroy({
|
|
|
|
...frame.options,
|
|
|
|
require: true,
|
|
|
|
destroyBy: {
|
|
|
|
user_id: frame.options.id
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
if (err instanceof models.ApiKey.NotFoundError) {
|
|
|
|
return; //Do nothing here as it's ok
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
})
|
2020-04-07 09:20:56 +03:00
|
|
|
.then(() => {
|
|
|
|
return models.User.destroy(Object.assign({status: 'all'}, frame.options));
|
|
|
|
})
|
|
|
|
.then(() => filename);
|
2019-08-09 17:11:24 +03:00
|
|
|
}).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: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|