Skip to content

Commit

Permalink
[#202] Implement EDIT_USERS role logic
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Sep 5, 2024
1 parent 5aa223d commit 62d2b09
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
36 changes: 28 additions & 8 deletions src/components/user/User.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import withI18n from "../../i18n/withI18n";
import { injectIntl } from "react-intl";
import HorizontalInput from "../HorizontalInput";
import UserValidator from "../../validation/UserValidator";
import { ACTION_STATUS, GROUP, ROLE_TYPE } from "../../constants/DefaultConstants";
import { ACTION_STATUS, GROUP, ROLE, ROLE_TYPE } from "../../constants/DefaultConstants";
import { processInstitutions } from "../../utils/Utils";
import { LoaderCard, LoaderSmall } from "../Loader";
import HelpIcon from "../HelpIcon";
import PropTypes from "prop-types";
import { FaRandom } from "react-icons/fa";
import { isUsingOidcAuth } from "../../utils/OidcUtils";
import { getRoles, isAdmin, roleToType } from "../../utils/SecurityUtils";
import { getRoles, hasRole, isAdmin, roleToType } from "../../utils/SecurityUtils";
import RoleSelector from "../../RoleSelector.jsx";

class User extends React.Component {
Expand Down Expand Up @@ -245,7 +245,10 @@ class User extends React.Component {
type="text"
name="firstName"
label={`${this.i18n("user.first-name")}*`}
disabled={(!isAdmin(currentUser) && currentUser.username !== user.username) || isUsingOidcAuth()}
disabled={
(currentUser.username !== user.username && !hasRole(currentUser, ROLE.EDIT_USERS)) ||
isUsingOidcAuth()
}
value={user.firstName}
labelWidth={3}
inputWidth={8}
Expand All @@ -257,7 +260,10 @@ class User extends React.Component {
type="text"
name="lastName"
label={`${this.i18n("user.last-name")}*`}
disabled={(!isAdmin(currentUser) && currentUser.username !== user.username) || isUsingOidcAuth()}
disabled={
(currentUser.username !== user.username && !hasRole(currentUser, ROLE.EDIT_USERS)) ||
isUsingOidcAuth()
}
value={user.lastName}
labelWidth={3}
inputWidth={8}
Expand All @@ -284,7 +290,10 @@ class User extends React.Component {
type="email"
name="emailAddress"
label={`${this.i18n("users.email")}*`}
disabled={(!isAdmin(currentUser) && currentUser.username !== user.username) || isUsingOidcAuth()}
disabled={
(currentUser.username !== user.username && !hasRole(currentUser, ROLE.EDIT_USERS)) ||
isUsingOidcAuth()
}
value={user.emailAddress}
labelWidth={3}
inputWidth={8}
Expand All @@ -298,7 +307,11 @@ class User extends React.Component {
type="select"
name="group"
label={`${this.i18n("user.group")}*`}
disabled={(!isAdmin(currentUser) && currentUser.username !== user.username) || isUsingOidcAuth()}
disabled={
!isAdmin(currentUser) ||
(currentUser.username !== user.username && !hasRole(currentUser, ROLE.EDIT_USERS)) ||
isUsingOidcAuth()
}
value={user.group}
labelWidth={3}
inputWidth={8}
Expand All @@ -314,7 +327,10 @@ class User extends React.Component {
name="institution"
label={`${this.i18n("institution.panel-title")}*`}
onChange={this._onInstitutionSelected}
disabled={!isAdmin(currentUser)}
disabled={
!isAdmin(currentUser) ||
(currentUser.username !== user.username && !hasRole(currentUser, ROLE.EDIT_USERS))
}
value={user.institution ? user.institution.uri : ""}
labelWidth={3}
inputWidth={8}
Expand Down Expand Up @@ -343,7 +359,11 @@ class User extends React.Component {
<RoleSelector
selected={getRoles(user)}
handler={this._onRoleSelected}
readOnly={(!isAdmin(currentUser) && currentUser.username !== user.username) || isUsingOidcAuth()}
readOnly={
!isAdmin(currentUser) ||
(currentUser.username !== user.username && !hasRole(currentUser, ROLE.EDIT_USERS)) ||
isUsingOidcAuth()
}
label={`${this.i18n("user.roles")}*`}
/>
</div>
Expand Down
8 changes: 3 additions & 5 deletions tests/__tests__/components/User.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe("User", function () {
</IntlProvider>,
);
const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input");
expect(result.length).toEqual(7);
expect(result.length).toEqual(6);
for (let input of result) {
switch (input.name) {
case "firstName":
Expand Down Expand Up @@ -293,7 +293,7 @@ describe("User", function () {
</IntlProvider>,
);
const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input");
expect(result.length).toEqual(6);
expect(result.length).toEqual(5);
for (let input of result) {
switch (input.name) {
case "firstName":
Expand All @@ -317,7 +317,6 @@ describe("User", function () {
}
const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select");
expect(selects.length).toEqual(2);
expect(selects[0].disabled).toBeFalsy();
const randomButton = TestUtils.scryRenderedDOMComponentsWithClass(tree, "glyphicon");
expect(randomButton.length).toEqual(0);
});
Expand All @@ -338,7 +337,7 @@ describe("User", function () {
</IntlProvider>,
);
const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input");
expect(result.length).toEqual(7);
expect(result.length).toEqual(5);
for (let input of result) {
switch (input.name) {
case "firstName":
Expand All @@ -362,7 +361,6 @@ describe("User", function () {
}
const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select");
expect(selects.length).toEqual(2);
expect(selects[0].disabled).toBeFalsy();
});

it("renders filled user's form", function () {
Expand Down

0 comments on commit 62d2b09

Please sign in to comment.