domma-cms 0.23.0 → 0.25.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/CLAUDE.md +14 -0
- package/admin/js/api.js +1 -1
- package/admin/js/app.js +4 -4
- package/admin/js/lib/crud-tutorial.js +1 -1
- package/admin/js/lib/project-context.js +1 -1
- package/admin/js/templates/api-endpoint-editor.html +120 -0
- package/admin/js/templates/api-endpoints.html +13 -0
- package/admin/js/templates/api-tokens.html +13 -0
- package/admin/js/templates/effects.html +752 -752
- package/admin/js/templates/form-submissions.html +30 -30
- package/admin/js/templates/forms.html +17 -17
- package/admin/js/templates/my-profile.html +17 -17
- package/admin/js/templates/role-editor.html +70 -70
- package/admin/js/templates/roles.html +10 -10
- package/admin/js/views/api-endpoint-editor.js +1 -0
- package/admin/js/views/api-endpoints.js +7 -0
- package/admin/js/views/api-tokens.js +8 -0
- package/admin/js/views/collection-editor.js +4 -4
- package/admin/js/views/index.js +1 -1
- package/admin/js/views/project-detail.js +1 -1
- package/admin/js/views/roles.js +1 -1
- package/bin/lib/config-merge.js +44 -44
- package/bin/update.js +547 -547
- package/config/menus/admin-sidebar.json +13 -1
- package/package.json +1 -1
- package/server/middleware/auth.js +253 -253
- package/server/routes/api/api-endpoints.js +96 -0
- package/server/routes/api/api-tokens.js +83 -0
- package/server/routes/api/auth.js +309 -309
- package/server/routes/api/collections.js +114 -17
- package/server/routes/api/endpoints-public.js +88 -0
- package/server/routes/api/navigation.js +42 -42
- package/server/routes/api/settings.js +141 -141
- package/server/routes/public.js +202 -202
- package/server/server.js +16 -1
- package/server/services/apiEndpoints.js +402 -0
- package/server/services/apiTokens.js +273 -0
- package/server/services/email.js +167 -167
- package/server/services/permissionRegistry.js +26 -0
- package/server/services/presetCollections.js +54 -0
- package/server/services/projects.js +18 -2
- package/server/services/roles.js +16 -0
- package/server/services/scaffolder.js +54 -1
- package/server/services/sidebar-migration.js +45 -0
- package/server/services/userProfiles.js +199 -199
- package/server/services/users.js +302 -302
- package/config/connections.json.bak +0 -9
|
@@ -1,199 +1,199 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* User Profiles Service
|
|
3
|
-
* Preset Collection — stores extended profile data for each user,
|
|
4
|
-
* keyed by user UUID. Seeded on startup; entries auto-managed on user lifecycle.
|
|
5
|
-
*/
|
|
6
|
-
import fs from 'fs/promises';
|
|
7
|
-
import path from 'path';
|
|
8
|
-
import {config} from '../config.js';
|
|
9
|
-
|
|
10
|
-
const COLLECTIONS_DIR = path.resolve(config.content.collectionsDir);
|
|
11
|
-
const SLUG = 'user-profiles';
|
|
12
|
-
const DIR = path.join(COLLECTIONS_DIR, SLUG);
|
|
13
|
-
const SCHEMA_PATH = path.join(DIR, 'schema.json');
|
|
14
|
-
const DATA_PATH = path.join(DIR, 'data.json');
|
|
15
|
-
|
|
16
|
-
const USERS_DIR = path.resolve(config.content.usersDir);
|
|
17
|
-
|
|
18
|
-
const PRESET_SCHEMA = {
|
|
19
|
-
slug: SLUG,
|
|
20
|
-
title: 'User Profiles',
|
|
21
|
-
description: 'Extended profile data linked to users by ID.',
|
|
22
|
-
preset: true,
|
|
23
|
-
fields: [],
|
|
24
|
-
api: {
|
|
25
|
-
create: {enabled: false, access: 'admin'},
|
|
26
|
-
read: {enabled: false, access: 'admin'},
|
|
27
|
-
update: {enabled: false, access: 'admin'},
|
|
28
|
-
delete: {enabled: false, access: 'admin'}
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// ---------------------------------------------------------------------------
|
|
33
|
-
// Helpers
|
|
34
|
-
// ---------------------------------------------------------------------------
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Read data.json, returning an empty array on any error.
|
|
38
|
-
*
|
|
39
|
-
* @returns {Promise<object[]>}
|
|
40
|
-
*/
|
|
41
|
-
async function readData() {
|
|
42
|
-
try {
|
|
43
|
-
const raw = await fs.readFile(DATA_PATH, 'utf8');
|
|
44
|
-
return JSON.parse(raw);
|
|
45
|
-
} catch {
|
|
46
|
-
return [];
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Write entries array to data.json.
|
|
52
|
-
*
|
|
53
|
-
* @param {object[]} entries
|
|
54
|
-
* @returns {Promise<void>}
|
|
55
|
-
*/
|
|
56
|
-
async function writeData(entries) {
|
|
57
|
-
await fs.writeFile(DATA_PATH, JSON.stringify(entries, null, 2) + '\n', 'utf8');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// ---------------------------------------------------------------------------
|
|
61
|
-
// Public API
|
|
62
|
-
// ---------------------------------------------------------------------------
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Seed the preset collection on boot.
|
|
66
|
-
* Schema is always overwritten; data.json is only created if absent.
|
|
67
|
-
*
|
|
68
|
-
* @returns {Promise<void>}
|
|
69
|
-
*/
|
|
70
|
-
export async function seed() {
|
|
71
|
-
await fs.mkdir(DIR, {recursive: true});
|
|
72
|
-
|
|
73
|
-
// Only write schema if absent — admin edits to fields must be preserved across restarts
|
|
74
|
-
try {
|
|
75
|
-
await fs.access(SCHEMA_PATH);
|
|
76
|
-
} catch {
|
|
77
|
-
await fs.writeFile(SCHEMA_PATH, JSON.stringify(PRESET_SCHEMA, null, 2) + '\n', 'utf8');
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Only create data file if absent
|
|
81
|
-
try {
|
|
82
|
-
await fs.access(DATA_PATH);
|
|
83
|
-
} catch {
|
|
84
|
-
await writeData([]);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Ensure a profile entry exists for the given user ID.
|
|
90
|
-
* Creates an empty profile if one does not already exist.
|
|
91
|
-
*
|
|
92
|
-
* @param {string} userId
|
|
93
|
-
* @returns {Promise<void>}
|
|
94
|
-
*/
|
|
95
|
-
export async function ensureProfile(userId) {
|
|
96
|
-
const entries = await readData();
|
|
97
|
-
const exists = entries.some(e => e.id === userId);
|
|
98
|
-
if (!exists) {
|
|
99
|
-
entries.push({
|
|
100
|
-
id: userId,
|
|
101
|
-
data: {},
|
|
102
|
-
meta: {createdAt: new Date().toISOString(), updatedAt: new Date().toISOString()}
|
|
103
|
-
});
|
|
104
|
-
await writeData(entries);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Get the profile entry for a user, or null if not found.
|
|
110
|
-
*
|
|
111
|
-
* @param {string} userId
|
|
112
|
-
* @returns {Promise<object|null>}
|
|
113
|
-
*/
|
|
114
|
-
export async function getProfile(userId) {
|
|
115
|
-
const entries = await readData();
|
|
116
|
-
return entries.find(e => e.id === userId) || null;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Merge new data into a user's profile.
|
|
121
|
-
* Creates the profile entry if it does not exist.
|
|
122
|
-
*
|
|
123
|
-
* @param {string} userId
|
|
124
|
-
* @param {object} data - Flat key/value pairs to merge into profile data
|
|
125
|
-
* @returns {Promise<object>} Updated profile entry
|
|
126
|
-
*/
|
|
127
|
-
export async function updateProfile(userId, data) {
|
|
128
|
-
const entries = await readData();
|
|
129
|
-
const index = entries.findIndex(e => e.id === userId);
|
|
130
|
-
|
|
131
|
-
if (index === -1) {
|
|
132
|
-
const entry = {
|
|
133
|
-
id: userId,
|
|
134
|
-
data: {...data},
|
|
135
|
-
meta: {createdAt: new Date().toISOString(), updatedAt: new Date().toISOString()}
|
|
136
|
-
};
|
|
137
|
-
entries.push(entry);
|
|
138
|
-
await writeData(entries);
|
|
139
|
-
return entry;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
entries[index] = {
|
|
143
|
-
...entries[index],
|
|
144
|
-
data: {...entries[index].data, ...data},
|
|
145
|
-
meta: {...entries[index].meta, updatedAt: new Date().toISOString()}
|
|
146
|
-
};
|
|
147
|
-
await writeData(entries);
|
|
148
|
-
return entries[index];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Remove a user's profile entry. Silent if not found.
|
|
153
|
-
*
|
|
154
|
-
* @param {string} userId
|
|
155
|
-
* @returns {Promise<void>}
|
|
156
|
-
*/
|
|
157
|
-
export async function deleteProfile(userId) {
|
|
158
|
-
const entries = await readData();
|
|
159
|
-
const filtered = entries.filter(e => e.id !== userId);
|
|
160
|
-
if (filtered.length !== entries.length) {
|
|
161
|
-
await writeData(filtered);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Return a Map of userId → profile data object.
|
|
167
|
-
* Reads data.json once — use this for list endpoints to avoid N+1 reads.
|
|
168
|
-
*
|
|
169
|
-
* @returns {Promise<Map<string, object>>}
|
|
170
|
-
*/
|
|
171
|
-
export async function listProfiles() {
|
|
172
|
-
const entries = await readData();
|
|
173
|
-
const map = new Map();
|
|
174
|
-
for (const entry of entries) {
|
|
175
|
-
map.set(entry.id, entry.data || {});
|
|
176
|
-
}
|
|
177
|
-
return map;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Migration helper — ensure every existing user has a profile entry.
|
|
182
|
-
* Called once on startup after seed().
|
|
183
|
-
*
|
|
184
|
-
* @returns {Promise<void>}
|
|
185
|
-
*/
|
|
186
|
-
export async function ensureAllProfiles() {
|
|
187
|
-
let userFiles;
|
|
188
|
-
try {
|
|
189
|
-
userFiles = await fs.readdir(USERS_DIR);
|
|
190
|
-
} catch {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const jsonFiles = userFiles.filter(f => f.endsWith('.json'));
|
|
195
|
-
for (const file of jsonFiles) {
|
|
196
|
-
const userId = file.replace(/\.json$/, '');
|
|
197
|
-
await ensureProfile(userId);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* User Profiles Service
|
|
3
|
+
* Preset Collection — stores extended profile data for each user,
|
|
4
|
+
* keyed by user UUID. Seeded on startup; entries auto-managed on user lifecycle.
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'fs/promises';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import {config} from '../config.js';
|
|
9
|
+
|
|
10
|
+
const COLLECTIONS_DIR = path.resolve(config.content.collectionsDir);
|
|
11
|
+
const SLUG = 'user-profiles';
|
|
12
|
+
const DIR = path.join(COLLECTIONS_DIR, SLUG);
|
|
13
|
+
const SCHEMA_PATH = path.join(DIR, 'schema.json');
|
|
14
|
+
const DATA_PATH = path.join(DIR, 'data.json');
|
|
15
|
+
|
|
16
|
+
const USERS_DIR = path.resolve(config.content.usersDir);
|
|
17
|
+
|
|
18
|
+
const PRESET_SCHEMA = {
|
|
19
|
+
slug: SLUG,
|
|
20
|
+
title: 'User Profiles',
|
|
21
|
+
description: 'Extended profile data linked to users by ID.',
|
|
22
|
+
preset: true,
|
|
23
|
+
fields: [],
|
|
24
|
+
api: {
|
|
25
|
+
create: {enabled: false, access: 'admin'},
|
|
26
|
+
read: {enabled: false, access: 'admin'},
|
|
27
|
+
update: {enabled: false, access: 'admin'},
|
|
28
|
+
delete: {enabled: false, access: 'admin'}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// Helpers
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Read data.json, returning an empty array on any error.
|
|
38
|
+
*
|
|
39
|
+
* @returns {Promise<object[]>}
|
|
40
|
+
*/
|
|
41
|
+
async function readData() {
|
|
42
|
+
try {
|
|
43
|
+
const raw = await fs.readFile(DATA_PATH, 'utf8');
|
|
44
|
+
return JSON.parse(raw);
|
|
45
|
+
} catch {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Write entries array to data.json.
|
|
52
|
+
*
|
|
53
|
+
* @param {object[]} entries
|
|
54
|
+
* @returns {Promise<void>}
|
|
55
|
+
*/
|
|
56
|
+
async function writeData(entries) {
|
|
57
|
+
await fs.writeFile(DATA_PATH, JSON.stringify(entries, null, 2) + '\n', 'utf8');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// Public API
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Seed the preset collection on boot.
|
|
66
|
+
* Schema is always overwritten; data.json is only created if absent.
|
|
67
|
+
*
|
|
68
|
+
* @returns {Promise<void>}
|
|
69
|
+
*/
|
|
70
|
+
export async function seed() {
|
|
71
|
+
await fs.mkdir(DIR, {recursive: true});
|
|
72
|
+
|
|
73
|
+
// Only write schema if absent — admin edits to fields must be preserved across restarts
|
|
74
|
+
try {
|
|
75
|
+
await fs.access(SCHEMA_PATH);
|
|
76
|
+
} catch {
|
|
77
|
+
await fs.writeFile(SCHEMA_PATH, JSON.stringify(PRESET_SCHEMA, null, 2) + '\n', 'utf8');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Only create data file if absent
|
|
81
|
+
try {
|
|
82
|
+
await fs.access(DATA_PATH);
|
|
83
|
+
} catch {
|
|
84
|
+
await writeData([]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Ensure a profile entry exists for the given user ID.
|
|
90
|
+
* Creates an empty profile if one does not already exist.
|
|
91
|
+
*
|
|
92
|
+
* @param {string} userId
|
|
93
|
+
* @returns {Promise<void>}
|
|
94
|
+
*/
|
|
95
|
+
export async function ensureProfile(userId) {
|
|
96
|
+
const entries = await readData();
|
|
97
|
+
const exists = entries.some(e => e.id === userId);
|
|
98
|
+
if (!exists) {
|
|
99
|
+
entries.push({
|
|
100
|
+
id: userId,
|
|
101
|
+
data: {},
|
|
102
|
+
meta: {createdAt: new Date().toISOString(), updatedAt: new Date().toISOString()}
|
|
103
|
+
});
|
|
104
|
+
await writeData(entries);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get the profile entry for a user, or null if not found.
|
|
110
|
+
*
|
|
111
|
+
* @param {string} userId
|
|
112
|
+
* @returns {Promise<object|null>}
|
|
113
|
+
*/
|
|
114
|
+
export async function getProfile(userId) {
|
|
115
|
+
const entries = await readData();
|
|
116
|
+
return entries.find(e => e.id === userId) || null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Merge new data into a user's profile.
|
|
121
|
+
* Creates the profile entry if it does not exist.
|
|
122
|
+
*
|
|
123
|
+
* @param {string} userId
|
|
124
|
+
* @param {object} data - Flat key/value pairs to merge into profile data
|
|
125
|
+
* @returns {Promise<object>} Updated profile entry
|
|
126
|
+
*/
|
|
127
|
+
export async function updateProfile(userId, data) {
|
|
128
|
+
const entries = await readData();
|
|
129
|
+
const index = entries.findIndex(e => e.id === userId);
|
|
130
|
+
|
|
131
|
+
if (index === -1) {
|
|
132
|
+
const entry = {
|
|
133
|
+
id: userId,
|
|
134
|
+
data: {...data},
|
|
135
|
+
meta: {createdAt: new Date().toISOString(), updatedAt: new Date().toISOString()}
|
|
136
|
+
};
|
|
137
|
+
entries.push(entry);
|
|
138
|
+
await writeData(entries);
|
|
139
|
+
return entry;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
entries[index] = {
|
|
143
|
+
...entries[index],
|
|
144
|
+
data: {...entries[index].data, ...data},
|
|
145
|
+
meta: {...entries[index].meta, updatedAt: new Date().toISOString()}
|
|
146
|
+
};
|
|
147
|
+
await writeData(entries);
|
|
148
|
+
return entries[index];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Remove a user's profile entry. Silent if not found.
|
|
153
|
+
*
|
|
154
|
+
* @param {string} userId
|
|
155
|
+
* @returns {Promise<void>}
|
|
156
|
+
*/
|
|
157
|
+
export async function deleteProfile(userId) {
|
|
158
|
+
const entries = await readData();
|
|
159
|
+
const filtered = entries.filter(e => e.id !== userId);
|
|
160
|
+
if (filtered.length !== entries.length) {
|
|
161
|
+
await writeData(filtered);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Return a Map of userId → profile data object.
|
|
167
|
+
* Reads data.json once — use this for list endpoints to avoid N+1 reads.
|
|
168
|
+
*
|
|
169
|
+
* @returns {Promise<Map<string, object>>}
|
|
170
|
+
*/
|
|
171
|
+
export async function listProfiles() {
|
|
172
|
+
const entries = await readData();
|
|
173
|
+
const map = new Map();
|
|
174
|
+
for (const entry of entries) {
|
|
175
|
+
map.set(entry.id, entry.data || {});
|
|
176
|
+
}
|
|
177
|
+
return map;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Migration helper — ensure every existing user has a profile entry.
|
|
182
|
+
* Called once on startup after seed().
|
|
183
|
+
*
|
|
184
|
+
* @returns {Promise<void>}
|
|
185
|
+
*/
|
|
186
|
+
export async function ensureAllProfiles() {
|
|
187
|
+
let userFiles;
|
|
188
|
+
try {
|
|
189
|
+
userFiles = await fs.readdir(USERS_DIR);
|
|
190
|
+
} catch {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const jsonFiles = userFiles.filter(f => f.endsWith('.json'));
|
|
195
|
+
for (const file of jsonFiles) {
|
|
196
|
+
const userId = file.replace(/\.json$/, '');
|
|
197
|
+
await ensureProfile(userId);
|
|
198
|
+
}
|
|
199
|
+
}
|