SDK: assigning a role to a policy when creating or updating it, always fails
维护者通常 2 天内回复
@hola-soy-milk 已经在做这个了。
开始于 2025年4月15日。
评估
这个 Issue 还没有评估数据。
描述
Describe the Bug
I'm creating a series of scripts to set up multi-tenancy programmatically.
I'm using the SDK v19.1.0.
ISSUE
Assigning a role when creating/updating a policy always makes the call fail despite the policy object having a roles property (so I'm assuming it's doable)
https://docs.directus.io/reference/system/policies.html
Similarly, assigning a policy to a role makes it fail despite the role object having a policies property
https://docs.directus.io/reference/system/roles.html
So it seems like the problem is related to creating the necessary relationship in the directus_access table.
Functions I tested that present the same problem:
- createPolicy
- createPolicies
- updatePolicy
- updateRole
To Reproduce
Test script to replicate the issue when creating a policy assigned to a specific role:
require('dotenv').config();
const DIRECTUS_URL = process.env.DIRECTUS_URL;
const DIRECTUS_ADMIN_EMAIL = process.env.DIRECTUS_ADMIN_EMAIL;
const DIRECTUS_ADMIN_PASSWORD = process.env.DIRECTUS_ADMIN_PASSWORD;
const userRole = {
id: 'replace-with-existing-role-id',
name: 'User'
};
const {
createDirectus,
rest,
login,
staticToken,
createPolicy,
} = require('@directus/sdk'); // "@directus/sdk": "^19.1.0"
async function setupPolicies() {
try {
console.log('Starting policies setup...');
// Get the token using a temporary client
console.log('Authenticating with Directus...');
const tempClient = createDirectus(DIRECTUS_URL)
.with(rest());
let loginResult;
try {
loginResult = await tempClient.request(
login(DIRECTUS_ADMIN_EMAIL, DIRECTUS_ADMIN_PASSWORD)
);
console.log('Successfully authenticated with Directus');
} catch (error) {
console.error('Failed to authenticate with Directus:', error);
throw error;
}
// Create a new client with the static token
const directus = createDirectus(DIRECTUS_URL)
.with(staticToken(loginResult.access_token))
.with(rest());
console.log('Setting up policy for User role...');
try {
const policyName = 'Read Own User Profile';
const policyDescription = 'Allows users to read their own user profile';
console.log(`Creating new policy "${policyName}" for role ${userRole.name} (${userRole.id})`);
const policy = await directus.request(createPolicy({
name: policyName,
description: policyDescription,
admin_access: false,
app_access: false,
roles: [userRole.id],
}));
console.log(`Successfully created policy with ID: ${policy.id}`);
} catch (error) {
console.error('Failed to set up User role policies:', error);
throw error;
}
} catch (error) {
console.error('Fatal error during policies setup:', error);
throw error;
}
}
// Run the setup
setupPolicies()
.then(() => console.log('Setup completed successfully'))
.catch(error => {
console.error('Setup failed:', error);
process.exit(1);
});
Directus Version
v11.6.1
Hosting Strategy
Self-Hosted (Docker Image)
Database
PostgreSQL 17.4
- 主要语言
- TypeScript
- 星标
- 24
- 派生
- 90
- 平均合并
- 3 天 22 小时
- 30 天内合并 PR
- 19
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
directus/docs 的其他 Issue
-
Docs Platform
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 2 天内回复
-
llms.txt links agents to /raw/ copies where partials are empty; the inlined .md copies aren't linked未关闭
难度 3/5 1-2 天 新手友好度 67/100
维护者通常 2 天内回复
-
Document required special flags per field type可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 2/5 1-3 小时 新手友好度 58/100
维护者通常 2 天内回复
-
难度 3/5 1-2 天 新手友好度 55/100
维护者通常 2 天内回复
-
content-improvement
难度 5/5 一周以上 新手友好度 35/100
维护者通常 2 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
ranxianglei/billion-context#7597 ·
维护者通常 1 天内回复
-
kubernetes-ingestor: fetchCRDMapping() runs even when crossplane.enabled is false, logging a 403 ERROR every run可能已有人在做 @kc9ddi 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
TeraSky-OSS/backstage-plugins#228 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
zmkfirmware/zmk#3571 ·
维护者通常 10 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
-
[Windows] Hidden title bar: a maximized window restored from minimized on a secondary monitor gets the primary monitor's work area as its client area可能已有人在做 @lijy91 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 67/100
libnativeapi/nativeapi#53 · 1 个 reaction ·
维护者通常 1 天内回复