@midwayjs/casbin-typeorm-adapter 3.5.3
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/README.md +12 -0
- package/dist/adapter.d.ts +26 -0
- package/dist/adapter.js +71 -0
- package/dist/casbinMongoRule.d.ts +13 -0
- package/dist/casbinMongoRule.js +72 -0
- package/dist/casbinRule.d.ts +13 -0
- package/dist/casbinRule.js +72 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +31 -0
- package/dist/interface.d.ts +10 -0
- package/dist/interface.js +3 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# midwayjs casbin with typeorm
|
|
2
|
+
|
|
3
|
+
[](http://packagequality.com/#?package=midway-core)
|
|
4
|
+
[](https://github.com/midwayjs/midway/pulls)
|
|
5
|
+
|
|
6
|
+
this is a sub package for midway.
|
|
7
|
+
|
|
8
|
+
Document: [https://midwayjs.org](https://midwayjs.org)
|
|
9
|
+
|
|
10
|
+
## License
|
|
11
|
+
|
|
12
|
+
[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { DataSource } from 'typeorm';
|
|
2
|
+
import { GenericCasbinRule, TypeORMAdapterConfig } from './interface';
|
|
3
|
+
import { BaseAdapter } from '@midwayjs/casbin';
|
|
4
|
+
/**
|
|
5
|
+
* TypeORMAdapter represents the TypeORM filtered adapter for policy storage.
|
|
6
|
+
*/
|
|
7
|
+
export declare class TypeORMAdapter extends BaseAdapter<GenericCasbinRule> {
|
|
8
|
+
private adapterConfig;
|
|
9
|
+
private typeorm;
|
|
10
|
+
constructor(dataSource: DataSource, options: TypeORMAdapterConfig);
|
|
11
|
+
private clearTable;
|
|
12
|
+
/**
|
|
13
|
+
* Returns either a {@link CasbinRule} or a {@link CasbinMongoRule}, depending on the type. If passed a custom entity through the adapter config it will use that entity type.
|
|
14
|
+
* This switch is required as the normal {@link CasbinRule} does not work when using MongoDB as a backend (due to a missing ObjectID field).
|
|
15
|
+
* @param type
|
|
16
|
+
* @param adapterConfig
|
|
17
|
+
*/
|
|
18
|
+
private static getCasbinRuleType;
|
|
19
|
+
private getRepository;
|
|
20
|
+
protected getAdapterLine(): new () => GenericCasbinRule;
|
|
21
|
+
protected loadPolicyByAdapter(): Promise<GenericCasbinRule[]>;
|
|
22
|
+
protected loadPolicyWithFilterByAdapter(filter: any): Promise<GenericCasbinRule[]>;
|
|
23
|
+
protected savePolicyByAdapter(policies: GenericCasbinRule[]): Promise<void>;
|
|
24
|
+
protected removePolicyByAdapter(removePolicy: GenericCasbinRule, newestPolicies?: GenericCasbinRule[]): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=adapter.d.ts.map
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypeORMAdapter = void 0;
|
|
4
|
+
const casbinRule_1 = require("./casbinRule");
|
|
5
|
+
const casbinMongoRule_1 = require("./casbinMongoRule");
|
|
6
|
+
const casbin_1 = require("@midwayjs/casbin");
|
|
7
|
+
/**
|
|
8
|
+
* TypeORMAdapter represents the TypeORM filtered adapter for policy storage.
|
|
9
|
+
*/
|
|
10
|
+
class TypeORMAdapter extends casbin_1.BaseAdapter {
|
|
11
|
+
constructor(dataSource, options) {
|
|
12
|
+
super();
|
|
13
|
+
this.typeorm = dataSource;
|
|
14
|
+
this.adapterConfig = options;
|
|
15
|
+
}
|
|
16
|
+
async clearTable() {
|
|
17
|
+
await this.getRepository().clear();
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns either a {@link CasbinRule} or a {@link CasbinMongoRule}, depending on the type. If passed a custom entity through the adapter config it will use that entity type.
|
|
21
|
+
* This switch is required as the normal {@link CasbinRule} does not work when using MongoDB as a backend (due to a missing ObjectID field).
|
|
22
|
+
* @param type
|
|
23
|
+
* @param adapterConfig
|
|
24
|
+
*/
|
|
25
|
+
static getCasbinRuleType(type, adapterConfig) {
|
|
26
|
+
if (adapterConfig === null || adapterConfig === void 0 ? void 0 : adapterConfig.customCasbinRuleEntity) {
|
|
27
|
+
return adapterConfig.customCasbinRuleEntity;
|
|
28
|
+
}
|
|
29
|
+
if (type === 'mongodb') {
|
|
30
|
+
return casbinMongoRule_1.CasbinMongoRule;
|
|
31
|
+
}
|
|
32
|
+
return casbinRule_1.CasbinRule;
|
|
33
|
+
}
|
|
34
|
+
getRepository() {
|
|
35
|
+
return this.typeorm.getRepository(this.getAdapterLine());
|
|
36
|
+
}
|
|
37
|
+
getAdapterLine() {
|
|
38
|
+
return TypeORMAdapter.getCasbinRuleType(this.adapterConfig.type, this.adapterConfig);
|
|
39
|
+
}
|
|
40
|
+
async loadPolicyByAdapter() {
|
|
41
|
+
return this.getRepository().find();
|
|
42
|
+
}
|
|
43
|
+
async loadPolicyWithFilterByAdapter(filter) {
|
|
44
|
+
return this.getRepository().find({ where: filter });
|
|
45
|
+
}
|
|
46
|
+
async savePolicyByAdapter(policies) {
|
|
47
|
+
await this.clearTable();
|
|
48
|
+
const queryRunner = this.typeorm.createQueryRunner();
|
|
49
|
+
await queryRunner.connect();
|
|
50
|
+
await queryRunner.startTransaction();
|
|
51
|
+
try {
|
|
52
|
+
await queryRunner.manager.save(policies);
|
|
53
|
+
await queryRunner.commitTransaction();
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
await queryRunner.rollbackTransaction();
|
|
57
|
+
throw err;
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
await queryRunner.release();
|
|
61
|
+
}
|
|
62
|
+
await this.getRepository().save(policies);
|
|
63
|
+
}
|
|
64
|
+
async removePolicyByAdapter(removePolicy, newestPolicies) {
|
|
65
|
+
await this.getRepository().delete({
|
|
66
|
+
...removePolicy,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.TypeORMAdapter = TypeORMAdapter;
|
|
71
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BaseEntity, ObjectID } from 'typeorm';
|
|
2
|
+
export declare class CasbinMongoRule extends BaseEntity {
|
|
3
|
+
id: ObjectID;
|
|
4
|
+
ptype: string;
|
|
5
|
+
v0: string;
|
|
6
|
+
v1: string;
|
|
7
|
+
v2: string;
|
|
8
|
+
v3: string;
|
|
9
|
+
v4: string;
|
|
10
|
+
v5: string;
|
|
11
|
+
v6: string;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=casbinMongoRule.d.ts.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CasbinMongoRule = void 0;
|
|
13
|
+
const typeorm_1 = require("typeorm");
|
|
14
|
+
let CasbinMongoRule = class CasbinMongoRule extends typeorm_1.BaseEntity {
|
|
15
|
+
};
|
|
16
|
+
__decorate([
|
|
17
|
+
(0, typeorm_1.ObjectIdColumn)(),
|
|
18
|
+
__metadata("design:type", typeorm_1.ObjectID)
|
|
19
|
+
], CasbinMongoRule.prototype, "id", void 0);
|
|
20
|
+
__decorate([
|
|
21
|
+
(0, typeorm_1.Column)({
|
|
22
|
+
nullable: true,
|
|
23
|
+
}),
|
|
24
|
+
__metadata("design:type", String)
|
|
25
|
+
], CasbinMongoRule.prototype, "ptype", void 0);
|
|
26
|
+
__decorate([
|
|
27
|
+
(0, typeorm_1.Column)({
|
|
28
|
+
nullable: true,
|
|
29
|
+
}),
|
|
30
|
+
__metadata("design:type", String)
|
|
31
|
+
], CasbinMongoRule.prototype, "v0", void 0);
|
|
32
|
+
__decorate([
|
|
33
|
+
(0, typeorm_1.Column)({
|
|
34
|
+
nullable: true,
|
|
35
|
+
}),
|
|
36
|
+
__metadata("design:type", String)
|
|
37
|
+
], CasbinMongoRule.prototype, "v1", void 0);
|
|
38
|
+
__decorate([
|
|
39
|
+
(0, typeorm_1.Column)({
|
|
40
|
+
nullable: true,
|
|
41
|
+
}),
|
|
42
|
+
__metadata("design:type", String)
|
|
43
|
+
], CasbinMongoRule.prototype, "v2", void 0);
|
|
44
|
+
__decorate([
|
|
45
|
+
(0, typeorm_1.Column)({
|
|
46
|
+
nullable: true,
|
|
47
|
+
}),
|
|
48
|
+
__metadata("design:type", String)
|
|
49
|
+
], CasbinMongoRule.prototype, "v3", void 0);
|
|
50
|
+
__decorate([
|
|
51
|
+
(0, typeorm_1.Column)({
|
|
52
|
+
nullable: true,
|
|
53
|
+
}),
|
|
54
|
+
__metadata("design:type", String)
|
|
55
|
+
], CasbinMongoRule.prototype, "v4", void 0);
|
|
56
|
+
__decorate([
|
|
57
|
+
(0, typeorm_1.Column)({
|
|
58
|
+
nullable: true,
|
|
59
|
+
}),
|
|
60
|
+
__metadata("design:type", String)
|
|
61
|
+
], CasbinMongoRule.prototype, "v5", void 0);
|
|
62
|
+
__decorate([
|
|
63
|
+
(0, typeorm_1.Column)({
|
|
64
|
+
nullable: true,
|
|
65
|
+
}),
|
|
66
|
+
__metadata("design:type", String)
|
|
67
|
+
], CasbinMongoRule.prototype, "v6", void 0);
|
|
68
|
+
CasbinMongoRule = __decorate([
|
|
69
|
+
(0, typeorm_1.Entity)()
|
|
70
|
+
], CasbinMongoRule);
|
|
71
|
+
exports.CasbinMongoRule = CasbinMongoRule;
|
|
72
|
+
//# sourceMappingURL=casbinMongoRule.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BaseEntity } from 'typeorm';
|
|
2
|
+
export declare class CasbinRule extends BaseEntity {
|
|
3
|
+
id: number;
|
|
4
|
+
ptype: string;
|
|
5
|
+
v0: string;
|
|
6
|
+
v1: string;
|
|
7
|
+
v2: string;
|
|
8
|
+
v3: string;
|
|
9
|
+
v4: string;
|
|
10
|
+
v5: string;
|
|
11
|
+
v6: string;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=casbinRule.d.ts.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CasbinRule = void 0;
|
|
13
|
+
const typeorm_1 = require("typeorm");
|
|
14
|
+
let CasbinRule = class CasbinRule extends typeorm_1.BaseEntity {
|
|
15
|
+
};
|
|
16
|
+
__decorate([
|
|
17
|
+
(0, typeorm_1.PrimaryGeneratedColumn)(),
|
|
18
|
+
__metadata("design:type", Number)
|
|
19
|
+
], CasbinRule.prototype, "id", void 0);
|
|
20
|
+
__decorate([
|
|
21
|
+
(0, typeorm_1.Column)({
|
|
22
|
+
nullable: true,
|
|
23
|
+
}),
|
|
24
|
+
__metadata("design:type", String)
|
|
25
|
+
], CasbinRule.prototype, "ptype", void 0);
|
|
26
|
+
__decorate([
|
|
27
|
+
(0, typeorm_1.Column)({
|
|
28
|
+
nullable: true,
|
|
29
|
+
}),
|
|
30
|
+
__metadata("design:type", String)
|
|
31
|
+
], CasbinRule.prototype, "v0", void 0);
|
|
32
|
+
__decorate([
|
|
33
|
+
(0, typeorm_1.Column)({
|
|
34
|
+
nullable: true,
|
|
35
|
+
}),
|
|
36
|
+
__metadata("design:type", String)
|
|
37
|
+
], CasbinRule.prototype, "v1", void 0);
|
|
38
|
+
__decorate([
|
|
39
|
+
(0, typeorm_1.Column)({
|
|
40
|
+
nullable: true,
|
|
41
|
+
}),
|
|
42
|
+
__metadata("design:type", String)
|
|
43
|
+
], CasbinRule.prototype, "v2", void 0);
|
|
44
|
+
__decorate([
|
|
45
|
+
(0, typeorm_1.Column)({
|
|
46
|
+
nullable: true,
|
|
47
|
+
}),
|
|
48
|
+
__metadata("design:type", String)
|
|
49
|
+
], CasbinRule.prototype, "v3", void 0);
|
|
50
|
+
__decorate([
|
|
51
|
+
(0, typeorm_1.Column)({
|
|
52
|
+
nullable: true,
|
|
53
|
+
}),
|
|
54
|
+
__metadata("design:type", String)
|
|
55
|
+
], CasbinRule.prototype, "v4", void 0);
|
|
56
|
+
__decorate([
|
|
57
|
+
(0, typeorm_1.Column)({
|
|
58
|
+
nullable: true,
|
|
59
|
+
}),
|
|
60
|
+
__metadata("design:type", String)
|
|
61
|
+
], CasbinRule.prototype, "v5", void 0);
|
|
62
|
+
__decorate([
|
|
63
|
+
(0, typeorm_1.Column)({
|
|
64
|
+
nullable: true,
|
|
65
|
+
}),
|
|
66
|
+
__metadata("design:type", String)
|
|
67
|
+
], CasbinRule.prototype, "v6", void 0);
|
|
68
|
+
CasbinRule = __decorate([
|
|
69
|
+
(0, typeorm_1.Entity)()
|
|
70
|
+
], CasbinRule);
|
|
71
|
+
exports.CasbinRule = CasbinRule;
|
|
72
|
+
//# sourceMappingURL=casbinRule.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IMidwayContainer } from '@midwayjs/core';
|
|
2
|
+
import { TypeORMAdapter } from './adapter';
|
|
3
|
+
import { TypeORMAdapterConfig } from './interface';
|
|
4
|
+
export * from './adapter';
|
|
5
|
+
export * from './casbinRule';
|
|
6
|
+
export * from './casbinMongoRule';
|
|
7
|
+
export declare function createAdapter(options: TypeORMAdapterConfig): (container: IMidwayContainer) => Promise<TypeORMAdapter>;
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.createAdapter = void 0;
|
|
18
|
+
const adapter_1 = require("./adapter");
|
|
19
|
+
const typeorm_1 = require("@midwayjs/typeorm");
|
|
20
|
+
__exportStar(require("./adapter"), exports);
|
|
21
|
+
__exportStar(require("./casbinRule"), exports);
|
|
22
|
+
__exportStar(require("./casbinMongoRule"), exports);
|
|
23
|
+
function createAdapter(options) {
|
|
24
|
+
return async (container) => {
|
|
25
|
+
const typeORMDataSourceManager = await container.getAsync(typeorm_1.TypeORMDataSourceManager);
|
|
26
|
+
const dataSource = typeORMDataSourceManager.getDataSource(options.dataSourceName);
|
|
27
|
+
return new adapter_1.TypeORMAdapter(dataSource, options);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
exports.createAdapter = createAdapter;
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CasbinRule } from './casbinRule';
|
|
2
|
+
import { CasbinMongoRule } from './casbinMongoRule';
|
|
3
|
+
export declare type GenericCasbinRule = CasbinRule | CasbinMongoRule;
|
|
4
|
+
export declare type CasbinRuleConstructor = new (...args: any[]) => GenericCasbinRule;
|
|
5
|
+
export interface TypeORMAdapterConfig {
|
|
6
|
+
dataSourceName?: string;
|
|
7
|
+
type?: string;
|
|
8
|
+
customCasbinRuleEntity?: CasbinRuleConstructor;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=interface.d.ts.map
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/casbin-typeorm-adapter",
|
|
3
|
+
"description": "midway casbin typeorm adapter",
|
|
4
|
+
"version": "3.5.3",
|
|
5
|
+
"main": "dist/index",
|
|
6
|
+
"typings": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/**/*.js",
|
|
9
|
+
"dist/**/*.d.ts"
|
|
10
|
+
],
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@midwayjs/core": "^3.5.3",
|
|
13
|
+
"@midwayjs/koa": "^3.5.3",
|
|
14
|
+
"@midwayjs/mock": "^3.5.3",
|
|
15
|
+
"casbin": "5.19.1",
|
|
16
|
+
"sqlite3": "5.1.2",
|
|
17
|
+
"typeorm": "0.3.10"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@midwayjs/casbin": "^3.5.3",
|
|
21
|
+
"@midwayjs/typeorm": "^3.5.3"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"midway",
|
|
25
|
+
"casbin",
|
|
26
|
+
"typeorm",
|
|
27
|
+
"adapter",
|
|
28
|
+
"typeorm-adapter"
|
|
29
|
+
],
|
|
30
|
+
"author": "czy88840616 <czy88840616@gmail.com>",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc",
|
|
34
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
35
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
|
|
36
|
+
"ci": "npm run test",
|
|
37
|
+
"lint": "mwts check"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=12"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "https://github.com/midwayjs/midway.git"
|
|
45
|
+
}
|
|
46
|
+
}
|