Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async _revokePermission(documentId, permission, script, userIds = null) {
const updateParams = this._getPermissionUpdateScriptParams({ documentId, permission, userIds, script });
try {
await this._cluster.callWithInternalUser('update', updateParams);
} catch (e) {
console.warn(e);
const reason = _.get(e, 'body.error.caused_by.caused_by.reason');
if (reason === 'conflict') {
throw Boom.conflict(`Cannot revoke permission for a single user when all users can ${permission} the resource.`);
} else if (reason === 'forbidden') {
throw Boom.forbidden('The user is not authorized to remove the permissions for the resource.');
} else if (reason === 'own_removal') {
throw Boom.badRequest('Permissions for the creator of the resource can\'t be revoked.');
}
throw Boom.internal('Failed to alter permissions for the resource.', e.body.error);
}
}
Link.create(link, (err, _link) => {
if (err) {
if (err.code === 11000) {
return cb(Boom.conflict(`Link "${link.id}" is a duplicate`))
}
log.error({err: err, link: link}, 'error creating link')
return cb(Boom.internal())
}
cb(null, _link.toObject({ getters: true }))
})
}
exports.doSignup = async (ctx) => {
const User = jollof.models.User;
const email = ctx.request.fields.email;
//first check if user exists
if ((await User.exists(email)) === true) {
return ctx.throw(new boom.conflict(`User with email ${email} already exists`));
}
await User.persist(ctx.request.fields);
//Use email of new user as username
ctx.request.fields.username = ctx.request.fields.email;
await exports.doLogin(ctx);
}
.then(function (user) {
if (user) {
return reply(Boom.conflict('Email already in use.'));
}
return reply(true);
})
.catch(function (error) {
server.decorate('reply', 'conflict', function (message) {
message = message || 'A conflict has occured, resource may already exists or be deleted';
return this.response(Boom.conflict(message));
});
Company.create(company, function (err, _company) {
if (err) {
if (err.code === 11000) {
log.warn({err: err, requestedCompany: company.id}, 'company is a duplicate')
return cb(Boom.conflict(dupKeyParser(err.err) + ' is a duplicate'))
}
log.error({err: err, company: company}, 'error creating company')
return cb(Boom.internal())
}
cb(null, _company.toObject({ getters: true }))
})
}
User.findByUsername(request.payload.username, (err, user) => {
if (err) {
return reply(err);
}
if (!user) {
return reply(Boom.notFound('User document not found.'));
}
if (user.roles &&
user.roles.admin &&
user.roles.admin.id !== request.params.id) {
return reply(Boom.conflict('User is already linked to another admin. Unlink first.'));
}
reply(user);
});
}
}).then(function (users) {
if (_.isEmpty(users)) {
return when.resolve(user)
} else {
return when.reject(Boom.conflict('E-Mail address is already registered.'))
}
}).then(function (user) {
return user.save()
.then(function (override) {
if (!override)
throw Boom.conflict('User chose not to override '
+ 'existing profile.', profile);
});
})
module.exports = function handleESError(error) {
if (!(error instanceof Error)) {
throw new Error('Expected an instance of Error');
}
if (error instanceof esErrors.ConnectionFault ||
error instanceof esErrors.ServiceUnavailable ||
error instanceof esErrors.NoConnections ||
error instanceof esErrors.RequestTimeout) {
return Boom.serverTimeout(error);
} else if (error instanceof esErrors.Conflict || _.includes(error.message, 'index_template_already_exists')) {
return Boom.conflict(error);
} else if (error instanceof esErrors[403]) {
return Boom.forbidden(error);
} else if (error instanceof esErrors.NotFound) {
return Boom.notFound(error);
} else if (error instanceof esErrors.BadRequest) {
return Boom.badRequest(error);
} else if (error.status || error.statusCode) {
return Boom.boomify(error, { statusCode: error.status || error.statusCode });
} else {
return error;
}
};