diff --git a/src/components/user/User.jsx b/src/components/user/User.jsx index 24f27783..787a8782 100644 --- a/src/components/user/User.jsx +++ b/src/components/user/User.jsx @@ -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 { @@ -245,7 +245,11 @@ 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 && + (!isAdmin(currentUser) || !hasRole(currentUser, ROLE.EDIT_USERS))) || + isUsingOidcAuth() + } value={user.firstName} labelWidth={3} inputWidth={8} @@ -257,7 +261,11 @@ 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 && + (!isAdmin(currentUser) || !hasRole(currentUser, ROLE.EDIT_USERS))) || + isUsingOidcAuth() + } value={user.lastName} labelWidth={3} inputWidth={8} @@ -284,7 +292,11 @@ 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 && + (!isAdmin(currentUser) || !hasRole(currentUser, ROLE.EDIT_USERS))) || + isUsingOidcAuth() + } value={user.emailAddress} labelWidth={3} inputWidth={8} @@ -298,7 +310,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} @@ -314,7 +330,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} @@ -343,7 +362,11 @@ class User extends React.Component { diff --git a/tests/__tests__/components/User.spec.jsx b/tests/__tests__/components/User.spec.jsx index 8008fa27..ebafc5da 100644 --- a/tests/__tests__/components/User.spec.jsx +++ b/tests/__tests__/components/User.spec.jsx @@ -162,7 +162,7 @@ describe("User", function () { , ); 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": @@ -293,7 +293,7 @@ describe("User", function () { , ); 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": @@ -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); }); @@ -338,7 +337,7 @@ describe("User", function () { , ); 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": @@ -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 () {