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
package/dist/cjs/index.js
CHANGED
|
@@ -530,6 +530,18 @@ const LogBuiltInMetadata = {
|
|
|
530
530
|
},
|
|
531
531
|
};
|
|
532
532
|
|
|
533
|
+
/**
|
|
534
|
+
* Allowed log levels
|
|
535
|
+
* @author Gabe Abrams
|
|
536
|
+
*/
|
|
537
|
+
var LogLevel;
|
|
538
|
+
(function (LogLevel) {
|
|
539
|
+
LogLevel["Warn"] = "Warn";
|
|
540
|
+
LogLevel["Info"] = "Info";
|
|
541
|
+
LogLevel["Debug"] = "Debug";
|
|
542
|
+
})(LogLevel || (LogLevel = {}));
|
|
543
|
+
var LogLevel$1 = LogLevel;
|
|
544
|
+
|
|
533
545
|
// Keep track of whether or not session expiry has already been handled
|
|
534
546
|
let sessionAlreadyExpired = false;
|
|
535
547
|
/*------------------------------------------------------------------------*/
|
|
@@ -648,7 +660,7 @@ const visitServerEndpoint = (opts) => __awaiter(void 0, void 0, void 0, function
|
|
|
648
660
|
* @author Gabe Abrams
|
|
649
661
|
*/
|
|
650
662
|
const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
651
|
-
var _a, _b, _c, _d, _e, _f;
|
|
663
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
652
664
|
return visitServerEndpoint({
|
|
653
665
|
path: LOG_ROUTE_PATH,
|
|
654
666
|
method: 'POST',
|
|
@@ -657,8 +669,9 @@ const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
657
669
|
? opts.context
|
|
658
670
|
: ((_b = ((_a = opts.context) !== null && _a !== void 0 ? _a : {})._) !== null && _b !== void 0 ? _b : LogBuiltInMetadata.Context.Uncategorized)),
|
|
659
671
|
subcontext: ((_c = opts.subcontext) !== null && _c !== void 0 ? _c : LogBuiltInMetadata.Context.Uncategorized),
|
|
660
|
-
|
|
661
|
-
|
|
672
|
+
level: ((_d = opts.level) !== null && _d !== void 0 ? _d : LogLevel$1.Info),
|
|
673
|
+
tags: JSON.stringify((_e = opts.tags) !== null && _e !== void 0 ? _e : []),
|
|
674
|
+
metadata: JSON.stringify((_f = opts.metadata) !== null && _f !== void 0 ? _f : {}),
|
|
662
675
|
errorMessage: (opts.error
|
|
663
676
|
? opts.error.message
|
|
664
677
|
: undefined),
|
|
@@ -669,7 +682,7 @@ const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
669
682
|
? opts.error.stack
|
|
670
683
|
: undefined),
|
|
671
684
|
target: (opts.action
|
|
672
|
-
? ((
|
|
685
|
+
? ((_g = opts.target) !== null && _g !== void 0 ? _g : LogBuiltInMetadata.Target.NoSpecificTarget)
|
|
673
686
|
: undefined),
|
|
674
687
|
action: (opts.action
|
|
675
688
|
? opts.action
|
|
@@ -3892,7 +3905,7 @@ const padZerosLeft = (num, numDigits) => {
|
|
|
3892
3905
|
* access to log review
|
|
3893
3906
|
* @author Gabe Abrams
|
|
3894
3907
|
*/
|
|
3895
|
-
const LOG_REVIEW_STATUS_ROUTE =
|
|
3908
|
+
const LOG_REVIEW_STATUS_ROUTE = `${ROUTE_PATH_PREFIX}/logs/access_allowed`;
|
|
3896
3909
|
|
|
3897
3910
|
// Stored copy of caccl functions
|
|
3898
3911
|
let _cacclGetLaunchInfo;
|
|
@@ -3934,7 +3947,7 @@ const internalGetLogCollection = () => {
|
|
|
3934
3947
|
* @param opts.getLaunchInfo CACCL LTI's get launch info function
|
|
3935
3948
|
* @param [opts.logCollection] mongo collection from dce-mango to use for
|
|
3936
3949
|
* storing logs. If none is included, logs are written to the console
|
|
3937
|
-
* @param [opts.logReviewAdmins=all
|
|
3950
|
+
* @param [opts.logReviewAdmins=all] info on which admins can review
|
|
3938
3951
|
* logs from the client. If not included, all Canvas admins are allowed to
|
|
3939
3952
|
* review logs. If null, no Canvas admins are allowed to review logs.
|
|
3940
3953
|
* If an array of Canvas userIds (numbers), only Canvas admins with those
|
|
@@ -4017,71 +4030,79 @@ const initServer = (opts) => {
|
|
|
4017
4030
|
/*----------------------------------------*/
|
|
4018
4031
|
/* Log Reviewer */
|
|
4019
4032
|
/*----------------------------------------*/
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4033
|
+
/**
|
|
4034
|
+
* Check if a given user is allowed to review logs
|
|
4035
|
+
* @author Gabe Abrams
|
|
4036
|
+
* @param userId the id of the user
|
|
4037
|
+
* @param isAdmin if true, the user is an admin
|
|
4038
|
+
* @returns true if the user can review logs
|
|
4039
|
+
*/
|
|
4040
|
+
const canReviewLogs = (userId, isAdmin) => __awaiter(void 0, void 0, void 0, function* () {
|
|
4041
|
+
// Immediately deny access if user is not an admin
|
|
4042
|
+
if (!isAdmin) {
|
|
4043
|
+
return false;
|
|
4044
|
+
}
|
|
4045
|
+
// If all admins are allowed, we're done
|
|
4046
|
+
if (!opts.logReviewAdmins) {
|
|
4047
|
+
return true;
|
|
4048
|
+
}
|
|
4049
|
+
// Do a dynamic check
|
|
4050
|
+
try {
|
|
4051
|
+
// Array of userIds
|
|
4052
|
+
if (Array.isArray(opts.logReviewAdmins)) {
|
|
4053
|
+
return opts.logReviewAdmins.some((allowedId) => {
|
|
4054
|
+
return (userId === allowedId);
|
|
4055
|
+
});
|
|
4039
4056
|
}
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4057
|
+
// Must be a collection
|
|
4058
|
+
const matches = yield opts.logReviewAdmins.find({ userId });
|
|
4059
|
+
// Make sure at least one entry matches
|
|
4060
|
+
return matches.length > 0;
|
|
4061
|
+
}
|
|
4062
|
+
catch (err) {
|
|
4063
|
+
// If an error occurred, simply return false
|
|
4064
|
+
return false;
|
|
4065
|
+
}
|
|
4066
|
+
});
|
|
4067
|
+
/**
|
|
4068
|
+
* Check if the current user has access to logs
|
|
4069
|
+
* @author Gabe Abrams
|
|
4070
|
+
* @returns {boolean} true if user has access
|
|
4071
|
+
*/
|
|
4072
|
+
opts.app.get(LOG_REVIEW_STATUS_ROUTE, genRouteHandler({
|
|
4073
|
+
handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
4074
|
+
const { userId, isAdmin } = params;
|
|
4075
|
+
const canReview = yield canReviewLogs(userId, isAdmin);
|
|
4076
|
+
return canReview;
|
|
4077
|
+
}),
|
|
4078
|
+
}));
|
|
4079
|
+
/**
|
|
4080
|
+
* Get all logs for a certain month
|
|
4081
|
+
* @author Gabe Abrams
|
|
4082
|
+
* @param {number} year the year to query (e.g. 2022)
|
|
4083
|
+
* @param {number} month the month to query (e.g. 1 = January)
|
|
4084
|
+
* @returns {Log[]} list of logs from the given month
|
|
4085
|
+
*/
|
|
4086
|
+
opts.app.get(`${LOG_REVIEW_ROUTE_PATH_PREFIX}/years/:year/months/:month`, genRouteHandler({
|
|
4087
|
+
paramTypes: {
|
|
4088
|
+
year: ParamType$1.Int,
|
|
4089
|
+
month: ParamType$1.Int,
|
|
4090
|
+
},
|
|
4091
|
+
handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
4092
|
+
// Get user info
|
|
4093
|
+
const { userId, isAdmin } = params;
|
|
4094
|
+
// Validate user
|
|
4095
|
+
// isAdmin is already checked because path starts with '/admin'
|
|
4096
|
+
const canReview = yield canReviewLogs(userId, isAdmin);
|
|
4097
|
+
if (!canReview) {
|
|
4098
|
+
throw new ErrorWithCode('You cannot access this resource because you do not have the appropriate permissions.', ReactKitErrorCode$1.NotAllowedToReviewLogs);
|
|
4043
4099
|
}
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
opts.app.get(LOG_REVIEW_STATUS_ROUTE, genRouteHandler({
|
|
4051
|
-
handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
4052
|
-
const { userId } = params;
|
|
4053
|
-
const canReview = yield canReviewLogs(userId);
|
|
4054
|
-
return canReview;
|
|
4055
|
-
}),
|
|
4056
|
-
}));
|
|
4057
|
-
/**
|
|
4058
|
-
* Get all logs for a certain month
|
|
4059
|
-
* @author Gabe Abrams
|
|
4060
|
-
* @param {number} year the year to query (e.g. 2022)
|
|
4061
|
-
* @param {number} month the month to query (e.g. 1 = January)
|
|
4062
|
-
* @returns {Log[]} list of logs from the given month
|
|
4063
|
-
*/
|
|
4064
|
-
opts.app.post(`${LOG_REVIEW_ROUTE_PATH_PREFIX}/years/:year/months/:month`, genRouteHandler({
|
|
4065
|
-
paramTypes: {
|
|
4066
|
-
year: ParamType$1.Int,
|
|
4067
|
-
month: ParamType$1.Int,
|
|
4068
|
-
},
|
|
4069
|
-
handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
4070
|
-
// Get user info
|
|
4071
|
-
const { userId } = params;
|
|
4072
|
-
// Validate user
|
|
4073
|
-
// isAdmin is already checked because path starts with '/admin'
|
|
4074
|
-
const canReview = yield canReviewLogs(userId);
|
|
4075
|
-
if (!canReview) {
|
|
4076
|
-
throw new ErrorWithCode('You cannot access this resource because you do not have the appropriate permissions.', ReactKitErrorCode$1.NotAllowedToReviewLogs);
|
|
4077
|
-
}
|
|
4078
|
-
// Query for logs
|
|
4079
|
-
const logs = yield _logCollection.find({ userId });
|
|
4080
|
-
// Return logs
|
|
4081
|
-
return logs;
|
|
4082
|
-
}),
|
|
4083
|
-
}));
|
|
4084
|
-
}
|
|
4100
|
+
// Query for logs
|
|
4101
|
+
const logs = yield _logCollection.find({ userId });
|
|
4102
|
+
// Return logs
|
|
4103
|
+
return logs;
|
|
4104
|
+
}),
|
|
4105
|
+
}));
|
|
4085
4106
|
};
|
|
4086
4107
|
|
|
4087
4108
|
// Import shared types
|
|
@@ -4381,18 +4402,6 @@ const parseUserAgent = (userAgent) => {
|
|
|
4381
4402
|
};
|
|
4382
4403
|
};
|
|
4383
4404
|
|
|
4384
|
-
/**
|
|
4385
|
-
* Allowed log levels
|
|
4386
|
-
* @author Gabe Abrams
|
|
4387
|
-
*/
|
|
4388
|
-
var LogLevel;
|
|
4389
|
-
(function (LogLevel) {
|
|
4390
|
-
LogLevel["Warn"] = "Warn";
|
|
4391
|
-
LogLevel["Info"] = "Info";
|
|
4392
|
-
LogLevel["Debug"] = "Debug";
|
|
4393
|
-
})(LogLevel || (LogLevel = {}));
|
|
4394
|
-
var LogLevel$1 = LogLevel;
|
|
4395
|
-
|
|
4396
4405
|
/**
|
|
4397
4406
|
* Generate an express API route handler
|
|
4398
4407
|
* @author Gabe Abrams
|