Merge branch 'feat/admin-rights' into 'main'

Set all app roles to admin when admin dashboard role is selected

Closes #62

See merge request stackspin/dashboard!42
This commit is contained in:
Varac 2022-06-28 14:30:57 +00:00
commit ff82530fb6
4 changed files with 34 additions and 8 deletions

View file

@ -2,7 +2,7 @@ import React from 'react';
import { useController } from 'react-hook-form'; import { useController } from 'react-hook-form';
/* eslint-disable react/react-in-jsx-scope */ /* eslint-disable react/react-in-jsx-scope */
export const Select = ({ control, name, label, options }: SelectProps) => { export const Select = ({ control, name, label, options, disabled = false }: SelectProps) => {
const { const {
field, field,
// fieldState: { invalid, isTouched, isDirty }, // fieldState: { invalid, isTouched, isDirty },
@ -27,6 +27,7 @@ export const Select = ({ control, name, label, options }: SelectProps) => {
name={name} // send down the input name name={name} // send down the input name
ref={field.ref} // send input ref, so we can focus on input when error appear ref={field.ref} // send input ref, so we can focus on input when error appear
className="shadow-sm focus:ring-primary-500 focus:border-primary-500 block w-full sm:text-sm border-gray-300 rounded-md" className="shadow-sm focus:ring-primary-500 focus:border-primary-500 block w-full sm:text-sm border-gray-300 rounded-md"
disabled={disabled}
> >
{options?.map((option) => ( {options?.map((option) => (
<option key={option.value} value={option.value}> <option key={option.value} value={option.value}>
@ -43,4 +44,5 @@ type SelectProps = {
name: string; name: string;
label?: string; label?: string;
options?: any[]; options?: any[];
disabled?: boolean;
}; };

View file

@ -173,7 +173,9 @@ const Header: React.FC<HeaderProps> = () => {
)} )}
</Disclosure> </Disclosure>
{currentUserModal && (
<UserModal open={currentUserModal} onClose={currentUserModalClose} userId={currentUserId} setUserId={_.noop} /> <UserModal open={currentUserModal} onClose={currentUserModalClose} userId={currentUserId} setUserId={_.noop} />
)}
</> </>
); );
}; };

View file

@ -1,8 +1,8 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import _ from 'lodash'; import _ from 'lodash';
import { TrashIcon } from '@heroicons/react/outline'; import { TrashIcon } from '@heroicons/react/outline';
import { useFieldArray, useForm } from 'react-hook-form'; import { useFieldArray, useForm, useWatch } from 'react-hook-form';
import { Modal, ConfirmationModal } from 'src/components'; import { Banner, Modal, ConfirmationModal } from 'src/components';
import { Input, Select } from 'src/components/Form'; import { Input, Select } from 'src/components/Form';
import { User, UserRole, useUsers } from 'src/services/users'; import { User, UserRole, useUsers } from 'src/services/users';
import { useAuth } from 'src/services/auth'; import { useAuth } from 'src/services/auth';
@ -19,7 +19,7 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
defaultValues: initialUserForm, defaultValues: initialUserForm,
}); });
const { fields } = useFieldArray({ const { fields, update } = useFieldArray({
control, control,
name: 'app_roles', name: 'app_roles',
}); });
@ -28,8 +28,6 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
if (userId) { if (userId) {
loadUser(userId); loadUser(userId);
} }
reset(initialUserForm);
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [userId, open]); }, [userId, open]);
@ -43,6 +41,18 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
}; };
}, [user, reset, open]); }, [user, reset, open]);
const dashboardRole = useWatch({
control,
name: 'app_roles.0.role',
});
useEffect(() => {
if (dashboardRole === UserRole.Admin) {
fields.forEach((field, index) => update(index, { name: field.name, role: UserRole.Admin }));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dashboardRole]);
const handleSave = async () => { const handleSave = async () => {
try { try {
if (userId) { if (userId) {
@ -174,6 +184,15 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
<h3 className="text-lg leading-6 font-medium text-gray-900">App Access</h3> <h3 className="text-lg leading-6 font-medium text-gray-900">App Access</h3>
</div> </div>
{dashboardRole === UserRole.Admin && (
<div className="sm:col-span-6">
<Banner
title="Admin users automatically have admin-level access to all apps."
titleSm="Admin user"
/>
</div>
)}
<div> <div>
<div className="flow-root mt-6"> <div className="flow-root mt-6">
<ul className="-my-5 divide-y divide-gray-200 "> <ul className="-my-5 divide-y divide-gray-200 ">
@ -200,6 +219,7 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
key={item.id} key={item.id}
control={control} control={control}
name={`app_roles.${index}.role`} name={`app_roles.${index}.role`}
disabled={dashboardRole === UserRole.Admin}
options={[ options={[
{ value: UserRole.NoAccess, name: 'No Access' }, { value: UserRole.NoAccess, name: 'No Access' },
{ value: UserRole.User, name: 'User' }, { value: UserRole.User, name: 'User' },

View file

@ -163,7 +163,9 @@ export const Users: React.FC = () => {
</div> </div>
</div> </div>
{configureModal && (
<UserModal open={configureModal} onClose={configureModalClose} userId={userId} setUserId={setUserId} /> <UserModal open={configureModal} onClose={configureModalClose} userId={userId} setUserId={setUserId} />
)}
</div> </div>
</div> </div>
); );