@devvit/security 0.11.4 → 0.11.5-next-2024-12-09-8401a61d8.0

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.
@@ -1,2 +1,14 @@
1
- export {};
1
+ import type { Context, ModeratorPermission } from '@devvit/public-api';
2
+ /**
3
+ * currentUserHasModPermissions verifies the current user is a mod with all of
4
+ * permissions provided in the `permissions` argument before executing
5
+ * the callback function.
6
+ * @param context Context instance from the request
7
+ * @param permissions List of moderator permissions to check
8
+ * @param callback Function to execute if the user has the required permissions
9
+ * @returns Promise<void>
10
+ * @throws Error if the user does not have all required permissions
11
+ * @example currentUserHasModPermissions(context.reddit, ['all'], () => { ... });
12
+ */
13
+ export declare function currentUserHasModPermissions(context: Context, permissions: ModeratorPermission[], callback: () => void): Promise<void>;
2
14
  //# sourceMappingURL=modPermissions.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"modPermissions.d.ts","sourceRoot":"","sources":["../../src/lib/modPermissions.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"modPermissions.d.ts","sourceRoot":"","sources":["../../src/lib/modPermissions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAoBvE;;;;;;;;;;GAUG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,OAAO,EAChB,WAAW,EAAE,mBAAmB,EAAE,EAClC,QAAQ,EAAE,MAAM,IAAI,GACnB,OAAO,CAAC,IAAI,CAAC,CAWf"}
@@ -1 +1,31 @@
1
- export {};
1
+ import { Header } from '@devvit/shared-types/Header.js';
2
+ // Internal function for getting the current user's moderator permissions in the current subreddit
3
+ async function getPermissions(context) {
4
+ const metadataPermissions = context.debug.metadata[Header.ModPermissions]?.values;
5
+ if (metadataPermissions) {
6
+ return new Set(metadataPermissions);
7
+ }
8
+ const user = await context.reddit.getCurrentUser();
9
+ return new Set(context.subredditName
10
+ ? ((await user?.getModPermissionsForSubreddit(context.subredditName)) ?? [])
11
+ : []);
12
+ }
13
+ /**
14
+ * currentUserHasModPermissions verifies the current user is a mod with all of
15
+ * permissions provided in the `permissions` argument before executing
16
+ * the callback function.
17
+ * @param context Context instance from the request
18
+ * @param permissions List of moderator permissions to check
19
+ * @param callback Function to execute if the user has the required permissions
20
+ * @returns Promise<void>
21
+ * @throws Error if the user does not have all required permissions
22
+ * @example currentUserHasModPermissions(context.reddit, ['all'], () => { ... });
23
+ */
24
+ export async function currentUserHasModPermissions(context, permissions, callback) {
25
+ const userPermissionSet = await getPermissions(context);
26
+ if (permissions.every((permission) => userPermissionSet.has(permission))) {
27
+ callback();
28
+ return;
29
+ }
30
+ throw new Error(`User[${context.userId}] does not have all required permissions in r/${context.subredditName}: [${permissions.join(', ')}]`);
31
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devvit/security",
3
- "version": "0.11.4",
3
+ "version": "0.11.5-next-2024-12-09-8401a61d8.0",
4
4
  "license": "BSD-3-Clause",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,12 +23,12 @@
23
23
  },
24
24
  "types": "./index.d.ts",
25
25
  "dependencies": {
26
- "@devvit/public-api": "0.11.4",
27
- "@devvit/shared-types": "0.11.4"
26
+ "@devvit/public-api": "0.11.5-next-2024-12-09-8401a61d8.0",
27
+ "@devvit/shared-types": "0.11.5-next-2024-12-09-8401a61d8.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@devvit/repo-tools": "0.11.4",
31
- "@devvit/tsconfig": "0.11.4",
30
+ "@devvit/repo-tools": "0.11.5-next-2024-12-09-8401a61d8.0",
31
+ "@devvit/tsconfig": "0.11.5-next-2024-12-09-8401a61d8.0",
32
32
  "eslint": "9.11.1",
33
33
  "typescript": "5.3.2",
34
34
  "vitest": "1.6.0"
@@ -37,5 +37,5 @@
37
37
  "directory": "dist"
38
38
  },
39
39
  "source": "./src/index.ts",
40
- "gitHead": "4e4f06374e9c54e277e08d75bc4b159952dc3e97"
40
+ "gitHead": "7bd26d3415b397d9ab9eeba1e878302bc5167a0a"
41
41
  }