-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add admin user profile with avatar (#75)
- Loading branch information
1 parent
087495b
commit a74c2eb
Showing
16 changed files
with
251 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import secretSchema from "./secret"; | ||
import auditSchema from "./audit"; | ||
import versionSchema from "./version"; | ||
import profileSchema from "./profile"; | ||
|
||
export default { | ||
"curator-secret": { schema: secretSchema }, | ||
"curator-audit-log": { schema: auditSchema }, | ||
"curator-version": { schema: versionSchema }, | ||
"curator-admin-user-profile": { schema: profileSchema }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export default { | ||
kind: "collectionType", | ||
collectionName: "curator_admin_user_profile", | ||
info: { | ||
singularName: "curator-admin-user-profile", | ||
pluralName: "curator-admin-user-profiles", | ||
displayName: "Curator Admin User Profile", | ||
description: "A profile for admin users.", | ||
}, | ||
options: { | ||
draftAndPublish: false, | ||
}, | ||
pluginOptions: { | ||
"content-type-builder": { | ||
visible: false, | ||
}, | ||
}, | ||
attributes: { | ||
user: { | ||
type: "relation", | ||
relation: "oneToOne", | ||
target: "admin::user", | ||
}, | ||
avatar: { | ||
type: "media", | ||
multiple: false, | ||
allowedTypes: ["images"], | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import dashboardController from "./dashboard.controller"; | ||
import secretsController from "./secrets.controller"; | ||
import versioningController from "./versioning.controller"; | ||
import profileController from "./profile.controller"; | ||
|
||
export default { | ||
secretsController, | ||
dashboardController, | ||
versioningController, | ||
profileController, | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/content-platform/server/controllers/profile.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Strapi } from "@strapi/strapi"; | ||
|
||
export default ({ strapi }: { strapi: Strapi }) => ({ | ||
getMe() { | ||
return strapi.plugin("curator").service("profileService").getMe(); | ||
}, | ||
updateMe() { | ||
return strapi.plugin("curator").service("profileService").updateMe(); | ||
}, | ||
}); |
48 changes: 48 additions & 0 deletions
48
packages/content-platform/server/lifecycles/profile.lifecycle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { Strapi } from "@strapi/strapi"; | ||
import * as R from "ramda"; | ||
|
||
const ADMIN_USER_UID = "admin::user"; | ||
const PROFILE_UID = "plugin::curator.curator-admin-user-profile"; | ||
|
||
export default async function profileLifecycle(strapi: Strapi) { | ||
/* | ||
* Make sure admin users always have a profile | ||
*/ | ||
const admins = (await strapi.entityService.findMany(ADMIN_USER_UID)) as any[]; | ||
|
||
const existingProfiles = (await strapi.entityService.findMany(PROFILE_UID, { | ||
filters: { | ||
user: R.pluck("id", admins), | ||
}, | ||
populate: ["user"], | ||
})) as any[]; | ||
|
||
const adminsWithoutProfile = R.without( | ||
existingProfiles.map(R.path(["user", "id"])), | ||
R.pluck("id", admins), | ||
); | ||
|
||
for await (const adminId of adminsWithoutProfile) { | ||
await strapi.entityService.create(PROFILE_UID, { data: { user: adminId } }); | ||
} | ||
|
||
strapi.db.lifecycles.subscribe({ | ||
models: [ADMIN_USER_UID], | ||
|
||
async afterCreate({ result }: any) { | ||
await strapi.entityService.create(PROFILE_UID, { | ||
data: { user: result.id }, | ||
}); | ||
}, | ||
|
||
async afterDelete({ result }: any) { | ||
const items = await strapi.entityService.findMany(PROFILE_UID, { | ||
filters: { user: result.id }, | ||
}); | ||
|
||
if (Array.isArray(items) && items[0]) { | ||
await strapi.entityService.delete(PROFILE_UID, items[0].id); | ||
} | ||
}, | ||
} as any); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import secretsService from "./secrets.service"; | ||
import dashboardService from "./dashboard.service"; | ||
import versioningService from "./versioning.service"; | ||
import profileService from "./profile.service"; | ||
|
||
export default { | ||
secretsService, | ||
dashboardService, | ||
versioningService, | ||
profileService, | ||
}; |
37 changes: 37 additions & 0 deletions
37
packages/content-platform/server/services/profile.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Strapi } from "@strapi/strapi"; | ||
|
||
const PROFILE_UID = "plugin::curator.curator-admin-user-profile"; | ||
|
||
export default ({ strapi }: { strapi: Strapi }) => ({ | ||
async getMe() { | ||
const ctx = strapi.requestContext.get(); | ||
const user = ctx.state.user; | ||
|
||
const query = await strapi.entityService.findMany(PROFILE_UID, { | ||
filters: { | ||
user: user.id, | ||
}, | ||
populate: ["avatar"], | ||
}); | ||
|
||
return Array.isArray(query) ? query[0] : {}; | ||
}, | ||
|
||
async updateMe() { | ||
const ctx = strapi.requestContext.get(); | ||
const user = ctx.state.user; | ||
|
||
const query = await strapi.entityService.findMany(PROFILE_UID, { | ||
filters: { | ||
user: user.id, | ||
}, | ||
}); | ||
|
||
if (Array.isArray(query) && query[0]) { | ||
return await strapi.entityService.update(PROFILE_UID, query[0].id, { | ||
data: ctx.request.body, | ||
populate: ["avatar"], | ||
}); | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.