1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

F #5422: Check user has access to view (#2097)

This commit is contained in:
Sergio Betanzos 2022-05-27 11:43:32 +02:00 committed by GitHub
parent dfd3094815
commit f78cbe83eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View File

@ -13,21 +13,28 @@
* See the License for the specific language governing permissions and *
* limitations under the License. *
* ------------------------------------------------------------------------- */
import { memo } from 'react'
import { memo, useMemo } from 'react'
import PropTypes from 'prop-types'
import { generatePath } from 'react-router-dom'
import { useViews } from 'client/features/Auth'
import { useGetUsersQuery } from 'client/features/OneApi/user'
import { useGetGroupsQuery } from 'client/features/OneApi/group'
import { List } from 'client/components/Tabs/Common'
import { T, SERVERADMIN_ID, ACTIONS } from 'client/constants'
import { T, SERVERADMIN_ID, ACTIONS, RESOURCE_NAMES } from 'client/constants'
import { PATH } from 'client/apps/sunstone/routesOne'
const { USER, GROUP } = RESOURCE_NAMES
const Ownership = memo(
({ actions, groupId, groupName, handleEdit, userId, userName }) => {
const { data: users = [] } = useGetUsersQuery()
const { data: groups = [] } = useGetGroupsQuery()
const { view, hasAccessToResource } = useViews()
const userAccess = useMemo(() => hasAccessToResource(USER), [view])
const groupAccess = useMemo(() => hasAccessToResource(GROUP), [view])
const getUserOptions = () =>
users
?.filter?.(({ ID } = {}) => ID !== SERVERADMIN_ID)
@ -44,7 +51,8 @@ const Ownership = memo(
name: T.Owner,
value: userName,
valueInOptionList: userId,
link: generatePath(PATH.SYSTEM.USERS.DETAIL, { id: userId }),
link:
userAccess && generatePath(PATH.SYSTEM.USERS.DETAIL, { id: userId }),
canEdit: actions?.includes?.(ACTIONS.CHANGE_OWNER),
handleGetOptionList: getUserOptions,
handleEdit: (_, user) => handleEdit?.({ user }),
@ -54,7 +62,9 @@ const Ownership = memo(
name: T.Group,
value: groupName,
valueInOptionList: groupId,
link: generatePath(PATH.SYSTEM.GROUPS.DETAIL, { id: groupId }),
link:
groupAccess &&
generatePath(PATH.SYSTEM.GROUPS.DETAIL, { id: groupId }),
canEdit: actions?.includes?.(ACTIONS.CHANGE_GROUP),
handleGetOptionList: getGroupOptions,
handleEdit: (_, group) => handleEdit?.({ group }),

View File

@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and *
* limitations under the License. *
* ------------------------------------------------------------------------- */
import { ReactElement } from 'react'
import { ReactElement, useMemo } from 'react'
import PropTypes from 'prop-types'
import { generatePath } from 'react-router-dom'
import { Stack } from '@mui/material'
import { useViews } from 'client/features/Auth'
import { useGetClusterQuery } from 'client/features/OneApi/cluster'
import { useRenameVmMutation } from 'client/features/OneApi/vm'
@ -37,9 +38,11 @@ import {
levelLockToString,
timeToString,
} from 'client/models/Helper'
import { T, VM, VM_ACTIONS } from 'client/constants'
import { T, VM, VM_ACTIONS, RESOURCE_NAMES } from 'client/constants'
import { PATH } from 'client/apps/sunstone/routesOne'
const { CLUSTER, HOST } = RESOURCE_NAMES
/**
* Renders mainly information tab.
*
@ -51,6 +54,10 @@ import { PATH } from 'client/apps/sunstone/routesOne'
const InformationPanel = ({ vm = {}, actions }) => {
const [renameVm] = useRenameVmMutation()
const { view, hasAccessToResource } = useViews()
const clusterAccess = useMemo(() => hasAccessToResource(CLUSTER), [view])
const hostAccess = useMemo(() => hasAccessToResource(HOST), [view])
const { ID, NAME, RESCHED, STIME, ETIME, LOCK, DEPLOY_ID } = vm
const { name: stateName, color: stateColor } = getState(vm)
@ -137,6 +144,7 @@ const InformationPanel = ({ vm = {}, actions }) => {
name: T.Host,
value: `#${hostId} ${hostname}`,
link:
hostAccess &&
!Number.isNaN(+hostId) &&
generatePath(PATH.INFRASTRUCTURE.HOSTS.DETAIL, { id: hostId }),
dataCy: 'hostid',
@ -145,6 +153,7 @@ const InformationPanel = ({ vm = {}, actions }) => {
name: T.Cluster,
value: `#${clusterId} ${clusterName}`,
link:
clusterAccess &&
!Number.isNaN(+clusterId) &&
generatePath(PATH.INFRASTRUCTURE.CLUSTERS.DETAIL, { id: clusterId }),
dataCy: 'clusterid',