ais_auth_lib 1.0.9 → 1.0.11

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,5 +1,7 @@
1
- import { Condition } from "./interfaces/condition";
2
- import { ConditionGroup } from "./interfaces/condition-group";
3
- export declare function evaluateCondition(obj: any, condition: Condition): boolean;
4
- export declare function evaluateConditionGroup(obj: any, group: ConditionGroup): boolean;
5
- export declare const getPropertiesByPath: (object: any, path: string) => any[];
1
+ import { Condition } from "./interfaces/condition";
2
+ import { ConditionGroup } from "./interfaces/condition-group";
3
+ import { EvaluationContext } from "./interfaces/evaluation-context";
4
+ export declare function resolveDynamicValue(condition: Condition, context?: EvaluationContext): string | string[];
5
+ export declare function evaluateCondition(obj: any, condition: Condition, context?: EvaluationContext): boolean;
6
+ export declare function evaluateConditionGroup(obj: any, group: ConditionGroup, context?: EvaluationContext): boolean;
7
+ export declare const getPropertiesByPath: (object: any, path: string) => any[];
@@ -1,61 +1,97 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPropertiesByPath = exports.evaluateConditionGroup = exports.evaluateCondition = void 0;
4
- function evaluateCondition(obj, condition) {
5
- const { attribute, value, operator } = condition;
6
- console.log("attr", (0, exports.getPropertiesByPath)(obj, condition.attribute));
7
- const attributeValues = (0, exports.getPropertiesByPath)(obj, condition.attribute);
8
- switch (operator) {
9
- case "Равно":
10
- return attributeValues.some(attributeValue => attributeValue === value);
11
- case "Не равно":
12
- return attributeValues.some(attributeValue => attributeValue !== value);
13
- case "Содержит":
14
- return attributeValues.some(attributeValue => attributeValue.includes(value));
15
- case "Не содержит":
16
- return attributeValues.some(attributeValue => !attributeValue.includes(value));
17
- default:
18
- return false;
19
- }
20
- }
21
- exports.evaluateCondition = evaluateCondition;
22
- function evaluateConditionGroup(obj, group) {
23
- const { conditions } = group;
24
- let result = evaluateCondition(obj, conditions[0]);
25
- for (let i = 1; i < conditions.length; i++) {
26
- const condition = conditions[i];
27
- const currentResult = evaluateCondition(obj, condition);
28
- if (condition.logicalCondition === "ИЛИ") {
29
- result = result || currentResult;
30
- }
31
- else if (condition.logicalCondition === "И") {
32
- result = result && currentResult;
33
- }
34
- }
35
- return result;
36
- }
37
- exports.evaluateConditionGroup = evaluateConditionGroup;
38
- const getPropertiesByPath = (object, path) => {
39
- const properties = path.split('.');
40
- let clone = object;
41
- for (let i = 0; i < properties.length; i++) {
42
- const property = properties[i];
43
- if (clone) {
44
- if (property === '*' && Array.isArray(clone)) {
45
- const result = [];
46
- for (const item of clone) {
47
- const values = (0, exports.getPropertiesByPath)(item, properties.slice(i + 1).join('.'));
48
- result.push(...values);
49
- }
50
- return result;
51
- }
52
- if (property in clone) {
53
- clone = clone[property];
54
- continue;
55
- }
56
- }
57
- return [];
58
- }
59
- return [clone];
60
- };
61
- exports.getPropertiesByPath = getPropertiesByPath;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPropertiesByPath = exports.evaluateConditionGroup = exports.evaluateCondition = exports.resolveDynamicValue = void 0;
4
+ function resolveDynamicValue(condition, context) {
5
+ if (!(context === null || context === void 0 ? void 0 : context.user))
6
+ return undefined;
7
+ switch (condition.attribute) {
8
+ case "userUid":
9
+ if (condition.value === "me") {
10
+ return context.user.uid;
11
+ }
12
+ break;
13
+ case "resources.*.companies.*.division.uid":
14
+ if (!Array.isArray(context.user.companies)) {
15
+ return undefined;
16
+ }
17
+ if (condition.value === "my_division") {
18
+ return context.user.companies
19
+ .map((company) => { var _a; return (_a = company === null || company === void 0 ? void 0 : company.division) === null || _a === void 0 ? void 0 : _a.uid; })
20
+ .filter(Boolean);
21
+ }
22
+ if (condition.value === "parent_division") {
23
+ return context.user.companies
24
+ .map((company) => { var _a; return (_a = company === null || company === void 0 ? void 0 : company.division) === null || _a === void 0 ? void 0 : _a.parentUid; })
25
+ .filter(Boolean);
26
+ }
27
+ }
28
+ return undefined;
29
+ }
30
+ exports.resolveDynamicValue = resolveDynamicValue;
31
+ function evaluateCondition(obj, condition, context) {
32
+ const { attribute, value, operator, valueType } = condition;
33
+ console.log("attr", (0, exports.getPropertiesByPath)(obj, attribute));
34
+ const attributeValues = (0, exports.getPropertiesByPath)(obj, attribute);
35
+ if (valueType === "dynamic" && !context) {
36
+ return false;
37
+ }
38
+ const resolved = condition.valueType === "dynamic"
39
+ ? resolveDynamicValue(condition, context)
40
+ : value;
41
+ if (resolved === undefined)
42
+ return false;
43
+ const compareValues = Array.isArray(resolved) ? resolved : [resolved];
44
+ switch (operator) {
45
+ case "Равно":
46
+ return attributeValues.some((v) => compareValues.includes(v));
47
+ case "Не равно":
48
+ return attributeValues.every((v) => !compareValues.includes(v));
49
+ case "Содержит":
50
+ return attributeValues.some((v) => typeof v === "string" && compareValues.some((c) => v.includes(c)));
51
+ case "Не содержит":
52
+ return attributeValues.every((v) => typeof v === "string" && compareValues.every((c) => !v.includes(c)));
53
+ default:
54
+ return false;
55
+ }
56
+ }
57
+ exports.evaluateCondition = evaluateCondition;
58
+ function evaluateConditionGroup(obj, group, context) {
59
+ const { conditions } = group;
60
+ let result = evaluateCondition(obj, conditions[0], context);
61
+ for (let i = 1; i < conditions.length; i++) {
62
+ const condition = conditions[i];
63
+ const currentResult = evaluateCondition(obj, condition, context);
64
+ if (condition.logicalCondition === "ИЛИ") {
65
+ result = result || currentResult;
66
+ }
67
+ else if (condition.logicalCondition === "И") {
68
+ result = result && currentResult;
69
+ }
70
+ }
71
+ return result;
72
+ }
73
+ exports.evaluateConditionGroup = evaluateConditionGroup;
74
+ const getPropertiesByPath = (object, path) => {
75
+ const properties = path.split(".");
76
+ let clone = object;
77
+ for (let i = 0; i < properties.length; i++) {
78
+ const property = properties[i];
79
+ if (clone) {
80
+ if (property === "*" && Array.isArray(clone)) {
81
+ const result = [];
82
+ for (const item of clone) {
83
+ const values = (0, exports.getPropertiesByPath)(item, properties.slice(i + 1).join("."));
84
+ result.push(...values);
85
+ }
86
+ return result;
87
+ }
88
+ if (property in clone) {
89
+ clone = clone[property];
90
+ continue;
91
+ }
92
+ }
93
+ return [];
94
+ }
95
+ return [clone];
96
+ };
97
+ exports.getPropertiesByPath = getPropertiesByPath;
package/dist/filter.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- import { Rule } from "./interfaces/rule";
2
- export declare function filterObjects(objects: any[], rules: Rule[]): any[];
3
- export declare function checkResourceMatchWithRules(obj: any, rules: Rule[]): any;
1
+ import { EvaluationContext } from "./interfaces/evaluation-context";
2
+ import { Rule } from "./interfaces/rule";
3
+ export declare function filterObjects(objects: any[], rules: Rule[], context?: EvaluationContext): any[];
4
+ export declare function checkResourceMatchWithRules(obj: any, rules: Rule[], context?: EvaluationContext): any;
package/dist/filter.js CHANGED
@@ -1,40 +1,40 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkResourceMatchWithRules = exports.filterObjects = void 0;
4
- const conditions_1 = require("./conditions");
5
- function filterObjects(objects, rules) {
6
- const rulesWithoutCreateAction = rules.map((rule) => ({
7
- ...rule,
8
- actions: rule.actions.filter((action) => action !== "create"),
9
- })).filter(rule => rule.actions.length > 0);
10
- return objects.filter((obj) => {
11
- return checkResourceMatchWithRules(obj, rulesWithoutCreateAction);
12
- });
13
- }
14
- exports.filterObjects = filterObjects;
15
- function checkResourceMatchWithRules(obj, rules) {
16
- let overallResult = false;
17
- const actions = new Set();
18
- for (const rule of rules) {
19
- let ruleResult = (0, conditions_1.evaluateConditionGroup)(obj, rule.conditionGroups[0]);
20
- for (let i = 1; i < rule.conditionGroups.length; i++) {
21
- const group = rule.conditionGroups[i];
22
- const currentResult = (0, conditions_1.evaluateConditionGroup)(obj, group);
23
- if (group.logicalCondition === "ИЛИ") {
24
- ruleResult = ruleResult || currentResult;
25
- }
26
- else if (group.logicalCondition === "И") {
27
- ruleResult = ruleResult && currentResult;
28
- }
29
- }
30
- if (ruleResult) {
31
- rule.actions.forEach((action) => actions.add(action));
32
- }
33
- overallResult = overallResult || ruleResult;
34
- }
35
- if (overallResult) {
36
- obj.actions = Array.from(actions);
37
- }
38
- return overallResult ? obj : null;
39
- }
40
- exports.checkResourceMatchWithRules = checkResourceMatchWithRules;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkResourceMatchWithRules = exports.filterObjects = void 0;
4
+ const conditions_1 = require("./conditions");
5
+ function filterObjects(objects, rules, context) {
6
+ const rulesWithoutCreateAction = rules.map((rule) => ({
7
+ ...rule,
8
+ actions: rule.actions.filter((action) => action !== "create"),
9
+ })).filter(rule => rule.actions.length > 0);
10
+ return objects.filter((obj) => {
11
+ return checkResourceMatchWithRules(obj, rulesWithoutCreateAction, context);
12
+ });
13
+ }
14
+ exports.filterObjects = filterObjects;
15
+ function checkResourceMatchWithRules(obj, rules, context) {
16
+ let overallResult = false;
17
+ const actions = new Set();
18
+ for (const rule of rules) {
19
+ let ruleResult = (0, conditions_1.evaluateConditionGroup)(obj, rule.conditionGroups[0], context);
20
+ for (let i = 1; i < rule.conditionGroups.length; i++) {
21
+ const group = rule.conditionGroups[i];
22
+ const currentResult = (0, conditions_1.evaluateConditionGroup)(obj, group, context);
23
+ if (group.logicalCondition === "ИЛИ") {
24
+ ruleResult = ruleResult || currentResult;
25
+ }
26
+ else if (group.logicalCondition === "И") {
27
+ ruleResult = ruleResult && currentResult;
28
+ }
29
+ }
30
+ if (ruleResult) {
31
+ rule.actions.forEach((action) => actions.add(action));
32
+ }
33
+ overallResult = overallResult || ruleResult;
34
+ }
35
+ if (overallResult) {
36
+ obj.actions = Array.from(actions);
37
+ }
38
+ return overallResult ? obj : null;
39
+ }
40
+ exports.checkResourceMatchWithRules = checkResourceMatchWithRules;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export * from "./conditions";
2
- export * from "./filter";
3
- export * from "./interfaces/rule";
4
- export * from "./propertyFilter";
1
+ export * from "./conditions";
2
+ export * from "./filter";
3
+ export * from "./interfaces/rule";
4
+ export * from "./propertyFilter";
5
+ export * from "./interfaces/evaluation-context";
package/dist/index.js CHANGED
@@ -1,20 +1,21 @@
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
- __exportStar(require("./conditions"), exports);
18
- __exportStar(require("./filter"), exports);
19
- __exportStar(require("./interfaces/rule"), exports);
20
- __exportStar(require("./propertyFilter"), exports);
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
+ __exportStar(require("./conditions"), exports);
18
+ __exportStar(require("./filter"), exports);
19
+ __exportStar(require("./interfaces/rule"), exports);
20
+ __exportStar(require("./propertyFilter"), exports);
21
+ __exportStar(require("./interfaces/evaluation-context"), exports);
@@ -0,0 +1,4 @@
1
+ export interface Company {
2
+ uid: string;
3
+ name: string;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,9 +1,9 @@
1
- import { Condition } from "./condition";
2
- import { Rule } from "./rule";
3
- export interface ConditionGroup {
4
- uid: string;
5
- logicalCondition: string | null;
6
- orderIndex: number;
7
- conditions: Condition[];
8
- rule?: Rule;
9
- }
1
+ import { Condition } from "./condition";
2
+ import { Rule } from "./rule";
3
+ export interface ConditionGroup {
4
+ uid: string;
5
+ logicalCondition: string | null;
6
+ orderIndex: number;
7
+ conditions: Condition[];
8
+ rule?: Rule;
9
+ }
@@ -1,2 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,10 +1,11 @@
1
- import { ConditionGroup } from "./condition-group";
2
- export interface Condition {
3
- uid: string;
4
- attribute: string;
5
- value: string;
6
- operator: string;
7
- orderIndex: number;
8
- logicalCondition: string;
9
- conditionGroup?: ConditionGroup;
10
- }
1
+ import { ConditionGroup } from "./condition-group";
2
+ export interface Condition {
3
+ uid: string;
4
+ attribute: string;
5
+ value: string;
6
+ operator: string;
7
+ valueType: "static" | "dynamic";
8
+ orderIndex: number;
9
+ logicalCondition: string;
10
+ conditionGroup?: ConditionGroup;
11
+ }
@@ -1,2 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export interface Division {
2
+ uid: string;
3
+ name: string;
4
+ parentUid: string | null;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ import { Company } from "./company";
2
+ import { Division } from "./division";
3
+ import { Post } from "./post";
4
+ import { User } from "./user";
5
+ import { UserToCompany } from "./user-to-company";
6
+ export declare type Employee = User & {
7
+ companies: (UserToCompany & Company & {
8
+ division?: Division;
9
+ post?: Post;
10
+ head?: User;
11
+ })[];
12
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ import { Employee } from "./employee";
2
+ export interface EvaluationContext {
3
+ user: Employee;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ export interface Post {
2
+ uid: string;
3
+ name: string;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,10 +1,10 @@
1
- import { ConditionGroup } from "./condition-group";
2
- export interface Rule {
3
- uid: string;
4
- resource: string;
5
- appName: string;
6
- name: string;
7
- actions: string[];
8
- description: string;
9
- conditionGroups: ConditionGroup[];
10
- }
1
+ import { ConditionGroup } from "./condition-group";
2
+ export interface Rule {
3
+ uid: string;
4
+ resource: string;
5
+ appName: string;
6
+ name: string;
7
+ actions: string[];
8
+ description: string;
9
+ conditionGroups: ConditionGroup[];
10
+ }
@@ -1,2 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ export interface UserToCompany {
2
+ userUid: string;
3
+ companyUid: string;
4
+ divisionUid: string | null;
5
+ postUid: string | null;
6
+ tNumber: string;
7
+ dateOfEmployment: string;
8
+ dateOfDismissal: string | null;
9
+ email1: string;
10
+ email2: string;
11
+ phone1: string;
12
+ phone2: string;
13
+ status: string;
14
+ typeOfEmployment: string;
15
+ isHead: boolean;
16
+ actions: string[];
17
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,18 @@
1
+ export interface User {
2
+ uid: string;
3
+ name: string;
4
+ type: UserType;
5
+ inn: string;
6
+ username: string;
7
+ email: string;
8
+ city: string;
9
+ avatarUrl: string;
10
+ avatarName: string;
11
+ dateOfBirth: string | null;
12
+ actions: string[];
13
+ }
14
+ export declare enum UserType {
15
+ DEFAULT = "default",
16
+ VIRTUAL = "virtual",
17
+ VACANCY = "vacancy"
18
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UserType = void 0;
4
+ var UserType;
5
+ (function (UserType) {
6
+ UserType["DEFAULT"] = "default";
7
+ UserType["VIRTUAL"] = "virtual";
8
+ UserType["VACANCY"] = "vacancy";
9
+ })(UserType = exports.UserType || (exports.UserType = {}));
@@ -1,18 +1,18 @@
1
- export interface Resource {
2
- name: string;
3
- alias: string;
4
- attributes: Attribute[];
5
- }
6
- export interface Attribute {
7
- name: string;
8
- alias: string;
9
- type: string;
10
- }
11
- export declare const filterProperties: (objects: any[], rules: {
12
- resource: string;
13
- propetiesAccess: {
14
- property: string;
15
- read: boolean;
16
- write: boolean;
17
- }[];
18
- }[], resources: Resource[]) => {}[];
1
+ export interface Resource {
2
+ name: string;
3
+ alias: string;
4
+ attributes: Attribute[];
5
+ }
6
+ export interface Attribute {
7
+ name: string;
8
+ alias: string;
9
+ type: string;
10
+ }
11
+ export declare const filterProperties: (objects: any[], rules: {
12
+ resource: string;
13
+ propetiesAccess: {
14
+ property: string;
15
+ read: boolean;
16
+ write: boolean;
17
+ }[];
18
+ }[], resources: Resource[]) => {}[];
@@ -1,28 +1,28 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.filterProperties = void 0;
4
- const filterProperties = (objects, rules, resources) => {
5
- var _a;
6
- if (rules.length > 0) {
7
- const resource = rules[0].resource;
8
- const properties = rules
9
- .map((rule) => rule.propetiesAccess.map((pa) => pa.property))
10
- .flat();
11
- const allAttributes = (_a = resources
12
- .find((r) => r.name === resource)) === null || _a === void 0 ? void 0 : _a.attributes.map((a) => a.name);
13
- if (allAttributes) {
14
- return objects.map((object) => {
15
- return Object.keys(object).reduce((acc, key) => {
16
- if (properties.includes(key) || !allAttributes.includes(key)) {
17
- acc[key] = object[key];
18
- }
19
- return acc;
20
- }, {});
21
- });
22
- }
23
- else
24
- return [];
25
- }
26
- return [];
27
- };
28
- exports.filterProperties = filterProperties;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filterProperties = void 0;
4
+ const filterProperties = (objects, rules, resources) => {
5
+ var _a;
6
+ if (rules.length > 0) {
7
+ const resource = rules[0].resource;
8
+ const properties = rules
9
+ .map((rule) => rule.propetiesAccess.map((pa) => pa.property))
10
+ .flat();
11
+ const allAttributes = (_a = resources
12
+ .find((r) => r.name === resource)) === null || _a === void 0 ? void 0 : _a.attributes.map((a) => a.name);
13
+ if (allAttributes) {
14
+ return objects.map((object) => {
15
+ return Object.keys(object).reduce((acc, key) => {
16
+ if (properties.includes(key) || !allAttributes.includes(key)) {
17
+ acc[key] = object[key];
18
+ }
19
+ return acc;
20
+ }, {});
21
+ });
22
+ }
23
+ else
24
+ return [];
25
+ }
26
+ return [];
27
+ };
28
+ exports.filterProperties = filterProperties;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ais_auth_lib",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "auth lib for microservices",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",