Skip to content

Commit

Permalink
feat(webui): authority checks
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 31, 2021
1 parent c3270c4 commit a9b4cf4
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 28 deletions.
12 changes: 8 additions & 4 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ function isBailed(value: any) {
return value !== null && value !== false && value !== undefined
}

function safeRequire(id: string) {
try {
return require(id)
} catch {}
}

type Filter = (session: Session) => boolean
type PartialSeletor<T> = (...values: T[]) => Context

Expand Down Expand Up @@ -165,13 +171,11 @@ export class Context {
}

with(dependency: string, callback: Plugin) {
let parent: Plugin
try {
parent = require(dependency)
} catch {}
const parent = safeRequire(dependency)
if (!parent) return
this.teleport(parent, callback)
this.on('plugin-added', (added) => {
const parent = safeRequire(dependency)
if (added === parent) this.teleport(parent, callback)
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-chat/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import 'vite/dynamic-import-polyfill'
router.addRoute({
path: '/chat',
name: '聊天',
meta: { icon: 'comments', require: ['user'] },
meta: { icon: 'comments', require: ['user'], authority: 4 },
component: Chat,
})
4 changes: 2 additions & 2 deletions packages/plugin-chat/src/debug.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Bot, Context, Session } from 'koishi-core'
import { Logger, segment, Time, interpolate, pick } from 'koishi-utils'

export interface DebugOptions {
export interface DebugConfig {
formatSend?: string
formatReceive?: string
includeUsers?: string[]
Expand Down Expand Up @@ -64,7 +64,7 @@ async function getChannelName(bot: Bot, channelId: string) {
}
}

export default function apply(ctx: Context, config: DebugOptions = {}) {
export default function apply(ctx: Context, config: DebugConfig = {}) {
const {
formatSend = '[{{ channelName }}] {{ content }}',
formatReceive = '[{{ channelName }}] {{ username }}: {{ content }}',
Expand Down
17 changes: 15 additions & 2 deletions packages/plugin-chat/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { Context } from 'koishi-core'
import {} from 'koishi-plugin-webui'
import { resolve } from 'path'
import debug, { DebugConfig } from './debug'

export interface Config extends DebugConfig {}

export const name = 'chat'

export function apply(ctx: Context) {
ctx.with('koishi-plugin-webui', () => {})
export function apply(ctx: Context, options: Config = {}) {
ctx.plugin(debug, options)
ctx.with('koishi-plugin-webui', () => {
const { config, entries } = ctx.app.webui

const filename = resolve(__dirname, config.devMode ? '../client' : '../dist/index.js')
entries.chat = filename
ctx.before('disconnect', () => {
delete entries.chat
})
})
}
11 changes: 5 additions & 6 deletions packages/plugin-teach/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ export function apply(ctx: Context, config: Config = {}) {
ctx.plugin(writer, config)

ctx.with('koishi-plugin-webui', (ctx) => {
const { webui } = ctx.app
const { stats, meta } = webui.sources
const { config, entries } = ctx.app.webui
const { stats, meta } = ctx.app.webui.sources

ctx.on('dialogue/before-send', ({ session, dialogue }) => {
session._sendType = 'dialogue'
Expand Down Expand Up @@ -236,11 +236,10 @@ export function apply(ctx: Context, config: Config = {}) {
payload.questions = Object.values(questionMap)
})

const filename = resolve(__dirname, webui.config.devMode ? '../client' : '../dist/index.js')
webui.entries['teach.js'] = filename

const filename = resolve(__dirname, config.devMode ? '../client' : '../dist/index.js')
entries.teach = filename
ctx.before('disconnect', () => {
delete webui.entries['teach.js']
delete entries.teach
})
})
}
2 changes: 1 addition & 1 deletion packages/plugin-webui/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare module 'vue-router' {
interface RouteMeta {
icon?: string
hidden?: boolean
authorize?: boolean
authority?: number
frameless?: boolean
require?: Keys<typeof client, Ref>[]
}
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-webui/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ router.addRoute({
router.addRoute({
path: '/sandbox',
name: '沙盒',
meta: { icon: 'laptop-code', require: ['user'] },
meta: { icon: 'laptop-code', authority: 1 },
component: () => import('./views/sandbox.vue'),
})

router.addRoute({
path: '/profile',
name: '资料',
meta: { icon: 'user-circle', require: ['user'], hidden: true },
meta: { icon: 'user-circle', authority: 1, hidden: true },
component: () => import('./views/profile.vue'),
})

Expand Down Expand Up @@ -77,7 +77,7 @@ receive('expire', () => {
})

router.beforeEach((route) => {
if (route.meta.require?.includes('user') && !user.value) {
if (route.meta.authority && !user.value) {
return '/login'
}
})
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-webui/client/views/layout/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<sidebar/>
</template>
<main :class="{ frameless }">
<router-view v-if="loaded"/>
<p v-if="invalid">权限不足。</p>
<router-view v-else-if="loaded"/>
<p v-else>正在加载数据……</p>
</main>
</template>
Expand All @@ -20,6 +21,7 @@ import { useRoute } from 'vue-router'
const route = useRoute()
const frameless = computed(() => route.meta.frameless)
const loaded = computed(() => (route.meta.require || []).every((key) => client[key].value))
const invalid = computed(() => route.meta.authority > client.user.value.authority)
</script>

Expand Down
20 changes: 15 additions & 5 deletions packages/plugin-webui/client/views/layout/sidebar.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<template>
<aside>
<ul>
<template v-for="(route, index) in $router.getRoutes()" :key="index">
<li v-if="!route.meta.hidden" :class="{ current: route.name === $route.name }">
<router-link :to="route.path">
<i :class="`fas fa-${route.meta.icon}`"/>
{{ route.name }}
<template v-for="({ name, path, meta }, index) in $router.getRoutes()" :key="index">
<li v-if="isShown(meta)" :class="{ current: name === $route.name }">
<router-link :to="path">
<i :class="`fas fa-${meta.icon}`"/>
{{ name }}
</router-link>
</li>
</template>
Expand All @@ -14,6 +14,16 @@
</template>

<script lang="ts" setup>
import { user } from '~/client'
import type { RouteMeta } from 'vue-router'
function isShown(meta: RouteMeta) {
if (meta.hidden) return false
if (meta.authority && meta.authority > 1 && meta.authority > user.value?.authority) return false
return true
}
</script>

<style lang="scss">
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-webui/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, Channel, App, Argv, User } from 'koishi-core'
import { Context, Channel, Argv, User } from 'koishi-core'
import { interpolate, Time } from 'koishi-utils'
import { Meta } from './data'
import { Statistics, Synchronizer } from './stats'
import { Synchronizer } from './stats'
import { SandboxBot } from './adapter'
import { WebServer } from './server'

Expand Down Expand Up @@ -65,7 +65,7 @@ User.extend(() => ({
expire: 0,
}))

export interface Config extends WebServer.Config, Statistics.Config {
export interface Config extends WebServer.Config {
format?: string
formatBot?: string
}
Expand Down

0 comments on commit a9b4cf4

Please sign in to comment.