-
Notifications
You must be signed in to change notification settings - Fork 4
chore: role base implementation #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
4dd7b85
b2a1d4f
2e96770
4a216b7
22ae5dc
5bb7617
ac4e7bc
c335d42
e735d67
660d31c
0f2aa6c
6626154
c230769
bee6e59
323eec5
e54574c
9e9bf26
d1d8585
6340390
fbb5a87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,7 @@ import { | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import { USER_MOVEMENT_STATUSES } from 'erxes-api-shared/core-modules'; | ||||||||||||||||||||||||||||||||||||||||||
| import { title } from 'process'; | ||||||||||||||||||||||||||||||||||||||||||
| import { PERMISSION_ROLES } from '~/modules/permissions/db/constants'; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const SALT_WORK_FACTOR = 10; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -234,7 +235,7 @@ export const loadUserClass = (models: IModels) => { | |||||||||||||||||||||||||||||||||||||||||
| this.checkPassword(password); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return models.Users.create({ | ||||||||||||||||||||||||||||||||||||||||||
| const user = await models.Users.create({ | ||||||||||||||||||||||||||||||||||||||||||
| isOwner, | ||||||||||||||||||||||||||||||||||||||||||
| username, | ||||||||||||||||||||||||||||||||||||||||||
| email, | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -246,6 +247,13 @@ export const loadUserClass = (models: IModels) => { | |||||||||||||||||||||||||||||||||||||||||
| password: notUsePassword ? '' : await this.generatePassword(password), | ||||||||||||||||||||||||||||||||||||||||||
| code: await this.generateUserCode(), | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| models.Roles.create({ | ||||||||||||||||||||||||||||||||||||||||||
| userId: user._id, | ||||||||||||||||||||||||||||||||||||||||||
| role: PERMISSION_ROLES.MEMBER, | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+251
to
+254
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Missing
Suggested change
Comment on lines
+251
to
+254
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assign OWNER role when
- models.Roles.create({
- userId: user._id,
- role: PERMISSION_ROLES.MEMBER,
- });
+ const role = isOwner
+ ? PERMISSION_ROLES.OWNER
+ : PERMISSION_ROLES.MEMBER;
+
+ models.Roles.create({
+ userId: user._id,
+ role,
+ });📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return user; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -329,7 +337,7 @@ export const loadUserClass = (models: IModels) => { | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| this.checkPassword(password); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| await models.Users.create({ | ||||||||||||||||||||||||||||||||||||||||||
| const user = await models.Users.create({ | ||||||||||||||||||||||||||||||||||||||||||
| email, | ||||||||||||||||||||||||||||||||||||||||||
| groupIds: [groupId], | ||||||||||||||||||||||||||||||||||||||||||
| isActive: true, | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -341,6 +349,11 @@ export const loadUserClass = (models: IModels) => { | |||||||||||||||||||||||||||||||||||||||||
| brandIds, | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| models.Roles.create({ | ||||||||||||||||||||||||||||||||||||||||||
| userId: user._id, | ||||||||||||||||||||||||||||||||||||||||||
| role: PERMISSION_ROLES.MEMBER, | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+352
to
+355
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Missing
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return token; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,11 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { IContext } from '~/connectionResolvers'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| IUser, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| IDetail, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ILink, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| IEmailSignature, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ILink, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| IUser, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } from 'erxes-api-shared/core-types'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { IContext } from '~/connectionResolvers'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { PERMISSION_ROLES } from '~/modules/permissions/db/constants'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface IUsersEdit extends IUser { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| channelIds?: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -48,7 +49,12 @@ export const userMutations = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| await models.Users.createUser(doc); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const user = await models.Users.createUser(doc); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| models.Roles.create({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, consider awaiting the call to models.Roles.create in the usersCreateOwner mutation for consistent error handling.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| userId: user._id, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| role: PERMISSION_ROLES.OWNER, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Role creation is not awaited after user creation. Not awaiting models.Roles.create may lead to permission issues if later code assumes the role exists. Please await this call. |
||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+55
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Missing
Suggested change
Comment on lines
+53
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent owner role downgrade and await persistence
- models.Roles.create({
- userId: user._id,
- role: PERMISSION_ROLES.OWNER,
- });
+ await models.Roles.findOneAndUpdate(
+ { userId: user._id },
+ { $set: { role: PERMISSION_ROLES.OWNER }, $setOnInsert: { userId: user._id } },
+ { upsert: true },
+ );📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if (subscribeEmail && process.env.NODE_ENV === 'production') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| await fetch('https://erxes.io/subscribe', { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -138,14 +144,14 @@ export const userMutations = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| details, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| links, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| employeeId, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| positionIds | ||||||||||||||||||||||||||||||||||||||||||||||||||
| positionIds, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| username: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| email: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| details: IDetail; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| links: ILink; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| employeeId: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| positionIds: string[] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| positionIds: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { user, models }: IContext, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -158,7 +164,7 @@ export const userMutations = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| links, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| employeeId, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| positionIds | ||||||||||||||||||||||||||||||||||||||||||||||||||
| positionIds, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| const updatedUser = await models.Users.editProfile(user._id, doc); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export const PERMISSION_ROLES = { | ||
| OWNER: 'owner', | ||
| ADMIN: 'admin', | ||
| MEMBER: 'member', | ||
| ALL: ['owner', 'admin', 'member'], | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Schema } from 'mongoose'; | ||
| import { PERMISSION_ROLES } from '~/modules/permissions/db/constants'; | ||
|
|
||
| export const roleSchema = new Schema( | ||
| { | ||
| userId: { type: String, label: 'User' }, | ||
| role: { | ||
| type: String, | ||
| enum: PERMISSION_ROLES.ALL, | ||
| label: 'Role', | ||
| }, | ||
| }, | ||
|
||
| { | ||
| timestamps: true, | ||
| }, | ||
| ); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,101 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IRole, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IRoleDocument, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IUserDocument, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from 'erxes-api-shared/core-types'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Model } from 'mongoose'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { IModels } from '~/connectionResolvers'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { PERMISSION_ROLES } from '~/modules/permissions/db/constants'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { roleSchema } from '../definitions/roles'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface IRoleModel extends Model<IRoleDocument> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getRole(_id: string): Promise<IRoleDocument>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| createRole(doc: IRole, user: IUserDocument): Promise<IRoleDocument>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| updateRole(doc: IRole, user: IUserDocument): Promise<IRoleDocument>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const loadRoleClass = (models: IModels) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Role { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static async validateRole(doc: IRole, user: IUserDocument) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { userId, role } = doc || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!PERMISSION_ROLES.ALL.includes(role)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error('Invalid role'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const userRole = await models.Roles.findOne({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userId: user._id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }).lean(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!userRole) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question (bug_risk): Throws error if user has no role, which may block legitimate role assignment. Consider allowing role assignment for users without an existing role, or clarify if this behavior is intentional. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error('Role not found for user.'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (userRole.role === PERMISSION_ROLES.OWNER) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (userRole.role === PERMISSION_ROLES.ADMIN) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isOwner = await models.Roles.findOne({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| role: PERMISSION_ROLES.OWNER, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }).lean(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isOwner) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error('Access denied'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isMember = await models.Roles.findOne({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| role: PERMISSION_ROLES.MEMBER, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }).lean(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isMember && role === PERMISSION_ROLES.OWNER) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error('Access denied'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Admins can still elevate users to owners
- if (userRole.role === PERMISSION_ROLES.ADMIN) {
+ if (userRole.role === PERMISSION_ROLES.ADMIN) {
+ if (role === PERMISSION_ROLES.OWNER) {
+ throw new Error('Access denied');
+ }
const isOwner = await models.Roles.findOne({
userId,
role: PERMISSION_ROLES.OWNER,
}).lean();
if (isOwner) {
throw new Error('Access denied');
}
const isMember = await models.Roles.findOne({
userId,
role: PERMISSION_ROLES.MEMBER,
}).lean();
if (isMember && role === PERMISSION_ROLES.OWNER) {
throw new Error('Access denied');
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (userRole.role === PERMISSION_ROLES.MEMBER) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error('Access denied'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static async getRole(_id: string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const role = await models.Roles.findOne({ _id }).lean(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!role) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error('Role not found'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return role; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static async createRole(doc: IRole, user: IUserDocument) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.validateRole(doc, user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { userId } = doc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const role = await models.Roles.findOne({ userId }).lean(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (role) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error('Role already exists'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return await models.Roles.create(doc); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static async updateRole(doc: IRole, user: IUserDocument) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.validateRole(doc, user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { userId } = doc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return await models.Roles.findOneAndUpdate({ userId }, doc, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roleSchema.loadClass(Role); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return roleSchema; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import Role from './role'; | ||
|
|
||
| export default { | ||
| Role, | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||
| import { IPermissionDocument } from 'erxes-api-shared/core-types'; | ||||||
|
||||||
| import { IPermissionDocument } from 'erxes-api-shared/core-types'; | |
| import { IRoleDocument } from 'erxes-api-shared/core-types'; |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Calling wrong model method - should be models.Roles.getRole(_id) not models.Permissions.getPermission(_id)
| return await models.Permissions.getPermission(_id); | |
| return await models.Roles.getRole(_id); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax: Parameter type should be IRoleDocument not IPermissionDocument
| user: async (permission: IPermissionDocument) => { | |
| user: async (role: IRoleDocument) => { |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax: Variable name should match the parameter name
| if (!permission.userId) { | |
| if (!role.userId) { |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax: Variable name should match the parameter name
| return { __typename: 'User', _id: permission.userId }; | |
| return { __typename: 'User', _id: role.userId }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider awaiting the asynchronous call to models.Roles.create when assigning a role, so that any errors in role creation are caught.