mirror of
https://github.com/RocketChat/Rocket.Chat.git
synced 2025-12-27 22:40:49 +00:00
chore!: remove deprecated setAdminStatus (#37388)
This commit is contained in:
parent
f66d95dad4
commit
e00a6b08f6
5
.changeset/gentle-dingos-retire.md
Normal file
5
.changeset/gentle-dingos-retire.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'@rocket.chat/meteor': major
|
||||
---
|
||||
|
||||
Removes deprecated `setAdminStatus` method
|
||||
@ -36,7 +36,6 @@ import './methods/saveSetting';
|
||||
import './methods/saveSettings';
|
||||
import './methods/sendMessage';
|
||||
import './methods/sendSMTPTestEmail';
|
||||
import './methods/setAdminStatus';
|
||||
import './methods/setEmail';
|
||||
import './methods/setRealName';
|
||||
import './methods/unarchiveRoom';
|
||||
|
||||
@ -1,48 +0,0 @@
|
||||
import { isUserFederated } from '@rocket.chat/core-typings';
|
||||
import type { ServerMethods } from '@rocket.chat/ddp-client';
|
||||
import { Users } from '@rocket.chat/models';
|
||||
import { Match, check } from 'meteor/check';
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
|
||||
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
|
||||
import { addUserToRole } from '../../../authorization/server/methods/addUserToRole';
|
||||
import { removeUserFromRole } from '../../../authorization/server/methods/removeUserFromRole';
|
||||
import { methodDeprecationLogger } from '../lib/deprecationWarningLogger';
|
||||
|
||||
declare module '@rocket.chat/ddp-client' {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
interface ServerMethods {
|
||||
setAdminStatus(userId: string, admin?: boolean): void;
|
||||
}
|
||||
}
|
||||
|
||||
Meteor.methods<ServerMethods>({
|
||||
async setAdminStatus(userId, admin) {
|
||||
methodDeprecationLogger.method('setAdminStatus', '8.0.0', 'Use `/v1/roles.addUserToRole` or `/v1/roles.removeUserFromRole`.');
|
||||
|
||||
check(userId, String);
|
||||
check(admin, Match.Optional(Boolean));
|
||||
|
||||
const uid = Meteor.userId();
|
||||
|
||||
if (!uid) {
|
||||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setAdminStatus' });
|
||||
}
|
||||
|
||||
if ((await hasPermissionAsync(uid, 'assign-admin-role')) !== true) {
|
||||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setAdminStatus' });
|
||||
}
|
||||
|
||||
const user = await Users.findOne({ _id: userId }, { projection: { username: 1, federated: 1 } });
|
||||
if (!user || isUserFederated(user)) {
|
||||
throw new Meteor.Error('error-not-allowed', 'Federated Users cant be admins', { method: 'setAdminStatus' });
|
||||
}
|
||||
|
||||
if (admin) {
|
||||
await addUserToRole(uid, 'admin', user?.username);
|
||||
return;
|
||||
}
|
||||
|
||||
await removeUserFromRole(uid, 'admin', user?.username);
|
||||
},
|
||||
});
|
||||
@ -88,7 +88,7 @@ const UsersTableRow = ({
|
||||
const isActive = user.active;
|
||||
const isFederatedUser = !!user.federated;
|
||||
|
||||
const changeAdminStatusAction = useChangeAdminStatusAction(userId, isAdmin, onReload);
|
||||
const changeAdminStatusAction = useChangeAdminStatusAction(username, isAdmin, onReload);
|
||||
const changeUserStatusAction = useChangeUserStatusAction(userId, isActive, onReload);
|
||||
const deleteUserAction = useDeleteUserAction(userId, onReload, onReload);
|
||||
const resetTOTPAction = useResetTOTPAction(userId);
|
||||
|
||||
@ -89,7 +89,7 @@ export const useAdminUserInfoActions = ({
|
||||
const canDirectMessage = usePermission('create-d');
|
||||
const canEditOtherUserInfo = usePermission('edit-other-user-info');
|
||||
|
||||
const changeAdminStatusAction = useChangeAdminStatusAction(userId, isAdmin, onChange);
|
||||
const changeAdminStatusAction = useChangeAdminStatusAction(username, isAdmin, onChange);
|
||||
const changeUserStatusAction = useChangeUserStatusAction(userId, isActive, onChange);
|
||||
const deleteUserAction = useDeleteUserAction(userId, onChange, onReload);
|
||||
const resetTOTPAction = useResetTOTPAction(userId);
|
||||
|
||||
@ -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: 'success', 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]);
|
||||
|
||||
return canAssignAdminRole
|
||||
? {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user