Skip to content

Commit

Permalink
feature userAccessPolicy: grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
light-source committed Jan 7, 2025
1 parent b319140 commit 830ebcb
Show file tree
Hide file tree
Showing 31 changed files with 217 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ import type SearchRuleFilters from "./search/searchRuleFilters.js";
import type SearchRuleFilterSettings from "./search/searchRuleFilterSettings.js";

class MongooseRulesStorage implements RulesStorage {
private model: Model<Rule> | null;

constructor(model: Model<Rule> | null) {
this.model = model;
constructor(
private readingModel: Model<Rule> | null,
private writingModel: Model<Rule> | null = null,
) {
if (null === this.writingModel) {
this.writingModel = this.readingModel;
}
}

public async insert(record: Rule): Promise<RuleRecord> {
if (!this.model) {
if (!this.writingModel) {
throw new Error("Model is not set");
}

const document = await this.model.create(record);
const document = await this.writingModel.create(record);

const ruleRecord = this.convertMongooseRecordToRuleRecord(
document.toObject(),
Expand All @@ -43,11 +46,11 @@ class MongooseRulesStorage implements RulesStorage {
}

public async insertMany(records: Rule[]): Promise<RuleRecord[]> {
if (!this.model) {
if (!this.writingModel) {
throw new Error("Model is not set");
}

const documents = await this.model.insertMany(records);
const documents = await this.writingModel.insertMany(records);
const objectDocuments = documents.map((document) => document.toObject());

const ruleRecords =
Expand All @@ -60,13 +63,13 @@ class MongooseRulesStorage implements RulesStorage {
filters: SearchRuleFilters,
filterSettings?: SearchRuleFilterSettings,
): Promise<RuleRecord[]> {
if (!this.model) {
if (!this.readingModel) {
throw new Error("Model is not set");
}

const query = this.createQuery(filters, filterSettings);

const mongooseRecords = await this.model.find(query).lean().exec();
const mongooseRecords = await this.readingModel.find(query).lean().exec();

const ruleRecords =
this.convertMongooseRecordsToRuleRecords(mongooseRecords);
Expand All @@ -75,17 +78,25 @@ class MongooseRulesStorage implements RulesStorage {
}

public async deleteMany(recordFilters: SearchRuleFilters[]): Promise<void> {
if (!this.model) {
if (!this.writingModel) {
throw new Error("Model is not set");
}

for (const recordFilter of recordFilters) {
await this.model.deleteOne(recordFilter).exec();
await this.writingModel.deleteOne(recordFilter).exec();
}
}

public setReadingModel(readingModel: Model<Rule>): void {
this.readingModel = readingModel;

if (null === this.writingModel) {
this.setWritingModel(readingModel);
}
}

public setModel(model: Model<Rule>): void {
this.model = model;
public setWritingModel(writingModel: Model<Rule>): void {
this.writingModel = writingModel;
}

protected createQuery(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Address4 } from "ip-address";
import { expect } from "vitest";
import TestFindRuleBase from "./testFindRuleBase.js";
import type Rule from "../../../rule.js";
import type SearchRuleFilters from "../searchRuleFilters.js";
import TestFindRuleBase from "../testFindRuleBase.js";
import type Rule from "../../../../rule.js";
import type SearchRuleFilters from "../../searchRuleFilters.js";

class TestFindRule extends TestFindRuleBase {
private readonly userIp: Address4 = new Address4("192.168.1.1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
SearchRuleFilters,
UserAccessRule,
} from "@prosopo/types-database";
import TestFindRuleBase from "./testFindRuleBase.js";
import TestFindRuleBase from "../testFindRuleBase.js";

class TestFindRuleByUserId extends TestFindRuleBase {
private readonly userId: string = "userId";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../mongooseRulesStorage.js";
import TestFindRule from "./testFindRule.js";

describe("MongooseFindRule", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindRule(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../mongooseRulesStorage.js";
import TestFindRuleByUserId from "./testFindRuleByUserId.js";

describe("MongooseFindRuleByUserId", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindRuleByUserId(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TestFindByMaskV4 from "../testFindByMaskV4.js";
import TestFindByMaskV4 from "../../testFindByMaskV4.js";

class TestFindByMaskV4RangeMax extends TestFindByMaskV4 {
protected override baseIpAsString = "192.168.0.0";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../../../mongooseRulesStorage.js";
import TestFindByMaskV4RangeMax from "./testFindByMaskV4RangeMax.js";

describe("MongooseFindByMaskV4RangeMax", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByMaskV4RangeMax(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TestFindByMaskV4 from "../testFindByMaskV4.js";
import TestFindByMaskV4 from "../../testFindByMaskV4.js";

class TestFindByMaskV4RangeMin extends TestFindByMaskV4 {
protected override baseIpAsString = "192.168.1.0";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../../../mongooseRulesStorage.js";
import TestFindByMaskV4RangeMin from "./testFindByMaskV4RangeMin.js";

describe("MongooseFindByMaskV4RangeMin", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByMaskV4RangeMin(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
import type { UserIp } from "@prosopo/types-database";
import { Address4 } from "ip-address";
import TestFindByIpV4 from "./testFindByIpV4.js";
import TestFindByIpV4 from "../testFindByIpV4.js";

class TestFindByMaskV4 extends TestFindByIpV4 {
protected baseIpAsString = "192.168.0.0";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../mongooseRulesStorage.js";
import TestFindByMaskV4 from "./testFindByMaskV4.js";

describe("MongooseFindByMaskV4", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByMaskV4(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../mongooseRulesStorage.js";
import TestFindByIpV4 from "./testFindByIpV4.js";

describe("MongooseFindByIpV4", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByIpV4(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TestFindByIpMaskV6 from "../testFindByIpMaskV6.js";
import TestFindByIpMaskV6 from "../../testFindByIpMaskV6.js";

class TestFindByIpMaskV6RangeMax extends TestFindByIpMaskV6 {
protected override baseIpAsString = "2001:db8:3333:4444:5555:6666:7777:8888";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../../../mongooseRulesStorage.js";
import TestFindByIpMaskV6RangeMax from "./testFindByIpMaskV6RangeMax.js";

describe("MongooseFindByIpMaskV6RangeMax", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByIpMaskV6RangeMax(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TestFindByIpMaskV6 from "../testFindByIpMaskV6.js";
import TestFindByIpMaskV6 from "../../testFindByIpMaskV6.js";

class TestFindByIpMaskV6RangeMin extends TestFindByIpMaskV6 {
protected override baseIpAsString = "2001:db8:3333:4444:5555:6666:7777:8888";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe } from "vitest";
import testMongooseRuleModel from "../../../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../../../mongooseRulesStorage.js";
import TestFindByIpMaskV6RangeMin from "./testFindByIpMaskV6RangeMin.js";

describe("MongooseFindByIpMaskV6RangeMin", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByIpMaskV6RangeMin(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address6 } from "ip-address";
import TestFindByIpV6 from "./testFindByIpV6.js";
import type Ip from "../../../../../../ip/ip.js";
import type Ip from "../../../../../../../ip/ip.js";
import TestFindByIpV6 from "../testFindByIpV6.js";

class TestFindByIpMaskV6 extends TestFindByIpV6 {
protected baseIpAsString = "2001:db8:3333:4444:5555:6666:7777:8888";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../mongooseRulesStorage.js";
import TestFindByIpMaskV6 from "./testFindByIpMaskV6.js";

describe("MongooseFindByIpMaskV6", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByIpMaskV6(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import TestFindByIpMaskV6 from "../../v6/testFindByIpMaskV6.js";
import TestFindByIpMaskV6 from "../../../../mask/testFindByIpMaskV6.js";

class TestFindByIpMaskV6ShortRangeMax extends TestFindByIpMaskV6 {
protected override baseIpAsString = "::1";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../../../../mongooseRulesStorage.js";
import {TestFindByIpMaskV6ShortRangeMax} from "./testFindByIpMaskV6ShortRangeMax.js";

describe("MongooseFindByIpV6MaskV6ShortRangeMax", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByIpMaskV6ShortRangeMax(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import TestFindByIpMaskV6 from "../../v6/testFindByIpMaskV6.js";
import TestFindByIpMaskV6 from "../../../../mask/testFindByIpMaskV6.js";

class TestFindRuleByIpMaskV6ShortRangeMin extends TestFindByIpMaskV6 {
class TestFindByIpMaskV6ShortRangeMin extends TestFindByIpMaskV6 {
protected override baseIpAsString = "::2";
protected override rangeMinIpAsString = "::2";
protected override rangeMaxIpAsString = "::4";
Expand All @@ -22,4 +22,4 @@ class TestFindRuleByIpMaskV6ShortRangeMin extends TestFindByIpMaskV6 {
protected override readonly anotherUserIp: string = "::1";
}

export default TestFindRuleByIpMaskV6ShortRangeMin;
export default TestFindByIpMaskV6ShortRangeMin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../../../../mongooseRulesStorage.js";
import TestFindByIpMaskV6ShortRangeMin from "./testFindByIpMaskV6ShortRangeMin.js";

describe("MongooseFindByIpMaskV6ShortRangeMin", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByIpMaskV6ShortRangeMin(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import TestFindByIpMaskV6 from "../v6/testFindByIpMaskV6.js";
import TestFindByIpMaskV6 from "../../mask/testFindByIpMaskV6.js";

class TestFindByIpMaskV6Short extends TestFindByIpMaskV6 {
protected override baseIpAsString = "::1";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../../mongooseRulesStorage.js";
import TestFindByIpMaskV6Short from "./testFindByIpMaskV6Short.js";

describe("MongooseFindByIpMaskV6Short", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByIpMaskV6Short(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import TestFindByIpV6 from "../testFindByIpV6.js";

class TestFindByIpV6Short extends TestFindByIpV6 {
protected override readonly userIp: string = "::1";
protected override readonly anotherUserIp: string = "::2";
}

export default TestFindByIpV6Short;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import TestFindByIpV6Short from "./testFindByIpV6Short.js";
import testMongooseRuleModel from "../../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../../mongooseRulesStorage.js";

describe("MongooseFindByIpV6Short", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByIpV6Short(storage);

tests.runAll();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe} from "vitest";
import testMongooseRuleModel from "../../../../test/testMongooseRuleModel.js";
import MongooseRulesStorage from "../../../../mongooseRulesStorage.js";
import TestFindByIpV6 from "./testFindByIpV6.js";

describe("MongooseFindByIpV6", async () => {
const testModel = await testMongooseRuleModel();
const storage = new MongooseRulesStorage(testModel);
const tests = new TestFindByIpV6(storage);

tests.runAll();
});

0 comments on commit 830ebcb

Please sign in to comment.