-
Notifications
You must be signed in to change notification settings - Fork 11.4k
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
refactor: remove authorization method calls (server) #34986
Open
MarcosSpessatto
wants to merge
4
commits into
develop
Choose a base branch
from
refactor/authorization-methods
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+158
−129
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f973a9c
refactor: remove authorization method calls (server)
MarcosSpessatto 4aba9b3
Merge branch 'develop' into refactor/authorization-methods
MarcosSpessatto aacaa02
Merge branch 'develop' into refactor/authorization-methods
MarcosSpessatto cca6795
Merge branch 'develop' into refactor/authorization-methods
MarcosSpessatto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -16,85 +16,96 @@ declare module '@rocket.chat/ddp-client' { | |
} | ||
} | ||
|
||
Meteor.methods<ServerMethods>({ | ||
async 'authorization:removeUserFromRole'(roleId, username, scope) { | ||
const userId = Meteor.userId(); | ||
export const removeUserFromRole = async (userId: string, roleId: string, username: IUser['username'], scope?: string): Promise<boolean> => { | ||
if (!(await hasPermissionAsync(userId, 'access-permissions'))) { | ||
throw new Meteor.Error('error-action-not-allowed', 'Access permissions is not allowed', { | ||
method: 'authorization:removeUserFromRole', | ||
action: 'Accessing_permissions', | ||
}); | ||
} | ||
|
||
if (!userId || !(await hasPermissionAsync(userId, 'access-permissions'))) { | ||
throw new Meteor.Error('error-action-not-allowed', 'Access permissions is not allowed', { | ||
method: 'authorization:removeUserFromRole', | ||
action: 'Accessing_permissions', | ||
}); | ||
} | ||
if (!roleId || typeof roleId.valueOf() !== 'string' || !username || typeof username.valueOf() !== 'string') { | ||
throw new Meteor.Error('error-invalid-arguments', 'Invalid arguments', { | ||
method: 'authorization:removeUserFromRole', | ||
}); | ||
Comment on lines
+28
to
+30
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. throw new Meteor.Error('error-invalid-arguments', `Invalid arguments: roleId and username must be non-empty strings`, { The error message for invalid arguments should be more specific about which argument(s) failed validation to help with debugging and maintainability Talk to Kody by mentioning @kody Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction. |
||
} | ||
|
||
if (!roleId || typeof roleId.valueOf() !== 'string' || !username || typeof username.valueOf() !== 'string') { | ||
throw new Meteor.Error('error-invalid-arguments', 'Invalid arguments', { | ||
let role = await Roles.findOneById<Pick<IRole, '_id'>>(roleId, { projection: { _id: 1 } }); | ||
if (!role) { | ||
role = await Roles.findOneByName<Pick<IRole, '_id'>>(roleId, { projection: { _id: 1 } }); | ||
if (!role) { | ||
throw new Meteor.Error('error-invalid-role', 'Invalid Role', { | ||
method: 'authorization:removeUserFromRole', | ||
}); | ||
} | ||
|
||
let role = await Roles.findOneById<Pick<IRole, '_id'>>(roleId, { projection: { _id: 1 } }); | ||
if (!role) { | ||
role = await Roles.findOneByName<Pick<IRole, '_id'>>(roleId, { projection: { _id: 1 } }); | ||
if (!role) { | ||
throw new Meteor.Error('error-invalid-role', 'Invalid Role', { | ||
method: 'authorization:removeUserFromRole', | ||
}); | ||
} | ||
methodDeprecationLogger.deprecatedParameterUsage( | ||
'authorization:removeUserFromRole', | ||
'role', | ||
'7.0.0', | ||
({ parameter, method, version }) => `Calling ${method} with ${parameter} names is deprecated and will be removed ${version}`, | ||
); | ||
} | ||
|
||
methodDeprecationLogger.deprecatedParameterUsage( | ||
'authorization:removeUserFromRole', | ||
'role', | ||
'7.0.0', | ||
({ parameter, method, version }) => `Calling ${method} with ${parameter} names is deprecated and will be removed ${version}`, | ||
); | ||
} | ||
const user = await Users.findOneByUsernameIgnoringCase(username, { | ||
projection: { | ||
_id: 1, | ||
roles: 1, | ||
}, | ||
}); | ||
|
||
const user = await Users.findOneByUsernameIgnoringCase(username, { | ||
projection: { | ||
_id: 1, | ||
roles: 1, | ||
if (!user?._id) { | ||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { | ||
method: 'authorization:removeUserFromRole', | ||
}); | ||
} | ||
|
||
// prevent removing last user from admin role | ||
if (role._id === 'admin') { | ||
const adminCount = await Users.col.countDocuments({ | ||
roles: { | ||
$in: ['admin'], | ||
}, | ||
}); | ||
|
||
if (!user?._id) { | ||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { | ||
method: 'authorization:removeUserFromRole', | ||
const userIsAdmin = user.roles?.indexOf('admin') > -1; | ||
if (adminCount === 1 && userIsAdmin) { | ||
throw new Meteor.Error('error-action-not-allowed', 'Leaving the app without admins is not allowed', { | ||
method: 'removeUserFromRole', | ||
action: 'Remove_last_admin', | ||
}); | ||
} | ||
} | ||
|
||
// prevent removing last user from admin role | ||
if (role._id === 'admin') { | ||
const adminCount = await Users.col.countDocuments({ | ||
roles: { | ||
$in: ['admin'], | ||
}, | ||
}); | ||
const remove = await removeUserFromRolesAsync(user._id, [role._id], scope); | ||
const event = { | ||
type: 'removed', | ||
_id: role._id, | ||
u: { | ||
_id: user._id, | ||
username, | ||
}, | ||
scope, | ||
} as const; | ||
if (settings.get('UI_DisplayRoles')) { | ||
void api.broadcast('user.roleUpdate', event); | ||
} | ||
void api.broadcast('federation.userRoleChanged', { ...event, givenByUserId: userId }); | ||
|
||
const userIsAdmin = user.roles?.indexOf('admin') > -1; | ||
if (adminCount === 1 && userIsAdmin) { | ||
throw new Meteor.Error('error-action-not-allowed', 'Leaving the app without admins is not allowed', { | ||
method: 'removeUserFromRole', | ||
action: 'Remove_last_admin', | ||
}); | ||
} | ||
} | ||
return remove; | ||
}; | ||
|
||
const remove = await removeUserFromRolesAsync(user._id, [role._id], scope); | ||
const event = { | ||
type: 'removed', | ||
_id: role._id, | ||
u: { | ||
_id: user._id, | ||
username, | ||
}, | ||
scope, | ||
} as const; | ||
if (settings.get('UI_DisplayRoles')) { | ||
void api.broadcast('user.roleUpdate', event); | ||
Meteor.methods<ServerMethods>({ | ||
async 'authorization:removeUserFromRole'(roleId, username, scope) { | ||
const userId = Meteor.userId(); | ||
|
||
if (!userId) { | ||
throw new Meteor.Error('error-action-not-allowed', 'Access permissions is not allowed', { | ||
method: 'authorization:removeUserFromRole', | ||
action: 'Accessing_permissions', | ||
}); | ||
} | ||
void api.broadcast('federation.userRoleChanged', { ...event, givenByUserId: userId }); | ||
|
||
return remove; | ||
return removeUserFromRole(userId, roleId, username, scope); | ||
}, | ||
}); |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Multiple functions lack proper error handling, which can lead to unhandled exceptions during role assignments and modifications.
This issue appears in multiple locations:
Please add try-catch blocks around all relevant function calls to handle potential errors gracefully.
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.