@schorts/shared-kernel 2.2.4 → 2.3.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.
- package/CHANGELOG +8 -2
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/rbac/index.js +6 -0
- package/dist/cjs/rbac/index.js.map +1 -0
- package/dist/cjs/rbac/permission.js +3 -0
- package/dist/cjs/rbac/permission.js.map +1 -0
- package/dist/cjs/rbac/rbac-policy.js +18 -0
- package/dist/cjs/rbac/rbac-policy.js.map +1 -0
- package/dist/cjs/rbac/resource.js +3 -0
- package/dist/cjs/rbac/resource.js.map +1 -0
- package/dist/cjs/value-objects/enum-value.js +4 -5
- package/dist/cjs/value-objects/enum-value.js.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/rbac/index.js +6 -0
- package/dist/esm/rbac/index.js.map +1 -0
- package/dist/esm/rbac/permission.js +3 -0
- package/dist/esm/rbac/permission.js.map +1 -0
- package/dist/esm/rbac/rbac-policy.js +18 -0
- package/dist/esm/rbac/rbac-policy.js.map +1 -0
- package/dist/esm/rbac/resource.js +3 -0
- package/dist/esm/rbac/resource.js.map +1 -0
- package/dist/esm/value-objects/enum-value.js +4 -5
- package/dist/esm/value-objects/enum-value.js.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/rbac/index.d.ts +4 -0
- package/dist/types/rbac/index.d.ts.map +1 -0
- package/dist/types/rbac/permission.d.ts +5 -0
- package/dist/types/rbac/permission.d.ts.map +1 -0
- package/dist/types/rbac/rbac-policy.d.ts +11 -0
- package/dist/types/rbac/rbac-policy.d.ts.map +1 -0
- package/dist/types/rbac/resource.d.ts +5 -0
- package/dist/types/rbac/resource.d.ts.map +1 -0
- package/dist/types/value-objects/enum-value.d.ts +5 -5
- package/dist/types/value-objects/enum-value.d.ts.map +1 -1
- package/package.json +6 -1
- package/src/index.ts +1 -0
- package/src/rbac/index.ts +4 -0
- package/src/rbac/permission.ts +4 -0
- package/src/rbac/rbac-policy.ts +26 -0
- package/src/rbac/resource.ts +4 -0
- package/src/value-objects/enum-value.ts +12 -10
package/CHANGELOG
CHANGED
|
@@ -5,13 +5,19 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
## [2.3.0] - 2025-10-11
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Added `RBAC` module.
|
|
13
|
+
|
|
14
|
+
## [2.2.4] - 2025-10-10
|
|
9
15
|
|
|
10
16
|
### Changed
|
|
11
17
|
|
|
12
18
|
- Remove `Optional` param in the `EnumValue`. Instead allow null values in the `allowedTypes` array.
|
|
13
19
|
|
|
14
|
-
|
|
20
|
+
## [2.2.3] - 2025-10-10
|
|
15
21
|
|
|
16
22
|
### Changed
|
|
17
23
|
|
package/dist/cjs/index.js
CHANGED
|
@@ -25,6 +25,7 @@ __exportStar(require("./i18n"), exports);
|
|
|
25
25
|
__exportStar(require("./json-api"), exports);
|
|
26
26
|
__exportStar(require("./messages"), exports);
|
|
27
27
|
__exportStar(require("./models"), exports);
|
|
28
|
+
__exportStar(require("./rbac"), exports);
|
|
28
29
|
__exportStar(require("./result"), exports);
|
|
29
30
|
__exportStar(require("./state-manager"), exports);
|
|
30
31
|
__exportStar(require("./unit-of-work"), exports);
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,6CAA2B;AAC3B,wCAAsB;AACtB,kDAAgC;AAChC,6CAA2B;AAC3B,+CAA6B;AAC7B,yCAAuB;AACvB,yCAAuB;AACvB,6CAA2B;AAC3B,6CAA2B;AAC3B,2CAAyB;AACzB,2CAAyB;AACzB,kDAAgC;AAChC,iDAA+B;AAC/B,0CAAwB;AACxB,kDAAgC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,6CAA2B;AAC3B,wCAAsB;AACtB,kDAAgC;AAChC,6CAA2B;AAC3B,+CAA6B;AAC7B,yCAAuB;AACvB,yCAAuB;AACvB,6CAA2B;AAC3B,6CAA2B;AAC3B,2CAAyB;AACzB,yCAAuB;AACvB,2CAAyB;AACzB,kDAAgC;AAChC,iDAA+B;AAC/B,0CAAwB;AACxB,kDAAgC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RBACPolicy = void 0;
|
|
4
|
+
var rbac_policy_1 = require("./rbac-policy");
|
|
5
|
+
Object.defineProperty(exports, "RBACPolicy", { enumerable: true, get: function () { return rbac_policy_1.RBACPolicy; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/rbac/index.ts"],"names":[],"mappings":";;;AAGA,6CAA2C;AAAlC,yGAAA,UAAU,OAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permission.js","sourceRoot":"","sources":["../../../src/rbac/permission.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RBACPolicy = void 0;
|
|
4
|
+
class RBACPolicy {
|
|
5
|
+
can(role, action, resource) {
|
|
6
|
+
const permissions = this.getPermissions(role);
|
|
7
|
+
return permissions.some((perm) => (perm.resource === '*' || perm.resource === resource.name) &&
|
|
8
|
+
(perm.action === action || perm.action === 'manage'));
|
|
9
|
+
}
|
|
10
|
+
canAccessOwnedResource(resource) {
|
|
11
|
+
return resource.owner_id === this.userID.value;
|
|
12
|
+
}
|
|
13
|
+
canWithOwnership(role, action, resource) {
|
|
14
|
+
return this.can(role, action, resource) && this.canAccessOwnedResource(resource);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.RBACPolicy = RBACPolicy;
|
|
18
|
+
//# sourceMappingURL=rbac-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rbac-policy.js","sourceRoot":"","sources":["../../../src/rbac/rbac-policy.ts"],"names":[],"mappings":";;;AAIA,MAAsB,UAAU;IAI9B,GAAG,CAAC,IAAU,EAAE,MAA4B,EAAE,QAAkB;QAC9D,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;QAE9C,OAAO,WAAW,CAAC,IAAI,CACrB,CAAC,IAAI,EAAE,EAAE,CACP,CAAC,IAAI,CAAC,QAAQ,KAAK,GAAG,IAAI,IAAI,CAAC,QAAQ,KAAK,QAAQ,CAAC,IAAI,CAAC;YAC1D,CAAC,IAAI,CAAC,MAAM,KAAK,MAAM,IAAI,IAAI,CAAC,MAAM,KAAK,QAAQ,CAAC,CACvD,CAAC;IACJ,CAAC;IAED,sBAAsB,CAAC,QAAkB;QACvC,OAAO,QAAQ,CAAC,QAAQ,KAAK,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;IACjD,CAAC;IAED,gBAAgB,CAAC,IAAU,EAAE,MAA4B,EAAE,QAAkB;QAC3E,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,IAAI,IAAI,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IACnF,CAAC;CACF;AArBD,gCAqBC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resource.js","sourceRoot":"","sources":["../../../src/rbac/resource.ts"],"names":[],"mappings":""}
|
|
@@ -13,11 +13,10 @@ class EnumValue {
|
|
|
13
13
|
return this.allowedValues.includes(this.value);
|
|
14
14
|
}
|
|
15
15
|
equals(valueObject) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return this.value === valueObject.value;
|
|
16
|
+
return (valueObject instanceof EnumValue &&
|
|
17
|
+
this.isValid &&
|
|
18
|
+
valueObject.isValid &&
|
|
19
|
+
this.value === valueObject.value);
|
|
21
20
|
}
|
|
22
21
|
}
|
|
23
22
|
exports.EnumValue = EnumValue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum-value.js","sourceRoot":"","sources":["../../../src/value-objects/enum-value.ts"],"names":[],"mappings":";;;AAEA,MAAsB,SAAS;IACpB,SAAS,GAAG,MAAM,CAAC;IACnB,aAAa,
|
|
1
|
+
{"version":3,"file":"enum-value.js","sourceRoot":"","sources":["../../../src/value-objects/enum-value.ts"],"names":[],"mappings":";;;AAEA,MAAsB,SAAS;IACpB,SAAS,GAAG,MAAM,CAAC;IACnB,aAAa,CAAU;IACvB,KAAK,CAAkB;IAEhC,YAAY,aAAsB,EAAE,KAAsB;QACxD,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACjD,CAAC;IAED,MAAM,CAAC,WAAoB;QACzB,OAAO,CACL,WAAW,YAAY,SAAS;YAChC,IAAI,CAAC,OAAO;YACZ,WAAW,CAAC,OAAO;YACnB,IAAI,CAAC,KAAK,KAAK,WAAW,CAAC,KAAK,CACjC,CAAC;IACJ,CAAC;CAGF;AAxBD,8BAwBC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -25,6 +25,7 @@ __exportStar(require("./i18n"), exports);
|
|
|
25
25
|
__exportStar(require("./json-api"), exports);
|
|
26
26
|
__exportStar(require("./messages"), exports);
|
|
27
27
|
__exportStar(require("./models"), exports);
|
|
28
|
+
__exportStar(require("./rbac"), exports);
|
|
28
29
|
__exportStar(require("./result"), exports);
|
|
29
30
|
__exportStar(require("./state-manager"), exports);
|
|
30
31
|
__exportStar(require("./unit-of-work"), exports);
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,6CAA2B;AAC3B,wCAAsB;AACtB,kDAAgC;AAChC,6CAA2B;AAC3B,+CAA6B;AAC7B,yCAAuB;AACvB,yCAAuB;AACvB,6CAA2B;AAC3B,6CAA2B;AAC3B,2CAAyB;AACzB,2CAAyB;AACzB,kDAAgC;AAChC,iDAA+B;AAC/B,0CAAwB;AACxB,kDAAgC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,6CAA2B;AAC3B,wCAAsB;AACtB,kDAAgC;AAChC,6CAA2B;AAC3B,+CAA6B;AAC7B,yCAAuB;AACvB,yCAAuB;AACvB,6CAA2B;AAC3B,6CAA2B;AAC3B,2CAAyB;AACzB,yCAAuB;AACvB,2CAAyB;AACzB,kDAAgC;AAChC,iDAA+B;AAC/B,0CAAwB;AACxB,kDAAgC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RBACPolicy = void 0;
|
|
4
|
+
var rbac_policy_1 = require("./rbac-policy");
|
|
5
|
+
Object.defineProperty(exports, "RBACPolicy", { enumerable: true, get: function () { return rbac_policy_1.RBACPolicy; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/rbac/index.ts"],"names":[],"mappings":";;;AAGA,6CAA2C;AAAlC,yGAAA,UAAU,OAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permission.js","sourceRoot":"","sources":["../../../src/rbac/permission.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RBACPolicy = void 0;
|
|
4
|
+
class RBACPolicy {
|
|
5
|
+
can(role, action, resource) {
|
|
6
|
+
const permissions = this.getPermissions(role);
|
|
7
|
+
return permissions.some((perm) => (perm.resource === '*' || perm.resource === resource.name) &&
|
|
8
|
+
(perm.action === action || perm.action === 'manage'));
|
|
9
|
+
}
|
|
10
|
+
canAccessOwnedResource(resource) {
|
|
11
|
+
return resource.owner_id === this.userID.value;
|
|
12
|
+
}
|
|
13
|
+
canWithOwnership(role, action, resource) {
|
|
14
|
+
return this.can(role, action, resource) && this.canAccessOwnedResource(resource);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.RBACPolicy = RBACPolicy;
|
|
18
|
+
//# sourceMappingURL=rbac-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rbac-policy.js","sourceRoot":"","sources":["../../../src/rbac/rbac-policy.ts"],"names":[],"mappings":";;;AAIA,MAAsB,UAAU;IAI9B,GAAG,CAAC,IAAU,EAAE,MAA4B,EAAE,QAAkB;QAC9D,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;QAE9C,OAAO,WAAW,CAAC,IAAI,CACrB,CAAC,IAAI,EAAE,EAAE,CACP,CAAC,IAAI,CAAC,QAAQ,KAAK,GAAG,IAAI,IAAI,CAAC,QAAQ,KAAK,QAAQ,CAAC,IAAI,CAAC;YAC1D,CAAC,IAAI,CAAC,MAAM,KAAK,MAAM,IAAI,IAAI,CAAC,MAAM,KAAK,QAAQ,CAAC,CACvD,CAAC;IACJ,CAAC;IAED,sBAAsB,CAAC,QAAkB;QACvC,OAAO,QAAQ,CAAC,QAAQ,KAAK,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;IACjD,CAAC;IAED,gBAAgB,CAAC,IAAU,EAAE,MAA4B,EAAE,QAAkB;QAC3E,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,IAAI,IAAI,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IACnF,CAAC;CACF;AArBD,gCAqBC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resource.js","sourceRoot":"","sources":["../../../src/rbac/resource.ts"],"names":[],"mappings":""}
|
|
@@ -13,11 +13,10 @@ class EnumValue {
|
|
|
13
13
|
return this.allowedValues.includes(this.value);
|
|
14
14
|
}
|
|
15
15
|
equals(valueObject) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return this.value === valueObject.value;
|
|
16
|
+
return (valueObject instanceof EnumValue &&
|
|
17
|
+
this.isValid &&
|
|
18
|
+
valueObject.isValid &&
|
|
19
|
+
this.value === valueObject.value);
|
|
21
20
|
}
|
|
22
21
|
}
|
|
23
22
|
exports.EnumValue = EnumValue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum-value.js","sourceRoot":"","sources":["../../../src/value-objects/enum-value.ts"],"names":[],"mappings":";;;AAEA,MAAsB,SAAS;IACpB,SAAS,GAAG,MAAM,CAAC;IACnB,aAAa,
|
|
1
|
+
{"version":3,"file":"enum-value.js","sourceRoot":"","sources":["../../../src/value-objects/enum-value.ts"],"names":[],"mappings":";;;AAEA,MAAsB,SAAS;IACpB,SAAS,GAAG,MAAM,CAAC;IACnB,aAAa,CAAU;IACvB,KAAK,CAAkB;IAEhC,YAAY,aAAsB,EAAE,KAAsB;QACxD,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACjD,CAAC;IAED,MAAM,CAAC,WAAoB;QACzB,OAAO,CACL,WAAW,YAAY,SAAS;YAChC,IAAI,CAAC,OAAO;YACZ,WAAW,CAAC,OAAO;YACnB,IAAI,CAAC,KAAK,KAAK,WAAW,CAAC,KAAK,CACjC,CAAC;IACJ,CAAC;CAGF;AAxBD,8BAwBC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/rbac/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,YAAY,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permission.d.ts","sourceRoot":"","sources":["../../../src/rbac/permission.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAC;CAChD,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Permission } from './permission';
|
|
2
|
+
import { Resource } from './resource';
|
|
3
|
+
import { ValueObject } from '../value-objects';
|
|
4
|
+
export declare abstract class RBACPolicy<Role extends string, UserID extends ValueObject> {
|
|
5
|
+
abstract userID: UserID;
|
|
6
|
+
abstract getPermissions(role: Role): Permission[];
|
|
7
|
+
can(role: Role, action: Permission['action'], resource: Resource): boolean;
|
|
8
|
+
canAccessOwnedResource(resource: Resource): boolean;
|
|
9
|
+
canWithOwnership(role: Role, action: Permission['action'], resource: Resource): boolean;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=rbac-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rbac-policy.d.ts","sourceRoot":"","sources":["../../../src/rbac/rbac-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,8BAAsB,UAAU,CAAC,IAAI,SAAS,MAAM,EAAE,MAAM,SAAS,WAAW;IAC9E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,UAAU,EAAE;IAEjD,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,QAAQ,GAAG,OAAO;IAU1E,sBAAsB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO;IAInD,gBAAgB,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,QAAQ,GAAG,OAAO;CAGxF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resource.d.ts","sourceRoot":"","sources":["../../../src/rbac/resource.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { ValueObject } from
|
|
2
|
-
export declare abstract class EnumValue<
|
|
1
|
+
import { ValueObject } from "./value-object";
|
|
2
|
+
export declare abstract class EnumValue<Allowed extends readonly (string | null)[]> implements ValueObject {
|
|
3
3
|
readonly valueType = "Enum";
|
|
4
|
-
readonly allowedValues:
|
|
5
|
-
readonly value:
|
|
6
|
-
constructor(allowedValues:
|
|
4
|
+
readonly allowedValues: Allowed;
|
|
5
|
+
readonly value: Allowed[number];
|
|
6
|
+
constructor(allowedValues: Allowed, value: Allowed[number]);
|
|
7
7
|
get isValid(): boolean;
|
|
8
8
|
equals(valueObject: unknown): boolean;
|
|
9
9
|
abstract readonly attributeName: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum-value.d.ts","sourceRoot":"","sources":["../../../src/value-objects/enum-value.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"enum-value.d.ts","sourceRoot":"","sources":["../../../src/value-objects/enum-value.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,8BAAsB,SAAS,CAAC,OAAO,SAAS,SAAS,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,CAAE,YAAW,WAAW;IAChG,QAAQ,CAAC,SAAS,UAAU;IAC5B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;gBAEpB,aAAa,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC;IAK1D,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,MAAM,CAAC,WAAW,EAAE,OAAO,GAAG,OAAO;IASrC,QAAQ,CAAC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;CACzC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schorts/shared-kernel",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "A modular, type-safe foundation for building expressive, maintainable applications. This package provides core abstractions for domain modeling, HTTP integration, authentication, state management, and more — designed to be framework-agnostic and highly extensible.",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -66,6 +66,11 @@
|
|
|
66
66
|
"require": "./dist/cjs/models/index.js",
|
|
67
67
|
"types": "./dist/types/models/index.d.ts"
|
|
68
68
|
},
|
|
69
|
+
"./rbac": {
|
|
70
|
+
"import": "./dist/esm/rbac/index.js",
|
|
71
|
+
"require": "./dist/cjs/rbac/index.js",
|
|
72
|
+
"types": "./dist/types/rbac/index.d.ts"
|
|
73
|
+
},
|
|
69
74
|
"./result": {
|
|
70
75
|
"import": "./dist/esm/result/index.js",
|
|
71
76
|
"require": "./dist/cjs/result/index.js",
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Permission } from './permission';
|
|
2
|
+
import { Resource } from './resource';
|
|
3
|
+
import { ValueObject } from '../value-objects';
|
|
4
|
+
|
|
5
|
+
export abstract class RBACPolicy<Role extends string, UserID extends ValueObject> {
|
|
6
|
+
abstract userID: UserID;
|
|
7
|
+
abstract getPermissions(role: Role): Permission[];
|
|
8
|
+
|
|
9
|
+
can(role: Role, action: Permission['action'], resource: Resource): boolean {
|
|
10
|
+
const permissions = this.getPermissions(role);
|
|
11
|
+
|
|
12
|
+
return permissions.some(
|
|
13
|
+
(perm) =>
|
|
14
|
+
(perm.resource === '*' || perm.resource === resource.name) &&
|
|
15
|
+
(perm.action === action || perm.action === 'manage')
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
canAccessOwnedResource(resource: Resource): boolean {
|
|
20
|
+
return resource.owner_id === this.userID.value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
canWithOwnership(role: Role, action: Permission['action'], resource: Resource): boolean {
|
|
24
|
+
return this.can(role, action, resource) && this.canAccessOwnedResource(resource);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
import { ValueObject } from
|
|
1
|
+
import { ValueObject } from "./value-object";
|
|
2
2
|
|
|
3
|
-
export abstract class EnumValue<
|
|
3
|
+
export abstract class EnumValue<Allowed extends readonly (string | null)[]> implements ValueObject {
|
|
4
4
|
readonly valueType = 'Enum';
|
|
5
|
-
readonly allowedValues:
|
|
6
|
-
readonly value:
|
|
5
|
+
readonly allowedValues: Allowed;
|
|
6
|
+
readonly value: Allowed[number];
|
|
7
7
|
|
|
8
|
-
constructor(allowedValues:
|
|
8
|
+
constructor(allowedValues: Allowed, value: Allowed[number]) {
|
|
9
9
|
this.allowedValues = allowedValues;
|
|
10
10
|
this.value = value;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
get isValid(): boolean {
|
|
14
|
-
return this.allowedValues.includes(this.value
|
|
14
|
+
return this.allowedValues.includes(this.value);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
equals(valueObject: unknown): boolean {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
return (
|
|
19
|
+
valueObject instanceof EnumValue &&
|
|
20
|
+
this.isValid &&
|
|
21
|
+
valueObject.isValid &&
|
|
22
|
+
this.value === valueObject.value
|
|
23
|
+
);
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
abstract readonly attributeName: string;
|