adula 0.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +10 -0
- package/README.md +38 -2
- package/build/agent/AGENTS.template.md +28 -0
- package/build/agent/capabilities.md +60 -0
- package/build/agent/skills/adula-frontend-design/SKILL.md +59 -0
- package/build/agent/skills/idea-review/SKILL.md +31 -0
- package/build/agent/skills/module-review/SKILL.md +22 -0
- package/build/agent/skills/perf-review/SKILL.md +24 -0
- package/build/agent/skills/schema-review/SKILL.md +22 -0
- package/build/agent/skills/security-review/SKILL.md +24 -0
- package/build/agent/skills/ui-review/SKILL.md +22 -0
- package/build/commands/capabilities.d.ts +10 -0
- package/build/commands/capabilities.js +40 -0
- package/build/commands/doctor.d.ts +9 -0
- package/build/commands/doctor.js +72 -0
- package/build/commands/gaps.d.ts +12 -0
- package/build/commands/gaps.js +62 -0
- package/build/commands/install.d.ts +9 -0
- package/build/commands/install.js +105 -0
- package/build/commands/main.d.ts +8 -0
- package/build/commands/main.js +33 -0
- package/build/commands/module_add.d.ts +8 -0
- package/build/commands/module_add.js +57 -0
- package/build/commands/module_remove.d.ts +10 -0
- package/build/commands/module_remove.js +94 -0
- package/build/commands/resource.d.ts +8 -0
- package/build/commands/resource.js +29 -0
- package/build/commands/resource_snapshot.d.ts +15 -0
- package/build/commands/resource_snapshot.js +61 -0
- package/build/commands/storage_migrate.d.ts +12 -0
- package/build/commands/storage_migrate.js +39 -0
- package/build/commands/ui.d.ts +11 -0
- package/build/commands/ui.js +210 -0
- package/build/commands/upgrade.d.ts +15 -0
- package/build/commands/upgrade.js +34 -0
- package/build/configure.d.ts +2 -0
- package/build/configure.js +79 -0
- package/build/database/migrations/1770000000000_core.d.ts +5 -0
- package/build/database/migrations/1770000000000_core.js +10 -0
- package/build/database/migrations/1770000000001_attachments.d.ts +5 -0
- package/build/database/migrations/1770000000001_attachments.js +10 -0
- package/build/database/migrations/1770000000002_saved_views.d.ts +5 -0
- package/build/database/migrations/1770000000002_saved_views.js +10 -0
- package/build/database/migrations/1770000000003_user_invitations.d.ts +5 -0
- package/build/database/migrations/1770000000003_user_invitations.js +18 -0
- package/build/database/migrations/1770000000004_collaboration.d.ts +5 -0
- package/build/database/migrations/1770000000004_collaboration.js +10 -0
- package/build/database/migrations/1770000000005_assignments.d.ts +5 -0
- package/build/database/migrations/1770000000005_assignments.js +10 -0
- package/build/database/migrations/1770000000006_messaging.d.ts +5 -0
- package/build/database/migrations/1770000000006_messaging.js +10 -0
- package/build/database/migrations/1770000000007_webhooks.d.ts +5 -0
- package/build/database/migrations/1770000000007_webhooks.js +10 -0
- package/build/database/migrations/1770000000008_imports.d.ts +5 -0
- package/build/database/migrations/1770000000008_imports.js +10 -0
- package/build/database/migrations/1770000000010_workflows.d.ts +5 -0
- package/build/database/migrations/1770000000010_workflows.js +10 -0
- package/build/database/migrations/1770000000011_managed_assignments.d.ts +5 -0
- package/build/database/migrations/1770000000011_managed_assignments.js +10 -0
- package/build/database/migrations/1770000000012_role_keys.d.ts +5 -0
- package/build/database/migrations/1770000000012_role_keys.js +10 -0
- package/build/database/migrations/1770000000013_notification_targets.d.ts +5 -0
- package/build/database/migrations/1770000000013_notification_targets.js +10 -0
- package/build/database/migrations/1770000000014_upload_grants.d.ts +5 -0
- package/build/database/migrations/1770000000014_upload_grants.js +10 -0
- package/build/database/migrations/1770000000015_inbound_webhooks.d.ts +5 -0
- package/build/database/migrations/1770000000015_inbound_webhooks.js +10 -0
- package/build/database/migrations/1770000000016_adulajs_names.d.ts +6 -0
- package/build/database/migrations/1770000000016_adulajs_names.js +11 -0
- package/build/index.d.ts +80 -0
- package/build/index.js +49 -0
- package/build/providers/adula_provider.d.ts +6 -0
- package/build/providers/adula_provider.js +10 -0
- package/build/src/admin/contracts.d.ts +19 -0
- package/build/src/admin/contracts.js +115 -0
- package/build/src/admin/controller.d.ts +32 -0
- package/build/src/admin/controller.js +167 -0
- package/build/src/admin/errors.d.ts +5 -0
- package/build/src/admin/errors.js +9 -0
- package/build/src/admin/presentation.d.ts +28 -0
- package/build/src/admin/presentation.js +1 -0
- package/build/src/admin/record_title.d.ts +14 -0
- package/build/src/admin/record_title.js +50 -0
- package/build/src/admin/resource_service.d.ts +376 -0
- package/build/src/admin/resource_service.js +1448 -0
- package/build/src/attachments/attachment_service.d.ts +100 -0
- package/build/src/attachments/attachment_service.js +270 -0
- package/build/src/attachments/doctor.d.ts +4 -0
- package/build/src/attachments/doctor.js +28 -0
- package/build/src/attachments/storage_migrate.d.ts +39 -0
- package/build/src/attachments/storage_migrate.js +64 -0
- package/build/src/attachments/upload_grants.d.ts +44 -0
- package/build/src/attachments/upload_grants.js +105 -0
- package/build/src/auth/ability.d.ts +24 -0
- package/build/src/auth/ability.js +34 -0
- package/build/src/auth/actor_store.d.ts +14 -0
- package/build/src/auth/actor_store.js +70 -0
- package/build/src/auth/conditions.d.ts +26 -0
- package/build/src/auth/conditions.js +117 -0
- package/build/src/auth/packed_rules.d.ts +4 -0
- package/build/src/auth/packed_rules.js +13 -0
- package/build/src/auth/sql.d.ts +11 -0
- package/build/src/auth/sql.js +118 -0
- package/build/src/collaboration/assignments.d.ts +129 -0
- package/build/src/collaboration/assignments.js +333 -0
- package/build/src/collaboration/record_collaboration.d.ts +86 -0
- package/build/src/collaboration/record_collaboration.js +348 -0
- package/build/src/commands/agent_assets.d.ts +7 -0
- package/build/src/commands/agent_assets.js +66 -0
- package/build/src/commands/capabilities.d.ts +19 -0
- package/build/src/commands/capabilities.js +179 -0
- package/build/src/commands/doctor.d.ts +42 -0
- package/build/src/commands/doctor.js +375 -0
- package/build/src/commands/gap_report.d.ts +56 -0
- package/build/src/commands/gap_report.js +110 -0
- package/build/src/commands/generator.d.ts +1 -0
- package/build/src/commands/generator.js +70 -0
- package/build/src/commands/snapshot.d.ts +28 -0
- package/build/src/commands/snapshot.js +48 -0
- package/build/src/commands/source_markers.d.ts +11 -0
- package/build/src/commands/source_markers.js +41 -0
- package/build/src/core/activity.d.ts +43 -0
- package/build/src/core/activity.js +77 -0
- package/build/src/core/administration_guard.d.ts +3 -0
- package/build/src/core/administration_guard.js +39 -0
- package/build/src/core/health.d.ts +42 -0
- package/build/src/core/health.js +44 -0
- package/build/src/core/mail_test.d.ts +18 -0
- package/build/src/core/mail_test.js +88 -0
- package/build/src/core/message_templates.d.ts +83 -0
- package/build/src/core/message_templates.js +293 -0
- package/build/src/core/module_seed.d.ts +15 -0
- package/build/src/core/module_seed.js +31 -0
- package/build/src/core/notifications.d.ts +42 -0
- package/build/src/core/notifications.js +91 -0
- package/build/src/core/org_units.d.ts +30 -0
- package/build/src/core/org_units.js +123 -0
- package/build/src/core/roles.d.ts +98 -0
- package/build/src/core/roles.js +447 -0
- package/build/src/core/saved_views.d.ts +37 -0
- package/build/src/core/saved_views.js +164 -0
- package/build/src/core/settings.d.ts +29 -0
- package/build/src/core/settings.js +113 -0
- package/build/src/core/setup.d.ts +14 -0
- package/build/src/core/setup.js +89 -0
- package/build/src/core/ui_preferences.d.ts +12 -0
- package/build/src/core/ui_preferences.js +30 -0
- package/build/src/core/user_invitations.d.ts +23 -0
- package/build/src/core/user_invitations.js +144 -0
- package/build/src/core/users.d.ts +44 -0
- package/build/src/core/users.js +182 -0
- package/build/src/database/configure.d.ts +3 -0
- package/build/src/database/configure.js +41 -0
- package/build/src/database/schema.d.ts +52 -0
- package/build/src/database/schema.js +538 -0
- package/build/src/eslint/index.d.ts +3 -0
- package/build/src/eslint/index.js +90 -0
- package/build/src/events/adonis_jobs.d.ts +9 -0
- package/build/src/events/adonis_jobs.js +13 -0
- package/build/src/events/outbox.d.ts +19 -0
- package/build/src/events/outbox.js +33 -0
- package/build/src/events/record_mutation.d.ts +24 -0
- package/build/src/events/record_mutation.js +62 -0
- package/build/src/integrations/imports.d.ts +74 -0
- package/build/src/integrations/imports.js +333 -0
- package/build/src/integrations/inbound_webhooks.d.ts +94 -0
- package/build/src/integrations/inbound_webhooks.js +276 -0
- package/build/src/integrations/openapi.d.ts +39 -0
- package/build/src/integrations/openapi.js +323 -0
- package/build/src/integrations/print.d.ts +37 -0
- package/build/src/integrations/print.js +124 -0
- package/build/src/integrations/webhooks.d.ts +98 -0
- package/build/src/integrations/webhooks.js +298 -0
- package/build/src/mcp/tools.d.ts +8 -0
- package/build/src/mcp/tools.js +156 -0
- package/build/src/org/org_service.d.ts +2 -0
- package/build/src/org/org_service.js +16 -0
- package/build/src/resource/define_resource.d.ts +5 -0
- package/build/src/resource/define_resource.js +68 -0
- package/build/src/resource/registry.d.ts +12 -0
- package/build/src/resource/registry.js +101 -0
- package/build/src/resource/types.d.ts +155 -0
- package/build/src/resource/types.js +1 -0
- package/build/src/resource/values.d.ts +5 -0
- package/build/src/resource/values.js +88 -0
- package/build/src/services/backup.d.ts +14 -0
- package/build/src/services/backup.js +59 -0
- package/build/src/services/settings.d.ts +21 -0
- package/build/src/services/settings.js +30 -0
- package/build/src/upgrade/legacy.d.ts +27 -0
- package/build/src/upgrade/legacy.js +58 -0
- package/build/src/upgrade/rename.d.ts +17 -0
- package/build/src/upgrade/rename.js +94 -0
- package/build/src/version.d.ts +1 -0
- package/build/src/version.js +2 -0
- package/build/src/workflows/define_workflow.d.ts +124 -0
- package/build/src/workflows/define_workflow.js +123 -0
- package/build/src/workflows/engine.d.ts +141 -0
- package/build/src/workflows/engine.js +752 -0
- package/build/stubs/README.md +6 -0
- package/build/stubs/controller.stub +8 -0
- package/build/stubs/main.d.ts +5 -0
- package/build/stubs/main.js +5 -0
- package/build/stubs/main.ts +5 -0
- package/build/stubs/modules.stub +6 -0
- package/build/stubs/resource_contract.stub +1 -0
- package/build/stubs/resource_contract.txt +452 -0
- package/build/stubs/routes.stub +16 -0
- package/build/stubs/service.stub +9 -0
- package/package.json +95 -6
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
import type { ResourceRegistry } from '../resource/registry.js';
|
|
3
|
+
import type { Label, ModuleRole } from '../resource/types.js';
|
|
4
|
+
import type { Conditions } from '../auth/conditions.js';
|
|
5
|
+
export type RoleSummary = {
|
|
6
|
+
id: number;
|
|
7
|
+
/**
|
|
8
|
+
* Stable identifier addressed by workflows and module defaults. The display name
|
|
9
|
+
* can change freely; the key is set once.
|
|
10
|
+
*/
|
|
11
|
+
key: string | null;
|
|
12
|
+
name: string;
|
|
13
|
+
permissionLevel: number;
|
|
14
|
+
rules: number;
|
|
15
|
+
users: number;
|
|
16
|
+
};
|
|
17
|
+
export type RoleRule = {
|
|
18
|
+
id: number;
|
|
19
|
+
subject: string;
|
|
20
|
+
action: string;
|
|
21
|
+
inverted: boolean;
|
|
22
|
+
conditions: Conditions | null;
|
|
23
|
+
fields: string[] | null;
|
|
24
|
+
};
|
|
25
|
+
export type RoleDetail = Omit<RoleSummary, 'rules'> & {
|
|
26
|
+
rules: RoleRule[];
|
|
27
|
+
};
|
|
28
|
+
export type RuleInput = {
|
|
29
|
+
subject: string;
|
|
30
|
+
action: string;
|
|
31
|
+
inverted?: boolean;
|
|
32
|
+
conditions?: Conditions | null;
|
|
33
|
+
fields?: string[] | null;
|
|
34
|
+
};
|
|
35
|
+
export type MatrixField = {
|
|
36
|
+
key: string;
|
|
37
|
+
label: Label;
|
|
38
|
+
type: string;
|
|
39
|
+
conditionable: boolean;
|
|
40
|
+
/** Accepts the current user ("$actor.id") as a condition value: user fields, createdBy, updatedBy. */
|
|
41
|
+
actor?: boolean;
|
|
42
|
+
};
|
|
43
|
+
export type MatrixSubject = {
|
|
44
|
+
name: string;
|
|
45
|
+
label: Label;
|
|
46
|
+
actions: string[];
|
|
47
|
+
fields: MatrixField[];
|
|
48
|
+
/** Standard columns the resource actually has plus its conditionable fields. */
|
|
49
|
+
conditionFields: MatrixField[];
|
|
50
|
+
};
|
|
51
|
+
export type RoleMatrix = {
|
|
52
|
+
actions: string[];
|
|
53
|
+
subjects: MatrixSubject[];
|
|
54
|
+
};
|
|
55
|
+
export declare const ALL_SUBJECT_LABEL: Label;
|
|
56
|
+
/** A role key: lower-case letters, digits and underscores, starting with a letter. */
|
|
57
|
+
export declare function roleKey(value: unknown): string;
|
|
58
|
+
export declare class RolesAdmin {
|
|
59
|
+
private db;
|
|
60
|
+
private registry;
|
|
61
|
+
constructor(db: Knex, registry: ResourceRegistry);
|
|
62
|
+
list(): Promise<RoleSummary[]>;
|
|
63
|
+
get(id: number): Promise<RoleDetail>;
|
|
64
|
+
create(actorId: number, input: {
|
|
65
|
+
name: string;
|
|
66
|
+
key?: string | null;
|
|
67
|
+
permissionLevel?: number;
|
|
68
|
+
}): Promise<{
|
|
69
|
+
id: number;
|
|
70
|
+
key: string | null;
|
|
71
|
+
name: string;
|
|
72
|
+
permissionLevel: number;
|
|
73
|
+
}>;
|
|
74
|
+
/**
|
|
75
|
+
* Gives a role its stable key. A key is set once: workflows and module defaults
|
|
76
|
+
* depend on it, so changing it would silently detach them.
|
|
77
|
+
*/
|
|
78
|
+
setKey(actorId: number, id: number, value: string): Promise<void>;
|
|
79
|
+
rename(actorId: number, id: number, value: string): Promise<void>;
|
|
80
|
+
setPermissionLevel(actorId: number, id: number, value: number): Promise<void>;
|
|
81
|
+
delete(actorId: number, id: number): Promise<void>;
|
|
82
|
+
/** Subjects × actions derived from the registry; the UI never hard-codes resources. */
|
|
83
|
+
matrix(): RoleMatrix;
|
|
84
|
+
setRule(actorId: number, roleId: number, input: RuleInput): Promise<RoleRule>;
|
|
85
|
+
removeRule(actorId: number, roleId: number, ruleId: number): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Creates module default roles that no role holds the key of yet, with their rules
|
|
88
|
+
* validated exactly like setRule. Existing roles are never modified. A keyless role
|
|
89
|
+
* with the same display name is adopted by receiving the key, rules untouched.
|
|
90
|
+
*/
|
|
91
|
+
ensureDefaults(actorId: number, roles: readonly ModuleRole[]): Promise<{
|
|
92
|
+
created: string[];
|
|
93
|
+
adopted: string[];
|
|
94
|
+
kept: string[];
|
|
95
|
+
}>;
|
|
96
|
+
private validateRule;
|
|
97
|
+
private find;
|
|
98
|
+
}
|
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
import { conditionSql } from '../auth/sql.js';
|
|
2
|
+
import { resolveActorConditions, usesActor } from '../auth/conditions.js';
|
|
3
|
+
import { AdulaError } from '../admin/errors.js';
|
|
4
|
+
import { logActivity } from './activity.js';
|
|
5
|
+
import { administrationGuard } from './administration_guard.js';
|
|
6
|
+
const RESOURCE = 'core.roles';
|
|
7
|
+
const ACTION_ORDER = ['view', 'create', 'update', 'delete', 'submit', 'cancel', 'amend'];
|
|
8
|
+
export const ALL_SUBJECT_LABEL = { ar: 'كل الكيانات', en: 'All resources' };
|
|
9
|
+
function standardConditionFields(resource) {
|
|
10
|
+
const field = (key, ar, en) => ({
|
|
11
|
+
key,
|
|
12
|
+
label: { ar, en },
|
|
13
|
+
type: 'integer',
|
|
14
|
+
conditionable: true,
|
|
15
|
+
...(['createdBy', 'updatedBy'].includes(key) ? { actor: true } : {}),
|
|
16
|
+
});
|
|
17
|
+
return [
|
|
18
|
+
field('id', 'المعرّف', 'ID'),
|
|
19
|
+
field('createdBy', 'أنشأه', 'Created by'),
|
|
20
|
+
field('updatedBy', 'عدّله', 'Updated by'),
|
|
21
|
+
...(resource.scoped ? [field('orgUnitId', 'الوحدة التنظيمية', 'Organization unit')] : []),
|
|
22
|
+
...(resource.submittable ? [field('docStatus', 'حالة المستند', 'Document status')] : []),
|
|
23
|
+
...(resource.version ? [field('version', 'الإصدار', 'Version')] : []),
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
function matrixFields(resource) {
|
|
27
|
+
return Object.entries(resource.fields).map(([key, field]) => ({
|
|
28
|
+
key,
|
|
29
|
+
label: field.label,
|
|
30
|
+
type: field.type,
|
|
31
|
+
conditionable: !['hasMany', 'json', 'attachment'].includes(field.type),
|
|
32
|
+
...(field.type === 'user' ? { actor: true } : {}),
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
function roleName(value) {
|
|
36
|
+
const name = typeof value === 'string' ? value.trim() : '';
|
|
37
|
+
if (!name || name.length > 100)
|
|
38
|
+
throw new AdulaError(422, 'E_ROLE_NAME', 'اسم الدور مطلوب (حتى 100 حرف)');
|
|
39
|
+
return name;
|
|
40
|
+
}
|
|
41
|
+
/** A role key: lower-case letters, digits and underscores, starting with a letter. */
|
|
42
|
+
export function roleKey(value) {
|
|
43
|
+
const key = typeof value === 'string' ? value.trim() : '';
|
|
44
|
+
if (!/^[a-z][a-z0-9_]{0,99}$/.test(key))
|
|
45
|
+
throw new AdulaError(422, 'E_ROLE_KEY', 'مفتاح الدور حروف إنجليزية صغيرة وأرقام وشرطة سفلية، ويبدأ بحرف (مثل project_manager)');
|
|
46
|
+
return key;
|
|
47
|
+
}
|
|
48
|
+
function permissionLevel(value) {
|
|
49
|
+
const level = Number(value ?? 0);
|
|
50
|
+
if (!Number.isSafeInteger(level) || level < 0 || level > 9)
|
|
51
|
+
throw new AdulaError(422, 'E_ROLE_LEVEL', 'مستوى الصلاحية رقم بين 0 و9');
|
|
52
|
+
return level;
|
|
53
|
+
}
|
|
54
|
+
function toRule(row) {
|
|
55
|
+
return {
|
|
56
|
+
id: Number(row.id),
|
|
57
|
+
subject: String(row.subject),
|
|
58
|
+
action: String(row.action),
|
|
59
|
+
inverted: Boolean(row.inverted),
|
|
60
|
+
conditions: row.conditions ?? null,
|
|
61
|
+
fields: row.fields ?? null,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export class RolesAdmin {
|
|
65
|
+
db;
|
|
66
|
+
registry;
|
|
67
|
+
constructor(db, registry) {
|
|
68
|
+
this.db = db;
|
|
69
|
+
this.registry = registry;
|
|
70
|
+
}
|
|
71
|
+
async list() {
|
|
72
|
+
const rows = await this.db('roles as r')
|
|
73
|
+
.select('r.id', 'r.key', 'r.name', 'r.permission_level', this.db.raw('(SELECT count(*) FROM role_rules rr WHERE rr.role_id = r.id) AS rules'), this.db.raw('(SELECT count(DISTINCT ur.user_id) FROM user_roles ur WHERE ur.role_id = r.id) AS users'))
|
|
74
|
+
.orderBy('r.name');
|
|
75
|
+
return rows.map((row) => ({
|
|
76
|
+
id: row.id,
|
|
77
|
+
key: row.key ?? null,
|
|
78
|
+
name: row.name,
|
|
79
|
+
permissionLevel: row.permission_level,
|
|
80
|
+
rules: Number(row.rules),
|
|
81
|
+
users: Number(row.users),
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
84
|
+
async get(id) {
|
|
85
|
+
const role = await this.find(this.db, id);
|
|
86
|
+
const rules = await this.db('role_rules')
|
|
87
|
+
.where('role_id', id)
|
|
88
|
+
.orderBy(['subject', 'action', 'inverted']);
|
|
89
|
+
const users = await this.db('user_roles')
|
|
90
|
+
.where('role_id', id)
|
|
91
|
+
.countDistinct('user_id as count')
|
|
92
|
+
.first();
|
|
93
|
+
return {
|
|
94
|
+
id: role.id,
|
|
95
|
+
key: role.key ?? null,
|
|
96
|
+
name: role.name,
|
|
97
|
+
permissionLevel: role.permission_level,
|
|
98
|
+
users: Number(users?.count ?? 0),
|
|
99
|
+
rules: rules.map(toRule),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
async create(actorId, input) {
|
|
103
|
+
const name = roleName(input.name);
|
|
104
|
+
const key = input.key === undefined || input.key === null || input.key === '' ? null : roleKey(input.key);
|
|
105
|
+
const level = permissionLevel(input.permissionLevel);
|
|
106
|
+
return this.db.transaction(async (trx) => {
|
|
107
|
+
if (await trx('roles').where('name', name).first())
|
|
108
|
+
throw new AdulaError(409, 'E_ROLE_EXISTS', 'يوجد دور بهذا الاسم');
|
|
109
|
+
if (key && (await trx('roles').where('key', key).first()))
|
|
110
|
+
throw new AdulaError(409, 'E_ROLE_KEY_EXISTS', 'يوجد دور بهذا المفتاح');
|
|
111
|
+
const [role] = await trx('roles')
|
|
112
|
+
.insert({ name, key, permission_level: level })
|
|
113
|
+
.returning('*');
|
|
114
|
+
await logActivity(trx, {
|
|
115
|
+
resource: RESOURCE,
|
|
116
|
+
recordId: role.id,
|
|
117
|
+
actorId,
|
|
118
|
+
action: 'create',
|
|
119
|
+
changes: { name, key, permissionLevel: level },
|
|
120
|
+
});
|
|
121
|
+
return { id: role.id, key, name, permissionLevel: level };
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Gives a role its stable key. A key is set once: workflows and module defaults
|
|
126
|
+
* depend on it, so changing it would silently detach them.
|
|
127
|
+
*/
|
|
128
|
+
async setKey(actorId, id, value) {
|
|
129
|
+
const key = roleKey(value);
|
|
130
|
+
return this.db.transaction(async (trx) => {
|
|
131
|
+
const role = await this.find(trx, id);
|
|
132
|
+
if (role.key === key)
|
|
133
|
+
return;
|
|
134
|
+
if (role.key)
|
|
135
|
+
throw new AdulaError(409, 'E_ROLE_KEY_FIXED', 'مفتاح الدور ثابت بعد تعيينه لأن تدفقات العمل والوحدات تعتمد عليه');
|
|
136
|
+
if (await trx('roles').where('key', key).whereNot('id', id).first())
|
|
137
|
+
throw new AdulaError(409, 'E_ROLE_KEY_EXISTS', 'يوجد دور بهذا المفتاح');
|
|
138
|
+
await trx('roles').where('id', id).update({ key });
|
|
139
|
+
await logActivity(trx, {
|
|
140
|
+
resource: RESOURCE,
|
|
141
|
+
recordId: id,
|
|
142
|
+
actorId,
|
|
143
|
+
action: 'set_key',
|
|
144
|
+
changes: { key },
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
async rename(actorId, id, value) {
|
|
149
|
+
const name = roleName(value);
|
|
150
|
+
return this.db.transaction(async (trx) => {
|
|
151
|
+
const role = await this.find(trx, id);
|
|
152
|
+
if (role.name === 'administrator' && name !== role.name)
|
|
153
|
+
throw new AdulaError(409, 'E_BOOTSTRAP_ROLE', 'اسم دور مدير النظام الأساسي محمي لاستعادة الوصول عند الحاجة');
|
|
154
|
+
if (await trx('roles').where('name', name).whereNot('id', id).first())
|
|
155
|
+
throw new AdulaError(409, 'E_ROLE_EXISTS', 'يوجد دور بهذا الاسم');
|
|
156
|
+
await trx('roles').where('id', id).update({ name });
|
|
157
|
+
await logActivity(trx, {
|
|
158
|
+
resource: RESOURCE,
|
|
159
|
+
recordId: id,
|
|
160
|
+
actorId,
|
|
161
|
+
action: 'rename',
|
|
162
|
+
changes: { from: role.name, to: name },
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
async setPermissionLevel(actorId, id, value) {
|
|
167
|
+
const level = permissionLevel(value);
|
|
168
|
+
return this.db.transaction(async (trx) => {
|
|
169
|
+
const role = await this.find(trx, id);
|
|
170
|
+
await trx('roles').where('id', id).update({ permission_level: level });
|
|
171
|
+
await logActivity(trx, {
|
|
172
|
+
resource: RESOURCE,
|
|
173
|
+
recordId: id,
|
|
174
|
+
actorId,
|
|
175
|
+
action: 'set_level',
|
|
176
|
+
changes: { from: role.permission_level, to: level },
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
async delete(actorId, id) {
|
|
181
|
+
return this.db.transaction(async (trx) => {
|
|
182
|
+
const role = await this.find(trx, id);
|
|
183
|
+
const assigned = await trx('user_roles').where('role_id', id).first();
|
|
184
|
+
if (assigned)
|
|
185
|
+
throw new AdulaError(409, 'E_ROLE_IN_USE', 'لا يمكن حذف دور مسند إلى مستخدمين');
|
|
186
|
+
await trx('roles').where('id', id).delete();
|
|
187
|
+
await logActivity(trx, {
|
|
188
|
+
resource: RESOURCE,
|
|
189
|
+
recordId: id,
|
|
190
|
+
actorId,
|
|
191
|
+
action: 'delete',
|
|
192
|
+
changes: { name: role.name },
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/** Subjects × actions derived from the registry; the UI never hard-codes resources. */
|
|
197
|
+
matrix() {
|
|
198
|
+
const resources = this.registry.all();
|
|
199
|
+
const union = ACTION_ORDER.filter((action) => resources.some((resource) => resource.actions.includes(action)));
|
|
200
|
+
return {
|
|
201
|
+
actions: [...union, 'invite', 'manage'],
|
|
202
|
+
subjects: [
|
|
203
|
+
{
|
|
204
|
+
name: 'all',
|
|
205
|
+
label: ALL_SUBJECT_LABEL,
|
|
206
|
+
actions: [...union, 'invite', 'manage'],
|
|
207
|
+
fields: [],
|
|
208
|
+
conditionFields: [],
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
name: 'core.users',
|
|
212
|
+
label: { ar: 'دعوات المستخدمين', en: 'User invitations' },
|
|
213
|
+
actions: ['invite', 'manage'],
|
|
214
|
+
fields: [],
|
|
215
|
+
conditionFields: [],
|
|
216
|
+
},
|
|
217
|
+
...resources.map((resource) => {
|
|
218
|
+
const fields = matrixFields(resource);
|
|
219
|
+
return {
|
|
220
|
+
name: resource.name,
|
|
221
|
+
label: resource.label,
|
|
222
|
+
actions: [...ACTION_ORDER.filter((a) => resource.actions.includes(a)), 'manage'],
|
|
223
|
+
fields,
|
|
224
|
+
conditionFields: [
|
|
225
|
+
...standardConditionFields(resource),
|
|
226
|
+
...fields.filter((field) => field.conditionable),
|
|
227
|
+
],
|
|
228
|
+
};
|
|
229
|
+
}),
|
|
230
|
+
],
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
async setRule(actorId, roleId, input) {
|
|
234
|
+
const rule = this.validateRule(input);
|
|
235
|
+
return this.db.transaction(async (trx) => {
|
|
236
|
+
const verifyAccess = await administrationGuard(trx, actorId);
|
|
237
|
+
await this.find(trx, roleId);
|
|
238
|
+
const values = {
|
|
239
|
+
conditions: rule.conditions ? JSON.stringify(rule.conditions) : null,
|
|
240
|
+
fields: rule.fields ? JSON.stringify(rule.fields) : null,
|
|
241
|
+
};
|
|
242
|
+
const existing = await trx('role_rules')
|
|
243
|
+
.where({
|
|
244
|
+
role_id: roleId,
|
|
245
|
+
subject: rule.subject,
|
|
246
|
+
action: rule.action,
|
|
247
|
+
inverted: rule.inverted,
|
|
248
|
+
})
|
|
249
|
+
.first();
|
|
250
|
+
const [row] = existing
|
|
251
|
+
? await trx('role_rules').where('id', existing.id).update(values).returning('*')
|
|
252
|
+
: await trx('role_rules')
|
|
253
|
+
.insert({
|
|
254
|
+
role_id: roleId,
|
|
255
|
+
subject: rule.subject,
|
|
256
|
+
action: rule.action,
|
|
257
|
+
inverted: rule.inverted,
|
|
258
|
+
...values,
|
|
259
|
+
})
|
|
260
|
+
.returning('*');
|
|
261
|
+
const saved = toRule(row);
|
|
262
|
+
await verifyAccess();
|
|
263
|
+
await logActivity(trx, {
|
|
264
|
+
resource: RESOURCE,
|
|
265
|
+
recordId: roleId,
|
|
266
|
+
actorId,
|
|
267
|
+
action: 'set_rule',
|
|
268
|
+
changes: { rule: saved },
|
|
269
|
+
});
|
|
270
|
+
return saved;
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
async removeRule(actorId, roleId, ruleId) {
|
|
274
|
+
return this.db.transaction(async (trx) => {
|
|
275
|
+
const verifyAccess = await administrationGuard(trx, actorId);
|
|
276
|
+
await this.find(trx, roleId);
|
|
277
|
+
const [row] = await trx('role_rules')
|
|
278
|
+
.where({ id: ruleId, role_id: roleId })
|
|
279
|
+
.delete()
|
|
280
|
+
.returning('*');
|
|
281
|
+
if (!row)
|
|
282
|
+
throw new AdulaError(404, 'E_RULE_NOT_FOUND', 'القاعدة غير موجودة');
|
|
283
|
+
await verifyAccess();
|
|
284
|
+
await logActivity(trx, {
|
|
285
|
+
resource: RESOURCE,
|
|
286
|
+
recordId: roleId,
|
|
287
|
+
actorId,
|
|
288
|
+
action: 'remove_rule',
|
|
289
|
+
changes: { rule: toRule(row) },
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Creates module default roles that no role holds the key of yet, with their rules
|
|
295
|
+
* validated exactly like setRule. Existing roles are never modified. A keyless role
|
|
296
|
+
* with the same display name is adopted by receiving the key, rules untouched.
|
|
297
|
+
*/
|
|
298
|
+
async ensureDefaults(actorId, roles) {
|
|
299
|
+
const prepared = roles.map((role) => {
|
|
300
|
+
const key = roleKey(role.key);
|
|
301
|
+
const name = roleName(role.name);
|
|
302
|
+
const level = permissionLevel(role.permissionLevel);
|
|
303
|
+
const rules = role.rules.map((rule) => {
|
|
304
|
+
try {
|
|
305
|
+
return this.validateRule(rule);
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
throw new Error(`Default role ${key}: ${error.message}`);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
return { key, name, level, rules };
|
|
312
|
+
});
|
|
313
|
+
const result = { created: [], adopted: [], kept: [] };
|
|
314
|
+
await this.db.transaction(async (trx) => {
|
|
315
|
+
for (const role of prepared) {
|
|
316
|
+
if (await trx('roles').where('key', role.key).first('id')) {
|
|
317
|
+
result.kept.push(role.key);
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
const named = await trx('roles').where('name', role.name).first();
|
|
321
|
+
if (named) {
|
|
322
|
+
// A role with this name but another key belongs to the administrator.
|
|
323
|
+
if (named.key) {
|
|
324
|
+
result.kept.push(role.key);
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
await trx('roles').where('id', named.id).update({ key: role.key });
|
|
328
|
+
await logActivity(trx, {
|
|
329
|
+
resource: RESOURCE,
|
|
330
|
+
recordId: named.id,
|
|
331
|
+
actorId,
|
|
332
|
+
action: 'set_key',
|
|
333
|
+
changes: { key: role.key, source: 'module default' },
|
|
334
|
+
});
|
|
335
|
+
result.adopted.push(role.key);
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
const [created] = await trx('roles')
|
|
339
|
+
.insert({ key: role.key, name: role.name, permission_level: role.level })
|
|
340
|
+
.returning('id');
|
|
341
|
+
for (const rule of role.rules)
|
|
342
|
+
await trx('role_rules').insert({
|
|
343
|
+
role_id: created.id,
|
|
344
|
+
subject: rule.subject,
|
|
345
|
+
action: rule.action,
|
|
346
|
+
inverted: rule.inverted,
|
|
347
|
+
conditions: rule.conditions ? JSON.stringify(rule.conditions) : null,
|
|
348
|
+
fields: rule.fields ? JSON.stringify(rule.fields) : null,
|
|
349
|
+
});
|
|
350
|
+
await logActivity(trx, {
|
|
351
|
+
resource: RESOURCE,
|
|
352
|
+
recordId: created.id,
|
|
353
|
+
actorId,
|
|
354
|
+
action: 'create',
|
|
355
|
+
changes: {
|
|
356
|
+
name: role.name,
|
|
357
|
+
key: role.key,
|
|
358
|
+
permissionLevel: role.level,
|
|
359
|
+
rules: role.rules.length,
|
|
360
|
+
source: 'module default',
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
result.created.push(role.key);
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
return result;
|
|
367
|
+
}
|
|
368
|
+
validateRule(input) {
|
|
369
|
+
const subject = typeof input.subject === 'string' ? input.subject : '';
|
|
370
|
+
let resource;
|
|
371
|
+
if (subject !== 'all' && subject !== 'core.users') {
|
|
372
|
+
try {
|
|
373
|
+
resource = this.registry.get(subject);
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
throw new AdulaError(422, 'E_RULE_SUBJECT', 'الكيان غير معروف');
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const action = typeof input.action === 'string' ? input.action : '';
|
|
380
|
+
const allowed = subject === 'core.users'
|
|
381
|
+
? ['invite', 'manage']
|
|
382
|
+
: resource
|
|
383
|
+
? [...resource.actions, 'manage']
|
|
384
|
+
: [...new Set(this.registry.all().flatMap((r) => [...r.actions])), 'invite', 'manage'];
|
|
385
|
+
if (!allowed.includes(action))
|
|
386
|
+
throw new AdulaError(422, 'E_RULE_ACTION', 'الإجراء غير متاح لهذا الكيان');
|
|
387
|
+
const inverted = Boolean(input.inverted);
|
|
388
|
+
let conditions = null;
|
|
389
|
+
if (input.conditions !== undefined &&
|
|
390
|
+
input.conditions !== null &&
|
|
391
|
+
!(typeof input.conditions === 'object' &&
|
|
392
|
+
!Array.isArray(input.conditions) &&
|
|
393
|
+
!Object.keys(input.conditions).length)) {
|
|
394
|
+
if (!resource)
|
|
395
|
+
throw new AdulaError(422, 'E_RULE_CONDITIONS', 'الشروط تتطلب اختيار كيان محدد');
|
|
396
|
+
if (typeof input.conditions !== 'object' || Array.isArray(input.conditions))
|
|
397
|
+
throw new AdulaError(422, 'E_RULE_CONDITIONS', 'الشروط يجب أن تكون كائناً');
|
|
398
|
+
if ('orgPath' in input.conditions)
|
|
399
|
+
throw new AdulaError(422, 'E_RULE_CONDITIONS', 'orgPath محجوز لنطاق الدور');
|
|
400
|
+
const conditionable = new Set([...standardConditionFields(resource), ...matrixFields(resource)]
|
|
401
|
+
.filter((field) => field.conditionable)
|
|
402
|
+
.map((field) => field.key));
|
|
403
|
+
for (const field of Object.keys(input.conditions))
|
|
404
|
+
if (!conditionable.has(field))
|
|
405
|
+
throw new AdulaError(422, 'E_RULE_CONDITIONS', `الحقل ${field} لا يقبل الشروط`);
|
|
406
|
+
const actorFields = new Set([...standardConditionFields(resource), ...matrixFields(resource)]
|
|
407
|
+
.filter((field) => field.actor)
|
|
408
|
+
.map((field) => field.key));
|
|
409
|
+
for (const [field, condition] of Object.entries(input.conditions))
|
|
410
|
+
if (usesActor({ [field]: condition })) {
|
|
411
|
+
if (!actorFields.has(field))
|
|
412
|
+
throw new AdulaError(422, 'E_RULE_CONDITIONS', `المستخدم الحالي يُستخدم مع حقول المستخدمين وأنشأه وعدّله فقط: ${field}`);
|
|
413
|
+
const operators = condition !== null && typeof condition === 'object' ? Object.keys(condition) : ['$eq'];
|
|
414
|
+
if (operators.some((operator) => !['$eq', '$ne', '$in'].includes(operator)))
|
|
415
|
+
throw new AdulaError(422, 'E_RULE_CONDITIONS', 'المستخدم الحالي يقبل عوامل يساوي ولا يساوي وضمن قائمة فقط');
|
|
416
|
+
}
|
|
417
|
+
try {
|
|
418
|
+
// The placeholder is an integer id at request time; validate it as one.
|
|
419
|
+
conditionSql(resolveActorConditions(input.conditions, 1), resource);
|
|
420
|
+
}
|
|
421
|
+
catch (error) {
|
|
422
|
+
throw new AdulaError(422, 'E_RULE_CONDITIONS', `شرط غير مدعوم: ${error.message}`);
|
|
423
|
+
}
|
|
424
|
+
conditions = input.conditions;
|
|
425
|
+
}
|
|
426
|
+
let fields = null;
|
|
427
|
+
if (input.fields !== undefined &&
|
|
428
|
+
input.fields !== null &&
|
|
429
|
+
!(Array.isArray(input.fields) && !input.fields.length)) {
|
|
430
|
+
if (!resource)
|
|
431
|
+
throw new AdulaError(422, 'E_RULE_FIELDS', 'قائمة الحقول تتطلب اختيار كيان محدد');
|
|
432
|
+
if (!Array.isArray(input.fields) || !input.fields.every((f) => typeof f === 'string'))
|
|
433
|
+
throw new AdulaError(422, 'E_RULE_FIELDS', 'قائمة الحقول غير صالحة');
|
|
434
|
+
for (const field of input.fields)
|
|
435
|
+
if (!(field in resource.fields))
|
|
436
|
+
throw new AdulaError(422, 'E_RULE_FIELDS', `الحقل ${field} غير معروف`);
|
|
437
|
+
fields = [...new Set(input.fields)];
|
|
438
|
+
}
|
|
439
|
+
return { subject, action, inverted, conditions, fields };
|
|
440
|
+
}
|
|
441
|
+
async find(db, id) {
|
|
442
|
+
const role = await db('roles').where('id', id).first();
|
|
443
|
+
if (!role)
|
|
444
|
+
throw new AdulaError(404, 'E_ROLE_NOT_FOUND', 'الدور غير موجود');
|
|
445
|
+
return role;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
import type { ResourceRegistry } from '../resource/registry.js';
|
|
3
|
+
import { type Actor } from '../auth/ability.js';
|
|
4
|
+
export type SavedViewQuery = {
|
|
5
|
+
search?: string;
|
|
6
|
+
sort?: string;
|
|
7
|
+
direction?: 'asc' | 'desc';
|
|
8
|
+
filters?: Record<string, string | number | boolean | null>;
|
|
9
|
+
};
|
|
10
|
+
export type SavedView = {
|
|
11
|
+
id: number;
|
|
12
|
+
name: string;
|
|
13
|
+
query: SavedViewQuery;
|
|
14
|
+
shared: boolean;
|
|
15
|
+
own: boolean;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A saved view is stored user input that later drives a list query, so every key
|
|
19
|
+
* is validated against the resource definition before it is written and again
|
|
20
|
+
* when it is read back: a field that stopped being sortable or filterable must
|
|
21
|
+
* not resurrect as an unchecked query parameter after an upgrade.
|
|
22
|
+
*/
|
|
23
|
+
export declare class SavedViews {
|
|
24
|
+
private db;
|
|
25
|
+
private registry;
|
|
26
|
+
constructor(db: Knex, registry: ResourceRegistry);
|
|
27
|
+
list(name: string, actor: Actor): Promise<SavedView[]>;
|
|
28
|
+
save(name: string, actor: Actor, input: {
|
|
29
|
+
name: string;
|
|
30
|
+
query: unknown;
|
|
31
|
+
shared?: boolean;
|
|
32
|
+
}): Promise<SavedView>;
|
|
33
|
+
remove(name: string, actor: Actor, id: number): Promise<void>;
|
|
34
|
+
private authorize;
|
|
35
|
+
/** Returns null for a stored view the resource no longer supports; throws on user input. */
|
|
36
|
+
private sanitize;
|
|
37
|
+
}
|