mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
commit
a7415d38c0
2 changed files with 17 additions and 5 deletions
|
@ -85,7 +85,9 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
_.each(attrs, function (value, key) {
|
_.each(attrs, function (value, key) {
|
||||||
if (value !== null && schema.tables[self.tableName][key].type === 'dateTime') {
|
if (value !== null
|
||||||
|
&& schema.tables[self.tableName].hasOwnProperty(key)
|
||||||
|
&& schema.tables[self.tableName][key].type === 'dateTime') {
|
||||||
// convert dateTime value into a native javascript Date object
|
// convert dateTime value into a native javascript Date object
|
||||||
attrs[key] = moment(value).toDate();
|
attrs[key] = moment(value).toDate();
|
||||||
}
|
}
|
||||||
|
@ -98,7 +100,8 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||||
fixBools: function (attrs) {
|
fixBools: function (attrs) {
|
||||||
var self = this;
|
var self = this;
|
||||||
_.each(attrs, function (value, key) {
|
_.each(attrs, function (value, key) {
|
||||||
if (schema.tables[self.tableName][key].type === 'bool') {
|
if (schema.tables[self.tableName].hasOwnProperty(key)
|
||||||
|
&& schema.tables[self.tableName][key].type === 'bool') {
|
||||||
attrs[key] = value ? true : false;
|
attrs[key] = value ? true : false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -301,7 +301,8 @@ User = ghostBookshelf.Model.extend({
|
||||||
edit: function (data, options) {
|
edit: function (data, options) {
|
||||||
var self = this,
|
var self = this,
|
||||||
adminRole,
|
adminRole,
|
||||||
ownerRole;
|
ownerRole,
|
||||||
|
roleId;
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
options.withRelated = _.union([ 'roles' ], options.include);
|
options.withRelated = _.union([ 'roles' ], options.include);
|
||||||
|
@ -309,6 +310,7 @@ User = ghostBookshelf.Model.extend({
|
||||||
return ghostBookshelf.Model.edit.call(this, data, options).then(function (user) {
|
return ghostBookshelf.Model.edit.call(this, data, options).then(function (user) {
|
||||||
|
|
||||||
if (data.roles) {
|
if (data.roles) {
|
||||||
|
roleId = parseInt(data.roles[0].id || data.roles[0], 10);
|
||||||
|
|
||||||
if (user.id === options.context.user) {
|
if (user.id === options.context.user) {
|
||||||
return when.reject(new errors.ValidationError('You are not allowed to assign a new role to yourself'));
|
return when.reject(new errors.ValidationError('You are not allowed to assign a new role to yourself'));
|
||||||
|
@ -316,7 +318,14 @@ User = ghostBookshelf.Model.extend({
|
||||||
if (data.roles.length > 1) {
|
if (data.roles.length > 1) {
|
||||||
return when.reject(new errors.ValidationError('Only one role per user is supported at the moment.'));
|
return when.reject(new errors.ValidationError('Only one role per user is supported at the moment.'));
|
||||||
}
|
}
|
||||||
return Role.findOne({id: data.roles[0].id || data.roles[0]}).then(function (role) {
|
|
||||||
|
return user.roles().fetch().then(function (roles) {
|
||||||
|
// return if the role is already assigned
|
||||||
|
if (roles.models[0].id === roleId) {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
return Role.findOne({id: roleId});
|
||||||
|
}).then(function (role) {
|
||||||
if (role && role.get('name') === 'Owner') {
|
if (role && role.get('name') === 'Owner') {
|
||||||
// Get admin and owner role
|
// Get admin and owner role
|
||||||
return Role.findOne({name: 'Administrator'}).then(function (result) {
|
return Role.findOne({name: 'Administrator'}).then(function (result) {
|
||||||
|
@ -339,7 +348,7 @@ User = ghostBookshelf.Model.extend({
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// assign all other roles
|
// assign all other roles
|
||||||
return user.roles().updatePivot({role_id: data.roles[0].id || data.roles[0]});
|
return user.roles().updatePivot({role_id: roleId});
|
||||||
}
|
}
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
return self.findOne(user, options);
|
return self.findOne(user, options);
|
||||||
|
|
Loading…
Add table
Reference in a new issue