mirror of
https://github.com/langgenius/dify.git
synced 2026-05-06 02:18:08 +08:00
feat: plugin permission
This commit is contained in:
@ -1,9 +1,13 @@
|
||||
import { useEffect } from 'react'
|
||||
import type { Permissions } from '../types'
|
||||
import { PermissionType } from '../types'
|
||||
import {
|
||||
usePluginPageContext,
|
||||
} from './context'
|
||||
import { useAppContext } from '@/context/app-context'
|
||||
import { updatePermission as doUpdatePermission, fetchPermission } from '@/service/plugins'
|
||||
import Toast from '../../base/toast'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
const hasPermission = (permission: PermissionType, isAdmin: boolean) => {
|
||||
if (permission === PermissionType.noOne)
|
||||
@ -16,23 +20,31 @@ const hasPermission = (permission: PermissionType, isAdmin: boolean) => {
|
||||
}
|
||||
|
||||
const usePermission = () => {
|
||||
const { t } = useTranslation()
|
||||
const { isCurrentWorkspaceManager, isCurrentWorkspaceOwner } = useAppContext()
|
||||
const [permissions, setPermissions] = usePluginPageContext(v => [v.permissions, v.setPermissions])
|
||||
const isAdmin = isCurrentWorkspaceManager || isCurrentWorkspaceOwner
|
||||
|
||||
useEffect(() => {
|
||||
// TODO: fetch permissions from server
|
||||
setPermissions({
|
||||
canManagement: PermissionType.everyone,
|
||||
canDebugger: PermissionType.everyone,
|
||||
const updatePermission = async (permission: Permissions) => {
|
||||
await doUpdatePermission(permission)
|
||||
setPermissions(permission)
|
||||
Toast.notify({
|
||||
type: 'success',
|
||||
message: t('common.api.actionSuccess'),
|
||||
})
|
||||
}
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
const permission = await fetchPermission()
|
||||
setPermissions(permission)
|
||||
})()
|
||||
}, [])
|
||||
return {
|
||||
canManagement: hasPermission(permissions.canManagement, isAdmin),
|
||||
canDebugger: hasPermission(permissions.canDebugger, isAdmin),
|
||||
canManagement: hasPermission(permissions.install_permission, isAdmin),
|
||||
canDebugger: hasPermission(permissions.debug_permission, isAdmin),
|
||||
canSetPermissions: isAdmin,
|
||||
permissions,
|
||||
setPermissions,
|
||||
setPermissions: updatePermission,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user