dce-reactkit 3.2.2-beta.3 → 3.2.2-beta.5
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/dist/cjs/index.js +90 -81
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/server/initServer.d.ts +1 -1
- package/dist/esm/index.js +90 -81
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/server/initServer.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/constants/LOG_REVIEW_STATUS_ROUTE.ts +1 -1
- package/src/helpers/logClientEvent.tsx +5 -0
- package/src/server/initServer.ts +86 -73
|
@@ -25,7 +25,7 @@ export declare const internalGetLogCollection: () => any;
|
|
|
25
25
|
* @param opts.getLaunchInfo CACCL LTI's get launch info function
|
|
26
26
|
* @param [opts.logCollection] mongo collection from dce-mango to use for
|
|
27
27
|
* storing logs. If none is included, logs are written to the console
|
|
28
|
-
* @param [opts.logReviewAdmins=all
|
|
28
|
+
* @param [opts.logReviewAdmins=all] info on which admins can review
|
|
29
29
|
* logs from the client. If not included, all Canvas admins are allowed to
|
|
30
30
|
* review logs. If null, no Canvas admins are allowed to review logs.
|
|
31
31
|
* If an array of Canvas userIds (numbers), only Canvas admins with those
|
package/dist/index.d.ts
CHANGED
|
@@ -822,7 +822,7 @@ declare type GetLaunchInfoFunction = (req: any) => {
|
|
|
822
822
|
* @param opts.getLaunchInfo CACCL LTI's get launch info function
|
|
823
823
|
* @param [opts.logCollection] mongo collection from dce-mango to use for
|
|
824
824
|
* storing logs. If none is included, logs are written to the console
|
|
825
|
-
* @param [opts.logReviewAdmins=all
|
|
825
|
+
* @param [opts.logReviewAdmins=all] info on which admins can review
|
|
826
826
|
* logs from the client. If not included, all Canvas admins are allowed to
|
|
827
827
|
* review logs. If null, no Canvas admins are allowed to review logs.
|
|
828
828
|
* If an array of Canvas userIds (numbers), only Canvas admins with those
|
package/package.json
CHANGED
|
@@ -5,6 +5,6 @@ import ROUTE_PATH_PREFIX from './ROUTE_PATH_PREFIX';
|
|
|
5
5
|
* access to log review
|
|
6
6
|
* @author Gabe Abrams
|
|
7
7
|
*/
|
|
8
|
-
const LOG_REVIEW_STATUS_ROUTE =
|
|
8
|
+
const LOG_REVIEW_STATUS_ROUTE = `${ROUTE_PATH_PREFIX}/logs/access_allowed`;
|
|
9
9
|
|
|
10
10
|
export default LOG_REVIEW_STATUS_ROUTE;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import LOG_ROUTE_PATH from '../constants/LOG_ROUTE_PATH';
|
|
3
3
|
import LogBuiltInMetadata from '../types/LogBuiltInMetadata';
|
|
4
4
|
import LogFunction from '../types/LogFunction';
|
|
5
|
+
import LogLevel from '../types/LogLevel';
|
|
5
6
|
|
|
6
7
|
// Import shared functions
|
|
7
8
|
import visitServerEndpoint from './visitServerEndpoint';
|
|
@@ -27,6 +28,10 @@ const logClientEvent: LogFunction = async (opts) => {
|
|
|
27
28
|
opts.subcontext
|
|
28
29
|
?? LogBuiltInMetadata.Context.Uncategorized
|
|
29
30
|
),
|
|
31
|
+
level: (
|
|
32
|
+
opts.level
|
|
33
|
+
?? LogLevel.Info
|
|
34
|
+
),
|
|
30
35
|
tags: JSON.stringify(opts.tags ?? []),
|
|
31
36
|
metadata: JSON.stringify(opts.metadata ?? {}),
|
|
32
37
|
errorMessage: (
|
package/src/server/initServer.ts
CHANGED
|
@@ -71,7 +71,7 @@ export const internalGetLogCollection = () => {
|
|
|
71
71
|
* @param opts.getLaunchInfo CACCL LTI's get launch info function
|
|
72
72
|
* @param [opts.logCollection] mongo collection from dce-mango to use for
|
|
73
73
|
* storing logs. If none is included, logs are written to the console
|
|
74
|
-
* @param [opts.logReviewAdmins=all
|
|
74
|
+
* @param [opts.logReviewAdmins=all] info on which admins can review
|
|
75
75
|
* logs from the client. If not included, all Canvas admins are allowed to
|
|
76
76
|
* review logs. If null, no Canvas admins are allowed to review logs.
|
|
77
77
|
* If an array of Canvas userIds (numbers), only Canvas admins with those
|
|
@@ -176,86 +176,99 @@ const initServer = (
|
|
|
176
176
|
/* Log Reviewer */
|
|
177
177
|
/*----------------------------------------*/
|
|
178
178
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
179
|
+
/**
|
|
180
|
+
* Check if a given user is allowed to review logs
|
|
181
|
+
* @author Gabe Abrams
|
|
182
|
+
* @param userId the id of the user
|
|
183
|
+
* @param isAdmin if true, the user is an admin
|
|
184
|
+
* @returns true if the user can review logs
|
|
185
|
+
*/
|
|
186
|
+
const canReviewLogs = async (
|
|
187
|
+
userId: number,
|
|
188
|
+
isAdmin: boolean,
|
|
189
|
+
): Promise<boolean> => {
|
|
190
|
+
// Immediately deny access if user is not an admin
|
|
191
|
+
if (!isAdmin) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
194
|
|
|
195
|
-
|
|
196
|
-
|
|
195
|
+
// If all admins are allowed, we're done
|
|
196
|
+
if (!opts.logReviewAdmins) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
197
199
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
return
|
|
200
|
+
// Do a dynamic check
|
|
201
|
+
try {
|
|
202
|
+
// Array of userIds
|
|
203
|
+
if (Array.isArray(opts.logReviewAdmins)) {
|
|
204
|
+
return opts.logReviewAdmins.some((allowedId) => {
|
|
205
|
+
return (userId === allowedId);
|
|
206
|
+
});
|
|
203
207
|
}
|
|
204
|
-
};
|
|
205
208
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
209
|
+
// Must be a collection
|
|
210
|
+
const matches = await opts.logReviewAdmins.find({ userId });
|
|
211
|
+
|
|
212
|
+
// Make sure at least one entry matches
|
|
213
|
+
return matches.length > 0;
|
|
214
|
+
} catch (err) {
|
|
215
|
+
// If an error occurred, simply return false
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Check if the current user has access to logs
|
|
222
|
+
* @author Gabe Abrams
|
|
223
|
+
* @returns {boolean} true if user has access
|
|
224
|
+
*/
|
|
225
|
+
opts.app.get(
|
|
226
|
+
LOG_REVIEW_STATUS_ROUTE,
|
|
227
|
+
genRouteHandler({
|
|
228
|
+
handler: async ({ params }) => {
|
|
229
|
+
const { userId, isAdmin } = params;
|
|
230
|
+
const canReview = await canReviewLogs(userId, isAdmin);
|
|
231
|
+
return canReview;
|
|
232
|
+
},
|
|
233
|
+
}),
|
|
234
|
+
);
|
|
221
235
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
236
|
+
/**
|
|
237
|
+
* Get all logs for a certain month
|
|
238
|
+
* @author Gabe Abrams
|
|
239
|
+
* @param {number} year the year to query (e.g. 2022)
|
|
240
|
+
* @param {number} month the month to query (e.g. 1 = January)
|
|
241
|
+
* @returns {Log[]} list of logs from the given month
|
|
242
|
+
*/
|
|
243
|
+
opts.app.get(
|
|
244
|
+
`${LOG_REVIEW_ROUTE_PATH_PREFIX}/years/:year/months/:month`,
|
|
245
|
+
genRouteHandler({
|
|
246
|
+
paramTypes: {
|
|
247
|
+
year: ParamType.Int,
|
|
248
|
+
month: ParamType.Int,
|
|
249
|
+
},
|
|
250
|
+
handler: async ({ params }) => {
|
|
251
|
+
// Get user info
|
|
252
|
+
const { userId, isAdmin } = params;
|
|
239
253
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
254
|
+
// Validate user
|
|
255
|
+
// isAdmin is already checked because path starts with '/admin'
|
|
256
|
+
const canReview = await canReviewLogs(userId, isAdmin);
|
|
257
|
+
if (!canReview) {
|
|
258
|
+
throw new ErrorWithCode(
|
|
259
|
+
'You cannot access this resource because you do not have the appropriate permissions.',
|
|
260
|
+
ReactKitErrorCode.NotAllowedToReviewLogs,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
249
263
|
|
|
250
|
-
|
|
251
|
-
|
|
264
|
+
// Query for logs
|
|
265
|
+
const logs: Log[] = await _logCollection.find({ userId });
|
|
252
266
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
267
|
+
// Return logs
|
|
268
|
+
return logs;
|
|
269
|
+
},
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
259
272
|
};
|
|
260
273
|
|
|
261
274
|
export default initServer;
|