@loomcore/api 0.2.29 → 0.2.30
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/utils/auth/assert-admin-or-feature.util.d.ts +2 -0
- package/dist/utils/auth/assert-admin-or-feature.util.js +8 -0
- package/dist/utils/auth/assert-user-has-feature.util.d.ts +1 -1
- package/dist/utils/auth/assert-user-has-feature.util.js +11 -3
- package/dist/utils/auth/index.d.ts +1 -0
- package/dist/utils/auth/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { isAdmin } from "./is-admin.util.js";
|
|
2
|
+
import { assertUserHasFeature } from "./assert-user-has-feature.util.js";
|
|
3
|
+
export function assertAdminOrFeature(userContext, features, requireAll = false) {
|
|
4
|
+
if (isAdmin(userContext)) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
assertUserHasFeature(userContext, features, requireAll);
|
|
8
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { IUserContext } from "@loomcore/common/models";
|
|
2
|
-
export declare function assertUserHasFeature(userContext: IUserContext, features: string[]): void;
|
|
2
|
+
export declare function assertUserHasFeature(userContext: IUserContext, features: string[], requireAll?: boolean): void;
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { UnauthorizedError } from "../../errors/index.js";
|
|
2
|
-
export function assertUserHasFeature(userContext, features) {
|
|
2
|
+
export function assertUserHasFeature(userContext, features, requireAll = false) {
|
|
3
3
|
if (features.length === 0) {
|
|
4
4
|
return;
|
|
5
5
|
}
|
|
6
6
|
const missingFeatures = features.filter(feature => !userContext.features.includes(feature));
|
|
7
|
-
if (
|
|
8
|
-
|
|
7
|
+
if (requireAll) {
|
|
8
|
+
if (missingFeatures.length > 0) {
|
|
9
|
+
throw new UnauthorizedError(missingFeatures);
|
|
10
|
+
}
|
|
9
11
|
}
|
|
12
|
+
else {
|
|
13
|
+
if (missingFeatures.length === features.length) {
|
|
14
|
+
throw new UnauthorizedError(missingFeatures);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return;
|
|
10
18
|
}
|
package/dist/utils/auth/index.js
CHANGED