-
Notifications
You must be signed in to change notification settings - Fork 13k
chore!: remove deprecated setAdminStatus
#37388
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': major | ||
| --- | ||
|
|
||
| Removes deprecated `setAdminStatus` method |
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,25 +1,37 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { IUser } from '@rocket.chat/core-typings'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useToastMessageDispatch, useMethod, useTranslation, usePermission } from '@rocket.chat/ui-contexts'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useToastMessageDispatch, usePermission, useEndpoint } from '@rocket.chat/ui-contexts'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useCallback } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useTranslation } from 'react-i18next'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { AdminUserAction } from './useAdminUserInfoActions'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const useChangeAdminStatusAction = (userId: IUser['_id'], isAdmin: boolean, onChange: () => void): AdminUserAction | undefined => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const t = useTranslation(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const useChangeAdminStatusAction = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| username: IUser['username'] = '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isAdmin: boolean, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChange: () => void, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): AdminUserAction | undefined => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { t } = useTranslation(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dispatchToastMessage = useToastMessageDispatch(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const setAdminStatus = useMethod('setAdminStatus'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const addUserToRoleEndpoint = useEndpoint('POST', '/v1/roles.addUserToRole'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const removeUserFromRoleEndpoint = useEndpoint('POST', '/v1/roles.removeUserFromRole'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const canAssignAdminRole = usePermission('assign-admin-role'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const changeAdminStatus = useCallback(async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await setAdminStatus(userId, !isAdmin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const message = isAdmin ? 'User_is_no_longer_an_admin' : 'User_is_now_an_admin'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dispatchToastMessage({ type: 'success', message: t(message) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isAdmin) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await removeUserFromRoleEndpoint({ roleId: 'admin', username }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dispatchToastMessage({ type: 'info', message: t('User_is_no_longer_an_admin') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChange(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await addUserToRoleEndpoint({ roleId: 'admin', username }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dispatchToastMessage({ type: 'success', message: t('User_is_now_an_admin') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChange(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dispatchToastMessage({ type: 'error', message: error }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [userId, dispatchToastMessage, isAdmin, onChange, setAdminStatus, t]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [isAdmin, removeUserFromRoleEndpoint, username, dispatchToastMessage, addUserToRoleEndpoint, t, onChange]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
13
to
+34
Contributor
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. Fix error message handling in catch block. At line 32, the error object is passed directly to Apply this diff to properly extract the error message: } catch (error) {
- dispatchToastMessage({ type: 'error', message: error });
+ dispatchToastMessage({ type: 'error', message: error instanceof Error ? error.message : String(error) });
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return canAssignAdminRole | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Add validation for username parameter.
The
usernameparameter is typed asIUser['username'], which isstring | undefinedbased on the IUser type definition. While it defaults to an empty string, the function should validate that username is non-empty before attempting to call the role endpoints.Apply this diff to add validation:
export const useChangeAdminStatusAction = ( username: IUser['username'] = '', isAdmin: boolean, onChange: () => void, ): AdminUserAction | undefined => { const { t } = useTranslation(); const dispatchToastMessage = useToastMessageDispatch(); const addUserToRoleEndpoint = useEndpoint('POST', '/v1/roles.addUserToRole'); const removeUserFromRoleEndpoint = useEndpoint('POST', '/v1/roles.removeUserFromRole'); const canAssignAdminRole = usePermission('assign-admin-role'); const changeAdminStatus = useCallback(async () => { + if (!username) { + dispatchToastMessage({ type: 'error', message: t('error-invalid-user') }); + return; + } + try { if (isAdmin) {🤖 Prompt for AI Agents