-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
4,397 additions
and
3,590 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import { IconChevronDown24, IconChevronUp24, NoticeBox } from '@dhis2/ui' | ||
import PropTypes from 'prop-types' | ||
import React, { useState } from 'react' | ||
import { useSystemInformation } from '../../providers/index.js' | ||
import styles from './AssignmentRestrictionsWarning.module.css' | ||
|
||
export const AssignmentRestrictionWarning = ({ | ||
currentUser, | ||
roleId, | ||
roleAuthorities, | ||
}) => { | ||
const [detailsExpanded, setDetailsExpanded] = useState(false) | ||
|
||
// check if user is able to assign this role if they are a member | ||
const { | ||
userRoleIds, | ||
authorities: userAuthorities, | ||
hasAllAuthority, | ||
} = currentUser | ||
const { usersCanAssignOwnUserRoles, authorityIdToNameMap } = | ||
useSystemInformation() | ||
const cannotAssignThisRole = | ||
!hasAllAuthority && | ||
!usersCanAssignOwnUserRoles && | ||
userRoleIds.includes(roleId) | ||
|
||
// check if role has authorities that user does not have | ||
const authoritiesUserDoesNotHave = hasAllAuthority | ||
? [] | ||
: roleAuthorities.filter( | ||
(roleAuth) => !userAuthorities.includes(roleAuth) | ||
) | ||
|
||
if (!cannotAssignThisRole && authoritiesUserDoesNotHave.length === 0) { | ||
return null | ||
} | ||
|
||
return ( | ||
<NoticeBox className={styles.noticeBox}> | ||
{cannotAssignThisRole && ( | ||
<div> | ||
{i18n.t( | ||
'You cannot assign this role because you are assigned to this role, and your system does not allow you to assign roles of which you are a member.' | ||
)} | ||
</div> | ||
)} | ||
{authoritiesUserDoesNotHave?.length > 0 && ( | ||
<div> | ||
<div className={styles.missingRolesMessage}> | ||
<span> | ||
{i18n.t( | ||
'You cannot assign this role because it has authorities that you do not have.' | ||
)} | ||
</span> | ||
<div | ||
onClick={() => { | ||
setDetailsExpanded((prev) => !prev) | ||
}} | ||
> | ||
{detailsExpanded ? ( | ||
<IconChevronUp24 /> | ||
) : ( | ||
<IconChevronDown24 /> | ||
)} | ||
</div> | ||
</div> | ||
|
||
{detailsExpanded && ( | ||
<ul> | ||
{authoritiesUserDoesNotHave | ||
.sort((a, b) => | ||
( | ||
authorityIdToNameMap.get(a) ?? a | ||
).localeCompare( | ||
authorityIdToNameMap.get(b) ?? b | ||
) | ||
) | ||
.map((auth) => ( | ||
<li key={auth.id}> | ||
{authorityIdToNameMap.get(auth) ?? auth} | ||
</li> | ||
))} | ||
</ul> | ||
)} | ||
</div> | ||
)} | ||
</NoticeBox> | ||
) | ||
} | ||
|
||
AssignmentRestrictionWarning.propTypes = { | ||
roleId: PropTypes.string.isRequired, | ||
currentUser: PropTypes.object, | ||
roleAuthorities: PropTypes.arrayOf(PropTypes.string), | ||
} |
13 changes: 13 additions & 0 deletions
13
src/components/RoleForm/AssignmentRestrictionsWarning.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.noticeBox { | ||
max-width: 700px; | ||
margin-bottom: var(--spacers-dp16); | ||
} | ||
|
||
.noticeBox > div { | ||
margin-block-end: var(--spacers-dp8); | ||
} | ||
|
||
.missingRolesMessage { | ||
display: flex; | ||
align-items: center; | ||
} |
Oops, something went wrong.