Skip to content

Commit

Permalink
[#202] Change implementation based on new roles
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan authored and blcham committed Nov 13, 2024
1 parent 2d09f2a commit bea10f9
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 27 deletions.
9 changes: 4 additions & 5 deletions src/components/MainView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MainView extends React.Component {
const path = this.props.location.pathname;

return (
<IfGranted expected={ROLE.ADMIN} actual={this.props.user.role}>
<IfGranted expected={ROLE.ADMIN} actual={this.props.user.roles}>
<NavItem>
<NavLink to={Routes.users.path} isActive={() => path.startsWith(Routes.users.path)} className="nav-link">
{this.i18n("main.users-nav")}
Expand Down Expand Up @@ -95,7 +95,6 @@ class MainView extends React.Component {
const user = this.props.user;
const name = user.firstName.substring(0, 1) + ". " + user.lastName;
const path = this.props.location.pathname;

return (
<div className="main-view-wrapper">
<header>
Expand Down Expand Up @@ -129,7 +128,7 @@ class MainView extends React.Component {
</NavLink>
</NavItem>
) : null}
<IfGranted expected={ROLE.ADMIN} actual={user.role}>
<IfGranted expected={ROLE.ADMIN} actual={user.roles}>
<NavItem>
<NavLink
className="nav-link"
Expand All @@ -140,10 +139,10 @@ class MainView extends React.Component {
</NavLink>
</NavItem>
</IfGranted>
<IfGranted expected={ROLE.ADMIN} actual={user.role}>
<IfGranted expected={ROLE.ADMIN} actual={user.roles}>
<NavItem>{this._renderStatisticsNavLink(path)}</NavItem>
</IfGranted>
<IfGranted expected={ROLE.ADMIN} actual={user.role}>
<IfGranted expected={ROLE.ADMIN} actual={user.roles}>
<NavItem>
<NavLink
className="nav-link"
Expand Down
2 changes: 1 addition & 1 deletion src/components/history/HistoryDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const HistoryDetail = () => {
}, [dispatch, key]);

return (
<IfGranted expected={ROLE.ADMIN} actual={currentUser.role}>
<IfGranted expected={ROLE.ADMIN} actual={currentUser.roles}>
<Card variant="primary">
<Card.Header className="text-light bg-primary" as="h6">
{i18n("history.panel-title")}
Expand Down
7 changes: 4 additions & 3 deletions src/components/institution/Institution.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { LoaderSmall } from "../Loader";
import InstitutionValidator from "../../validation/InstitutionValidator";
import HelpIcon from "../HelpIcon";
import PromiseTrackingMask from "../misc/PromiseTrackingMask";
import { isAdmin } from "../../utils/SecurityUtils.js";

/**
* Institution detail. Editable only for admins.
Expand Down Expand Up @@ -63,7 +64,7 @@ class Institution extends React.Component {
name="name"
label={`${this.i18n("institution.name")}*`}
value={institution.name}
readOnly={currentUser.role !== ROLE.ADMIN}
readOnly={!isAdmin(currentUser)}
onChange={this._onChange}
labelWidth={3}
inputWidth={8}
Expand All @@ -75,7 +76,7 @@ class Institution extends React.Component {
name="emailAddress"
label={this.i18n("institution.email")}
value={institution.emailAddress || ""}
readOnly={currentUser.role !== ROLE.ADMIN}
readOnly={!isAdmin(currentUser)}
onChange={this._onChange}
labelWidth={3}
inputWidth={8}
Expand Down Expand Up @@ -122,7 +123,7 @@ class Institution extends React.Component {

_renderButtons() {
const { currentUser, handlers, institutionSaved } = this.props;
if (currentUser.role !== ROLE.ADMIN) {
if (!isAdmin(currentUser)) {
return (
<div className="row justify-content-center">
<Button variant="primary" size="sm" className="action-button" onClick={handlers.onCancel}>
Expand Down
5 changes: 3 additions & 2 deletions src/components/institution/InstitutionsController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { bindActionCreators } from "redux";
import { deleteInstitution } from "../../actions/InstitutionActions";
import { trackPromise } from "react-promise-tracker";
import PropTypes from "prop-types";
import { isAdmin } from "../../utils/SecurityUtils.js";

class InstitutionsController extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -47,7 +48,7 @@ class InstitutionsController extends React.Component {

render() {
const { currentUser, institutionsLoaded, institutionDeleted } = this.props;
if (!currentUser || currentUser.role !== ROLE.ADMIN) {
if (!currentUser || !isAdmin(currentUser)) {
return null;
}
const handlers = {
Expand All @@ -70,7 +71,7 @@ InstitutionsController.propTypes = {
transitionToWithOpts: PropTypes.func.isRequired,
deleteInstitution: PropTypes.func.isRequired,
currentUser: PropTypes.shape({
role: PropTypes.string.isRequired,
roles: PropTypes.array.isRequired,
}).isRequired,
institutionsLoaded: PropTypes.object,
institutionDeleted: PropTypes.object,
Expand Down
4 changes: 2 additions & 2 deletions src/components/record/RecordRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const RecordRow = (props) => {

return (
<tr className="position-relative">
<IfGranted expected={ROLE.ADMIN} actual={props.currentUser.role}>
<IfGranted expected={ROLE.ADMIN} actual={props.currentUser.roles}>
<td className="report-row">
<Button variant="link" size="sm" onClick={() => props.onEdit(record)}>
{record.key}
Expand All @@ -59,7 +59,7 @@ const RecordRow = (props) => {
{record.localName}
</Button>
</td>
<IfGranted expected={ROLE.ADMIN} actual={props.currentUser.role}>
<IfGranted expected={ROLE.ADMIN} actual={props.currentUser.roles}>
<td className="report-row content-center">{record.institution.name}</td>
<td className="report-row content-center">
{getFormTemplateOptionName(record.formTemplate, formTemplateOptions)}
Expand Down
4 changes: 2 additions & 2 deletions src/components/record/RecordTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ class RecordTable extends React.Component {
return (
<thead>
<tr>
<IfGranted expected={ROLE.ADMIN} actual={this.props.currentUser.role}>
<IfGranted expected={ROLE.ADMIN} actual={this.props.currentUser.roles}>
<th className="col-1 content-center">{this.i18n("records.id")}</th>
</IfGranted>
<th className="col-2 content-center">{this.i18n("records.local-name")}</th>
<IfGranted expected={ROLE.ADMIN} actual={this.props.currentUser.role}>
<IfGranted expected={ROLE.ADMIN} actual={this.props.currentUser.roles}>
<FilterableInstitutionHeader filters={filters} onFilterChange={onChange} />
<FilterableTemplateHeader filters={filters} onFilterChange={onChange} />
</IfGranted>
Expand Down
6 changes: 3 additions & 3 deletions src/components/record/Records.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from "react";
import { Alert, Button, Card } from "react-bootstrap";
import { injectIntl } from "react-intl";
import withI18n from "../../i18n/withI18n";
import { EXTENSION_CONSTANTS } from "../../constants/DefaultConstants";
import { EXTENSION_CONSTANTS, ROLE } from "../../constants/DefaultConstants";
import PropTypes from "prop-types";
import { processTypeaheadOptions } from "./TypeaheadAnswer";
import { EXTENSIONS } from "../../../config";
import ExportRecordsDropdown from "./ExportRecordsDropdown";
import { isAdmin } from "../../utils/SecurityUtils";
import { hasRole, isAdmin } from "../../utils/SecurityUtils";
import ImportRecordsDialog from "./ImportRecordsDialog";
import PromiseTrackingMask from "../misc/PromiseTrackingMask";
import { trackPromise } from "react-promise-tracker";
Expand Down Expand Up @@ -57,7 +57,7 @@ class Records extends React.Component {
const showCreateButton = STUDY_CREATE_AT_MOST_ONE_RECORD
? !recordsLoaded.records || recordsLoaded.records.length < 1
: true;
const showPublishButton = isAdmin(this.props.currentUser) && EXTENSIONS === EXTENSION_CONSTANTS.OPERATOR;
const showPublishButton = isAdmin(this.props.currentUser) && hasRole(this.props.currentUser, ROLE.PUBLISH_RECORDS);
const createRecordDisabled = STUDY_CLOSED_FOR_ADDITION && !isAdmin(this.props.currentUser);
const createRecordTooltip = this.i18n(
createRecordDisabled ? "records.closed-study.create-tooltip" : "records.opened-study.create-tooltip",
Expand Down
5 changes: 3 additions & 2 deletions src/components/user/PasswordChangeController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ROLE } from "../../constants/DefaultConstants";
import { changePassword } from "../../actions/UserActions";
import * as UserFactory from "../../utils/EntityFactory";
import PropTypes from "prop-types";
import { isAdmin } from "../../utils/SecurityUtils.js";

class PasswordChangeController extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -51,7 +52,7 @@ class PasswordChangeController extends React.Component {

render() {
const { currentUser, passwordChange, match } = this.props;
if (!currentUser || (currentUser.role !== ROLE.ADMIN && currentUser.username !== match.params.username)) {
if (!currentUser || (!isAdmin(currentUser) && currentUser.username !== match.params.username)) {
return null;
}
const handlers = {
Expand Down Expand Up @@ -81,7 +82,7 @@ PasswordChangeController.propTypes = {
}).isRequired,
transitionToWithOpts: PropTypes.func.isRequired,
currentUser: PropTypes.shape({
role: PropTypes.string.isRequired,
roles: PropTypes.array.isRequired,
username: PropTypes.string.isRequired,
}).isRequired,
passwordChange: PropTypes.object.isRequired,
Expand Down
4 changes: 1 addition & 3 deletions src/components/user/User.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ class User extends React.Component {
type="text"
name="firstName"
label={`${this.i18n("user.first-name")}*`}
disabled={
(currentUser.role !== ROLE.ADMIN && currentUser.username !== user.username) || isUsingOidcAuth()
}
disabled={(!isAdmin(currentUser) && currentUser.username !== user.username) || isUsingOidcAuth()}
value={user.firstName}
labelWidth={3}
inputWidth={8}
Expand Down
5 changes: 3 additions & 2 deletions src/components/user/UsersController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ROLE } from "../../constants/DefaultConstants";
import { deleteUser } from "../../actions/UserActions";
import { trackPromise } from "react-promise-tracker";
import PropTypes from "prop-types";
import { isAdmin } from "../../utils/SecurityUtils.js";

class UsersController extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -51,7 +52,7 @@ class UsersController extends React.Component {

render() {
const { currentUser, usersLoaded, userDeleted } = this.props;
if (!currentUser || currentUser.role !== ROLE.ADMIN) {
if (!currentUser || !isAdmin(currentUser)) {
return null;
}
const handlers = {
Expand All @@ -70,7 +71,7 @@ UsersController.propTypes = {
transitionToWithOpts: PropTypes.func.isRequired,
deleteUser: PropTypes.func.isRequired,
currentUser: PropTypes.shape({
role: PropTypes.string.isRequired,
roles: PropTypes.array.isRequired,
}).isRequired,
usersLoaded: PropTypes.object,
userDeleted: PropTypes.object,
Expand Down
4 changes: 2 additions & 2 deletions src/reducers/AuthReducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ActionConstants from "../constants/ActionConstants";
import { getRole } from "../utils/Utils";
import { getRoles } from "../utils/SecurityUtils.js";
import { ACTION_STATUS } from "../constants/DefaultConstants";

const initialState = {
Expand Down Expand Up @@ -47,7 +47,7 @@ export default function (state = initialState, action) {
status: ACTION_STATUS.SUCCESS,
user: {
...action.user,
role: getRole(action.user),
roles: getRoles(action.user),
},
};
case ActionConstants.LOAD_USER_PROFILE_ERROR:
Expand Down

0 comments on commit bea10f9

Please sign in to comment.