pulse-hub 3.5.0 → 3.6.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/CHANGELOG.md +6 -0
- package/backend/.env.example +52 -52
- package/backend/agent.js +372 -372
- package/backend/cli.js +23 -1
- package/backend/emails/env-backup.html +52 -52
- package/backend/emails/invite.html +53 -53
- package/backend/lib/agent-rpc.js +72 -72
- package/backend/lib/async-route.js +17 -17
- package/backend/lib/authz/bootstrap.js +35 -35
- package/backend/lib/authz/catalog.js +291 -291
- package/backend/lib/authz/delegation.js +104 -104
- package/backend/lib/authz/effective.js +104 -104
- package/backend/lib/authz/engine.js +251 -251
- package/backend/lib/authz/index.js +134 -134
- package/backend/lib/authz/migrate.js +871 -871
- package/backend/lib/authz/mutate.js +87 -87
- package/backend/lib/authz/preview.js +327 -327
- package/backend/lib/authz/scope.js +282 -282
- package/backend/lib/env-mask.js +131 -131
- package/backend/lib/invite-helpers.js +83 -83
- package/backend/lib/invites.js +160 -160
- package/backend/lib/logs.js +244 -244
- package/backend/lib/metrics.js +249 -249
- package/backend/lib/relay-state.js +564 -564
- package/backend/lib/session.js +48 -48
- package/backend/middleware/authz.js +65 -65
- package/backend/public/assets/favicon.svg +6 -6
- package/backend/public/assets/index.js +1 -1
- package/backend/public/index.html +23 -24
- package/backend/routes/access.js +123 -123
- package/backend/routes/audit.js +41 -41
- package/backend/routes/auth.js +207 -207
- package/backend/routes/invites-public.js +91 -91
- package/backend/routes/invites.js +121 -121
- package/backend/routes/logs.js +68 -68
- package/backend/routes/metrics.js +76 -76
- package/backend/routes/policies.js +240 -240
- package/backend/routes/teams.js +221 -221
- package/backend/routes/users.js +100 -100
- package/backend/seed-admins.js +68 -68
- package/backend/server.js +106 -106
- package/backend/ws/agent-relay.js +159 -159
- package/package.json +1 -1
package/backend/routes/teams.js
CHANGED
|
@@ -1,221 +1,221 @@
|
|
|
1
|
-
// Teams: CRUD plus membership and policy attachment. Every route is gated by the policy
|
|
2
|
-
// engine (teams.* actions). Changes that alter anyone's effective access go through the
|
|
3
|
-
// delegation guard (you can't hand out what you don't hold) and an impact confirmation
|
|
4
|
-
// (409 with a preview unless the request carries confirmRevision). See docs/auth-model.md.
|
|
5
|
-
|
|
6
|
-
const express = require('express');
|
|
7
|
-
const db = require('../lib/db');
|
|
8
|
-
const store = require('../lib/authz-store');
|
|
9
|
-
const audit = require('../lib/audit');
|
|
10
|
-
const { forbidden, readJsonBody } = require('../lib/http-helpers');
|
|
11
|
-
const {
|
|
12
|
-
mutate,
|
|
13
|
-
sendJson,
|
|
14
|
-
requireDelegable,
|
|
15
|
-
requireConfirmation,
|
|
16
|
-
handleStale,
|
|
17
|
-
} = require('../lib/authz/mutate');
|
|
18
|
-
|
|
19
|
-
const router = express.Router();
|
|
20
|
-
|
|
21
|
-
function need(req, res, action) {
|
|
22
|
-
if (req.authz.can(action)) return true;
|
|
23
|
-
audit.record({ action, targetType: 'team', outcome: 'denied', summary: `Denied ${action}` }, req);
|
|
24
|
-
forbidden(res, 'You do not have permission to do that.');
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function shape(team) {
|
|
29
|
-
return {
|
|
30
|
-
id: team.id,
|
|
31
|
-
name: team.name,
|
|
32
|
-
description: team.description || '',
|
|
33
|
-
memberIds: store.getTeamMembers(team.id).map((m) => m.user_id),
|
|
34
|
-
policyIds: store.getPoliciesForTeam(team.id).map((p) => p.id),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function cleanName(raw) {
|
|
39
|
-
return typeof raw === 'string' ? raw.trim().slice(0, 80) : '';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function isUnique(e) {
|
|
43
|
-
return /UNIQUE/.test(String(e.message));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
router.get('/api/teams', (req, res) => {
|
|
47
|
-
if (!need(req, res, 'teams.view')) return;
|
|
48
|
-
sendJson(res, 200, store.listTeams().map(shape));
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
router.post('/api/teams', async (req, res) => {
|
|
52
|
-
if (!need(req, res, 'teams.create')) return;
|
|
53
|
-
const body = await readJsonBody(req);
|
|
54
|
-
const name = cleanName(body.name);
|
|
55
|
-
if (!name) return sendJson(res, 400, { error: 'A team needs a name.' });
|
|
56
|
-
try {
|
|
57
|
-
const team = mutate(() => {
|
|
58
|
-
const t = store.createTeam({ name, description: body.description }, req.sessionUser.id);
|
|
59
|
-
audit.record(
|
|
60
|
-
{
|
|
61
|
-
action: 'team.create',
|
|
62
|
-
targetType: 'team',
|
|
63
|
-
targetId: t.id,
|
|
64
|
-
outcome: 'succeeded',
|
|
65
|
-
summary: `Created team ${name}`,
|
|
66
|
-
},
|
|
67
|
-
req,
|
|
68
|
-
);
|
|
69
|
-
return t;
|
|
70
|
-
});
|
|
71
|
-
sendJson(res, 200, shape(team));
|
|
72
|
-
} catch (e) {
|
|
73
|
-
if (isUnique(e)) return sendJson(res, 409, { error: 'A team with that name already exists.' });
|
|
74
|
-
throw e;
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
router.patch('/api/teams/:id', async (req, res) => {
|
|
79
|
-
if (!need(req, res, 'teams.update')) return;
|
|
80
|
-
const team = store.getTeam(req.params.id);
|
|
81
|
-
if (!team) return sendJson(res, 404, { error: 'No such team.' });
|
|
82
|
-
const body = await readJsonBody(req);
|
|
83
|
-
const name = body.name === undefined ? team.name : cleanName(body.name);
|
|
84
|
-
if (!name) return sendJson(res, 400, { error: 'A team needs a name.' });
|
|
85
|
-
const description = body.description === undefined ? team.description : body.description;
|
|
86
|
-
try {
|
|
87
|
-
const updated = mutate(() => {
|
|
88
|
-
const t = store.updateTeam(team.id, { name, description });
|
|
89
|
-
audit.record(
|
|
90
|
-
{
|
|
91
|
-
action: 'team.update',
|
|
92
|
-
targetType: 'team',
|
|
93
|
-
targetId: team.id,
|
|
94
|
-
outcome: 'succeeded',
|
|
95
|
-
summary: `Updated team ${name}`,
|
|
96
|
-
},
|
|
97
|
-
req,
|
|
98
|
-
);
|
|
99
|
-
return t;
|
|
100
|
-
});
|
|
101
|
-
sendJson(res, 200, shape(updated));
|
|
102
|
-
} catch (e) {
|
|
103
|
-
if (isUnique(e)) return sendJson(res, 409, { error: 'A team with that name already exists.' });
|
|
104
|
-
throw e;
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
router.delete('/api/teams/:id', (req, res) => {
|
|
109
|
-
if (!need(req, res, 'teams.delete')) return;
|
|
110
|
-
const team = store.getTeam(req.params.id);
|
|
111
|
-
if (!team) return sendJson(res, 404, { error: 'No such team.' });
|
|
112
|
-
const change = { kind: 'team.delete', teamId: team.id };
|
|
113
|
-
const gate = requireConfirmation(req, res, change, req.query.confirmRevision);
|
|
114
|
-
if (!gate.proceed) return;
|
|
115
|
-
try {
|
|
116
|
-
mutate(
|
|
117
|
-
() => {
|
|
118
|
-
store.deleteTeam(team.id);
|
|
119
|
-
audit.record(
|
|
120
|
-
{
|
|
121
|
-
action: 'team.delete',
|
|
122
|
-
targetType: 'team',
|
|
123
|
-
targetId: team.id,
|
|
124
|
-
outcome: 'succeeded',
|
|
125
|
-
summary: `Deleted team ${team.name}`,
|
|
126
|
-
},
|
|
127
|
-
req,
|
|
128
|
-
);
|
|
129
|
-
},
|
|
130
|
-
{ expectedRevision: gate.expectedRevision },
|
|
131
|
-
);
|
|
132
|
-
} catch (e) {
|
|
133
|
-
if (handleStale(res, e, change, req)) return;
|
|
134
|
-
throw e;
|
|
135
|
-
}
|
|
136
|
-
sendJson(res, 200, { ok: true });
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
router.put('/api/teams/:id/members', async (req, res) => {
|
|
140
|
-
if (!need(req, res, 'teams.members.manage')) return;
|
|
141
|
-
const team = store.getTeam(req.params.id);
|
|
142
|
-
if (!team) return sendJson(res, 404, { error: 'No such team.' });
|
|
143
|
-
const body = await readJsonBody(req);
|
|
144
|
-
const userIds = Array.isArray(body.userIds) ? [...new Set(body.userIds.map(String))] : null;
|
|
145
|
-
if (!userIds) return sendJson(res, 400, { error: 'userIds must be an array.' });
|
|
146
|
-
if (userIds.some((id) => !db.getUserById(id)))
|
|
147
|
-
return sendJson(res, 400, { error: 'One of those users does not exist.' });
|
|
148
|
-
const change = { kind: 'team.members.set', teamId: team.id, userIds };
|
|
149
|
-
if (!requireDelegable(req, res, change)) return;
|
|
150
|
-
const gate = requireConfirmation(req, res, change, body.confirmRevision);
|
|
151
|
-
if (!gate.proceed) return;
|
|
152
|
-
try {
|
|
153
|
-
mutate(
|
|
154
|
-
() => {
|
|
155
|
-
store.setTeamMembers(team.id, userIds, req.sessionUser.id);
|
|
156
|
-
audit.record(
|
|
157
|
-
{
|
|
158
|
-
action: 'team.members.set',
|
|
159
|
-
targetType: 'team',
|
|
160
|
-
targetId: team.id,
|
|
161
|
-
outcome: 'succeeded',
|
|
162
|
-
summary: `Set ${userIds.length} member(s) of ${team.name}`,
|
|
163
|
-
metadata: { count: userIds.length },
|
|
164
|
-
},
|
|
165
|
-
req,
|
|
166
|
-
);
|
|
167
|
-
},
|
|
168
|
-
{ expectedRevision: gate.expectedRevision },
|
|
169
|
-
);
|
|
170
|
-
} catch (e) {
|
|
171
|
-
if (handleStale(res, e, change, req)) return;
|
|
172
|
-
throw e;
|
|
173
|
-
}
|
|
174
|
-
sendJson(res, 200, shape(store.getTeam(team.id)));
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
router.put('/api/teams/:id/policies', async (req, res) => {
|
|
178
|
-
if (!need(req, res, 'teams.policies.manage')) return;
|
|
179
|
-
const team = store.getTeam(req.params.id);
|
|
180
|
-
if (!team) return sendJson(res, 404, { error: 'No such team.' });
|
|
181
|
-
const body = await readJsonBody(req);
|
|
182
|
-
const policyIds = Array.isArray(body.policyIds) ? [...new Set(body.policyIds.map(String))] : null;
|
|
183
|
-
if (!policyIds) return sendJson(res, 400, { error: 'policyIds must be an array.' });
|
|
184
|
-
for (const id of policyIds) {
|
|
185
|
-
const p = store.getPolicy(id);
|
|
186
|
-
if (!p) return sendJson(res, 400, { error: 'One of those policies does not exist.' });
|
|
187
|
-
if (p.builtin)
|
|
188
|
-
return sendJson(res, 400, {
|
|
189
|
-
error: 'Built-in templates cannot be attached directly. Copy the template first.',
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
const change = { kind: 'team.policies.set', teamId: team.id, policyIds };
|
|
193
|
-
if (!requireDelegable(req, res, change)) return;
|
|
194
|
-
const gate = requireConfirmation(req, res, change, body.confirmRevision);
|
|
195
|
-
if (!gate.proceed) return;
|
|
196
|
-
try {
|
|
197
|
-
mutate(
|
|
198
|
-
() => {
|
|
199
|
-
store.setTeamPolicies(team.id, policyIds, req.sessionUser.id);
|
|
200
|
-
audit.record(
|
|
201
|
-
{
|
|
202
|
-
action: 'team.policies.set',
|
|
203
|
-
targetType: 'team',
|
|
204
|
-
targetId: team.id,
|
|
205
|
-
outcome: 'succeeded',
|
|
206
|
-
summary: `Set ${policyIds.length} polic(ies) on ${team.name}`,
|
|
207
|
-
metadata: { count: policyIds.length },
|
|
208
|
-
},
|
|
209
|
-
req,
|
|
210
|
-
);
|
|
211
|
-
},
|
|
212
|
-
{ expectedRevision: gate.expectedRevision },
|
|
213
|
-
);
|
|
214
|
-
} catch (e) {
|
|
215
|
-
if (handleStale(res, e, change, req)) return;
|
|
216
|
-
throw e;
|
|
217
|
-
}
|
|
218
|
-
sendJson(res, 200, shape(store.getTeam(team.id)));
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
module.exports = router;
|
|
1
|
+
// Teams: CRUD plus membership and policy attachment. Every route is gated by the policy
|
|
2
|
+
// engine (teams.* actions). Changes that alter anyone's effective access go through the
|
|
3
|
+
// delegation guard (you can't hand out what you don't hold) and an impact confirmation
|
|
4
|
+
// (409 with a preview unless the request carries confirmRevision). See docs/auth-model.md.
|
|
5
|
+
|
|
6
|
+
const express = require('express');
|
|
7
|
+
const db = require('../lib/db');
|
|
8
|
+
const store = require('../lib/authz-store');
|
|
9
|
+
const audit = require('../lib/audit');
|
|
10
|
+
const { forbidden, readJsonBody } = require('../lib/http-helpers');
|
|
11
|
+
const {
|
|
12
|
+
mutate,
|
|
13
|
+
sendJson,
|
|
14
|
+
requireDelegable,
|
|
15
|
+
requireConfirmation,
|
|
16
|
+
handleStale,
|
|
17
|
+
} = require('../lib/authz/mutate');
|
|
18
|
+
|
|
19
|
+
const router = express.Router();
|
|
20
|
+
|
|
21
|
+
function need(req, res, action) {
|
|
22
|
+
if (req.authz.can(action)) return true;
|
|
23
|
+
audit.record({ action, targetType: 'team', outcome: 'denied', summary: `Denied ${action}` }, req);
|
|
24
|
+
forbidden(res, 'You do not have permission to do that.');
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function shape(team) {
|
|
29
|
+
return {
|
|
30
|
+
id: team.id,
|
|
31
|
+
name: team.name,
|
|
32
|
+
description: team.description || '',
|
|
33
|
+
memberIds: store.getTeamMembers(team.id).map((m) => m.user_id),
|
|
34
|
+
policyIds: store.getPoliciesForTeam(team.id).map((p) => p.id),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function cleanName(raw) {
|
|
39
|
+
return typeof raw === 'string' ? raw.trim().slice(0, 80) : '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isUnique(e) {
|
|
43
|
+
return /UNIQUE/.test(String(e.message));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
router.get('/api/teams', (req, res) => {
|
|
47
|
+
if (!need(req, res, 'teams.view')) return;
|
|
48
|
+
sendJson(res, 200, store.listTeams().map(shape));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
router.post('/api/teams', async (req, res) => {
|
|
52
|
+
if (!need(req, res, 'teams.create')) return;
|
|
53
|
+
const body = await readJsonBody(req);
|
|
54
|
+
const name = cleanName(body.name);
|
|
55
|
+
if (!name) return sendJson(res, 400, { error: 'A team needs a name.' });
|
|
56
|
+
try {
|
|
57
|
+
const team = mutate(() => {
|
|
58
|
+
const t = store.createTeam({ name, description: body.description }, req.sessionUser.id);
|
|
59
|
+
audit.record(
|
|
60
|
+
{
|
|
61
|
+
action: 'team.create',
|
|
62
|
+
targetType: 'team',
|
|
63
|
+
targetId: t.id,
|
|
64
|
+
outcome: 'succeeded',
|
|
65
|
+
summary: `Created team ${name}`,
|
|
66
|
+
},
|
|
67
|
+
req,
|
|
68
|
+
);
|
|
69
|
+
return t;
|
|
70
|
+
});
|
|
71
|
+
sendJson(res, 200, shape(team));
|
|
72
|
+
} catch (e) {
|
|
73
|
+
if (isUnique(e)) return sendJson(res, 409, { error: 'A team with that name already exists.' });
|
|
74
|
+
throw e;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
router.patch('/api/teams/:id', async (req, res) => {
|
|
79
|
+
if (!need(req, res, 'teams.update')) return;
|
|
80
|
+
const team = store.getTeam(req.params.id);
|
|
81
|
+
if (!team) return sendJson(res, 404, { error: 'No such team.' });
|
|
82
|
+
const body = await readJsonBody(req);
|
|
83
|
+
const name = body.name === undefined ? team.name : cleanName(body.name);
|
|
84
|
+
if (!name) return sendJson(res, 400, { error: 'A team needs a name.' });
|
|
85
|
+
const description = body.description === undefined ? team.description : body.description;
|
|
86
|
+
try {
|
|
87
|
+
const updated = mutate(() => {
|
|
88
|
+
const t = store.updateTeam(team.id, { name, description });
|
|
89
|
+
audit.record(
|
|
90
|
+
{
|
|
91
|
+
action: 'team.update',
|
|
92
|
+
targetType: 'team',
|
|
93
|
+
targetId: team.id,
|
|
94
|
+
outcome: 'succeeded',
|
|
95
|
+
summary: `Updated team ${name}`,
|
|
96
|
+
},
|
|
97
|
+
req,
|
|
98
|
+
);
|
|
99
|
+
return t;
|
|
100
|
+
});
|
|
101
|
+
sendJson(res, 200, shape(updated));
|
|
102
|
+
} catch (e) {
|
|
103
|
+
if (isUnique(e)) return sendJson(res, 409, { error: 'A team with that name already exists.' });
|
|
104
|
+
throw e;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
router.delete('/api/teams/:id', (req, res) => {
|
|
109
|
+
if (!need(req, res, 'teams.delete')) return;
|
|
110
|
+
const team = store.getTeam(req.params.id);
|
|
111
|
+
if (!team) return sendJson(res, 404, { error: 'No such team.' });
|
|
112
|
+
const change = { kind: 'team.delete', teamId: team.id };
|
|
113
|
+
const gate = requireConfirmation(req, res, change, req.query.confirmRevision);
|
|
114
|
+
if (!gate.proceed) return;
|
|
115
|
+
try {
|
|
116
|
+
mutate(
|
|
117
|
+
() => {
|
|
118
|
+
store.deleteTeam(team.id);
|
|
119
|
+
audit.record(
|
|
120
|
+
{
|
|
121
|
+
action: 'team.delete',
|
|
122
|
+
targetType: 'team',
|
|
123
|
+
targetId: team.id,
|
|
124
|
+
outcome: 'succeeded',
|
|
125
|
+
summary: `Deleted team ${team.name}`,
|
|
126
|
+
},
|
|
127
|
+
req,
|
|
128
|
+
);
|
|
129
|
+
},
|
|
130
|
+
{ expectedRevision: gate.expectedRevision },
|
|
131
|
+
);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
if (handleStale(res, e, change, req)) return;
|
|
134
|
+
throw e;
|
|
135
|
+
}
|
|
136
|
+
sendJson(res, 200, { ok: true });
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
router.put('/api/teams/:id/members', async (req, res) => {
|
|
140
|
+
if (!need(req, res, 'teams.members.manage')) return;
|
|
141
|
+
const team = store.getTeam(req.params.id);
|
|
142
|
+
if (!team) return sendJson(res, 404, { error: 'No such team.' });
|
|
143
|
+
const body = await readJsonBody(req);
|
|
144
|
+
const userIds = Array.isArray(body.userIds) ? [...new Set(body.userIds.map(String))] : null;
|
|
145
|
+
if (!userIds) return sendJson(res, 400, { error: 'userIds must be an array.' });
|
|
146
|
+
if (userIds.some((id) => !db.getUserById(id)))
|
|
147
|
+
return sendJson(res, 400, { error: 'One of those users does not exist.' });
|
|
148
|
+
const change = { kind: 'team.members.set', teamId: team.id, userIds };
|
|
149
|
+
if (!requireDelegable(req, res, change)) return;
|
|
150
|
+
const gate = requireConfirmation(req, res, change, body.confirmRevision);
|
|
151
|
+
if (!gate.proceed) return;
|
|
152
|
+
try {
|
|
153
|
+
mutate(
|
|
154
|
+
() => {
|
|
155
|
+
store.setTeamMembers(team.id, userIds, req.sessionUser.id);
|
|
156
|
+
audit.record(
|
|
157
|
+
{
|
|
158
|
+
action: 'team.members.set',
|
|
159
|
+
targetType: 'team',
|
|
160
|
+
targetId: team.id,
|
|
161
|
+
outcome: 'succeeded',
|
|
162
|
+
summary: `Set ${userIds.length} member(s) of ${team.name}`,
|
|
163
|
+
metadata: { count: userIds.length },
|
|
164
|
+
},
|
|
165
|
+
req,
|
|
166
|
+
);
|
|
167
|
+
},
|
|
168
|
+
{ expectedRevision: gate.expectedRevision },
|
|
169
|
+
);
|
|
170
|
+
} catch (e) {
|
|
171
|
+
if (handleStale(res, e, change, req)) return;
|
|
172
|
+
throw e;
|
|
173
|
+
}
|
|
174
|
+
sendJson(res, 200, shape(store.getTeam(team.id)));
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
router.put('/api/teams/:id/policies', async (req, res) => {
|
|
178
|
+
if (!need(req, res, 'teams.policies.manage')) return;
|
|
179
|
+
const team = store.getTeam(req.params.id);
|
|
180
|
+
if (!team) return sendJson(res, 404, { error: 'No such team.' });
|
|
181
|
+
const body = await readJsonBody(req);
|
|
182
|
+
const policyIds = Array.isArray(body.policyIds) ? [...new Set(body.policyIds.map(String))] : null;
|
|
183
|
+
if (!policyIds) return sendJson(res, 400, { error: 'policyIds must be an array.' });
|
|
184
|
+
for (const id of policyIds) {
|
|
185
|
+
const p = store.getPolicy(id);
|
|
186
|
+
if (!p) return sendJson(res, 400, { error: 'One of those policies does not exist.' });
|
|
187
|
+
if (p.builtin)
|
|
188
|
+
return sendJson(res, 400, {
|
|
189
|
+
error: 'Built-in templates cannot be attached directly. Copy the template first.',
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const change = { kind: 'team.policies.set', teamId: team.id, policyIds };
|
|
193
|
+
if (!requireDelegable(req, res, change)) return;
|
|
194
|
+
const gate = requireConfirmation(req, res, change, body.confirmRevision);
|
|
195
|
+
if (!gate.proceed) return;
|
|
196
|
+
try {
|
|
197
|
+
mutate(
|
|
198
|
+
() => {
|
|
199
|
+
store.setTeamPolicies(team.id, policyIds, req.sessionUser.id);
|
|
200
|
+
audit.record(
|
|
201
|
+
{
|
|
202
|
+
action: 'team.policies.set',
|
|
203
|
+
targetType: 'team',
|
|
204
|
+
targetId: team.id,
|
|
205
|
+
outcome: 'succeeded',
|
|
206
|
+
summary: `Set ${policyIds.length} polic(ies) on ${team.name}`,
|
|
207
|
+
metadata: { count: policyIds.length },
|
|
208
|
+
},
|
|
209
|
+
req,
|
|
210
|
+
);
|
|
211
|
+
},
|
|
212
|
+
{ expectedRevision: gate.expectedRevision },
|
|
213
|
+
);
|
|
214
|
+
} catch (e) {
|
|
215
|
+
if (handleStale(res, e, change, req)) return;
|
|
216
|
+
throw e;
|
|
217
|
+
}
|
|
218
|
+
sendJson(res, 200, shape(store.getTeam(team.id)));
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
module.exports = router;
|
package/backend/routes/users.js
CHANGED
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
// Session info + people management (list, remove). Access itself is granted via teams and
|
|
2
|
-
// policies (routes/teams.js, routes/policies.js) — a person's role no longer exists.
|
|
3
|
-
// See docs/auth-model.md.
|
|
4
|
-
|
|
5
|
-
const express = require('express');
|
|
6
|
-
const db = require('../lib/db');
|
|
7
|
-
const store = require('../lib/authz-store');
|
|
8
|
-
const audit = require('../lib/audit');
|
|
9
|
-
const gmailSend = require('../lib/gmail-send');
|
|
10
|
-
const { forbidden } = require('../lib/http-helpers');
|
|
11
|
-
const { mutate, sendJson } = require('../lib/authz/mutate');
|
|
12
|
-
|
|
13
|
-
const router = express.Router();
|
|
14
|
-
|
|
15
|
-
// Every action id the user holds anywhere — drives nav/visibility in the UI. Root holds
|
|
16
|
-
// everything. (Enforcement is always server-side; this is only for showing/hiding things.)
|
|
17
|
-
function heldActions(authz, user) {
|
|
18
|
-
const { ACTIONS } = require('../lib/authz/catalog');
|
|
19
|
-
if (user.is_root) return ACTIONS.map((a) => a.id);
|
|
20
|
-
return ACTIONS.filter((a) => authz.anyScope(a.id)).map((a) => a.id);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
router.get('/api/session-info', (req, res) => {
|
|
24
|
-
const u = req.sessionUser;
|
|
25
|
-
sendJson(res, 200, {
|
|
26
|
-
email: u.email,
|
|
27
|
-
picture: u.picture,
|
|
28
|
-
isRoot: !!u.is_root,
|
|
29
|
-
teams: store.getUserTeams(u.id).map((t) => ({ id: t.id, name: t.name })),
|
|
30
|
-
permissions: heldActions(req.authz, u),
|
|
31
|
-
inviteEmailEnabled: gmailSend.isEnabled(),
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
router.get('/api/users', (req, res) => {
|
|
36
|
-
if (!req.authz.can('people.view'))
|
|
37
|
-
return forbidden(res, 'You do not have permission to do that.');
|
|
38
|
-
const teamIdsByUser = new Map();
|
|
39
|
-
for (const m of store.loadState().teamMembers) {
|
|
40
|
-
if (!teamIdsByUser.has(m.user_id)) teamIdsByUser.set(m.user_id, []);
|
|
41
|
-
teamIdsByUser.get(m.user_id).push(m.team_id);
|
|
42
|
-
}
|
|
43
|
-
sendJson(
|
|
44
|
-
res,
|
|
45
|
-
200,
|
|
46
|
-
db.listUsers().map((u) => ({
|
|
47
|
-
id: u.id,
|
|
48
|
-
email: u.email,
|
|
49
|
-
picture: u.picture,
|
|
50
|
-
isRoot: !!u.is_root,
|
|
51
|
-
isYou: u.id === req.sessionUser.id,
|
|
52
|
-
linked: !!u.google_id,
|
|
53
|
-
createdAt: u.created_at,
|
|
54
|
-
teamIds: teamIdsByUser.get(u.id) || [],
|
|
55
|
-
invitedByLabel: undefined,
|
|
56
|
-
})),
|
|
57
|
-
);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
router.delete('/api/users/:id', (req, res) => {
|
|
61
|
-
if (!req.authz.can('people.remove')) {
|
|
62
|
-
audit.record(
|
|
63
|
-
{
|
|
64
|
-
action: 'people.remove',
|
|
65
|
-
targetType: 'user',
|
|
66
|
-
targetId: req.params.id,
|
|
67
|
-
outcome: 'denied',
|
|
68
|
-
summary: 'Denied people.remove',
|
|
69
|
-
},
|
|
70
|
-
req,
|
|
71
|
-
);
|
|
72
|
-
return forbidden(res, 'You do not have permission to do that.');
|
|
73
|
-
}
|
|
74
|
-
const target = db.getUserById(req.params.id);
|
|
75
|
-
if (!target) return sendJson(res, 404, { error: 'No such user.' });
|
|
76
|
-
if (target.id === req.sessionUser.id) return forbidden(res, "You can't remove yourself.");
|
|
77
|
-
if (target.is_root) return forbidden(res, 'Root users cannot be removed here.');
|
|
78
|
-
try {
|
|
79
|
-
mutate(() => {
|
|
80
|
-
db.deleteUser(target.id);
|
|
81
|
-
audit.record(
|
|
82
|
-
{
|
|
83
|
-
action: 'people.remove',
|
|
84
|
-
targetType: 'user',
|
|
85
|
-
targetId: target.id,
|
|
86
|
-
outcome: 'succeeded',
|
|
87
|
-
summary: `Removed ${target.email || target.id}`,
|
|
88
|
-
},
|
|
89
|
-
req,
|
|
90
|
-
);
|
|
91
|
-
});
|
|
92
|
-
} catch (e) {
|
|
93
|
-
return sendJson(res, 400, {
|
|
94
|
-
error: 'This user has sent invites still on record and cannot be removed.',
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
sendJson(res, 200, { ok: true });
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
module.exports = router;
|
|
1
|
+
// Session info + people management (list, remove). Access itself is granted via teams and
|
|
2
|
+
// policies (routes/teams.js, routes/policies.js) — a person's role no longer exists.
|
|
3
|
+
// See docs/auth-model.md.
|
|
4
|
+
|
|
5
|
+
const express = require('express');
|
|
6
|
+
const db = require('../lib/db');
|
|
7
|
+
const store = require('../lib/authz-store');
|
|
8
|
+
const audit = require('../lib/audit');
|
|
9
|
+
const gmailSend = require('../lib/gmail-send');
|
|
10
|
+
const { forbidden } = require('../lib/http-helpers');
|
|
11
|
+
const { mutate, sendJson } = require('../lib/authz/mutate');
|
|
12
|
+
|
|
13
|
+
const router = express.Router();
|
|
14
|
+
|
|
15
|
+
// Every action id the user holds anywhere — drives nav/visibility in the UI. Root holds
|
|
16
|
+
// everything. (Enforcement is always server-side; this is only for showing/hiding things.)
|
|
17
|
+
function heldActions(authz, user) {
|
|
18
|
+
const { ACTIONS } = require('../lib/authz/catalog');
|
|
19
|
+
if (user.is_root) return ACTIONS.map((a) => a.id);
|
|
20
|
+
return ACTIONS.filter((a) => authz.anyScope(a.id)).map((a) => a.id);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
router.get('/api/session-info', (req, res) => {
|
|
24
|
+
const u = req.sessionUser;
|
|
25
|
+
sendJson(res, 200, {
|
|
26
|
+
email: u.email,
|
|
27
|
+
picture: u.picture,
|
|
28
|
+
isRoot: !!u.is_root,
|
|
29
|
+
teams: store.getUserTeams(u.id).map((t) => ({ id: t.id, name: t.name })),
|
|
30
|
+
permissions: heldActions(req.authz, u),
|
|
31
|
+
inviteEmailEnabled: gmailSend.isEnabled(),
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
router.get('/api/users', (req, res) => {
|
|
36
|
+
if (!req.authz.can('people.view'))
|
|
37
|
+
return forbidden(res, 'You do not have permission to do that.');
|
|
38
|
+
const teamIdsByUser = new Map();
|
|
39
|
+
for (const m of store.loadState().teamMembers) {
|
|
40
|
+
if (!teamIdsByUser.has(m.user_id)) teamIdsByUser.set(m.user_id, []);
|
|
41
|
+
teamIdsByUser.get(m.user_id).push(m.team_id);
|
|
42
|
+
}
|
|
43
|
+
sendJson(
|
|
44
|
+
res,
|
|
45
|
+
200,
|
|
46
|
+
db.listUsers().map((u) => ({
|
|
47
|
+
id: u.id,
|
|
48
|
+
email: u.email,
|
|
49
|
+
picture: u.picture,
|
|
50
|
+
isRoot: !!u.is_root,
|
|
51
|
+
isYou: u.id === req.sessionUser.id,
|
|
52
|
+
linked: !!u.google_id,
|
|
53
|
+
createdAt: u.created_at,
|
|
54
|
+
teamIds: teamIdsByUser.get(u.id) || [],
|
|
55
|
+
invitedByLabel: undefined,
|
|
56
|
+
})),
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
router.delete('/api/users/:id', (req, res) => {
|
|
61
|
+
if (!req.authz.can('people.remove')) {
|
|
62
|
+
audit.record(
|
|
63
|
+
{
|
|
64
|
+
action: 'people.remove',
|
|
65
|
+
targetType: 'user',
|
|
66
|
+
targetId: req.params.id,
|
|
67
|
+
outcome: 'denied',
|
|
68
|
+
summary: 'Denied people.remove',
|
|
69
|
+
},
|
|
70
|
+
req,
|
|
71
|
+
);
|
|
72
|
+
return forbidden(res, 'You do not have permission to do that.');
|
|
73
|
+
}
|
|
74
|
+
const target = db.getUserById(req.params.id);
|
|
75
|
+
if (!target) return sendJson(res, 404, { error: 'No such user.' });
|
|
76
|
+
if (target.id === req.sessionUser.id) return forbidden(res, "You can't remove yourself.");
|
|
77
|
+
if (target.is_root) return forbidden(res, 'Root users cannot be removed here.');
|
|
78
|
+
try {
|
|
79
|
+
mutate(() => {
|
|
80
|
+
db.deleteUser(target.id);
|
|
81
|
+
audit.record(
|
|
82
|
+
{
|
|
83
|
+
action: 'people.remove',
|
|
84
|
+
targetType: 'user',
|
|
85
|
+
targetId: target.id,
|
|
86
|
+
outcome: 'succeeded',
|
|
87
|
+
summary: `Removed ${target.email || target.id}`,
|
|
88
|
+
},
|
|
89
|
+
req,
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
} catch (e) {
|
|
93
|
+
return sendJson(res, 400, {
|
|
94
|
+
error: 'This user has sent invites still on record and cannot be removed.',
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
sendJson(res, 200, { ok: true });
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
module.exports = router;
|