Skip to content

Commit

Permalink
fix: qa review in user in role
Browse files Browse the repository at this point in the history
  • Loading branch information
dougfabris committed Jul 20, 2021
1 parent f538871 commit 318d7f6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
10 changes: 5 additions & 5 deletions app/api/server/v1/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ API.v1.addRoute('roles.delete', { authRequired: true }, {
API.v1.addRoute('roles.removeUserFromRole', { authRequired: true }, {
post() {
check(this.bodyParams, {
roleName: String,
roleId: String,
username: String,
});

const data = {
roleName: this.bodyParams.roleName,
roleId: this.bodyParams.roleId,
username: this.bodyParams.username,
};

Expand All @@ -236,13 +236,13 @@ API.v1.addRoute('roles.removeUserFromRole', { authRequired: true }, {
throw new Meteor.Error('error-invalid-user', 'There is no user with this username');
}

const role = Roles.findOneByIdOrName(data.roleName);
const role = Roles.findOneByIdOrName(data.roleId);

if (!role) {
throw new Meteor.Error('error-invalid-roleId', 'This role does not exist');
}

if (user.roles.indexOf(role.name) === -1) {
if (user.roles.indexOf(role._id) === -1) {
throw new Meteor.Error('error-user-not-in-role', 'User is not in this role');
}

Expand All @@ -253,7 +253,7 @@ API.v1.addRoute('roles.removeUserFromRole', { authRequired: true }, {
}
}

Roles.removeUserRoles(user._id, role.name, role.scope);
Roles.removeUserRoles(user._id, role._id, role.scope);

if (settings.get('UI_DisplayRoles')) {
api.broadcast('user.roleUpdate', {
Expand Down
14 changes: 13 additions & 1 deletion app/api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import _ from 'underscore';
import Busboy from 'busboy';

import { Users, Subscriptions } from '../../../models/server';
import { Users, Subscriptions, Roles } from '../../../models/server';
import { Users as UsersRaw } from '../../../models/server/raw';
import { hasPermission } from '../../../authorization';
import { settings } from '../../../settings';
Expand Down Expand Up @@ -216,6 +216,18 @@ API.v1.addRoute('users.info', { authRequired: true }, {
}).fetch();
}

if (user.roles) {
const rolesId = user.roles;
user.rolesId = user.roles;

const rolesArr = [];
for (const roleId of rolesId) {
rolesArr.push(...Roles.find({ _id: roleId }, { fields: { name: 1, description: 1 } }).fetch().map(({ name, description }) => description || name));
}

user.roles = rolesArr;
}

return API.v1.success({
user,
});
Expand Down
14 changes: 8 additions & 6 deletions client/views/admin/permissions/UsersInRolePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Page from '../../../components/Page';
import RoomAutoComplete from '../../../components/RoomAutoComplete';
import UserAutoComplete from '../../../components/UserAutoComplete';
import { useRoute } from '../../../contexts/RouterContext';
import { useMethod } from '../../../contexts/ServerContext';
import { useEndpoint } from '../../../contexts/ServerContext';
import { useToastMessageDispatch } from '../../../contexts/ToastMessagesContext';
import { useTranslation } from '../../../contexts/TranslationContext';
import UsersInRoleTableContainer from './UsersInRoleTableContainer';
Expand All @@ -20,11 +20,11 @@ const UsersInRolePage = ({ data }) => {
const [user, setUser] = useState();
const [rid, setRid] = useState();

const { _id, name } = data;
const { _id, name, description } = data;

const router = useRoute('admin-permissions');

const addUser = useMethod('authorization:addUserToRole');
const addUser = useEndpoint('POST', 'roles.addUserToRole');

const handleReturn = useMutableCallback(() => {
router.push({
Expand All @@ -35,7 +35,7 @@ const UsersInRolePage = ({ data }) => {

const handleAdd = useMutableCallback(async () => {
try {
await addUser(name, user, rid);
await addUser({ roleName: _id, username: user, roomId: rid });
dispatchToastMessage({ type: 'success', message: t('User_added') });
setUser();
reload.current();
Expand All @@ -46,7 +46,7 @@ const UsersInRolePage = ({ data }) => {

return (
<Page>
<Page.Header title={`${t('Users_in_role')} "${name}"`}>
<Page.Header title={`${t('Users_in_role')} "${description || name}"`}>
<ButtonGroup>
<Button onClick={handleReturn}>{t('Back')}</Button>
</ButtonGroup>
Expand Down Expand Up @@ -83,7 +83,9 @@ const UsersInRolePage = ({ data }) => {
reloadRef={reload}
scope={data.scope}
rid={rid}
roleName={data.name}
roleId={_id}
roleName={name}
description={description}
/>
)}
{data.scope !== 'Users' && !rid && <Callout type='info'>{t('Select_a_room')}</Callout>}
Expand Down
23 changes: 17 additions & 6 deletions client/views/admin/permissions/UsersInRoleTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,51 @@ import React from 'react';
import GenericModal from '../../../components/GenericModal';
import GenericTable from '../../../components/GenericTable';
import { useSetModal } from '../../../contexts/ModalContext';
import { useMethod } from '../../../contexts/ServerContext';
import { useEndpoint } from '../../../contexts/ServerContext';
import { useToastMessageDispatch } from '../../../contexts/ToastMessagesContext';
import { useTranslation } from '../../../contexts/TranslationContext';
import UserRow from './UserRow';

function UsersInRoleTable({ data, reload, roleName, total, params, setParams, rid }) {
function UsersInRoleTable({
data,
reload,
roleId,
roleName,
description,
total,
params,
setParams,
rid,
}) {
const t = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();

const setModal = useSetModal();

const removeUser = useMethod('authorization:removeUserFromRole');
const removeUser = useEndpoint('POST', 'roles.removeUserFromRole');

const closeModal = () => setModal();

const onRemove = useMutableCallback((username) => {
const remove = async () => {
try {
await removeUser(roleName, username, rid);
await removeUser({ roleId, username, rid });
dispatchToastMessage({ type: 'success', message: t('User_removed') });
} catch (error) {
dispatchToastMessage({ type: 'erroor', message: error });
dispatchToastMessage({ type: 'error', message: error });
}
closeModal();
reload();
};

setModal(
<GenericModal
variant='danger'
onConfirm={remove}
onCancel={closeModal}
confirmText={t('Delete')}
>
{t('The_user_s_will_be_removed_from_role_s', username, roleName)}
{t('The_user_s_will_be_removed_from_role_s', username, description || roleName)}
</GenericModal>,
);
});
Expand Down
8 changes: 5 additions & 3 deletions client/views/admin/permissions/UsersInRoleTableContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import React, { useState, useMemo } from 'react';
import { useEndpointData } from '../../../hooks/useEndpointData';
import UsersInRoleTable from './UsersInRoleTable';

const UsersInRoleTableContainer = ({ rid, roleName, reloadRef }) => {
const UsersInRoleTableContainer = ({ rid, roleId, roleName, description, reloadRef }) => {
const [params, setParams] = useState({ current: 0, itemsPerPage: 25 });

const debouncedParams = useDebouncedValue(params, 500);

const query = useMemo(
() => ({
roomId: rid,
role: roleName,
role: roleId,
...(debouncedParams.itemsPerPage && { count: debouncedParams.itemsPerPage }),
...(debouncedParams.current && { offset: debouncedParams.current }),
}),
[debouncedParams, rid, roleName],
[debouncedParams, rid, roleId],
);

const { value: data = {}, reload } = useEndpointData('roles.getUsersInRole', query);
Expand All @@ -33,6 +33,8 @@ const UsersInRoleTableContainer = ({ rid, roleName, reloadRef }) => {
params={params}
setParams={setParams}
roleName={roleName}
roleId={roleId}
description={description}
rid={rid}
/>
);
Expand Down
4 changes: 2 additions & 2 deletions client/views/admin/users/EditUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useForm } from '../../../hooks/useForm';
import UserForm from './UserForm';

const getInitialValue = (data) => ({
roles: data.roles,
roles: data.rolesId,
name: data.name ?? '',
password: '',
username: data.username,
Expand Down Expand Up @@ -147,7 +147,7 @@ function EditUser({ data, roles, ...props }) {
}
}, [avatarObj, data._id, goToUser, saveAction, updateAvatar, values, errors, validationKeys]);

const availableRoles = roles.map(({ _id, description }) => [_id, description || _id]);
const availableRoles = roles.map(({ _id, name, description }) => [_id, description || name]);

const canSaveOrReset = hasUnsavedChanges || avatarObj;

Expand Down
2 changes: 2 additions & 0 deletions client/views/admin/users/UserInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function UserInfoWithData({ uid, username, ...props }) {

const user = useMemo(() => {
const { user } = data || { user: {} };

const {
name,
username,
Expand All @@ -47,6 +48,7 @@ export function UserInfoWithData({ uid, username, ...props }) {
lastLogin,
nickname,
} = user;

return {
name,
username,
Expand Down

0 comments on commit 318d7f6

Please sign in to comment.