adula 0.0.0-stage → 2.0.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 +40 -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 -3
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { buildAbility } from '../auth/ability.js';
|
|
2
|
+
import { AdulaError } from '../admin/errors.js';
|
|
3
|
+
import { canQueryField } from '../admin/resource_service.js';
|
|
4
|
+
const NAME_LIMIT = 60;
|
|
5
|
+
const VIEW_LIMIT = 50;
|
|
6
|
+
/** Shared views are visible to every viewer of a resource; cap them per resource. */
|
|
7
|
+
const SHARED_LIMIT = 100;
|
|
8
|
+
/**
|
|
9
|
+
* A saved view is stored user input that later drives a list query, so every key
|
|
10
|
+
* is validated against the resource definition before it is written and again
|
|
11
|
+
* when it is read back: a field that stopped being sortable or filterable must
|
|
12
|
+
* not resurrect as an unchecked query parameter after an upgrade.
|
|
13
|
+
*/
|
|
14
|
+
export class SavedViews {
|
|
15
|
+
db;
|
|
16
|
+
registry;
|
|
17
|
+
constructor(db, registry) {
|
|
18
|
+
this.db = db;
|
|
19
|
+
this.registry = registry;
|
|
20
|
+
}
|
|
21
|
+
async list(name, actor) {
|
|
22
|
+
const { resource } = this.authorize(name, actor);
|
|
23
|
+
// Own views first, so colleagues' shared views can never crowd them out.
|
|
24
|
+
const rows = await this.db('saved_views')
|
|
25
|
+
.where('resource', resource.name)
|
|
26
|
+
.where((where) => where.where('user_id', actor.id).orWhere('shared', true))
|
|
27
|
+
.orderByRaw('(user_id = ?) DESC', [actor.id])
|
|
28
|
+
.orderBy('name')
|
|
29
|
+
.limit(VIEW_LIMIT + SHARED_LIMIT);
|
|
30
|
+
return rows.flatMap((row) => {
|
|
31
|
+
const query = this.sanitize(resource, row.query);
|
|
32
|
+
return query
|
|
33
|
+
? [
|
|
34
|
+
{
|
|
35
|
+
id: Number(row.id),
|
|
36
|
+
name: String(row.name),
|
|
37
|
+
query,
|
|
38
|
+
shared: Boolean(row.shared),
|
|
39
|
+
own: Number(row.user_id) === actor.id,
|
|
40
|
+
},
|
|
41
|
+
]
|
|
42
|
+
: [];
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async save(name, actor, input) {
|
|
46
|
+
const { resource, ability } = this.authorize(name, actor);
|
|
47
|
+
const label = typeof input.name === 'string' ? input.name.trim() : '';
|
|
48
|
+
if (!label || label.length > NAME_LIMIT)
|
|
49
|
+
throw new AdulaError(422, 'E_VIEW_NAME', 'اسم العرض مطلوب ولا يتجاوز 60 حرفاً');
|
|
50
|
+
const query = this.sanitize(resource, input.query, true);
|
|
51
|
+
// A view may only query what its author may query; stored filters must not probe hidden fields.
|
|
52
|
+
const queried = [
|
|
53
|
+
...(query.sort ? [query.sort] : []),
|
|
54
|
+
...Object.keys(query.filters ?? {}),
|
|
55
|
+
...(query.search
|
|
56
|
+
? Object.entries(resource.fields)
|
|
57
|
+
.filter(([, field]) => field.searchable)
|
|
58
|
+
.map(([key]) => key)
|
|
59
|
+
: []),
|
|
60
|
+
];
|
|
61
|
+
for (const key of queried)
|
|
62
|
+
if (!canQueryField(resource, actor, ability, key))
|
|
63
|
+
throw new AdulaError(403, 'E_FIELD_FORBIDDEN', `لا يمكنك الاستعلام بهذا الحقل: ${key}`);
|
|
64
|
+
const shared = input.shared === true;
|
|
65
|
+
const existing = await this.db('saved_views')
|
|
66
|
+
.where({ user_id: actor.id, resource: resource.name, name: label })
|
|
67
|
+
.first('id', 'shared');
|
|
68
|
+
if (shared && !existing?.shared) {
|
|
69
|
+
const [{ count }] = await this.db('saved_views')
|
|
70
|
+
.where({ resource: resource.name, shared: true })
|
|
71
|
+
.count('* as count');
|
|
72
|
+
if (Number(count) >= SHARED_LIMIT)
|
|
73
|
+
throw new AdulaError(422, 'E_VIEW_LIMIT', 'بلغ هذا الكيان الحد الأقصى للعروض المشتركة');
|
|
74
|
+
}
|
|
75
|
+
if (!existing) {
|
|
76
|
+
const [{ count }] = await this.db('saved_views')
|
|
77
|
+
.where({ user_id: actor.id, resource: resource.name })
|
|
78
|
+
.count('* as count');
|
|
79
|
+
if (Number(count) >= VIEW_LIMIT)
|
|
80
|
+
throw new AdulaError(422, 'E_VIEW_LIMIT', 'بلغت الحد الأقصى للعروض المحفوظة لهذا الكيان');
|
|
81
|
+
}
|
|
82
|
+
const values = {
|
|
83
|
+
user_id: actor.id,
|
|
84
|
+
resource: resource.name,
|
|
85
|
+
name: label,
|
|
86
|
+
query: JSON.stringify(query),
|
|
87
|
+
shared,
|
|
88
|
+
updated_at: this.db.fn.now(),
|
|
89
|
+
};
|
|
90
|
+
const [row] = existing
|
|
91
|
+
? await this.db('saved_views').where('id', existing.id).update(values).returning('*')
|
|
92
|
+
: await this.db('saved_views').insert(values).returning('*');
|
|
93
|
+
return { id: Number(row.id), name: label, query, shared, own: true };
|
|
94
|
+
}
|
|
95
|
+
async remove(name, actor, id) {
|
|
96
|
+
const { resource } = this.authorize(name, actor);
|
|
97
|
+
const removed = await this.db('saved_views')
|
|
98
|
+
.where({ id, resource: resource.name, user_id: actor.id })
|
|
99
|
+
.del();
|
|
100
|
+
if (!removed)
|
|
101
|
+
throw new AdulaError(404, 'E_VIEW_NOT_FOUND', 'العرض المحفوظ غير موجود');
|
|
102
|
+
}
|
|
103
|
+
authorize(name, actor) {
|
|
104
|
+
const resource = this.registry.get(name);
|
|
105
|
+
const ability = buildAbility(actor.rules, this.registry.all());
|
|
106
|
+
if (!resource.actions.includes('view') || !ability.can('view', resource.name))
|
|
107
|
+
throw new AdulaError(403, 'E_FORBIDDEN', 'ليس لديك صلاحية لهذا الكيان');
|
|
108
|
+
return { resource, ability };
|
|
109
|
+
}
|
|
110
|
+
/** Returns null for a stored view the resource no longer supports; throws on user input. */
|
|
111
|
+
sanitize(resource, value, strict = false) {
|
|
112
|
+
const reject = (code, message) => {
|
|
113
|
+
if (strict)
|
|
114
|
+
throw new AdulaError(422, code, message);
|
|
115
|
+
return null;
|
|
116
|
+
};
|
|
117
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
118
|
+
return reject('E_VIEW_QUERY', 'تعريف العرض غير صالح');
|
|
119
|
+
const input = value;
|
|
120
|
+
for (const key of Object.keys(input))
|
|
121
|
+
if (!['search', 'sort', 'direction', 'filters'].includes(key))
|
|
122
|
+
return reject('E_VIEW_QUERY', `مفتاح غير مدعوم في العرض: ${key}`);
|
|
123
|
+
const query = {};
|
|
124
|
+
if (input.search !== undefined && input.search !== null && input.search !== '') {
|
|
125
|
+
if (typeof input.search !== 'string' || input.search.length > 200)
|
|
126
|
+
return reject('E_VIEW_QUERY', 'نص البحث غير صالح');
|
|
127
|
+
if (!Object.values(resource.fields).some((field) => field.searchable))
|
|
128
|
+
return reject('E_VIEW_QUERY', 'هذا الكيان لا يدعم البحث النصي');
|
|
129
|
+
query.search = input.search;
|
|
130
|
+
}
|
|
131
|
+
if (input.sort !== undefined && input.sort !== null && input.sort !== '') {
|
|
132
|
+
if (typeof input.sort !== 'string' || !resource.fields[input.sort]?.sortable)
|
|
133
|
+
return reject('E_VIEW_QUERY', 'حقل الفرز غير مدعوم');
|
|
134
|
+
query.sort = input.sort;
|
|
135
|
+
if (input.direction !== undefined && input.direction !== null) {
|
|
136
|
+
if (input.direction !== 'asc' && input.direction !== 'desc')
|
|
137
|
+
return reject('E_VIEW_QUERY', 'اتجاه الفرز غير صالح');
|
|
138
|
+
query.direction = input.direction;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (input.direction !== undefined && input.direction !== null && input.direction !== '')
|
|
142
|
+
return reject('E_VIEW_QUERY', 'اتجاه الفرز يحتاج حقل فرز');
|
|
143
|
+
if (input.filters !== undefined && input.filters !== null) {
|
|
144
|
+
if (typeof input.filters !== 'object' || Array.isArray(input.filters))
|
|
145
|
+
return reject('E_VIEW_QUERY', 'تصفية العرض غير صالحة');
|
|
146
|
+
const filters = {};
|
|
147
|
+
for (const [key, entry] of Object.entries(input.filters)) {
|
|
148
|
+
if (!resource.fields[key]?.filterable)
|
|
149
|
+
return reject('E_VIEW_QUERY', `حقل التصفية غير مدعوم: ${key}`);
|
|
150
|
+
if (entry === undefined || entry === '')
|
|
151
|
+
continue;
|
|
152
|
+
if (entry !== null &&
|
|
153
|
+
typeof entry !== 'string' &&
|
|
154
|
+
typeof entry !== 'number' &&
|
|
155
|
+
typeof entry !== 'boolean')
|
|
156
|
+
return reject('E_VIEW_QUERY', `قيمة التصفية غير صالحة: ${key}`);
|
|
157
|
+
filters[key] = entry;
|
|
158
|
+
}
|
|
159
|
+
if (Object.keys(filters).length)
|
|
160
|
+
query.filters = filters;
|
|
161
|
+
}
|
|
162
|
+
return query;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
import type { JsonValue } from '../resource/types.js';
|
|
3
|
+
export type SettingScope = 'system' | 'org_unit' | 'user';
|
|
4
|
+
export type SettingRow = {
|
|
5
|
+
id: number;
|
|
6
|
+
key: string;
|
|
7
|
+
scope: SettingScope;
|
|
8
|
+
scopeId: string;
|
|
9
|
+
value: JsonValue;
|
|
10
|
+
readOnly: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type SettingInput = {
|
|
13
|
+
key: string;
|
|
14
|
+
scope: SettingScope;
|
|
15
|
+
scopeId?: string;
|
|
16
|
+
value: unknown;
|
|
17
|
+
};
|
|
18
|
+
export declare const SETTING_SCOPES: SettingScope[];
|
|
19
|
+
export declare const PROTECTED_SETTING_KEYS: string[];
|
|
20
|
+
export declare function isProtectedSetting(key: string): boolean;
|
|
21
|
+
export declare function parseSettingValue(text: string): unknown;
|
|
22
|
+
export declare class SettingsAdmin {
|
|
23
|
+
private db;
|
|
24
|
+
constructor(db: Knex);
|
|
25
|
+
list(scope: SettingScope, scopeId?: string): Promise<SettingRow[]>;
|
|
26
|
+
upsert(actorId: number, input: SettingInput): Promise<SettingRow>;
|
|
27
|
+
delete(actorId: number, id: number): Promise<void>;
|
|
28
|
+
private scopeId;
|
|
29
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { AdulaError } from '../admin/errors.js';
|
|
2
|
+
import { logActivity } from './activity.js';
|
|
3
|
+
import { UI_PREFERENCES_KEY, validateUiPreferences } from './ui_preferences.js';
|
|
4
|
+
export const SETTING_SCOPES = ['system', 'org_unit', 'user'];
|
|
5
|
+
// Written by install, scheduler, worker and backup verification; never by the settings screen.
|
|
6
|
+
export const PROTECTED_SETTING_KEYS = [
|
|
7
|
+
'adula.version',
|
|
8
|
+
'scheduler.heartbeat',
|
|
9
|
+
'worker.heartbeat',
|
|
10
|
+
'mail.delivery_test',
|
|
11
|
+
];
|
|
12
|
+
const RESOURCE = 'core.settings';
|
|
13
|
+
export function isProtectedSetting(key) {
|
|
14
|
+
return (PROTECTED_SETTING_KEYS.includes(key) || key.startsWith('backup.') || key.startsWith('setup.'));
|
|
15
|
+
}
|
|
16
|
+
export function parseSettingValue(text) {
|
|
17
|
+
try {
|
|
18
|
+
return JSON.parse(text);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
throw new AdulaError(422, 'E_SETTING_JSON', 'القيمة يجب أن تكون JSON صالحاً');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function settingKey(value) {
|
|
25
|
+
if (typeof value !== 'string' || !/^[a-z][a-z0-9_.-]{0,99}$/i.test(value))
|
|
26
|
+
throw new AdulaError(422, 'E_SETTING_KEY', 'المفتاح يبدأ بحرف ويحوي حروفاً وأرقاماً ونقاطاً فقط');
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
function settingScope(value) {
|
|
30
|
+
if (!SETTING_SCOPES.includes(value))
|
|
31
|
+
throw new AdulaError(422, 'E_SETTING_SCOPE', 'النطاق غير معروف');
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
function toRow(row) {
|
|
35
|
+
return {
|
|
36
|
+
id: Number(row.id),
|
|
37
|
+
key: String(row.key),
|
|
38
|
+
scope: row.scope,
|
|
39
|
+
scopeId: String(row.scope_id),
|
|
40
|
+
value: row.value,
|
|
41
|
+
readOnly: isProtectedSetting(String(row.key)),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export class SettingsAdmin {
|
|
45
|
+
db;
|
|
46
|
+
constructor(db) {
|
|
47
|
+
this.db = db;
|
|
48
|
+
}
|
|
49
|
+
async list(scope, scopeId = '0') {
|
|
50
|
+
const rows = await this.db('settings')
|
|
51
|
+
.where({ scope: settingScope(scope), scope_id: scopeId })
|
|
52
|
+
.orderBy('key');
|
|
53
|
+
return rows.map(toRow);
|
|
54
|
+
}
|
|
55
|
+
async upsert(actorId, input) {
|
|
56
|
+
const key = settingKey(input.key);
|
|
57
|
+
const scope = settingScope(input.scope);
|
|
58
|
+
if (key === UI_PREFERENCES_KEY) {
|
|
59
|
+
if (scope !== 'system')
|
|
60
|
+
throw new AdulaError(422, 'E_SETTING_SCOPE', 'تفضيلات الواجهة تُضبط على مستوى النظام');
|
|
61
|
+
validateUiPreferences(input.value);
|
|
62
|
+
}
|
|
63
|
+
if (isProtectedSetting(key))
|
|
64
|
+
throw new AdulaError(403, 'E_SETTING_READ_ONLY', 'هذا المفتاح تشغيلي للقراءة فقط');
|
|
65
|
+
const serialized = JSON.stringify(input.value);
|
|
66
|
+
if (serialized === undefined)
|
|
67
|
+
throw new AdulaError(422, 'E_SETTING_JSON', 'القيمة يجب أن تكون JSON صالحاً');
|
|
68
|
+
return this.db.transaction(async (trx) => {
|
|
69
|
+
const scopeId = await this.scopeId(trx, scope, input.scopeId);
|
|
70
|
+
const [row] = await trx('settings')
|
|
71
|
+
.insert({ key, scope, scope_id: scopeId, value: serialized })
|
|
72
|
+
.onConflict(['key', 'scope', 'scope_id'])
|
|
73
|
+
.merge(['value'])
|
|
74
|
+
.returning('*');
|
|
75
|
+
await logActivity(trx, {
|
|
76
|
+
resource: RESOURCE,
|
|
77
|
+
recordId: row.id,
|
|
78
|
+
actorId,
|
|
79
|
+
action: 'upsert',
|
|
80
|
+
changes: { key, scope, scopeId, value: input.value },
|
|
81
|
+
});
|
|
82
|
+
return toRow(row);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
async delete(actorId, id) {
|
|
86
|
+
return this.db.transaction(async (trx) => {
|
|
87
|
+
const row = await trx('settings').where('id', id).first();
|
|
88
|
+
if (!row)
|
|
89
|
+
throw new AdulaError(404, 'E_SETTING_NOT_FOUND', 'الإعداد غير موجود');
|
|
90
|
+
if (isProtectedSetting(row.key))
|
|
91
|
+
throw new AdulaError(403, 'E_SETTING_READ_ONLY', 'هذا المفتاح تشغيلي للقراءة فقط');
|
|
92
|
+
await trx('settings').where('id', id).delete();
|
|
93
|
+
await logActivity(trx, {
|
|
94
|
+
resource: RESOURCE,
|
|
95
|
+
recordId: id,
|
|
96
|
+
actorId,
|
|
97
|
+
action: 'delete',
|
|
98
|
+
changes: { key: row.key, scope: row.scope, scopeId: row.scope_id },
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async scopeId(db, scope, value) {
|
|
103
|
+
if (scope === 'system')
|
|
104
|
+
return '0';
|
|
105
|
+
const id = Number(value);
|
|
106
|
+
if (!Number.isSafeInteger(id) || id <= 0)
|
|
107
|
+
throw new AdulaError(422, 'E_SETTING_SCOPE', 'معرّف النطاق مطلوب');
|
|
108
|
+
const table = scope === 'org_unit' ? 'org_units' : 'users';
|
|
109
|
+
if (!(await db(table).where('id', id).first('id')))
|
|
110
|
+
throw new AdulaError(404, 'E_SETTING_SCOPE', scope === 'org_unit' ? 'الوحدة التنظيمية غير موجودة' : 'المستخدم غير موجود');
|
|
111
|
+
return String(id);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
export type SetupCheck = {
|
|
3
|
+
status: 'checking' | 'passed' | 'failed';
|
|
4
|
+
checkedAt: string;
|
|
5
|
+
fingerprint: string;
|
|
6
|
+
attempt: string;
|
|
7
|
+
};
|
|
8
|
+
export declare class InitialSetup {
|
|
9
|
+
private db;
|
|
10
|
+
constructor(db: Knex);
|
|
11
|
+
check(actorId: number, name: 'storage' | 'infrastructure', fingerprint: string, probe: () => Promise<void>): Promise<void>;
|
|
12
|
+
acknowledgeIdentity(actorId: number, fingerprint: string): Promise<void>;
|
|
13
|
+
testNotification(actorId: number): Promise<number>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { Settings } from '../services/settings.js';
|
|
3
|
+
import { AdulaError } from '../admin/errors.js';
|
|
4
|
+
import { logActivity } from './activity.js';
|
|
5
|
+
export class InitialSetup {
|
|
6
|
+
db;
|
|
7
|
+
constructor(db) {
|
|
8
|
+
this.db = db;
|
|
9
|
+
}
|
|
10
|
+
async check(actorId, name, fingerprint, probe) {
|
|
11
|
+
const key = `setup.check.${name}`;
|
|
12
|
+
const state = {
|
|
13
|
+
status: 'checking',
|
|
14
|
+
checkedAt: new Date().toISOString(),
|
|
15
|
+
fingerprint,
|
|
16
|
+
attempt: randomUUID(),
|
|
17
|
+
};
|
|
18
|
+
await this.db.transaction(async (trx) => {
|
|
19
|
+
await trx.raw('SELECT pg_advisory_xact_lock(?)', [717013]);
|
|
20
|
+
const previous = await new Settings(trx).get(key);
|
|
21
|
+
if (previous &&
|
|
22
|
+
Date.now() - Date.parse(previous.checkedAt) <
|
|
23
|
+
(previous.status === 'checking' ? 300_000 : 30_000))
|
|
24
|
+
throw new AdulaError(429, 'E_SETUP_COOLDOWN', 'انتظر قليلًا قبل إعادة الفحص');
|
|
25
|
+
await new Settings(trx).set(key, state);
|
|
26
|
+
});
|
|
27
|
+
try {
|
|
28
|
+
await probe();
|
|
29
|
+
state.status = 'passed';
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
state.status = 'failed';
|
|
33
|
+
}
|
|
34
|
+
await this.db.transaction(async (trx) => {
|
|
35
|
+
await trx('settings')
|
|
36
|
+
.where({ key, scope: 'system', scope_id: '0' })
|
|
37
|
+
.whereRaw("value->>'attempt' = ?", [state.attempt])
|
|
38
|
+
.update({ value: JSON.stringify(state) });
|
|
39
|
+
await logActivity(trx, {
|
|
40
|
+
resource: 'core.setup',
|
|
41
|
+
recordId: 0,
|
|
42
|
+
actorId,
|
|
43
|
+
action: 'check',
|
|
44
|
+
changes: { service: name, status: state.status },
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
if (state.status === 'failed')
|
|
48
|
+
throw new AdulaError(422, 'E_SETUP_CHECK_FAILED', 'لم ينجح الفحص. راجع إعدادات الخدمة واتصالها ثم أعد المحاولة.');
|
|
49
|
+
}
|
|
50
|
+
async acknowledgeIdentity(actorId, fingerprint) {
|
|
51
|
+
await this.db.transaction(async (trx) => {
|
|
52
|
+
await new Settings(trx).set('setup.identity', {
|
|
53
|
+
fingerprint,
|
|
54
|
+
actorId,
|
|
55
|
+
at: new Date().toISOString(),
|
|
56
|
+
});
|
|
57
|
+
await logActivity(trx, {
|
|
58
|
+
resource: 'core.setup',
|
|
59
|
+
recordId: 0,
|
|
60
|
+
actorId,
|
|
61
|
+
action: 'identity_confirmed',
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async testNotification(actorId) {
|
|
66
|
+
return this.db.transaction(async (trx) => {
|
|
67
|
+
await trx.raw('SELECT pg_advisory_xact_lock(?, ?)', [717014, actorId]);
|
|
68
|
+
const settings = new Settings(trx);
|
|
69
|
+
const previous = await settings.get('setup.notification', 'user', String(actorId));
|
|
70
|
+
if (previous && Date.now() - Date.parse(previous.at) < 60_000)
|
|
71
|
+
throw new AdulaError(429, 'E_SETUP_COOLDOWN', 'انتظر دقيقة قبل إعادة الإشعار التجريبي');
|
|
72
|
+
const [notification] = await trx('notifications')
|
|
73
|
+
.insert({
|
|
74
|
+
user_id: actorId,
|
|
75
|
+
title: 'اختبار الإشعارات',
|
|
76
|
+
body: 'وصل هذا الإشعار من معالج الإعداد الأولي. حدده كمقروء ثم ارجع إلى الإعداد الأولي للتحقق من النتيجة.',
|
|
77
|
+
})
|
|
78
|
+
.returning('id');
|
|
79
|
+
await settings.set('setup.notification', { id: notification.id, at: new Date().toISOString() }, 'user', String(actorId));
|
|
80
|
+
await logActivity(trx, {
|
|
81
|
+
resource: 'core.setup',
|
|
82
|
+
recordId: notification.id,
|
|
83
|
+
actorId,
|
|
84
|
+
action: 'notification_test',
|
|
85
|
+
});
|
|
86
|
+
return notification.id;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
export type CalendarSystem = 'gregory' | 'islamic-umalqura';
|
|
3
|
+
export type CalendarPreference = CalendarSystem | 'both';
|
|
4
|
+
export type UiPreferences = {
|
|
5
|
+
calendar: CalendarPreference;
|
|
6
|
+
confirmDialogClose: boolean;
|
|
7
|
+
pageTransitions: boolean;
|
|
8
|
+
};
|
|
9
|
+
export declare const UI_PREFERENCES_KEY = "ui.preferences";
|
|
10
|
+
export declare const DEFAULT_UI_PREFERENCES: UiPreferences;
|
|
11
|
+
export declare function validateUiPreferences(value: unknown): UiPreferences;
|
|
12
|
+
export declare function uiPreferences(db: Knex): Promise<UiPreferences>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Settings } from '../services/settings.js';
|
|
2
|
+
import { AdulaError } from '../admin/errors.js';
|
|
3
|
+
export const UI_PREFERENCES_KEY = 'ui.preferences';
|
|
4
|
+
export const DEFAULT_UI_PREFERENCES = {
|
|
5
|
+
calendar: 'gregory',
|
|
6
|
+
confirmDialogClose: true,
|
|
7
|
+
pageTransitions: true,
|
|
8
|
+
};
|
|
9
|
+
export function validateUiPreferences(value) {
|
|
10
|
+
const input = value;
|
|
11
|
+
if (!input ||
|
|
12
|
+
typeof input !== 'object' ||
|
|
13
|
+
Array.isArray(input) ||
|
|
14
|
+
!['gregory', 'islamic-umalqura', 'both'].includes(input.calendar) ||
|
|
15
|
+
typeof input.confirmDialogClose !== 'boolean' ||
|
|
16
|
+
typeof input.pageTransitions !== 'boolean' ||
|
|
17
|
+
Object.keys(input).some((key) => !(key in DEFAULT_UI_PREFERENCES)))
|
|
18
|
+
throw new AdulaError(422, 'E_UI_PREFERENCES', 'اختر تقويمًا صالحًا وحدد خيارات تأكيد الإغلاق والانتقالات');
|
|
19
|
+
return {
|
|
20
|
+
calendar: input.calendar,
|
|
21
|
+
confirmDialogClose: input.confirmDialogClose,
|
|
22
|
+
pageTransitions: input.pageTransitions,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export async function uiPreferences(db) {
|
|
26
|
+
const value = await new Settings(db).get(UI_PREFERENCES_KEY);
|
|
27
|
+
if (value === undefined)
|
|
28
|
+
return { ...DEFAULT_UI_PREFERENCES };
|
|
29
|
+
return validateUiPreferences(value);
|
|
30
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
export type InvitationDelivery = {
|
|
3
|
+
email: string;
|
|
4
|
+
fullName: string;
|
|
5
|
+
token: string;
|
|
6
|
+
};
|
|
7
|
+
/** Pending invitations are not accounts. Password hashing and mail transport belong to the host. */
|
|
8
|
+
export declare class UserInvitations {
|
|
9
|
+
private db;
|
|
10
|
+
constructor(db: Knex);
|
|
11
|
+
canInvite(actorId: number): Promise<boolean>;
|
|
12
|
+
invite(actorId: number, input: {
|
|
13
|
+
email: string;
|
|
14
|
+
fullName: string;
|
|
15
|
+
}, send: (invitation: InvitationDelivery) => Promise<void>): Promise<{
|
|
16
|
+
id: number;
|
|
17
|
+
email: string;
|
|
18
|
+
}>;
|
|
19
|
+
valid(token: string): Promise<boolean>;
|
|
20
|
+
accept(token: string, password: string, hashPassword: (password: string) => Promise<string>): Promise<{
|
|
21
|
+
id: number;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import { subject } from '@casl/ability';
|
|
3
|
+
import { ActorStore } from '../auth/actor_store.js';
|
|
4
|
+
import { buildAbility } from '../auth/ability.js';
|
|
5
|
+
import { ResourceRegistry } from '../resource/registry.js';
|
|
6
|
+
import { AdulaError } from '../admin/errors.js';
|
|
7
|
+
import { logActivity } from './activity.js';
|
|
8
|
+
const RESOURCE = 'core.users';
|
|
9
|
+
const INVITATIONS = 'core.user_invitations';
|
|
10
|
+
const digest = (token) => createHash('sha256').update(token).digest('hex');
|
|
11
|
+
const invalid = () => new AdulaError(422, 'E_INVITATION_INVALID', 'الدعوة غير صالحة أو انتهت صلاحيتها. اطلب دعوة جديدة من المسؤول.');
|
|
12
|
+
/** Pending invitations are not accounts. Password hashing and mail transport belong to the host. */
|
|
13
|
+
export class UserInvitations {
|
|
14
|
+
db;
|
|
15
|
+
constructor(db) {
|
|
16
|
+
this.db = db;
|
|
17
|
+
}
|
|
18
|
+
async canInvite(actorId) {
|
|
19
|
+
const user = await this.db('users').where('id', actorId).first('disabled_at');
|
|
20
|
+
if (!user || user.disabled_at)
|
|
21
|
+
return false;
|
|
22
|
+
const actor = await new ActorStore(this.db, new ResourceRegistry()).load(actorId);
|
|
23
|
+
return buildAbility(actor.rules).can('invite', subject(RESOURCE, {}));
|
|
24
|
+
}
|
|
25
|
+
async invite(actorId, input, send) {
|
|
26
|
+
const email = typeof input.email === 'string' ? input.email.trim().toLowerCase() : '';
|
|
27
|
+
const fullName = typeof input.fullName === 'string' ? input.fullName.trim() : '';
|
|
28
|
+
if (!email ||
|
|
29
|
+
email.length > 254 ||
|
|
30
|
+
!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) ||
|
|
31
|
+
!fullName ||
|
|
32
|
+
fullName.length > 120)
|
|
33
|
+
throw new AdulaError(422, 'E_INVITATION_INPUT', 'أدخل اسمًا وبريدًا إلكترونيًا صحيحين');
|
|
34
|
+
const token = randomBytes(32).toString('base64url');
|
|
35
|
+
const tokenHash = digest(token);
|
|
36
|
+
const id = await this.db.transaction(async (trx) => {
|
|
37
|
+
// Serialize resends and acceptance for the same normalized email.
|
|
38
|
+
await trx.raw('SELECT pg_advisory_xact_lock(hashtextextended(?, 0))', [email]);
|
|
39
|
+
if (!(await new UserInvitations(trx).canInvite(actorId)))
|
|
40
|
+
throw new AdulaError(403, 'E_FORBIDDEN', 'ليس لديك صلاحية دعوة المستخدمين');
|
|
41
|
+
if (await trx('users').whereRaw('lower(email) = ?', [email]).first('id'))
|
|
42
|
+
throw new AdulaError(409, 'E_USER_EXISTS', 'يوجد حساب بهذا البريد بالفعل');
|
|
43
|
+
const previous = await trx('user_invitations').where({ email }).first();
|
|
44
|
+
if (previous && Date.now() - new Date(previous.created_at).getTime() < 60_000)
|
|
45
|
+
throw new AdulaError(429, 'E_INVITATION_WAIT', 'انتظر دقيقة قبل إعادة إرسال الدعوة');
|
|
46
|
+
const [row] = await trx('user_invitations')
|
|
47
|
+
.insert({
|
|
48
|
+
email,
|
|
49
|
+
full_name: fullName,
|
|
50
|
+
invited_by: actorId,
|
|
51
|
+
token_hash: tokenHash,
|
|
52
|
+
expires_at: new Date(Date.now() + 24 * 60 * 60 * 1000),
|
|
53
|
+
accepted_at: null,
|
|
54
|
+
created_at: trx.fn.now(),
|
|
55
|
+
})
|
|
56
|
+
.onConflict('email')
|
|
57
|
+
.merge()
|
|
58
|
+
.returning('id');
|
|
59
|
+
await logActivity(trx, {
|
|
60
|
+
resource: INVITATIONS,
|
|
61
|
+
recordId: row.id,
|
|
62
|
+
actorId,
|
|
63
|
+
action: 'invitation_created',
|
|
64
|
+
changes: { email },
|
|
65
|
+
});
|
|
66
|
+
return row.id;
|
|
67
|
+
});
|
|
68
|
+
try {
|
|
69
|
+
await send({ email, fullName, token });
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// Revoke only this attempt; a newer resend must survive an older transport failure.
|
|
73
|
+
await this.db('user_invitations')
|
|
74
|
+
.where({ id, token_hash: tokenHash })
|
|
75
|
+
.whereNull('accepted_at')
|
|
76
|
+
.delete();
|
|
77
|
+
await logActivity(this.db, {
|
|
78
|
+
resource: INVITATIONS,
|
|
79
|
+
recordId: id,
|
|
80
|
+
actorId,
|
|
81
|
+
action: 'invitation_delivery_failed',
|
|
82
|
+
changes: { email },
|
|
83
|
+
});
|
|
84
|
+
throw new AdulaError(503, 'E_INVITATION_DELIVERY', 'تعذر إرسال الدعوة. تحقق من إعدادات البريد ثم أعد المحاولة.');
|
|
85
|
+
}
|
|
86
|
+
await logActivity(this.db, {
|
|
87
|
+
resource: INVITATIONS,
|
|
88
|
+
recordId: id,
|
|
89
|
+
actorId,
|
|
90
|
+
action: 'invitation_sent',
|
|
91
|
+
changes: { email },
|
|
92
|
+
});
|
|
93
|
+
return { id, email };
|
|
94
|
+
}
|
|
95
|
+
async valid(token) {
|
|
96
|
+
if (!/^[\w-]{43}$/.test(token))
|
|
97
|
+
return false;
|
|
98
|
+
return Boolean(await this.db('user_invitations')
|
|
99
|
+
.where('token_hash', digest(token))
|
|
100
|
+
.whereNull('accepted_at')
|
|
101
|
+
.where('expires_at', '>', this.db.fn.now())
|
|
102
|
+
.first('id'));
|
|
103
|
+
}
|
|
104
|
+
async accept(token, password, hashPassword) {
|
|
105
|
+
if (!(await this.valid(token)))
|
|
106
|
+
throw invalid();
|
|
107
|
+
if (typeof password !== 'string' || password.length < 8 || password.length > 64)
|
|
108
|
+
throw new AdulaError(422, 'E_PASSWORD', 'كلمة المرور من 8 إلى 64 حرفًا');
|
|
109
|
+
const passwordHash = await hashPassword(password);
|
|
110
|
+
return this.db.transaction(async (trx) => {
|
|
111
|
+
const pending = await trx('user_invitations').where('token_hash', digest(token)).first();
|
|
112
|
+
if (!pending)
|
|
113
|
+
throw invalid();
|
|
114
|
+
await trx.raw('SELECT pg_advisory_xact_lock(hashtextextended(?, 0))', [pending.email]);
|
|
115
|
+
const row = await trx('user_invitations')
|
|
116
|
+
.where({ id: pending.id, token_hash: digest(token) })
|
|
117
|
+
.whereNull('accepted_at')
|
|
118
|
+
.where('expires_at', '>', trx.fn.now())
|
|
119
|
+
.forUpdate()
|
|
120
|
+
.first();
|
|
121
|
+
if (!row || (await trx('users').whereRaw('lower(email) = ?', [row.email]).first('id')))
|
|
122
|
+
throw invalid();
|
|
123
|
+
const [user] = await trx('users')
|
|
124
|
+
.insert({
|
|
125
|
+
email: row.email,
|
|
126
|
+
full_name: row.full_name,
|
|
127
|
+
password: passwordHash,
|
|
128
|
+
created_at: trx.fn.now(),
|
|
129
|
+
updated_at: trx.fn.now(),
|
|
130
|
+
})
|
|
131
|
+
.returning('id');
|
|
132
|
+
await trx('user_invitations').where('id', row.id).update({ accepted_at: trx.fn.now() });
|
|
133
|
+
// No role or organizational membership can be supplied by either HTTP request.
|
|
134
|
+
await logActivity(trx, {
|
|
135
|
+
resource: RESOURCE,
|
|
136
|
+
recordId: user.id,
|
|
137
|
+
actorId: user.id,
|
|
138
|
+
action: 'invitation_accepted',
|
|
139
|
+
changes: { invitedBy: row.invited_by },
|
|
140
|
+
});
|
|
141
|
+
return { id: user.id };
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
export type UserRoleAssignment = {
|
|
3
|
+
id: number;
|
|
4
|
+
roleId: number;
|
|
5
|
+
role: string;
|
|
6
|
+
orgUnitId: number | null;
|
|
7
|
+
orgUnit: string | null;
|
|
8
|
+
};
|
|
9
|
+
export type UserOrgUnit = {
|
|
10
|
+
id: number;
|
|
11
|
+
name: string;
|
|
12
|
+
path: string;
|
|
13
|
+
};
|
|
14
|
+
export type UserSummary = {
|
|
15
|
+
id: number;
|
|
16
|
+
fullName: string | null;
|
|
17
|
+
email: string;
|
|
18
|
+
disabledAt: string | null;
|
|
19
|
+
roles: UserRoleAssignment[];
|
|
20
|
+
orgUnits: UserOrgUnit[];
|
|
21
|
+
};
|
|
22
|
+
export type UserPage = {
|
|
23
|
+
data: UserSummary[];
|
|
24
|
+
nextCursor: number | null;
|
|
25
|
+
};
|
|
26
|
+
/** Host applications own the users table; AdulaJS expects id, email, full_name and disabled_at. */
|
|
27
|
+
export declare class UsersAdmin {
|
|
28
|
+
private db;
|
|
29
|
+
constructor(db: Knex);
|
|
30
|
+
list(options?: {
|
|
31
|
+
search?: string;
|
|
32
|
+
cursor?: number;
|
|
33
|
+
limit?: number;
|
|
34
|
+
}): Promise<UserPage>;
|
|
35
|
+
get(id: number): Promise<UserSummary>;
|
|
36
|
+
assignRole(actorId: number, userId: number, roleId: number, orgUnitId?: number | null): Promise<number>;
|
|
37
|
+
removeRole(actorId: number, userId: number, assignmentId: number): Promise<void>;
|
|
38
|
+
assignOrgUnit(actorId: number, userId: number, orgUnitId: number): Promise<void>;
|
|
39
|
+
removeOrgUnit(actorId: number, userId: number, orgUnitId: number): Promise<void>;
|
|
40
|
+
disable(actorId: number, userId: number): Promise<void>;
|
|
41
|
+
enable(actorId: number, userId: number): Promise<void>;
|
|
42
|
+
private find;
|
|
43
|
+
private hydrate;
|
|
44
|
+
}
|