@wxn0brp/gate-warden 0.5.3 → 0.6.1
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/LICENSE +1 -1
- package/dist/check.js +4 -4
- package/dist/mgr.d.ts +4 -4
- package/dist/mgr.js +18 -18
- package/dist/user.js +8 -7
- package/dist/warden.js +1 -1
- package/package.json +6 -7
package/LICENSE
CHANGED
package/dist/check.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import hasFieldsAdvanced from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
|
|
1
|
+
import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
|
|
2
2
|
import { COLORS } from "./log.js";
|
|
3
3
|
import { convertPath } from "./utils.js";
|
|
4
4
|
import { collections } from "./const.js";
|
|
@@ -16,7 +16,7 @@ import { collections } from "./const.js";
|
|
|
16
16
|
export async function aclCheck({ db, entityId, flag, user }) {
|
|
17
17
|
if (!await db.issetCollection(collections.acl + "/" + entityId))
|
|
18
18
|
return -1;
|
|
19
|
-
const rules = await db.
|
|
19
|
+
const rules = await db.c(collections.acl + "/" + entityId).find({
|
|
20
20
|
$or: [
|
|
21
21
|
{ uid: user._id },
|
|
22
22
|
{
|
|
@@ -44,7 +44,7 @@ export async function aclCheck({ db, entityId, flag, user }) {
|
|
|
44
44
|
*/
|
|
45
45
|
export async function rbacCheck({ db, flag, user, entityId }) {
|
|
46
46
|
for (const role of user.roles) {
|
|
47
|
-
const rolesEntity = await db.
|
|
47
|
+
const rolesEntity = await db.c(collections.role + "/" + role).find({ _id: entityId });
|
|
48
48
|
for (const entity of rolesEntity) {
|
|
49
49
|
if (entity.p & flag)
|
|
50
50
|
return true;
|
|
@@ -64,7 +64,7 @@ export async function rbacCheck({ db, flag, user, entityId }) {
|
|
|
64
64
|
export async function abacCheck({ db, entityId, flag, user, debugLog }) {
|
|
65
65
|
if (!await db.issetCollection(collections.abac + "/" + entityId))
|
|
66
66
|
return false;
|
|
67
|
-
const rules = await db.
|
|
67
|
+
const rules = await db.c(collections.abac + "/" + entityId).find({ flag });
|
|
68
68
|
if (rules.length === 0)
|
|
69
69
|
return false;
|
|
70
70
|
for (const rule of rules) {
|
package/dist/mgr.d.ts
CHANGED
|
@@ -8,8 +8,8 @@ export declare class WardenManager {
|
|
|
8
8
|
addACLRule(entityId: string, p: number, uid?: Id): Promise<ACLRule>;
|
|
9
9
|
addRBACRule(role_id: string, entity_id: string, p: number): Promise<RoleRule>;
|
|
10
10
|
addABACRule(entity_id: string, flag: number, condition: ABACRule["condition"]): Promise<ABACRule>;
|
|
11
|
-
removeRole(roleId: string): Promise<
|
|
12
|
-
removeACLRule(entityId: string, uid?: string): Promise<
|
|
13
|
-
removeRBACRule(roleId: string, entityId: string): Promise<
|
|
14
|
-
removeABACRule(entityId: string, flag: number): Promise<
|
|
11
|
+
removeRole(roleId: string): Promise<Role | null>;
|
|
12
|
+
removeACLRule(entityId: string, uid?: string): Promise<ACLRule | null>;
|
|
13
|
+
removeRBACRule(roleId: string, entityId: string): Promise<RoleRule | null>;
|
|
14
|
+
removeABACRule(entityId: string, flag: number): Promise<ABACRule | null>;
|
|
15
15
|
}
|
package/dist/mgr.js
CHANGED
|
@@ -4,37 +4,37 @@ export class WardenManager {
|
|
|
4
4
|
constructor(db) {
|
|
5
5
|
this.db = db;
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
return
|
|
7
|
+
changeRoleNameToId(name) {
|
|
8
|
+
return this.db.c(collections.roles).findOne({ name }).then((r) => r._id);
|
|
9
9
|
}
|
|
10
10
|
// ADD
|
|
11
|
-
|
|
12
|
-
return
|
|
11
|
+
addRole(role) {
|
|
12
|
+
return this.db.c(collections.roles).add(role);
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
addACLRule(entityId, p, uid) {
|
|
15
15
|
const rule = { p };
|
|
16
16
|
if (uid)
|
|
17
17
|
rule.uid = uid;
|
|
18
|
-
return
|
|
18
|
+
return this.db.c(collections.acl + "/" + entityId).add(rule, false);
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
return
|
|
20
|
+
addRBACRule(role_id, entity_id, p) {
|
|
21
|
+
return this.db.c(collections.role + "/" + role_id).add({ _id: entity_id, p }, false);
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
return
|
|
23
|
+
addABACRule(entity_id, flag, condition) {
|
|
24
|
+
return this.db.c(collections.abac + "/" + entity_id).add({ flag, condition }, true);
|
|
25
25
|
}
|
|
26
26
|
// DELETE
|
|
27
|
-
|
|
28
|
-
return
|
|
27
|
+
removeRole(roleId) {
|
|
28
|
+
return this.db.c(collections.roles).removeOne({ _id: roleId });
|
|
29
29
|
}
|
|
30
|
-
|
|
30
|
+
removeACLRule(entityId, uid) {
|
|
31
31
|
const q = uid ? { uid } : { $not: { $exists: { "uid": true } } };
|
|
32
|
-
return
|
|
32
|
+
return this.db.c(collections.acl + "/" + entityId).removeOne(q);
|
|
33
33
|
}
|
|
34
|
-
|
|
35
|
-
return
|
|
34
|
+
removeRBACRule(roleId, entityId) {
|
|
35
|
+
return this.db.c(collections.role + "/" + roleId).removeOne({ _id: entityId });
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
return
|
|
37
|
+
removeABACRule(entityId, flag) {
|
|
38
|
+
return this.db.c(collections.abac + "/" + entityId).removeOne({ flag });
|
|
39
39
|
}
|
|
40
40
|
}
|
package/dist/user.js
CHANGED
|
@@ -14,7 +14,8 @@ export class UserManager {
|
|
|
14
14
|
roles: userData.roles || [],
|
|
15
15
|
attrib: userData.attrib || {},
|
|
16
16
|
};
|
|
17
|
-
return await this.db.add(collections.users, newUser, false);
|
|
17
|
+
// return await this.db.add<User<A>>(collections.users, newUser, false);
|
|
18
|
+
return await this.db.c(collections.users).add(newUser, false);
|
|
18
19
|
}
|
|
19
20
|
/**
|
|
20
21
|
* Retrieves a user by _id
|
|
@@ -22,7 +23,7 @@ export class UserManager {
|
|
|
22
23
|
* @returns User or null if it doesn't exist
|
|
23
24
|
*/
|
|
24
25
|
async getUser(user_id) {
|
|
25
|
-
return this.db.
|
|
26
|
+
return this.db.c(collections.users).findOne({ _id: user_id });
|
|
26
27
|
}
|
|
27
28
|
/**
|
|
28
29
|
* Updates a user's data
|
|
@@ -34,14 +35,14 @@ export class UserManager {
|
|
|
34
35
|
if (!existingUser)
|
|
35
36
|
throw new Error("User not found");
|
|
36
37
|
const updatedUser = { ...existingUser, ...updates };
|
|
37
|
-
await this.db.
|
|
38
|
+
await this.db.c(collections.users).update({ _id: user_id }, updatedUser);
|
|
38
39
|
}
|
|
39
40
|
/**
|
|
40
41
|
* Deletes a user
|
|
41
42
|
* @param user_id User _id
|
|
42
43
|
*/
|
|
43
44
|
async deleteUser(user_id) {
|
|
44
|
-
await this.db.
|
|
45
|
+
await this.db.c(collections.users).removeOne({ _id: user_id });
|
|
45
46
|
}
|
|
46
47
|
/**
|
|
47
48
|
* Adds a role to a user
|
|
@@ -54,7 +55,7 @@ export class UserManager {
|
|
|
54
55
|
throw new Error("User not found");
|
|
55
56
|
if (!user.roles.includes(role_id)) {
|
|
56
57
|
user.roles.push(role_id);
|
|
57
|
-
await this.db.
|
|
58
|
+
await this.db.c(collections.users).update({ _id: user_id }, user);
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
61
|
/**
|
|
@@ -69,7 +70,7 @@ export class UserManager {
|
|
|
69
70
|
const index = user.roles.indexOf(role_id);
|
|
70
71
|
if (index !== -1) {
|
|
71
72
|
user.roles.splice(index, 1);
|
|
72
|
-
await this.db.
|
|
73
|
+
await this.db.c(collections.users).update({ _id: user_id }, user);
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
76
|
/**
|
|
@@ -82,6 +83,6 @@ export class UserManager {
|
|
|
82
83
|
if (!user)
|
|
83
84
|
throw new Error("User not found");
|
|
84
85
|
user.attrib = { ...user.attrib, ...attributes };
|
|
85
|
-
await this.db.
|
|
86
|
+
await this.db.c(collections.users).update({ _id: user_id }, user);
|
|
86
87
|
}
|
|
87
88
|
}
|
package/dist/warden.js
CHANGED
|
@@ -2,7 +2,7 @@ import { abacCheck, aclCheck, rbacCheck } from "./check.js";
|
|
|
2
2
|
import { COLORS, logAccess } from "./log.js";
|
|
3
3
|
import { collections } from "./const.js";
|
|
4
4
|
export async function fetchUser(db, userId) {
|
|
5
|
-
const user = await db.
|
|
5
|
+
const user = await db.c(collections.users).findOne({ _id: userId });
|
|
6
6
|
if (!user)
|
|
7
7
|
throw new Error("User not found");
|
|
8
8
|
return user;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/gate-warden",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"author": "wxn0brP",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"type": "module",
|
|
9
|
-
"description": "A simple and flexible access control library for Node.
|
|
9
|
+
"description": "A simple and flexible access control library for Node/Bun.",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "https://github.com/wxn0brP/gate-warden.git"
|
|
@@ -14,13 +14,12 @@
|
|
|
14
14
|
"homepage": "https://github.com/wxn0brP/gate-warden",
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/bun": "*",
|
|
17
|
-
"@
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"typescript": "*"
|
|
17
|
+
"@wxn0brp/db-core": "^0.10.2",
|
|
18
|
+
"tsc-alias": "^1",
|
|
19
|
+
"typescript": "^6"
|
|
21
20
|
},
|
|
22
21
|
"peerDependencies": {
|
|
23
|
-
"@wxn0brp/db-core": "
|
|
22
|
+
"@wxn0brp/db-core": "^0.10.2"
|
|
24
23
|
},
|
|
25
24
|
"files": [
|
|
26
25
|
"dist"
|