Fix admin model related issues

This commit is contained in:
Alexandre Bodin 2019-04-16 09:35:13 +02:00
parent eb9e53028e
commit 3cc3b626e2
6 changed files with 95 additions and 158 deletions

View File

@ -18,4 +18,4 @@
"type": "integer"
}
}
}
}

View File

@ -1,57 +1,54 @@
const _ = require('lodash');
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
/* eslint-disable indent */
module.exports = {
find: async function(params, populate) {
const model = this;
module.exports = ({ model }) => ({
find(params, populate) {
const filters = convertRestQueryParams(params);
return this.query(buildQuery({ model, filters }))
return model
.query(buildQuery({ model, filters }))
.fetchAll({
withRelated: populate || this.associations.map(x => x.alias),
withRelated: populate || model.associations.map(x => x.alias),
})
.then(data => data.toJSON());
},
count: async function(params = {}) {
const model = this;
count(params = {}) {
const { where } = convertRestQueryParams(params);
return this.query(buildQuery({ model, filters: { where } })).count();
return model.query(buildQuery({ model, filters: { where } })).count();
},
findOne: async function(params, populate) {
const primaryKey = params[this.primaryKey] || params._id;
async findOne(params, populate) {
const primaryKey = params[model.primaryKey] || params.id;
if (primaryKey) {
params = {
[this.primaryKey]: primaryKey,
[model.primaryKey]: primaryKey,
};
}
const record = await this.forge(params).fetch({
withRelated: populate || this.associations.map(x => x.alias),
const record = await model.forge(params).fetch({
withRelated: populate || model.associations.map(x => x.alias),
});
return record ? record.toJSON() : record;
},
create: async function(params) {
return this.forge()
async create(params) {
return model
.forge()
.save(
Object.keys(params).reduce((acc, current) => {
if (
_.get(this._attributes, [current, 'type']) ||
_.get(this._attributes, [current, 'model'])
_.get(model._attributes, [current, 'type']) ||
_.get(model._attributes, [current, 'model'])
) {
acc[current] = params[current];
}
return acc;
}, {}),
}, {})
)
.catch(err => {
if (err.detail) {
@ -63,26 +60,27 @@ module.exports = {
});
},
update: async function(search, params = {}) {
async update(search, params = {}) {
if (_.isEmpty(params)) {
params = search;
}
const primaryKey = search[this.primaryKey] || search.id;
const primaryKey = search[model.primaryKey] || search.id;
if (primaryKey) {
search = {
[this.primaryKey]: primaryKey,
[model.primaryKey]: primaryKey,
};
} else {
const entry = await module.exports.findOne.call(this, search);
const entry = await this.findOne(search);
search = {
[this.primaryKey]: entry[this.primaryKey] || entry.id,
[model.primaryKey]: entry[model.primaryKey] || entry.id,
};
}
return this.forge(search)
return model
.forge(search)
.save(params, {
patch: true,
})
@ -92,43 +90,5 @@ module.exports = {
throw error;
});
},
delete: async function(params) {
return await this.forge({
[this.primaryKey]: params[this.primaryKey] || params.id,
}).destroy();
},
deleteMany: async function(params) {
return await this.query(qb => {
qb.whereIn(this.primaryKey, params[this.primaryKey] || params.id);
}).destroy();
},
search: async function(params) {
return this.query(function(qb) {
qb.where('username', 'LIKE', `%${params.id}%`).orWhere(
'email',
'LIKE',
`%${params.id}%`,
);
}).fetchAll();
},
addPermission: async function(params) {
return this.forge(params).save();
},
removePermission: async function(params) {
const value = params[this.primaryKey]
? {
[this.primaryKey]: params[this.primaryKey] || params.id,
}
: params;
return this.forge()
.where(value)
.destroy();
},
};
}
});

View File

@ -1,10 +1,8 @@
const _ = require('lodash');
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
/* eslint-disable indent */
module.exports = {
find: async function(params, populate) {
const model = this;
module.exports = ({ model }) => ({
find(params, populate) {
const filters = convertRestQueryParams(params);
return buildQuery({
@ -14,9 +12,7 @@ module.exports = {
}).lean();
},
count: async function(params) {
const model = this;
count(params) {
const filters = convertRestQueryParams(params);
return buildQuery({
@ -25,97 +21,70 @@ module.exports = {
}).count();
},
findOne: async function(params, populate) {
const primaryKey = params[this.primaryKey] || params.id;
findOne(params, populate) {
const primaryKey = params[model.primaryKey] || params.id;
if (primaryKey) {
params = {
[this.primaryKey]: primaryKey,
[model.primaryKey]: primaryKey,
};
}
return this.findOne(params)
.populate(populate || this.associations.map(x => x.alias).join(' '))
return model
.findOne(params)
.populate(populate || model.associations.map(x => x.alias).join(' '))
.lean();
},
create: async function(params) {
return this.create(
Object.keys(params).reduce((acc, current) => {
if (
_.get(this._attributes, [current, 'type']) ||
_.get(this._attributes, [current, 'model'])
) {
acc[current] = params[current];
create(params) {
return model
.create(
Object.keys(params).reduce((acc, current) => {
if (
_.get(model._attributes, [current, 'type']) ||
_.get(model._attributes, [current, 'model'])
) {
acc[current] = params[current];
}
return acc;
}, {})
)
.catch(err => {
if (err.message.indexOf('index:') !== -1) {
const message = err.message.split('index:');
const field = _.words(_.last(message).split('_')[0]);
const error = { message: `This ${field} is already taken`, field };
throw error;
}
return acc;
}, {}),
).catch(err => {
if (err.message.indexOf('index:') !== -1) {
const message = err.message.split('index:');
const field = _.words(_.last(message).split('_')[0]);
const error = { message: `This ${field} is already taken`, field };
throw error;
}
throw err;
});
throw err;
});
},
update: async function(search, params = {}) {
update(search, params = {}) {
if (_.isEmpty(params)) {
params = search;
}
const primaryKey = search[this.primaryKey] || search.id;
const primaryKey = search[model.primaryKey] || search.id;
if (primaryKey) {
search = {
[this.primaryKey]: primaryKey,
[model.primaryKey]: primaryKey,
};
}
return this.updateOne(search, params, {
strict: false,
}).catch(error => {
const field = _.last(_.words(error.message.split('_')[0]));
const err = { message: `This ${field} is already taken`, field };
return model
.updateOne(search, params, {
strict: false,
})
.catch(error => {
const field = _.last(_.words(error.message.split('_')[0]));
const err = { message: `This ${field} is already taken`, field };
throw err;
});
throw err;
});
},
delete: async function(params) {
// Delete entry.
return this.deleteOne({
[this.primaryKey]: params[this.primaryKey] || params.id,
});
},
deleteMany: async function(params) {
// Delete entry.
return this.deleteMany({
[this.primaryKey]: {
$in: params[this.primaryKey] || params.id,
},
});
},
search: async function(params) {
const re = new RegExp(params.id);
return this.find({
$or: [{ username: re }, { email: re }],
});
},
addPermission: async function(params) {
return this.create(params);
},
removePermission: async function(params) {
return this.remove(params);
},
};
});

View File

@ -144,10 +144,9 @@ module.exports = {
}
// First, check if the admin is the first one to register as admin.
const admins = await strapi.query('administrator', 'admin').find();
const hasAdmin = admins.length > 0;
const adminsCount = await strapi.query('administrator', 'admin').count();
if (hasAdmin) {
if (adminsCount > 0) {
return ctx.badRequest(
null,
ctx.request.admin
@ -199,7 +198,7 @@ module.exports = {
]),
});
} catch (err) {
console.log('err', err);
strapi.log.error(err);
const adminError = _.includes(err.message, 'username')
? 'Auth.form.error.username.taken'
: 'Auth.form.error.email.taken';

View File

@ -102,3 +102,9 @@ node_modules
testApp
coverage
############################
# Strapi
############################
exports

View File

@ -431,18 +431,21 @@ class Strapi extends EventEmitter {
);
}
if (!_.isNil(plugin) && !_.has(strapi.plugins, plugin)) {
throw new Error(`Plugin ${plugin} not found`);
let buildQueries = defaultQueries[connector];
if (plugin === 'admin') {
buildQueries = _.get(
this.admin,
['config', 'queries', connector],
defaultQueries[connector]
);
} else if (plugin) {
buildQueries = _.get(
this.plugins,
[plugin, 'config', 'queries', connector],
defaultQueries[connector]
);
}
const buildQueries = plugin
? _.get(
this.plugins,
[plugin, 'config', 'queries', connector],
defaultQueries[connector]
)
: defaultQueries[connector];
let queries = buildQueries({ model, strapi: this });
return Object.assign(queries, {