feat: plugin permission

This commit is contained in:
Joel 2024-10-29 17:18:23 +08:00
parent 0dcbb34cab
commit 6f52edb157
7 changed files with 45 additions and 24 deletions

View File

@ -31,8 +31,8 @@ const PluginSettingModal: FC<Props> = ({
} }
}, [tempPrivilege]) }, [tempPrivilege])
const handleSave = useCallback(() => { const handleSave = useCallback(async () => {
onSave(tempPrivilege) await onSave(tempPrivilege)
onHide() onHide()
}, [tempPrivilege]) }, [tempPrivilege])
@ -49,8 +49,8 @@ const PluginSettingModal: FC<Props> = ({
</div> </div>
<div className='flex px-6 py-3 flex-col justify-center items-start gap-4 self-stretch'> <div className='flex px-6 py-3 flex-col justify-center items-start gap-4 self-stretch'>
{[ {[
{ title: t(`${i18nPrefix}.whoCanInstall`), key: 'canManagement', value: tempPrivilege.canManagement }, { title: t(`${i18nPrefix}.whoCanInstall`), key: 'install_permission', value: tempPrivilege.install_permission },
{ title: t(`${i18nPrefix}.whoCanDebug`), key: 'canDebugger', value: tempPrivilege.canDebugger }, { title: t(`${i18nPrefix}.whoCanDebug`), key: 'debug_permission', value: tempPrivilege.debug_permission },
].map(({ title, key, value }) => ( ].map(({ title, key, value }) => (
<div key={key} className='flex flex-col items-start gap-1 self-stretch'> <div key={key} className='flex flex-col items-start gap-1 self-stretch'>
<div className='flex h-6 items-center gap-0.5'> <div className='flex h-6 items-center gap-0.5'>

View File

@ -22,8 +22,8 @@ export type PluginPageContextValue = {
export const PluginPageContext = createContext<PluginPageContextValue>({ export const PluginPageContext = createContext<PluginPageContextValue>({
containerRef: { current: null }, containerRef: { current: null },
permissions: { permissions: {
canManagement: PermissionType.noOne, install_permission: PermissionType.noOne,
canDebugger: PermissionType.noOne, debug_permission: PermissionType.noOne,
}, },
setPermissions: () => { }, setPermissions: () => { },
}) })
@ -41,8 +41,8 @@ export const PluginPageContextProvider = ({
}: PluginPageContextProviderProps) => { }: PluginPageContextProviderProps) => {
const containerRef = useRef<HTMLDivElement>(null) const containerRef = useRef<HTMLDivElement>(null)
const [permissions, setPermissions] = useState<PluginPageContextValue['permissions']>({ const [permissions, setPermissions] = useState<PluginPageContextValue['permissions']>({
canManagement: PermissionType.noOne, install_permission: PermissionType.noOne,
canDebugger: PermissionType.noOne, debug_permission: PermissionType.noOne,
}) })
return ( return (

View File

@ -1,9 +1,13 @@
import { useEffect } from 'react' import { useEffect } from 'react'
import type { Permissions } from '../types'
import { PermissionType } from '../types' import { PermissionType } from '../types'
import { import {
usePluginPageContext, usePluginPageContext,
} from './context' } from './context'
import { useAppContext } from '@/context/app-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) => { const hasPermission = (permission: PermissionType, isAdmin: boolean) => {
if (permission === PermissionType.noOne) if (permission === PermissionType.noOne)
@ -16,23 +20,31 @@ const hasPermission = (permission: PermissionType, isAdmin: boolean) => {
} }
const usePermission = () => { const usePermission = () => {
const { t } = useTranslation()
const { isCurrentWorkspaceManager, isCurrentWorkspaceOwner } = useAppContext() const { isCurrentWorkspaceManager, isCurrentWorkspaceOwner } = useAppContext()
const [permissions, setPermissions] = usePluginPageContext(v => [v.permissions, v.setPermissions]) const [permissions, setPermissions] = usePluginPageContext(v => [v.permissions, v.setPermissions])
const isAdmin = isCurrentWorkspaceManager || isCurrentWorkspaceOwner const isAdmin = isCurrentWorkspaceManager || isCurrentWorkspaceOwner
useEffect(() => { const updatePermission = async (permission: Permissions) => {
// TODO: fetch permissions from server await doUpdatePermission(permission)
setPermissions({ setPermissions(permission)
canManagement: PermissionType.everyone, Toast.notify({
canDebugger: PermissionType.everyone, type: 'success',
message: t('common.api.actionSuccess'),
}) })
}
useEffect(() => {
(async () => {
const permission = await fetchPermission()
setPermissions(permission)
})()
}, []) }, [])
return { return {
canManagement: hasPermission(permissions.canManagement, isAdmin), canManagement: hasPermission(permissions.install_permission, isAdmin),
canDebugger: hasPermission(permissions.canDebugger, isAdmin), canDebugger: hasPermission(permissions.debug_permission, isAdmin),
canSetPermissions: isAdmin, canSetPermissions: isAdmin,
permissions, permissions,
setPermissions, setPermissions: updatePermission,
} }
} }

View File

@ -121,13 +121,13 @@ export type Plugin = {
export enum PermissionType { export enum PermissionType {
everyone = 'everyone', everyone = 'everyone',
admin = 'admin', admin = 'admins',
noOne = 'noOne', noOne = 'noone',
} }
export type Permissions = { export type Permissions = {
canManagement: PermissionType install_permission: PermissionType
canDebugger: PermissionType debug_permission: PermissionType
} }
export enum InstallStepFromGitHub { export enum InstallStepFromGitHub {

View File

@ -42,8 +42,8 @@ const translation = {
whoCanInstall: 'Who can install and manage plugins?', whoCanInstall: 'Who can install and manage plugins?',
whoCanDebug: 'Who can debug plugins?', whoCanDebug: 'Who can debug plugins?',
everyone: 'Everyone', everyone: 'Everyone',
admin: 'Admins', admins: 'Admins',
noOne: 'No one', noone: 'No one',
}, },
pluginInfoModal: { pluginInfoModal: {
title: 'Plugin info', title: 'Plugin info',

View File

@ -42,8 +42,8 @@ const translation = {
whoCanInstall: '谁可以安装和管理插件?', whoCanInstall: '谁可以安装和管理插件?',
whoCanDebug: '谁可以调试插件?', whoCanDebug: '谁可以调试插件?',
everyone: '所有人', everyone: '所有人',
admin: '管理员', admins: '管理员',
noOne: '无人', noone: '无人',
}, },
pluginInfoModal: { pluginInfoModal: {
title: '插件信息', title: '插件信息',

View File

@ -6,6 +6,7 @@ import type {
EndpointsRequest, EndpointsRequest,
EndpointsResponse, EndpointsResponse,
InstallPackageResponse, InstallPackageResponse,
Permissions,
PluginDeclaration, PluginDeclaration,
PluginManifestInMarket, PluginManifestInMarket,
TaskStatusResponse, TaskStatusResponse,
@ -101,3 +102,11 @@ export const fetchMarketplaceCollectionPlugins: Fetcher<MarketplaceCollectionPlu
export const checkTaskStatus = async (taskId: string) => { export const checkTaskStatus = async (taskId: string) => {
return get<TaskStatusResponse>(`/workspaces/current/plugin/tasks/${taskId}`) return get<TaskStatusResponse>(`/workspaces/current/plugin/tasks/${taskId}`)
} }
export const fetchPermission = async () => {
return get<Permissions>('/workspaces/current/plugin/permission/fetch')
}
export const updatePermission = async (permissions: Permissions) => {
return post('/workspaces/current/plugin/permission/change', { body: permissions })
}