0.12.1 : update user
This commit is contained in:
@@ -33,12 +33,10 @@ module.exports = (plugin) => {
|
||||
throw new Error("No access_token.");
|
||||
}
|
||||
|
||||
// Get the profile.
|
||||
const profile = await getProfile(provider, query);
|
||||
|
||||
const email = lod.toLower(profile.email);
|
||||
|
||||
// We need at least the mail.
|
||||
if (!email) {
|
||||
throw new Error("Email was not available.");
|
||||
}
|
||||
@@ -70,15 +68,13 @@ module.exports = (plugin) => {
|
||||
throw new Error("Email is already taken.");
|
||||
}
|
||||
|
||||
// Retrieve default role.
|
||||
const defaultRole = await strapi.db
|
||||
.query("plugin::users-permissions.role")
|
||||
.findOne({ where: { type: advancedSettings.default_role } });
|
||||
|
||||
// Create the new user.
|
||||
const newUser = {
|
||||
...profile,
|
||||
email, // overwrite with lowercased email
|
||||
email,
|
||||
provider,
|
||||
role: defaultRole.id,
|
||||
confirmed: true,
|
||||
@@ -93,6 +89,73 @@ module.exports = (plugin) => {
|
||||
};
|
||||
};
|
||||
|
||||
const originalMe = plugin.controllers.user.me;
|
||||
|
||||
plugin.controllers.user.me = async (ctx) => {
|
||||
const fullUser = await strapi.db
|
||||
.query("plugin::users-permissions.user")
|
||||
.findOne({
|
||||
where: { id: ctx.state.user.id },
|
||||
populate: true,
|
||||
});
|
||||
|
||||
const user = ctx.state.user;
|
||||
|
||||
const userId = user.id;
|
||||
|
||||
const [
|
||||
contactsCount,
|
||||
groupsCount,
|
||||
postsCount,
|
||||
eventsCount,
|
||||
followersCount,
|
||||
followingCount,
|
||||
] = await Promise.all([
|
||||
strapi.db.query("api::contact.contact").count({
|
||||
where: {
|
||||
$or: [{ owner: userId }, { user: userId }],
|
||||
},
|
||||
}),
|
||||
strapi.db.query("api::group-membership.group-membership").count({
|
||||
where: {
|
||||
user: userId,
|
||||
role: { $in: ["owner", "member", "admin"] },
|
||||
},
|
||||
}),
|
||||
strapi.db
|
||||
.query("api::post-ownership.post-ownership")
|
||||
.count({ where: { author: userId } }),
|
||||
strapi.db
|
||||
.query("api::event-relationship.event-relationship")
|
||||
.count({ where: { author: userId } }),
|
||||
strapi.db.query("api::contact.contact").count({
|
||||
where: {
|
||||
user: userId,
|
||||
state: "follow",
|
||||
},
|
||||
}),
|
||||
strapi.db.query("api::contact.contact").count({
|
||||
where: {
|
||||
owner: userId,
|
||||
state: "follow",
|
||||
},
|
||||
}),
|
||||
]);
|
||||
const result = {
|
||||
...JSON.parse(JSON.stringify(fullUser)),
|
||||
stats: {
|
||||
contacts: contactsCount,
|
||||
groups: groupsCount,
|
||||
posts: postsCount,
|
||||
events: eventsCount,
|
||||
followers: followersCount,
|
||||
following: followingCount,
|
||||
},
|
||||
};
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const uploadImage = async (ctx, label: string, username: string) => {
|
||||
const key = `${label}Image`;
|
||||
if (ctx.request.files[key]) {
|
||||
@@ -122,7 +185,6 @@ module.exports = (plugin) => {
|
||||
plugin.controllers.user.updateMe = async (ctx) => {
|
||||
const user = ctx.state.user;
|
||||
|
||||
// User has to be logged in to update themselves
|
||||
if (!user) {
|
||||
return ctx.unauthorized();
|
||||
}
|
||||
@@ -139,9 +201,24 @@ module.exports = (plugin) => {
|
||||
"voice",
|
||||
"job",
|
||||
"dob",
|
||||
"phone",
|
||||
"bio",
|
||||
"experience",
|
||||
]);
|
||||
|
||||
// Make sure there is no duplicate user with the same username
|
||||
if (data.tags) {
|
||||
newData.tags = data.tags.map((tag) => ({
|
||||
text: tag.text,
|
||||
}));
|
||||
}
|
||||
|
||||
if (data.languages) {
|
||||
newData.languages = data.languages.map((lang) => ({
|
||||
language: lang.language,
|
||||
level: lang.level,
|
||||
}));
|
||||
}
|
||||
|
||||
if (newData.username) {
|
||||
const userWithSameUsername = await strapi
|
||||
.query("plugin::users-permissions.user")
|
||||
@@ -152,7 +229,6 @@ module.exports = (plugin) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure there is no duplicate user with the same email
|
||||
if (newData.email) {
|
||||
const userWithSameEmail = await strapi
|
||||
.query("plugin::users-permissions.user")
|
||||
@@ -169,9 +245,6 @@ module.exports = (plugin) => {
|
||||
|
||||
const avatarId = await uploadImage(ctx, "avatar", newData.username);
|
||||
if (avatarId != 0) ctx.request.body.avatar = avatarId;
|
||||
const backgroundId = await uploadImage(ctx, "background", newData.username);
|
||||
if (backgroundId != 0) ctx.request.body.background = backgroundId;
|
||||
|
||||
return plugin.controllers.user.update(ctx);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user