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.
@@ -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 admins] info on which admins can review
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/esm/index.js CHANGED
@@ -522,6 +522,18 @@ const LogBuiltInMetadata = {
522
522
  },
523
523
  };
524
524
 
525
+ /**
526
+ * Allowed log levels
527
+ * @author Gabe Abrams
528
+ */
529
+ var LogLevel;
530
+ (function (LogLevel) {
531
+ LogLevel["Warn"] = "Warn";
532
+ LogLevel["Info"] = "Info";
533
+ LogLevel["Debug"] = "Debug";
534
+ })(LogLevel || (LogLevel = {}));
535
+ var LogLevel$1 = LogLevel;
536
+
525
537
  // Keep track of whether or not session expiry has already been handled
526
538
  let sessionAlreadyExpired = false;
527
539
  /*------------------------------------------------------------------------*/
@@ -640,7 +652,7 @@ const visitServerEndpoint = (opts) => __awaiter(void 0, void 0, void 0, function
640
652
  * @author Gabe Abrams
641
653
  */
642
654
  const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
643
- var _a, _b, _c, _d, _e, _f;
655
+ var _a, _b, _c, _d, _e, _f, _g;
644
656
  return visitServerEndpoint({
645
657
  path: LOG_ROUTE_PATH,
646
658
  method: 'POST',
@@ -649,8 +661,9 @@ const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* ()
649
661
  ? opts.context
650
662
  : ((_b = ((_a = opts.context) !== null && _a !== void 0 ? _a : {})._) !== null && _b !== void 0 ? _b : LogBuiltInMetadata.Context.Uncategorized)),
651
663
  subcontext: ((_c = opts.subcontext) !== null && _c !== void 0 ? _c : LogBuiltInMetadata.Context.Uncategorized),
652
- tags: JSON.stringify((_d = opts.tags) !== null && _d !== void 0 ? _d : []),
653
- metadata: JSON.stringify((_e = opts.metadata) !== null && _e !== void 0 ? _e : {}),
664
+ level: ((_d = opts.level) !== null && _d !== void 0 ? _d : LogLevel$1.Info),
665
+ tags: JSON.stringify((_e = opts.tags) !== null && _e !== void 0 ? _e : []),
666
+ metadata: JSON.stringify((_f = opts.metadata) !== null && _f !== void 0 ? _f : {}),
654
667
  errorMessage: (opts.error
655
668
  ? opts.error.message
656
669
  : undefined),
@@ -661,7 +674,7 @@ const logClientEvent = (opts) => __awaiter(void 0, void 0, void 0, function* ()
661
674
  ? opts.error.stack
662
675
  : undefined),
663
676
  target: (opts.action
664
- ? ((_f = opts.target) !== null && _f !== void 0 ? _f : LogBuiltInMetadata.Target.NoSpecificTarget)
677
+ ? ((_g = opts.target) !== null && _g !== void 0 ? _g : LogBuiltInMetadata.Target.NoSpecificTarget)
665
678
  : undefined),
666
679
  action: (opts.action
667
680
  ? opts.action
@@ -3884,7 +3897,7 @@ const padZerosLeft = (num, numDigits) => {
3884
3897
  * access to log review
3885
3898
  * @author Gabe Abrams
3886
3899
  */
3887
- const LOG_REVIEW_STATUS_ROUTE = `/admin${ROUTE_PATH_PREFIX}/logs/access`;
3900
+ const LOG_REVIEW_STATUS_ROUTE = `${ROUTE_PATH_PREFIX}/logs/access_allowed`;
3888
3901
 
3889
3902
  // Stored copy of caccl functions
3890
3903
  let _cacclGetLaunchInfo;
@@ -3926,7 +3939,7 @@ const internalGetLogCollection = () => {
3926
3939
  * @param opts.getLaunchInfo CACCL LTI's get launch info function
3927
3940
  * @param [opts.logCollection] mongo collection from dce-mango to use for
3928
3941
  * storing logs. If none is included, logs are written to the console
3929
- * @param [opts.logReviewAdmins=all admins] info on which admins can review
3942
+ * @param [opts.logReviewAdmins=all] info on which admins can review
3930
3943
  * logs from the client. If not included, all Canvas admins are allowed to
3931
3944
  * review logs. If null, no Canvas admins are allowed to review logs.
3932
3945
  * If an array of Canvas userIds (numbers), only Canvas admins with those
@@ -4009,71 +4022,79 @@ const initServer = (opts) => {
4009
4022
  /*----------------------------------------*/
4010
4023
  /* Log Reviewer */
4011
4024
  /*----------------------------------------*/
4012
- if (opts.logReviewAdmins !== null) {
4013
- /**
4014
- * Check if a given user is allowed to review logs
4015
- * @author Gabe Abrams
4016
- * @param userId the id of the user
4017
- * @returns true if the user can review logs
4018
- */
4019
- const canReviewLogs = (userId) => __awaiter(void 0, void 0, void 0, function* () {
4020
- try {
4021
- // Array of userIds
4022
- if (Array.isArray(opts.logReviewAdmins)) {
4023
- return opts.logReviewAdmins.some((allowedId) => {
4024
- return (userId === allowedId);
4025
- });
4026
- }
4027
- // Must be a collection
4028
- const matches = yield opts.logReviewAdmins.find({ userId });
4029
- // Make sure at least one entry matches
4030
- return matches.length > 0;
4025
+ /**
4026
+ * Check if a given user is allowed to review logs
4027
+ * @author Gabe Abrams
4028
+ * @param userId the id of the user
4029
+ * @param isAdmin if true, the user is an admin
4030
+ * @returns true if the user can review logs
4031
+ */
4032
+ const canReviewLogs = (userId, isAdmin) => __awaiter(void 0, void 0, void 0, function* () {
4033
+ // Immediately deny access if user is not an admin
4034
+ if (!isAdmin) {
4035
+ return false;
4036
+ }
4037
+ // If all admins are allowed, we're done
4038
+ if (!opts.logReviewAdmins) {
4039
+ return true;
4040
+ }
4041
+ // Do a dynamic check
4042
+ try {
4043
+ // Array of userIds
4044
+ if (Array.isArray(opts.logReviewAdmins)) {
4045
+ return opts.logReviewAdmins.some((allowedId) => {
4046
+ return (userId === allowedId);
4047
+ });
4031
4048
  }
4032
- catch (err) {
4033
- // If an error occurred, simply return false
4034
- return false;
4049
+ // Must be a collection
4050
+ const matches = yield opts.logReviewAdmins.find({ userId });
4051
+ // Make sure at least one entry matches
4052
+ return matches.length > 0;
4053
+ }
4054
+ catch (err) {
4055
+ // If an error occurred, simply return false
4056
+ return false;
4057
+ }
4058
+ });
4059
+ /**
4060
+ * Check if the current user has access to logs
4061
+ * @author Gabe Abrams
4062
+ * @returns {boolean} true if user has access
4063
+ */
4064
+ opts.app.get(LOG_REVIEW_STATUS_ROUTE, genRouteHandler({
4065
+ handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
4066
+ const { userId, isAdmin } = params;
4067
+ const canReview = yield canReviewLogs(userId, isAdmin);
4068
+ return canReview;
4069
+ }),
4070
+ }));
4071
+ /**
4072
+ * Get all logs for a certain month
4073
+ * @author Gabe Abrams
4074
+ * @param {number} year the year to query (e.g. 2022)
4075
+ * @param {number} month the month to query (e.g. 1 = January)
4076
+ * @returns {Log[]} list of logs from the given month
4077
+ */
4078
+ opts.app.get(`${LOG_REVIEW_ROUTE_PATH_PREFIX}/years/:year/months/:month`, genRouteHandler({
4079
+ paramTypes: {
4080
+ year: ParamType$1.Int,
4081
+ month: ParamType$1.Int,
4082
+ },
4083
+ handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
4084
+ // Get user info
4085
+ const { userId, isAdmin } = params;
4086
+ // Validate user
4087
+ // isAdmin is already checked because path starts with '/admin'
4088
+ const canReview = yield canReviewLogs(userId, isAdmin);
4089
+ if (!canReview) {
4090
+ throw new ErrorWithCode('You cannot access this resource because you do not have the appropriate permissions.', ReactKitErrorCode$1.NotAllowedToReviewLogs);
4035
4091
  }
4036
- });
4037
- /**
4038
- * Check if the current user has access to logs
4039
- * @author Gabe Abrams
4040
- * @returns {boolean} true if user has access
4041
- */
4042
- opts.app.get(LOG_REVIEW_STATUS_ROUTE, genRouteHandler({
4043
- handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
4044
- const { userId } = params;
4045
- const canReview = yield canReviewLogs(userId);
4046
- return canReview;
4047
- }),
4048
- }));
4049
- /**
4050
- * Get all logs for a certain month
4051
- * @author Gabe Abrams
4052
- * @param {number} year the year to query (e.g. 2022)
4053
- * @param {number} month the month to query (e.g. 1 = January)
4054
- * @returns {Log[]} list of logs from the given month
4055
- */
4056
- opts.app.post(`${LOG_REVIEW_ROUTE_PATH_PREFIX}/years/:year/months/:month`, genRouteHandler({
4057
- paramTypes: {
4058
- year: ParamType$1.Int,
4059
- month: ParamType$1.Int,
4060
- },
4061
- handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
4062
- // Get user info
4063
- const { userId } = params;
4064
- // Validate user
4065
- // isAdmin is already checked because path starts with '/admin'
4066
- const canReview = yield canReviewLogs(userId);
4067
- if (!canReview) {
4068
- throw new ErrorWithCode('You cannot access this resource because you do not have the appropriate permissions.', ReactKitErrorCode$1.NotAllowedToReviewLogs);
4069
- }
4070
- // Query for logs
4071
- const logs = yield _logCollection.find({ userId });
4072
- // Return logs
4073
- return logs;
4074
- }),
4075
- }));
4076
- }
4092
+ // Query for logs
4093
+ const logs = yield _logCollection.find({ userId });
4094
+ // Return logs
4095
+ return logs;
4096
+ }),
4097
+ }));
4077
4098
  };
4078
4099
 
4079
4100
  // Import shared types
@@ -4373,18 +4394,6 @@ const parseUserAgent = (userAgent) => {
4373
4394
  };
4374
4395
  };
4375
4396
 
4376
- /**
4377
- * Allowed log levels
4378
- * @author Gabe Abrams
4379
- */
4380
- var LogLevel;
4381
- (function (LogLevel) {
4382
- LogLevel["Warn"] = "Warn";
4383
- LogLevel["Info"] = "Info";
4384
- LogLevel["Debug"] = "Debug";
4385
- })(LogLevel || (LogLevel = {}));
4386
- var LogLevel$1 = LogLevel;
4387
-
4388
4397
  /**
4389
4398
  * Generate an express API route handler
4390
4399
  * @author Gabe Abrams