Skip to content

Commit

Permalink
[#202] Implement role group assignment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Sep 6, 2024
1 parent 19998a7 commit 7e5bb61
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 70 deletions.
25 changes: 12 additions & 13 deletions src/RoleSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ const roleOptions = Object.keys(ROLE).map((key) => ({
label: ROLE[key],
}));

const RoleSelector = ({ selected = [], handler, readOnly = true, label = "Roles" }) => {
const RoleSelector = ({ selected = [], handler = () => {}, readOnly = true, label = "Roles" }) => {
const formatSelected = (selected) => {
return selected.map((value) => ({
value: value,
label: value,
}));
};

const [selectedRoles, setSelectedRoles] = useState(formatSelected(selected));
// const [selectedRoles, setSelectedRoles] = useState(formatSelected(selected));
//
// useEffect(() => {
// setSelectedRoles(formatSelected(selected));
// }, [selected]);

useEffect(() => {
setSelectedRoles(formatSelected(selected));
}, [selected]);

const handleChange = (selectedOptions) => {
setSelectedRoles(selectedOptions);
const selectedValues = selectedOptions.map((option) => option.value);
handler(selectedValues);
};
// const handleChange = (selectedOptions) => {
// setSelectedRoles(selectedOptions);
// const selectedValues = selectedOptions.map((option) => option.value);
// handler(selectedValues);
// };

return (
<FormGroup as={Row}>
Expand All @@ -37,8 +37,7 @@ const RoleSelector = ({ selected = [], handler, readOnly = true, label = "Roles"
</Col>
<Col lg={10}>
<Select
value={selectedRoles}
onChange={handleChange}
value={formatSelected(selected)}
isMulti
name="roles"
options={roleOptions}
Expand Down
81 changes: 41 additions & 40 deletions src/components/user/User.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import HelpIcon from "../HelpIcon";
import PropTypes from "prop-types";
import { FaRandom } from "react-icons/fa";
import { isUsingOidcAuth } from "../../utils/OidcUtils";
import { getRoles, hasRole, isAdmin, roleToType } from "../../utils/SecurityUtils";
import { getRoles, hasRole, isAdmin } from "../../utils/SecurityUtils";
import RoleSelector from "../../RoleSelector.jsx";
import IfInternalAuth from "../misc/oidc/IfInternalAuth.jsx";

class User extends React.Component {
static propTypes = {
Expand All @@ -38,7 +39,7 @@ class User extends React.Component {
this.i18n = this.props.i18n;
this.formatMessage = this.props.formatMessage;
this.state = { savedWithEmail: false };
this._onRoleSelected = this._onRoleSelected.bind(this);
// this._onRoleSelected = this._onRoleSelected.bind(this);
}

_onChange = (e) => {
Expand Down Expand Up @@ -197,15 +198,15 @@ class User extends React.Component {
}
}

_onRoleSelected(roles) {
const types = roles.map((role) => {
return ROLE_TYPE[role];
});
this.props.handlers.onChange({ types: types });
}
// _onRoleSelected(roles) {
// const types = roles.map((role) => {
// return ROLE_TYPE[role];
// });
// this.props.handlers.onChange({ types: types });
// }

_onGroupSelected(group) {
this.props.handlers.onChange({ group: group });
_onRoleGroupSelected(roleGroup) {
this.props.handlers.onChange({ roleGroup: roleGroup });
}

_onSaveAndSendEmail() {
Expand Down Expand Up @@ -302,24 +303,26 @@ class User extends React.Component {
</div>
</div>
<div className="row">
<div className="col-12 col-sm-6">
<HorizontalInput
type="select"
name="group"
label={`${this.i18n("user.group")}*`}
disabled={
!isAdmin(currentUser) ||
(currentUser.username !== user.username && !hasRole(currentUser, ROLE.EDIT_USERS)) ||
isUsingOidcAuth()
}
value={user.group}
labelWidth={3}
inputWidth={8}
onChange={this._onGroupSelected}
>
{this._generateGroupOptions()}
</HorizontalInput>
</div>
<IfInternalAuth>
<div className="col-12 col-sm-6">
<HorizontalInput
type="select"
name="roleGroup"
label={`${this.i18n("user.role-group")}*`}
disabled={
!isAdmin(currentUser) ||
(currentUser.username !== user.username && !hasRole(currentUser, ROLE.EDIT_USERS)) ||
isUsingOidcAuth()
}
labelWidth={3}
inputWidth={8}
onChange={this._onChange}
value={user.roleGroup ? user.roleGroup : ""}
>
{this._generateGroupOptions()}
</HorizontalInput>
</div>
</IfInternalAuth>
{isAdmin(currentUser) && (
<div className="col-12 col-sm-6">
<HorizontalInput
Expand Down Expand Up @@ -355,18 +358,16 @@ class User extends React.Component {
</div>
</div>
)}
<div className="col-12 col-sm-8">
<RoleSelector
selected={getRoles(user)}
handler={this._onRoleSelected}
readOnly={
!isAdmin(currentUser) ||
(currentUser.username !== user.username && !hasRole(currentUser, ROLE.EDIT_USERS)) ||
isUsingOidcAuth()
}
label={`${this.i18n("user.roles")}*`}
/>
</div>
<IfInternalAuth>
<div className="col-12 col-sm-8">
<RoleSelector
selected={getRoles(user)}
// handler={this._onRoleSelected}
readOnly={true}
label={`${this.i18n("user.roles")}*`}
/>
</div>
</IfInternalAuth>
<div className="buttons-line-height mt-3 text-center">
{this._impersonateButton()}
{isUsingOidcAuth() ? this._redirectToKeycloakButton() : this._passwordChangeButton()}
Expand Down
1 change: 1 addition & 0 deletions src/components/user/UserController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class UserController extends React.Component {
if (user.isNew || (this._isNew() && this.props.userSaved.status === ACTION_STATUS.ERROR)) {
this.props.createUser(omit(user, "isNew"));
} else {
delete user.types;
this.props.updateUser(user, this.props.currentUser, sendEmail);
}
};
Expand Down
8 changes: 5 additions & 3 deletions src/constants/DefaultConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ export const ROLE = {
};

export const GROUP = {
OPERATOR: "Operator",
SUPPLIER: "Supplier",
EXTERNAL_USER: "External User",
OPERATOR_ADMIN: "OPERATOR_ADMIN",
OPERATOR_USER: "OPERATOR_USER",
SUPPLIER: "SUPPLIER_USER",
SUPPLIER_ADMIN: "SUPPLIER_ADMIN",
EXTERNAL_USER: "EXTERNAL_USER",
};

export const TYPE_ROLE = {
Expand Down
22 changes: 11 additions & 11 deletions src/constants/Vocabulary.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ export const RDFS_COMMENT = "http://www.w3.org/2000/01/rdf-schema#comment";
export const ADMIN_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/administrator";
export const DOCTOR_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/doctor";
export const IMPERSONATOR_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/impersonator";
export const COMPLETE_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/complete_records";
export const REJECT_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/reject_records";
export const PUBLISH_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/publish_records";
export const DELETE_ALL_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/delete_all_records";
export const EDIT_ALL_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/edit_all_records";
export const VIEW_ALL_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/view_all_records";
export const COMPLETE_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/complete-records";
export const REJECT_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/reject-records";
export const PUBLISH_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/publish-records";
export const DELETE_ALL_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/delete-all-records";
export const EDIT_ALL_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/edit-all-records";
export const VIEW_ALL_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/view-all-records";
export const DELETE_ORGANIZATION_RECORDS_TYPE =
"http://onto.fel.cvut.cz/ontologies/record-manager/delete_organization_records";
"http://onto.fel.cvut.cz/ontologies/record-manager/delete-organization-records";
export const EDIT_ORGANIZATION_RECORDS_TYPE =
"http://onto.fel.cvut.cz/ontologies/record-manager/edit_organization_records";
"http://onto.fel.cvut.cz/ontologies/record-manager/edit-organization-records";
export const VIEW_ORGANIZATION_RECORDS_TYPE =
"http://onto.fel.cvut.cz/ontologies/record-manager/view_organization_records";
export const EDIT_USERS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/edit_users";
export const IMPORT_CODELISTS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/import_codelists";
"http://onto.fel.cvut.cz/ontologies/record-manager/view-organization-records";
export const EDIT_USERS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/edit-users";
export const IMPORT_CODELISTS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/import-codelists";
2 changes: 1 addition & 1 deletion src/i18n/cs.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default {
"user.passwords-not-matching-tooltip": "Hesla se neshodují",
"user.role": "Role",
"user.roles": "Roles",
"user.group": "Skupina",
"user.group": "Skupina rolí",
"user.save-success": "Uživatel úspěšně uložen",
"user.save-success-with-email": "Uživatel úspěšně uložen a informován emailem.",
"user.save-error": "Uživatele se nepodařilo uložit. {error}",
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default {
"user.passwords-not-matching-tooltip": "Passwords don't match",
"user.role": "Role",
"user.roles": "Roles",
"user.group": "Group",
"user.role-group": "Role Group",
"user.save-success": "User saved successfully",
"user.save-success-with-email": "User saved successfully and informed by email.",
"user.save-error": "Unable to save user. {error}",
Expand Down
4 changes: 3 additions & 1 deletion tests/__tests__/components/User.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from "react";
import { IntlProvider } from "react-intl";
import TestUtils from "react-dom/test-utils";
import User from "../../../src/components/user/User";
import { ACTION_STATUS, ROLE } from "../../../src/constants/DefaultConstants";
import { ACTION_STATUS, GROUP, ROLE } from "../../../src/constants/DefaultConstants";
import * as EntityFactory from "../../../src/utils/EntityFactory";
import enLang from "../../../src/i18n/en";
import { describe, expect, it, vi, beforeEach } from "vitest";
Expand Down Expand Up @@ -56,6 +56,7 @@ describe("User", function () {
"http://onto.fel.cvut.cz/ontologies/record-manager/administrator",
"http://onto.fel.cvut.cz/ontologies/record-manager/doctor",
],
roleGroup: GROUP.OPERATOR_ADMIN,
};

user = {
Expand All @@ -69,6 +70,7 @@ describe("User", function () {
key: 18691,
},
types: ["http://onto.fel.cvut.cz/ontologies/record-manager/doctor"],
roleGroup: GROUP.OPERATOR_USER,
};

institutions = [
Expand Down

0 comments on commit 7e5bb61

Please sign in to comment.