@ -9,6 +9,7 @@ import LoadingSpinner from '../../Common/LoadingSpinner';
import PageTitle from '../../Common/PageTitle' ;
import ProfileHeader from '../ProfileHeader' ;
import useSettings from '../../../hooks/useSettings' ;
import Alert from '../../Common/Alert' ;
const messages = defineMessages ( {
settings : 'User Settings' ,
@ -16,6 +17,9 @@ const messages = defineMessages({
menuChangePass : 'Password' ,
menuNotifications : 'Notifications' ,
menuPermissions : 'Permissions' ,
unauthorized : 'Unauthorized' ,
unauthorizedDescription :
"You do not have permission to modify this user's settings." ,
} ) ;
interface SettingsRoute {
@ -24,6 +28,7 @@ interface SettingsRoute {
regex : RegExp ;
requiredPermission? : Permission | Permission [ ] ;
permissionType ? : { type : 'and' | 'or' } ;
hidden? : boolean ;
}
const UserSettings : React.FC = ( { children } ) = > {
@ -51,6 +56,15 @@ const UserSettings: React.FC = ({ children }) => {
text : intl.formatMessage ( messages . menuChangePass ) ,
route : '/settings/password' ,
regex : /\/settings\/password/ ,
hidden :
( ! settings . currentSettings . localLogin &&
! hasPermission (
Permission . MANAGE_SETTINGS ,
currentUser ? . permissions ? ? 0
) ) ||
( currentUser ? . id !== 1 &&
currentUser ? . id !== user ? . id &&
hasPermission ( Permission . ADMIN , user ? . permissions ? ? 0 ) ) ,
} ,
{
text : intl.formatMessage ( messages . menuNotifications ) ,
@ -62,6 +76,7 @@ const UserSettings: React.FC = ({ children }) => {
route : '/settings/permissions' ,
regex : /\/settings\/permissions/ ,
requiredPermission : Permission.MANAGE_USERS ,
hidden : currentUser?.id !== 1 && currentUser ? . id === user . id ,
} ,
] ;
@ -76,20 +91,6 @@ const UserSettings: React.FC = ({ children }) => {
regex : RegExp ;
isMobile? : boolean ;
} > = ( { children , route , regex , isMobile = false } ) = > {
if (
route === '/settings/password' &&
( ( ! settings . currentSettings . localLogin &&
! hasPermission (
Permission . MANAGE_SETTINGS ,
currentUser ? . permissions ? ? 0
) ) ||
( currentUser ? . id !== 1 &&
currentUser ? . id !== user ? . id &&
hasPermission ( Permission . ADMIN , user ? . permissions ? ? 0 ) ) )
) {
return null ;
}
const finalRoute = router . asPath . includes ( '/profile' )
? ` /profile ${ route } `
: ` /users/ ${ user . id } ${ route } ` ;
@ -111,6 +112,20 @@ const UserSettings: React.FC = ({ children }) => {
) ;
} ;
if ( currentUser ? . id !== 1 && user . id === 1 ) {
return (
< >
< PageTitle title = { intl . formatMessage ( messages . settings ) } / >
< ProfileHeader user = { user } isSettingsPage / >
< div className = "mt-6" >
< Alert title = { intl . formatMessage ( messages . unauthorized ) } type = "error" >
{ intl . formatMessage ( messages . unauthorizedDescription ) }
< / Alert >
< / div >
< / >
) ;
}
const currentRoute = settingsRoutes . find (
( route ) = > ! ! router . pathname . match ( route . regex )
) ? . route ;
@ -136,14 +151,16 @@ const UserSettings: React.FC = ({ children }) => {
aria - label = "Selected tab"
>
{ settingsRoutes
. filter ( ( route ) = >
route . requiredPermission
? hasPermission (
route . requiredPermission ,
currentUser ? . permissions ? ? 0 ,
route . permissionType
)
: true
. filter (
( route ) = >
! route . hidden &&
( route . requiredPermission
? hasPermission (
route . requiredPermission ,
currentUser ? . permissions ? ? 0 ,
route . permissionType
)
: true )
)
. map ( ( route , index ) = > (
< SettingsLink
@ -161,14 +178,16 @@ const UserSettings: React.FC = ({ children }) => {
< div className = "border-b border-gray-600" >
< nav className = "flex -mb-px" >
{ settingsRoutes
. filter ( ( route ) = >
route . requiredPermission
? hasPermission (
route . requiredPermission ,
currentUser ? . permissions ? ? 0 ,
route . permissionType
)
: true
. filter (
( route ) = >
! route . hidden &&
( route . requiredPermission
? hasPermission (
route . requiredPermission ,
currentUser ? . permissions ? ? 0 ,
route . permissionType
)
: true )
)
. map ( ( route , index ) = > (
< SettingsLink