ZH
测试版翻译

@core / rbac

1.0.1 ▾
已验证MIT
GitHub

基于 @core/auth 的角色与权限:通配符、仅限本人规则、防提权授予

代码10 个文件上下文约 615 个 token扫描通过

应用 .genpmignore 后将被注入的确切目录树。固定于

src/lib/rbac/adapters/hono.ts只读 · decbadd
// Adaptador Hono: `requirePermission('posts:publish')` después de `sessionMiddleware` de @core/auth.
import type { MiddlewareHandler } from 'hono';
import type { AuthVariables } from '../../auth/adapters/hono.ts';
import { can, type Grants, getGrants } from '../index.ts';

export type RbacVariables = AuthVariables & { grants: Grants | null };

/** 401 sin usuario, 403 sin permiso. Deja `c.get('grants')` para comprobaciones `:own` dentro del handler. */
export function requirePermission(permission: string): MiddlewareHandler<{ Variables: RbacVariables }> {
  return async (c, next) => {
    const user = c.get('user');
    if (!user) return c.json({ error: 'unauthorized' }, 401);
    const g = await getGrants(user.id);
    c.set('grants', g);
    if (!(await can(g, permission))) return c.json({ error: 'forbidden' }, 403);
    await next();
  };
}

举报 @core/rbac

使用 GitHub 登录后才能举报包。