dce-reactkit 3.2.2-beta.2 → 3.2.2-beta.4

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 CHANGED
@@ -3892,7 +3892,7 @@ const padZerosLeft = (num, numDigits) => {
3892
3892
  * access to log review
3893
3893
  * @author Gabe Abrams
3894
3894
  */
3895
- const LOG_REVIEW_STATUS_ROUTE = `/admin${ROUTE_PATH_PREFIX}/logs/access`;
3895
+ const LOG_REVIEW_STATUS_ROUTE = `${ROUTE_PATH_PREFIX}/logs/access_allowed`;
3896
3896
 
3897
3897
  // Stored copy of caccl functions
3898
3898
  let _cacclGetLaunchInfo;
@@ -3934,7 +3934,7 @@ const internalGetLogCollection = () => {
3934
3934
  * @param opts.getLaunchInfo CACCL LTI's get launch info function
3935
3935
  * @param [opts.logCollection] mongo collection from dce-mango to use for
3936
3936
  * storing logs. If none is included, logs are written to the console
3937
- * @param [opts.logReviewAdmins=all admins] info on which admins can review
3937
+ * @param [opts.logReviewAdmins=all] info on which admins can review
3938
3938
  * logs from the client. If not included, all Canvas admins are allowed to
3939
3939
  * review logs. If null, no Canvas admins are allowed to review logs.
3940
3940
  * If an array of Canvas userIds (numbers), only Canvas admins with those
@@ -4017,71 +4017,79 @@ const initServer = (opts) => {
4017
4017
  /*----------------------------------------*/
4018
4018
  /* Log Reviewer */
4019
4019
  /*----------------------------------------*/
4020
- if (opts.logReviewAdmins !== null) {
4021
- /**
4022
- * Check if a given user is allowed to review logs
4023
- * @author Gabe Abrams
4024
- * @param userId the id of the user
4025
- * @returns true if the user can review logs
4026
- */
4027
- const canReviewLogs = (userId) => __awaiter(void 0, void 0, void 0, function* () {
4028
- try {
4029
- // Array of userIds
4030
- if (Array.isArray(opts.logReviewAdmins)) {
4031
- return opts.logReviewAdmins.some((allowedId) => {
4032
- return (userId === allowedId);
4033
- });
4034
- }
4035
- // Must be a collection
4036
- const matches = yield opts.logReviewAdmins.find({ userId });
4037
- // Make sure at least one entry matches
4038
- return matches.length > 0;
4020
+ /**
4021
+ * Check if a given user is allowed to review logs
4022
+ * @author Gabe Abrams
4023
+ * @param userId the id of the user
4024
+ * @param isAdmin if true, the user is an admin
4025
+ * @returns true if the user can review logs
4026
+ */
4027
+ const canReviewLogs = (userId, isAdmin) => __awaiter(void 0, void 0, void 0, function* () {
4028
+ // Immediately deny access if user is not an admin
4029
+ if (!isAdmin) {
4030
+ return false;
4031
+ }
4032
+ // If all admins are allowed, we're done
4033
+ if (!opts.logReviewAdmins) {
4034
+ return true;
4035
+ }
4036
+ // Do a dynamic check
4037
+ try {
4038
+ // Array of userIds
4039
+ if (Array.isArray(opts.logReviewAdmins)) {
4040
+ return opts.logReviewAdmins.some((allowedId) => {
4041
+ return (userId === allowedId);
4042
+ });
4039
4043
  }
4040
- catch (err) {
4041
- // If an error occurred, simply return false
4042
- return false;
4044
+ // Must be a collection
4045
+ const matches = yield opts.logReviewAdmins.find({ userId });
4046
+ // Make sure at least one entry matches
4047
+ return matches.length > 0;
4048
+ }
4049
+ catch (err) {
4050
+ // If an error occurred, simply return false
4051
+ return false;
4052
+ }
4053
+ });
4054
+ /**
4055
+ * Check if the current user has access to logs
4056
+ * @author Gabe Abrams
4057
+ * @returns {boolean} true if user has access
4058
+ */
4059
+ opts.app.get(LOG_REVIEW_STATUS_ROUTE, genRouteHandler({
4060
+ handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
4061
+ const { userId, isAdmin } = params;
4062
+ const canReview = yield canReviewLogs(userId, isAdmin);
4063
+ return canReview;
4064
+ }),
4065
+ }));
4066
+ /**
4067
+ * Get all logs for a certain month
4068
+ * @author Gabe Abrams
4069
+ * @param {number} year the year to query (e.g. 2022)
4070
+ * @param {number} month the month to query (e.g. 1 = January)
4071
+ * @returns {Log[]} list of logs from the given month
4072
+ */
4073
+ opts.app.post(`${LOG_REVIEW_ROUTE_PATH_PREFIX}/years/:year/months/:month`, genRouteHandler({
4074
+ paramTypes: {
4075
+ year: ParamType$1.Int,
4076
+ month: ParamType$1.Int,
4077
+ },
4078
+ handler: ({ params }) => __awaiter(void 0, void 0, void 0, function* () {
4079
+ // Get user info
4080
+ const { userId, isAdmin } = params;
4081
+ // Validate user
4082
+ // isAdmin is already checked because path starts with '/admin'
4083
+ const canReview = yield canReviewLogs(userId, isAdmin);
4084
+ if (!canReview) {
4085
+ throw new ErrorWithCode('You cannot access this resource because you do not have the appropriate permissions.', ReactKitErrorCode$1.NotAllowedToReviewLogs);
4043
4086
  }
4044
- });
4045
- /**
4046
- * Check if the current user has access to logs
4047
- * @author Gabe Abrams
4048
- * @returns {boolean} true if user has access
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
- }
4087
+ // Query for logs
4088
+ const logs = yield _logCollection.find({ userId });
4089
+ // Return logs
4090
+ return logs;
4091
+ }),
4092
+ }));
4085
4093
  };
4086
4094
 
4087
4095
  // Import shared types
@@ -5132,6 +5140,31 @@ const initLogCollection = (Collection) => {
5132
5140
  });
5133
5141
  };
5134
5142
 
5143
+ // Cache user's ability
5144
+ let canReview = undefined;
5145
+ /**
5146
+ * Check if the current user can review logs
5147
+ * @author Gabe Abrams
5148
+ * @returns true if current user can review logs
5149
+ */
5150
+ const canReviewLogs = () => __awaiter(void 0, void 0, void 0, function* () {
5151
+ // If cached, use that value
5152
+ if (canReview !== undefined) {
5153
+ return canReview;
5154
+ }
5155
+ // Ask on server
5156
+ try {
5157
+ canReview = !!(yield visitServerEndpoint({
5158
+ path: LOG_REVIEW_STATUS_ROUTE,
5159
+ method: 'GET',
5160
+ }));
5161
+ }
5162
+ catch (err) {
5163
+ canReview = false;
5164
+ }
5165
+ return canReview;
5166
+ });
5167
+
5135
5168
  /**
5136
5169
  * Days of the week
5137
5170
  * @author Gabe Abrams
@@ -5184,6 +5217,7 @@ exports.Variant = Variant$1;
5184
5217
  exports.abbreviate = abbreviate;
5185
5218
  exports.alert = alert$1;
5186
5219
  exports.avg = avg;
5220
+ exports.canReviewLogs = canReviewLogs;
5187
5221
  exports.ceilToNumDecimals = ceilToNumDecimals;
5188
5222
  exports.confirm = confirm;
5189
5223
  exports.floorToNumDecimals = floorToNumDecimals;