-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocumentRoots.ts
More file actions
174 lines (161 loc) Β· 6.32 KB
/
documentRoots.ts
File metadata and controls
174 lines (161 loc) Β· 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { RequestHandler } from 'express';
import DocumentRoot, { Config as CreateConfig, UpdateConfig } from '../models/DocumentRoot.js';
import { ChangedRecord, IoEvent, RecordType } from '../routes/socketEventTypes.js';
import { IoRoom } from '../routes/socketEvents.js';
import { HTTP400Error, HTTP403Error } from '../utils/errors/Errors.js';
import Document from '../models/Document.js';
import { NoneAccess, RO_RW_DocumentRootAccess } from '../helpers/accessPolicy.js';
import { hasElevatedAccess } from '../models/User.js';
export const find: RequestHandler<{ id: string }> = async (req, res, next) => {
const document = await DocumentRoot.findModel((req as any).user!, req.params.id);
res.json(document);
};
export const findMany: RequestHandler<any, any, any, { ids: string[] }> = async (req, res, next) => {
const ids = Array.isArray(req.query.ids) ? req.query.ids : [req.query.ids];
if (ids.length === 0 || !req.query.ids) {
return res.json([]);
}
const documents = await DocumentRoot.findManyModels((req as any).user!.id, ids);
res.json(documents);
};
export const findManyFor: RequestHandler<
{ id: string /** userId */ },
any,
any,
{ ignoreMissingRoots?: boolean; type?: string; ids: string[] }
> = async (req, res, next) => {
if (!req.params.id) {
throw new HTTP400Error('Missing user id');
}
const canLoad = (req as any).user!.id === req.params.id || hasElevatedAccess((req as any).user?.role);
if (!canLoad) {
throw new HTTP403Error('Not Authorized');
}
const ids = Array.isArray(req.query.ids) ? req.query.ids : [req.query.ids];
if (ids.length === 0 || !req.query.ids) {
return res.json([]);
}
const documents = await DocumentRoot.findManyModels(req.params.id, ids, {
ignoreMissingRoots: !!req.query.ignoreMissingRoots,
documentType: req.query.type && (req.query.type as string | undefined)
});
res.json(documents);
};
export const findMultipleFor: RequestHandler<
{ id: string /** userId */ },
any,
{ documentRootIds: string[]; ignoreMissingRoots?: boolean; type?: string }
> = async (req, res, next) => {
if (!req.params.id) {
throw new HTTP400Error('Missing user id');
}
const canLoad = req.user!.id === req.params.id || hasElevatedAccess(req.user?.role);
if (!canLoad) {
throw new HTTP403Error('Not Authorized');
}
const ids = req.body.documentRootIds;
if (ids.length === 0) {
return res.json([]);
}
const documents = await DocumentRoot.findManyModels(req.params.id, ids, {
ignoreMissingRoots: !!req.body.ignoreMissingRoots,
documentType: req.body.type && (req.body.type as string | undefined)
});
res.json(documents);
};
export const allDocuments: RequestHandler<any, any, any, { rids: string[] }> = async (req, res, next) => {
if (!hasElevatedAccess((req as any).user!.role)) {
throw new HTTP403Error('Not Authorized');
}
const ids = Array.isArray(req.query.rids) ? req.query.rids : [req.query.rids];
if (ids.length === 0) {
return res.json([]);
}
const documents = await Document.allOfDocumentRoots((req as any).user!, ids);
res.json(documents);
};
export const multipleDocuments: RequestHandler<
any,
any,
{ documentRootIds: string[]; userId?: string }
> = async (req, res, next) => {
const user = req.user;
if (!hasElevatedAccess(user.role)) {
throw new HTTP403Error('Not Authorized');
}
const ids = req.body.documentRootIds;
if (ids.length === 0) {
return res.json([]);
}
const documents = await Document.allOfDocumentRoots(
{ role: user.role, id: req.body.userId ?? user.id },
ids
);
res.json(documents);
};
export const create: RequestHandler<{ id: string }, any, CreateConfig | undefined> = async (
req,
res,
next
) => {
const documentRoot = await DocumentRoot.createModel(req.params.id, req.body);
/**
* Notifications to
* - the user who created the document
* - users with ro/rw access to the document root
* - student groups with ro/rw access to the document root
*/
const groupIds = documentRoot.groupPermissions
.filter((p) => !NoneAccess.has(p.access))
.map((p) => p.groupId);
const userIds = documentRoot.userPermissions
.filter((p) => !NoneAccess.has(p.access))
.map((p) => p.userId);
const sharedAccess = RO_RW_DocumentRootAccess.has(documentRoot.sharedAccess) ? IoRoom.ALL : IoRoom.ADMIN;
res.notifications = [
{
event: IoEvent.NEW_RECORD,
message: { type: RecordType.DocumentRoot, record: documentRoot },
to: [...groupIds, ...userIds, sharedAccess, (req as any).user!.id] // overlappings are handled by socket.io: https://socket.io/docs/v3/rooms/#joining-and-leaving
}
];
res.json(documentRoot);
};
export const update: RequestHandler<{ id: string }, any, UpdateConfig> = async (req, res, next) => {
const model = await DocumentRoot.updateModel(req.params.id, req.body);
/**
* Notifications to All users since the document root is a global entity.
* --> even with restricted access, the document root can be seen by all users.
*/
const change: ChangedRecord<RecordType.DocumentRoot> = {
type: RecordType.DocumentRoot,
record: model
};
res.notifications = [
{
event: IoEvent.CHANGED_RECORD,
message: change,
to: [IoRoom.ALL]
}
];
res.status(204).send();
};
export const permissions: RequestHandler<{ id: string }> = async (req, res, next) => {
const permissions = await DocumentRoot.getPermissions((req as any).user!, req.params.id);
res.json(permissions);
};
export const destroy: RequestHandler<{ id: string }> = async (req, res, next) => {
const model = await DocumentRoot.deleteModel((req as any).user!, req.params.id);
res.notifications = [
{
event: IoEvent.DELETED_RECORD,
message: { type: RecordType.DocumentRoot, id: model.id },
to: [
...model.rootGroupPermissions.map((p) => p.studentGroupId),
...model.rootUserPermissions.map((u) => u.userId),
RO_RW_DocumentRootAccess.has(model.sharedAccess) ? IoRoom.ALL : IoRoom.ADMIN
]
}
];
res.json(model);
};