diff --git a/packages/client/src/pages/AccountsHome/Section/Body/TableView/TableContainer.js b/packages/client/src/pages/AccountsHome/Section/Body/TableView/TableContainer.js index 5717a8742d..d17aacb742 100644 --- a/packages/client/src/pages/AccountsHome/Section/Body/TableView/TableContainer.js +++ b/packages/client/src/pages/AccountsHome/Section/Body/TableView/TableContainer.js @@ -107,6 +107,7 @@ const Table = ({ hasMoreAccounts, filterTotal, withPaging, + canChangeUserType, }) => { const ref = useRef(null); @@ -156,6 +157,7 @@ const Table = ({ isOwner={isOwner} changeUserType={changeType} userId={userId} + canChangeUserType={canChangeUserType} /> ))} @@ -165,7 +167,7 @@ const Table = ({ ); }; -export default inject(({ peopleStore, auth }) => { +export default inject(({ peopleStore, auth, accessRights }) => { const { usersStore, filterStore, @@ -180,6 +182,8 @@ export default inject(({ peopleStore, auth }) => { const { isVisible: infoPanelVisible } = auth.infoPanelStore; const { isAdmin, isOwner, id: userId } = auth.userStore.user; + const { canChangeUserType } = accessRights; + return { peopleList, viewAs, @@ -195,5 +199,6 @@ export default inject(({ peopleStore, auth }) => { fetchMoreAccounts, hasMoreAccounts, filterTotal, + canChangeUserType, }; })(observer(Table)); diff --git a/packages/client/src/pages/AccountsHome/Section/Body/TableView/TableRow.js b/packages/client/src/pages/AccountsHome/Section/Body/TableView/TableRow.js index f504a5f9cc..064516cb75 100644 --- a/packages/client/src/pages/AccountsHome/Section/Body/TableView/TableRow.js +++ b/packages/client/src/pages/AccountsHome/Section/Body/TableView/TableRow.js @@ -129,14 +129,15 @@ const PeopleTableRow = (props) => { checkedProps, onContentRowSelect, onEmailClick, - isAdmin, + isOwner, theme, changeUserType, - userId, + setBufferSelection, isActive, isSeveralSelection, + canChangeUserType, } = props; const { @@ -147,10 +148,7 @@ const PeopleTableRow = (props) => { position, rooms, - id, - role, - isVisitor, } = item; @@ -278,26 +276,9 @@ const PeopleTableRow = (props) => { ); - if (userId === id || statusType === "disabled") return text; + const canChange = canChangeUserType(item); - switch (role) { - case "owner": - return text; - - case "admin": - case "manager": - if (isOwner) { - return combobox; - } else { - return text; - } - - case "user": - return combobox; - - default: - return text; - } + return canChange ? combobox : text; }; const typeCell = renderTypeCell(); diff --git a/packages/client/src/pages/AccountsHome/Section/Header/index.js b/packages/client/src/pages/AccountsHome/Section/Header/index.js index 76839571a1..64e7e7f375 100644 --- a/packages/client/src/pages/AccountsHome/Section/Header/index.js +++ b/packages/client/src/pages/AccountsHome/Section/Header/index.js @@ -163,7 +163,7 @@ const SectionHeaderContent = (props) => { setInfoPanelIsVisible, isInfoPanelVisible, isOwner, - isAdmin, + setInvitePanelOptions, } = props; diff --git a/packages/client/src/pages/Home/InfoPanel/Body/index.js b/packages/client/src/pages/Home/InfoPanel/Body/index.js index c70e6a2732..2084b6abaa 100644 --- a/packages/client/src/pages/Home/InfoPanel/Body/index.js +++ b/packages/client/src/pages/Home/InfoPanel/Body/index.js @@ -77,6 +77,7 @@ const InfoPanelBodyContent = ({ isOwner: props.isOwner, isAdmin: props.isAdmin, changeUserType: props.changeUserType, + canChangeUserType: props.canChangeUserType, }, galleryProps: { getIcon, @@ -229,6 +230,7 @@ export default inject( oformsStore, contextOptionsStore, peopleStore, + accessRights, }) => { const { isOwner, isAdmin, id: selfId } = auth.userStore.user; const { personal, culture } = auth.settingsStore; @@ -241,6 +243,7 @@ export default inject( const { getFilesContextOptions: getContextOptionActions, } = contextOptionsStore; + const { canChangeUserType } = accessRights; const { selection, @@ -342,6 +345,7 @@ export default inject( gallerySelected, isRootFolder, + canChangeUserType, }; } )(withRouter(observer(InfoPanelBodyContent))); diff --git a/packages/client/src/pages/Home/InfoPanel/Body/views/Accounts/index.js b/packages/client/src/pages/Home/InfoPanel/Body/views/Accounts/index.js index 8ec667f25e..34f1e77ea5 100644 --- a/packages/client/src/pages/Home/InfoPanel/Body/views/Accounts/index.js +++ b/packages/client/src/pages/Home/InfoPanel/Body/views/Accounts/index.js @@ -16,6 +16,7 @@ const Accounts = ({ isOwner, isAdmin, changeUserType, + canChangeUserType, selfId, }) => { const [statusLabel, setStatusLabel] = React.useState(""); @@ -126,26 +127,9 @@ const Accounts = ({ const status = getUserStatus(selection); - if (selfId === id || status === "disabled") return text; + const canChange = canChangeUserType({ ...selection, statusType: status }); - switch (role) { - case "owner": - return text; - - case "admin": - case "manager": - if (isOwner) { - return combobox; - } else { - return text; - } - - case "user": - return combobox; - - default: - return text; - } + return canChange ? combobox : text; }; const typeData = renderTypeData(); diff --git a/packages/client/src/store/AccessRights.js b/packages/client/src/store/AccessRights.js new file mode 100644 index 0000000000..2e818e22fb --- /dev/null +++ b/packages/client/src/store/AccessRights.js @@ -0,0 +1,142 @@ +import { makeAutoObservable } from "mobx"; + +import { + EmployeeActivationStatus, + EmployeeStatus, +} from "@docspace/common/constants"; + +class AccessRights { + authStore = null; + + constructor(authStore) { + this.authStore = authStore; + + makeAutoObservable(this); + } + + canChangeUserType = (user) => { + const { id, isOwner } = this.authStore; + + const { id: userId, statusType, role } = user; + + if (userId === id || statusType === EmployeeStatus.Disabled) return text; + + switch (role) { + case "owner": + return false; + + case "admin": + case "manager": + if (isOwner) { + return true; + } else { + return false; + } + + case "user": + return true; + + default: + return false; + } + }; + + canMakeEmployeeUser = (user) => { + const { id, isOwner } = this.authStore; + + const { + status, + id: userId, + isAdmin: userIsAdmin, + isOwner: userIsOwner, + isVisitor: userIsVisitor, + } = user; + + const needMakeEmployee = + status !== EmployeeStatus.Disabled && userId !== id; + + if (isOwner) return needMakeEmployee; + + return needMakeEmployee && !userIsAdmin && !userIsOwner && userIsVisitor; + }; + + canActivateUser = (user) => { + const { id, isOwner, isAdmin } = this.authStore; + + const { + status, + id: userId, + isAdmin: userIsAdmin, + isOwner: userIsOwner, + } = user; + + const needActivate = status !== EmployeeStatus.Active && userId !== id; + + if (isOwner) return needActivate; + + if (isAdmin) return needActivate && !userIsAdmin && !userIsOwner; + + return false; + }; + + canDisableUser = (user) => { + const { id, isOwner, isAdmin } = this.authStore; + + const { + status, + id: userId, + isAdmin: userIsAdmin, + isOwner: userIsOwner, + } = user; + + const needDisable = status !== EmployeeStatus.Disabled && userId !== id; + + if (isOwner) return needDisable; + + if (isAdmin) return needDisable && !userIsAdmin && !userIsOwner; + + return false; + }; + + canInviteUser = (user) => { + const { id, isOwner } = this.authStore; + + const { + activationStatus, + status, + id: userId, + isAdmin: userIsAdmin, + isOwner: userIsOwner, + } = user; + + const needInvite = + activationStatus === EmployeeActivationStatus.Pending && + status === EmployeeStatus.Active && + userId !== id; + + if (isOwner) return needInvite; + + return needInvite && !userIsAdmin && !userIsOwner; + }; + + canRemoveUser = (user) => { + const { id, isOwner, isAdmin } = this.authStore; + + const { + status, + id: userId, + isAdmin: userIsAdmin, + isOwner: userIsOwner, + } = user; + + const needRemove = status === EmployeeStatus.Disabled && userId !== id; + + if (isOwner) return needRemove; + + if (isAdmin) return needRemove && !userIsAdmin && !userIsOwner; + + return false; + }; +} + +export default AccessRights; diff --git a/packages/client/src/store/PeopleStore.js b/packages/client/src/store/PeopleStore.js index 4e7efce778..90658ad2a9 100644 --- a/packages/client/src/store/PeopleStore.js +++ b/packages/client/src/store/PeopleStore.js @@ -39,10 +39,11 @@ class PeopleStore { loadingStore = null; infoPanelStore = null; setupStore = null; + accessRights = null; isInit = false; viewAs = isMobileRDD ? "row" : "table"; - constructor(authStore, infoPanelStore, setupStore) { + constructor(authStore, infoPanelStore, setupStore, accessRights) { this.authStore = authStore; this.groupsStore = new GroupsStore(this); this.usersStore = new UsersStore(this, authStore); @@ -58,6 +59,7 @@ class PeopleStore { this.loadingStore = new LoadingStore(); this.infoPanelStore = infoPanelStore; this.setupStore = setupStore; + this.accessRights = accessRights; this.contextOptionsStore = new AccountsContextOptionsStore(this); diff --git a/packages/client/src/store/SelectionPeopleStore.js b/packages/client/src/store/SelectionPeopleStore.js index 732dcc151f..a6cee617ef 100644 --- a/packages/client/src/store/SelectionPeopleStore.js +++ b/packages/client/src/store/SelectionPeopleStore.js @@ -1,8 +1,5 @@ import { makeAutoObservable } from "mobx"; -import { - EmployeeStatus, - EmployeeActivationStatus, -} from "@docspace/common/constants"; +import { EmployeeStatus } from "@docspace/common/constants"; import { getUserStatus } from "../helpers/people-helpers"; class SelectionStore { @@ -102,47 +99,17 @@ class SelectionStore { } get hasUsersToMakeEmployees() { - const { id, isOwner } = this.peopleStore.authStore.userStore.user; + const { canMakeEmployeeUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => x.status !== EmployeeStatus.Disabled && x.id !== id - ); - - return users.length > 0; - } - - const users = this.selection.filter( - (x) => - x.status !== EmployeeStatus.Disabled && - x.id !== id && - !x.isAdmin && - !x.isOwner && - x.isVisitor - ); + const users = this.selection.filter((x) => canMakeEmployeeUser(x)); return users.length > 0; } get getUsersToMakeEmployees() { - const { id, isOwner } = this.peopleStore.authStore.userStore.user; + const { canMakeEmployeeUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => x.status !== EmployeeStatus.Disabled && x.id !== id - ); - - return users.map((u) => u); - } - - const users = this.selection.filter( - (x) => - x.status !== EmployeeStatus.Disabled && - x.id !== id && - !x.isAdmin && - !x.isOwner && - x.isVisitor - ); + const users = this.selection.filter((x) => canMakeEmployeeUser(x)); return users.map((u) => u); } @@ -156,211 +123,67 @@ class SelectionStore { } get hasUsersToActivate() { - const { id, isOwner, isAdmin } = this.peopleStore.authStore.userStore.user; + const { canActivateUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => x.status !== EmployeeStatus.Active && x.id !== id - ); + const users = this.selection.filter((x) => canActivateUser(x)); - return users.length > 0; - } - - if (isAdmin && !isOwner) { - const users = this.selection.filter( - (x) => - x.status !== EmployeeStatus.Active && - x.id !== id && - !x.isAdmin && - !x.isOwner - ); - - return users.length > 0; - } - - return false; + return users.length > 0; } get getUsersToActivate() { - const { id, isOwner, isAdmin } = this.peopleStore.authStore.userStore.user; + const { canActivateUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => x.status !== EmployeeStatus.Active && x.id !== id - ); + const users = this.selection.filter((x) => canActivateUser(x)); - return users.map((u) => u); - } - - if (isAdmin && !isOwner) { - const users = this.selection.filter( - (x) => - x.status !== EmployeeStatus.Active && - x.id !== id && - !x.isAdmin && - !x.isOwner - ); - - return users.map((u) => u); - } - - return []; + return users.map((u) => u); } get hasUsersToDisable() { - const { id, isOwner, isAdmin } = this.peopleStore.authStore.userStore.user; + const { canDisableUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => x.status !== EmployeeStatus.Disabled && x.id !== id - ); + const users = this.selection.filter((x) => canDisableUser(x)); - return users.length > 0; - } - - if (isAdmin && !isOwner) { - const users = this.selection.filter( - (x) => - x.status !== EmployeeStatus.Disabled && - x.id !== id && - !x.isAdmin && - !x.isOwner - ); - - return users.length > 0; - } - - return false; + return users.length > 0; } get getUsersToDisable() { - const { id, isOwner, isAdmin } = this.peopleStore.authStore.userStore.user; + const { canDisableUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => x.status !== EmployeeStatus.Disabled && x.id !== id - ); + const users = this.selection.filter((x) => canDisableUser(x)); - return users.map((u) => u); - } - - if (isAdmin && !isOwner) { - const users = this.selection.filter( - (x) => - x.status !== EmployeeStatus.Disabled && - x.id !== id && - !x.isAdmin && - !x.isOwner - ); - - return users.map((u) => u); - } - - return []; + return users.map((u) => u); } get hasUsersToInvite() { - const { id, isOwner, isAdmin } = this.peopleStore.authStore.userStore.user; + const { canInviteUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => - x.activationStatus === EmployeeActivationStatus.Pending && - x.status === EmployeeStatus.Active && - x.id !== id - ); - - return users.length > 0; - } - - const users = this.selection.filter( - (x) => - x.activationStatus === EmployeeActivationStatus.Pending && - x.status === EmployeeStatus.Active && - x.id !== id && - !x.isAdmin && - !x.isOwner - ); + const users = this.selection.filter((x) => canInviteUser(x)); return users.length > 0; } get getUsersToInviteIds() { - const { id, isOwner, isAdmin } = this.peopleStore.authStore.userStore.user; + const { canRemoveUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => - x.activationStatus === EmployeeActivationStatus.Pending && - x.status === EmployeeStatus.Active && - x.id !== id - ); + const users = this.selection.filter((x) => canRemoveUser(x)); - return users.map((u) => u.id); - } - - const users = this.selection.filter( - (x) => - x.activationStatus === EmployeeActivationStatus.Pending && - x.status === EmployeeStatus.Active && - x.id !== id && - !x.isAdmin && - !x.isOwner - ); - - return users.map((u) => u.id); + return users.length > 0 ? users.map((u) => u.id) : false; } get hasUsersToRemove() { - const { id, isOwner, isAdmin } = this.peopleStore.authStore.userStore.user; + const { canRemoveUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => x.status === EmployeeStatus.Disabled && x.id !== id - ); + const users = this.selection.filter((x) => canRemoveUser(x)); - return users.length > 0; - } - - if (isAdmin && !isOwner) { - const users = this.selection.filter( - (x) => - x.status === EmployeeStatus.Disabled && - x.id !== id && - !x.isAdmin && - !x.isOwner - ); - - return users.length > 0; - } - - return false; + return users.length > 0; } get getUsersToRemoveIds() { - const { id, isOwner, isAdmin } = this.peopleStore.authStore.userStore.user; + const { canRemoveUser } = this.peopleStore.accessRights; - if (isOwner) { - const users = this.selection.filter( - (x) => x.status === EmployeeStatus.Disabled && x.id !== id - ); + const users = this.selection.filter((x) => canRemoveUser(x)); - return users.map((u) => u.id); - } - - if (isAdmin && !isOwner) { - const users = this.selection.filter( - (x) => - x.status === EmployeeStatus.Disabled && - x.id !== id && - !x.isAdmin && - !x.isOwner - ); - - return users.map((u) => u.id); - } - - return false; + return users.length > 0 ? users : false; } } diff --git a/packages/client/src/store/index.js b/packages/client/src/store/index.js index 83389d9cf8..d7f6e541fb 100644 --- a/packages/client/src/store/index.js +++ b/packages/client/src/store/index.js @@ -30,9 +30,12 @@ import selectFileDialogStore from "./SelectFileDialogStore"; import TagsStore from "./TagsStore"; import PeopleStore from "./PeopleStore"; import OformsStore from "./OformsStore"; +import AccessRights from "./AccessRights"; const oformsStore = new OformsStore(authStore); +const accessRights = new AccessRights(authStore); + const paymentStore = new PaymentStore(); const wizardStore = new WizardStore(); const setupStore = new SettingsSetupStore(); @@ -46,7 +49,8 @@ const ssoStore = new SsoFormStore(); const peopleStore = new PeopleStore( authStore, authStore.infoPanelStore, - setupStore + setupStore, + accessRights ); const tagsStore = new TagsStore(); @@ -166,6 +170,8 @@ const store = { tagsStore, peopleStore, + + accessRights, }; export default store;