insite-users-server 1.0.334
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 +21 -0
- package/README.md +13 -0
- package/lib/abilities/Abilities.js +145 -0
- package/lib/abilities/index.js +1 -0
- package/lib/abilities/tree.js +26 -0
- package/lib/index.js +4 -0
- package/lib/lib/index.js +1 -0
- package/lib/lib/regexps.js +11 -0
- package/lib/orgs/Org.js +70 -0
- package/lib/orgs/Orgs.js +232 -0
- package/lib/orgs/index.js +1 -0
- package/lib/orgs/schema.js +39 -0
- package/lib/roles/Role.js +70 -0
- package/lib/roles/Roles.js +224 -0
- package/lib/roles/index.js +1 -0
- package/lib/roles/schema.js +45 -0
- package/lib/sessions/Session.js +72 -0
- package/lib/sessions/Sessions.js +118 -0
- package/lib/sessions/index.js +1 -0
- package/lib/sessions/schema.js +45 -0
- package/lib/users/User.js +251 -0
- package/lib/users/Users.js +265 -0
- package/lib/users/avatars/Avatars.js +16 -0
- package/lib/users/avatars/index.js +1 -0
- package/lib/users/avatars/schema.js +33 -0
- package/lib/users/index.js +1 -0
- package/lib/users/schema.js +91 -0
- package/lib/ws/UsersServer.js +212 -0
- package/lib/ws/index.js +1 -0
- package/lib/ws/publications.js +258 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Eugene Nesvetaev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { basisTree } from "./tree";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function mergeTrees(target, source) {
|
|
5
|
+
for (const sourceItem of source) {
|
|
6
|
+
const targetItem = target.find(({ _id }) => _id === sourceItem._id);
|
|
7
|
+
if (targetItem) {
|
|
8
|
+
for (const key in sourceItem)
|
|
9
|
+
if (key !== "_id") {
|
|
10
|
+
const sourceItemField = sourceItem[key];
|
|
11
|
+
if (Array.isArray(sourceItemField))
|
|
12
|
+
mergeTrees(targetItem[key] ??= [], sourceItemField);
|
|
13
|
+
else
|
|
14
|
+
targetItem[key] = sourceItemField;
|
|
15
|
+
}
|
|
16
|
+
} else
|
|
17
|
+
target.push(sourceItem);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return target;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export class Abilities extends Map {
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
super();
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
tree: customTree,
|
|
30
|
+
basis = true
|
|
31
|
+
} = options;
|
|
32
|
+
|
|
33
|
+
this.tree =
|
|
34
|
+
customTree ?
|
|
35
|
+
basis ?
|
|
36
|
+
mergeTrees(basisTree, customTree) :
|
|
37
|
+
customTree :
|
|
38
|
+
basisTree;
|
|
39
|
+
|
|
40
|
+
this.#parse(this.tree);
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#parse(list) {
|
|
45
|
+
for (const ability of list) {
|
|
46
|
+
this.set(ability._id, ability);
|
|
47
|
+
if (ability.subAbilities)
|
|
48
|
+
this.#parse(ability.subAbilities);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
merge(target, source) {
|
|
54
|
+
for (const [ abilityId, ability ] of Object.entries(source)) {
|
|
55
|
+
const schema = this.get(abilityId);
|
|
56
|
+
if (schema) {
|
|
57
|
+
if (schema.isInheritable !== false && ability)
|
|
58
|
+
if (!target[abilityId])
|
|
59
|
+
target[abilityId] = Object.deepClone(ability);
|
|
60
|
+
else if (schema.params)
|
|
61
|
+
for (const param of schema.params)
|
|
62
|
+
if (param.isInheritable !== false && ability[param._id] !== undefined)
|
|
63
|
+
if (!target[abilityId][param._id])
|
|
64
|
+
target[abilityId][param._id] = Object.deepClone(ability[param._id]);
|
|
65
|
+
else
|
|
66
|
+
if (param.type === "number") {
|
|
67
|
+
if (target[abilityId][param._id] < ability[param._id])
|
|
68
|
+
target[abilityId][param._id] = ability[param._id];
|
|
69
|
+
} else if (param.type === "items") {
|
|
70
|
+
const targetItems = target[abilityId][param._id];
|
|
71
|
+
for (const itemId of ability[param._id])
|
|
72
|
+
if (!targetItems.includes(itemId))
|
|
73
|
+
targetItems.push(itemId);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
} else
|
|
78
|
+
delete source[abilityId];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return target;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
getMinimumOf(_id) {
|
|
85
|
+
const schema = this.get(_id);
|
|
86
|
+
if (schema.params) {
|
|
87
|
+
const ability = {};
|
|
88
|
+
for (const param of schema.params)
|
|
89
|
+
if (param.type === "number")
|
|
90
|
+
ability[param._id] = param.min ?? 0;
|
|
91
|
+
else if (param.type === "items")
|
|
92
|
+
ability[param._id] = [];
|
|
93
|
+
|
|
94
|
+
return ability;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
getMaximum() {
|
|
101
|
+
return [ ...this.values() ].reduce((abilities, schema) => {
|
|
102
|
+
if (schema.params) {
|
|
103
|
+
const ability = {};
|
|
104
|
+
for (const param of schema.params)
|
|
105
|
+
if (param.type === "number")
|
|
106
|
+
ability[param._id] = param.max ?? 0;
|
|
107
|
+
else if (param.type === "items")
|
|
108
|
+
ability[param._id] = param.items.ids();
|
|
109
|
+
|
|
110
|
+
abilities[schema._id] = ability;
|
|
111
|
+
} else
|
|
112
|
+
abilities[schema._id] = true;
|
|
113
|
+
|
|
114
|
+
return abilities;
|
|
115
|
+
}, {});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getSchemeFor(user) {
|
|
119
|
+
return (function resolve(abilities) {
|
|
120
|
+
for (let i = 0; i < abilities.length; i++) {
|
|
121
|
+
const schema = abilities[i];
|
|
122
|
+
const userAbility = user.abilities[schema._id];
|
|
123
|
+
if (userAbility) {
|
|
124
|
+
if (schema.params)
|
|
125
|
+
for (const param of schema.params) {
|
|
126
|
+
const userParam = userAbility[param._id];
|
|
127
|
+
if (param.type === "number") {
|
|
128
|
+
if (param.max !== undefined)
|
|
129
|
+
param.max = userParam;
|
|
130
|
+
} else if (param.type === "items")
|
|
131
|
+
for (let j = 0; j < param.items.length; j++)
|
|
132
|
+
if (!userParam.includes(param.items[j]._id))
|
|
133
|
+
param.items.splice(j--, 1);
|
|
134
|
+
}
|
|
135
|
+
if (schema.subAbilities)
|
|
136
|
+
resolve(schema.subAbilities);
|
|
137
|
+
} else
|
|
138
|
+
abilities.splice(i--, 1);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return abilities;
|
|
142
|
+
})(Object.deepClone(this.tree));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Abilities";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const basisTree = [
|
|
2
|
+
{
|
|
3
|
+
_id: "login",
|
|
4
|
+
title: "Вход в систему",
|
|
5
|
+
params: [
|
|
6
|
+
{
|
|
7
|
+
_id: "sessionsLimit",
|
|
8
|
+
title: "Лимит активных сессий",
|
|
9
|
+
type: "number",
|
|
10
|
+
min: 1,
|
|
11
|
+
max: 128
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
_id: "sections",
|
|
15
|
+
title: "Доступные разделы",
|
|
16
|
+
type: "items",
|
|
17
|
+
items: [
|
|
18
|
+
{
|
|
19
|
+
_id: "users",
|
|
20
|
+
title: "Пользователи"
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
];
|
package/lib/index.js
ADDED
package/lib/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./regexps";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const regexps = {
|
|
2
|
+
_id: /^[a-fA-F\d]{24}$/,
|
|
3
|
+
email: /^[^\s@]{1,63}@[^\s@]{1,63}\.[^\s@]{1,63}$/,
|
|
4
|
+
argon2: /^\$argon2(?:i|d|id)\$v=\d+\$m=\d+,t=\d+,p=\d+\$[a-zA-Z\d+/]+\$[a-zA-Z\d+/]+$/,
|
|
5
|
+
sessionId: /^[a-fA-F\d]{16,32}\$[a-fA-F\d]{16,32}$/,
|
|
6
|
+
role: /^[a-z\d\-_]{1,64}$/,
|
|
7
|
+
ip: /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
for (const key in regexps)
|
|
11
|
+
regexps[key].pattern = regexps[key].toString().slice(1, -1);
|
package/lib/orgs/Org.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const snapshots = new Map();
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class Org {
|
|
5
|
+
constructor(orgs, orgDoc) {
|
|
6
|
+
this.#orgs = orgs;
|
|
7
|
+
|
|
8
|
+
this._id = orgDoc._id;
|
|
9
|
+
orgs.set(this._id, this);
|
|
10
|
+
|
|
11
|
+
this.update(orgDoc);
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#orgs;
|
|
16
|
+
|
|
17
|
+
isOrg = true;
|
|
18
|
+
|
|
19
|
+
ownerOrgs = new Set();
|
|
20
|
+
ownerUsers = new Set();
|
|
21
|
+
|
|
22
|
+
users = new Set();
|
|
23
|
+
|
|
24
|
+
slaveOrgs = new Set();
|
|
25
|
+
|
|
26
|
+
update({ _id, title, owners: ownerIds, ...restProps }, next) {
|
|
27
|
+
|
|
28
|
+
const orgs = this.#orgs;
|
|
29
|
+
|
|
30
|
+
Object.assign(this, restProps);
|
|
31
|
+
|
|
32
|
+
if (title !== undefined) {
|
|
33
|
+
this.title = title;
|
|
34
|
+
this.initials = title?.[0]?.toUpperCase() ?? "";
|
|
35
|
+
this.displayLabel = title;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (ownerIds) {
|
|
39
|
+
this.ownerIds = ownerIds;
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
const snapshot = this.ownerIds.join(",");
|
|
43
|
+
|
|
44
|
+
if (snapshot !== snapshots.get(this._id)) {
|
|
45
|
+
snapshots.set(this._id, snapshot);
|
|
46
|
+
|
|
47
|
+
if (orgs.users.isInited)
|
|
48
|
+
orgs.updateDebounced();
|
|
49
|
+
|
|
50
|
+
orgs.users.emit("orgs-org-update", this, next);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
delete() {
|
|
57
|
+
|
|
58
|
+
const orgs = this.#orgs;
|
|
59
|
+
|
|
60
|
+
orgs.delete(this._id);
|
|
61
|
+
|
|
62
|
+
snapshots.delete(this._id);
|
|
63
|
+
|
|
64
|
+
orgs.replace(this._id);
|
|
65
|
+
|
|
66
|
+
orgs.updateDebounced();
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
}
|
package/lib/orgs/Orgs.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { debounce } from "@nesvet/n";
|
|
2
|
+
import { ObjectId } from "insite-db";
|
|
3
|
+
import { Org } from "./Org";
|
|
4
|
+
import { basisSchema } from "./schema";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const indexes = [
|
|
8
|
+
[ { owners: 1 } ]
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
function sortOrgs(a, b) {
|
|
12
|
+
return (
|
|
13
|
+
(b.slaveOrgs.size - a.slaveOrgs.size) || (
|
|
14
|
+
a.title > b.title ?
|
|
15
|
+
1 :
|
|
16
|
+
a.title < b.title ?
|
|
17
|
+
-1 :
|
|
18
|
+
0
|
|
19
|
+
)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function preventDirectDelete() {
|
|
24
|
+
throw new Error("Direct usage of orgs collection.deleteOne or .deleteMany is forbidden. Use orgs.collectionDelete instead.");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
export class Orgs extends Map {
|
|
29
|
+
constructor(users, options = {}) {
|
|
30
|
+
super();
|
|
31
|
+
|
|
32
|
+
const {
|
|
33
|
+
schema: customSchema,
|
|
34
|
+
indexes: customIndexes,
|
|
35
|
+
null: nullProps
|
|
36
|
+
} = options;
|
|
37
|
+
|
|
38
|
+
this.users = users;
|
|
39
|
+
this.collections = users.collections;
|
|
40
|
+
|
|
41
|
+
if (customSchema) {
|
|
42
|
+
customSchema.required?.removeAll(basisSchema.required);
|
|
43
|
+
if (customSchema.properties)
|
|
44
|
+
Object.delete(customSchema, ...Object.keys(basisSchema.properties));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const jsonSchema = {
|
|
48
|
+
...basisSchema,
|
|
49
|
+
...customSchema,
|
|
50
|
+
required: [ ...basisSchema.required, ...customSchema?.required ?? [] ],
|
|
51
|
+
properties: { ...basisSchema.properties, ...customSchema?.properties }
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return (async () => {
|
|
55
|
+
|
|
56
|
+
this.collection = await this.collections.ensure("orgs", { jsonSchema });
|
|
57
|
+
|
|
58
|
+
this.collection.ensureIndexes([ ...indexes, ...customIndexes ?? [] ]);
|
|
59
|
+
|
|
60
|
+
this.collection.deleteOne =
|
|
61
|
+
this.collection.deleteMany =
|
|
62
|
+
preventDirectDelete;
|
|
63
|
+
|
|
64
|
+
this.null = new Org(this, {
|
|
65
|
+
...nullProps,
|
|
66
|
+
_id: null,
|
|
67
|
+
title: "",
|
|
68
|
+
displayLabel: "",
|
|
69
|
+
note: "",
|
|
70
|
+
owners: [],
|
|
71
|
+
createdAt: Date.now()
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
this.null.ownerIds = new Array.Empty();
|
|
75
|
+
this.null.ownerOrgs = new Set.Empty();
|
|
76
|
+
this.null.ownerUsers = new Set.Empty();
|
|
77
|
+
this.null.slaveOrgs = new Set.Empty();
|
|
78
|
+
|
|
79
|
+
return this;
|
|
80
|
+
})();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
load(orgDoc) {
|
|
84
|
+
new Org(this, orgDoc);
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static resolve(org, slaveOrgs) {
|
|
89
|
+
slaveOrgs = [ org, ...slaveOrgs ];
|
|
90
|
+
|
|
91
|
+
for (const ownerOrg of org.ownerOrgs) {
|
|
92
|
+
Orgs.resolve(ownerOrg, slaveOrgs);
|
|
93
|
+
ownerOrg.slaveOrgs.addAll(slaveOrgs);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
sorted = [];
|
|
99
|
+
|
|
100
|
+
async update() {
|
|
101
|
+
|
|
102
|
+
const array = [ ...this.values() ];
|
|
103
|
+
const sorted = [];
|
|
104
|
+
|
|
105
|
+
for (const org of array) {
|
|
106
|
+
org.ownerOrgs.clear();
|
|
107
|
+
org.ownerUsers.clear();
|
|
108
|
+
org.slaveOrgs.clear();
|
|
109
|
+
|
|
110
|
+
for (const ownerId of org.ownerIds) {
|
|
111
|
+
const ownerOrg = this.get(ownerId);
|
|
112
|
+
if (ownerOrg)
|
|
113
|
+
org.ownerOrgs.add(ownerOrg);
|
|
114
|
+
else {
|
|
115
|
+
const ownerUser = this.users.get(ownerId);
|
|
116
|
+
if (ownerUser)
|
|
117
|
+
org.ownerUsers.add(ownerUser);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!org.ownerOrgs.size && org._id)
|
|
122
|
+
sorted.push(org);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (const org of array)
|
|
126
|
+
if (!array.some(anotherOrg => anotherOrg.ownerOrgs.has(org)))
|
|
127
|
+
Orgs.resolve(org, []);
|
|
128
|
+
|
|
129
|
+
sorted.sort(sortOrgs);
|
|
130
|
+
for (const org of array)
|
|
131
|
+
org.slaveOrgs.sort(sortOrgs);
|
|
132
|
+
|
|
133
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
134
|
+
const org = sorted[i];
|
|
135
|
+
for (const slaveOrg of [ ...org.slaveOrgs ].reverse())
|
|
136
|
+
if (slaveOrg.ownerOrgs.has(org)) {
|
|
137
|
+
const slaveOrgIndex = sorted.indexOf(slaveOrg);
|
|
138
|
+
if (~slaveOrgIndex) {
|
|
139
|
+
sorted.splice(slaveOrgIndex, 1);
|
|
140
|
+
i--;
|
|
141
|
+
}
|
|
142
|
+
sorted.splice(i + 1, 0, slaveOrg);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
for (let o = 0, { length } = sorted; o < length;)
|
|
146
|
+
sorted[o]._o = o++;
|
|
147
|
+
|
|
148
|
+
this.sorted = sorted;
|
|
149
|
+
|
|
150
|
+
this.users.emit("orgs-update");
|
|
151
|
+
|
|
152
|
+
if (this.users.isInited) {
|
|
153
|
+
this.users.updateDebounced.clear();
|
|
154
|
+
this.users.update();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
updateDebounced = debounce(this.update, 250);
|
|
160
|
+
|
|
161
|
+
initialLoad = async () => {
|
|
162
|
+
|
|
163
|
+
for (const orgDoc of await this.collection.find().toArray())
|
|
164
|
+
this.load(orgDoc);
|
|
165
|
+
|
|
166
|
+
delete this.initialLoad;
|
|
167
|
+
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
init = async () => {
|
|
171
|
+
|
|
172
|
+
await this.update();
|
|
173
|
+
|
|
174
|
+
this.collection.changeListeners.add(next => {
|
|
175
|
+
switch (next.operationType) {
|
|
176
|
+
case "insert": {
|
|
177
|
+
const org = new Org(this, next.fullDocument);
|
|
178
|
+
this.users.emit("orgs-org-update", org, next);
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
case "update":
|
|
183
|
+
case "replace":
|
|
184
|
+
this.get(next.documentKey._id).update(next.updateDescription?.updatedFields || next.fullDocument, next);
|
|
185
|
+
break;
|
|
186
|
+
|
|
187
|
+
case "delete":
|
|
188
|
+
this.get(next.documentKey._id).delete();
|
|
189
|
+
this.users.emit("orgs-org-update", null, next);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
delete this.init;
|
|
195
|
+
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
new({ title, note, ...restProps }, ownerId) {
|
|
199
|
+
return this.collection.insertOne({
|
|
200
|
+
_id: (new ObjectId()).toString(),
|
|
201
|
+
title,
|
|
202
|
+
note: note || "",
|
|
203
|
+
owners: ownerId ? [ ownerId ] : [],
|
|
204
|
+
...Object.delete(restProps, "_id", "owners"),
|
|
205
|
+
createdAt: Date.now()
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
collectionDelete(org) {
|
|
210
|
+
const _id = typeof org == "string" ? org : org._id;
|
|
211
|
+
|
|
212
|
+
return this.collection.bulkWrite([
|
|
213
|
+
{ updateMany: { filter: { owners: _id }, update: { $pullAll: { owners: [ _id ] } } } },
|
|
214
|
+
{ deleteOne: { filter: { _id } } }
|
|
215
|
+
]);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
replaceMap = new Map();
|
|
219
|
+
|
|
220
|
+
replaceHandlers = new Set();
|
|
221
|
+
|
|
222
|
+
async replace(fromId) {
|
|
223
|
+
const toId = this.replaceMap.get(fromId) ?? null;
|
|
224
|
+
if (toId)
|
|
225
|
+
this.replaceMap.delete(fromId);
|
|
226
|
+
|
|
227
|
+
for (const handler of this.replaceHandlers)
|
|
228
|
+
await handler(fromId, toId);
|
|
229
|
+
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Orgs";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { regexps } from "../lib";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export const basisSchema = {
|
|
5
|
+
required: [
|
|
6
|
+
"_id",
|
|
7
|
+
"title",
|
|
8
|
+
"note",
|
|
9
|
+
"owners",
|
|
10
|
+
"createdAt"
|
|
11
|
+
],
|
|
12
|
+
properties: {
|
|
13
|
+
_id: {
|
|
14
|
+
bsonType: "string",
|
|
15
|
+
pattern: regexps._id.pattern
|
|
16
|
+
},
|
|
17
|
+
title: {
|
|
18
|
+
bsonType: "string",
|
|
19
|
+
maxLength: 512
|
|
20
|
+
},
|
|
21
|
+
note: {
|
|
22
|
+
bsonType: "string",
|
|
23
|
+
maxLength: 131072
|
|
24
|
+
},
|
|
25
|
+
owners: {
|
|
26
|
+
bsonType: "array",
|
|
27
|
+
uniqueItems: true,
|
|
28
|
+
items: {
|
|
29
|
+
bsonType: "string",
|
|
30
|
+
pattern: regexps._id.pattern
|
|
31
|
+
},
|
|
32
|
+
additionalItems: false
|
|
33
|
+
},
|
|
34
|
+
createdAt: {
|
|
35
|
+
bsonType: "number"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
additionalProperties: false
|
|
39
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const snapshots = new Map();
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class Role {
|
|
5
|
+
constructor(roles, roleDoc) {
|
|
6
|
+
this.#roles = roles;
|
|
7
|
+
|
|
8
|
+
this._id = roleDoc._id;
|
|
9
|
+
roles.set(this._id, this);
|
|
10
|
+
|
|
11
|
+
this.update(roleDoc);
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#roles;
|
|
16
|
+
|
|
17
|
+
ownInvolves = [];
|
|
18
|
+
involves = new Set();
|
|
19
|
+
abilities = {};
|
|
20
|
+
inheritedAbilities = {};
|
|
21
|
+
|
|
22
|
+
update({ _id, involves: ownInvolveIds, abilities: ownAbilities, title, ...restProps }, next) {
|
|
23
|
+
|
|
24
|
+
const roles = this.#roles;
|
|
25
|
+
|
|
26
|
+
Object.assign(this, restProps);
|
|
27
|
+
|
|
28
|
+
if (ownInvolveIds)
|
|
29
|
+
this.ownInvolveIds = ownInvolveIds;
|
|
30
|
+
|
|
31
|
+
if (ownAbilities)
|
|
32
|
+
this.ownAbilities = ownAbilities;
|
|
33
|
+
|
|
34
|
+
if (title !== undefined) {
|
|
35
|
+
this.title = title;
|
|
36
|
+
this.displayTitle = title || this._id;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
if (ownInvolveIds || ownAbilities) {
|
|
41
|
+
const snapshot = [
|
|
42
|
+
this.ownInvolveIds.join(","),
|
|
43
|
+
JSON.stringify(this.ownAbilities)
|
|
44
|
+
].join("\n");
|
|
45
|
+
|
|
46
|
+
if (snapshot != snapshots.get(this._id)) {
|
|
47
|
+
snapshots.set(this._id, snapshot);
|
|
48
|
+
|
|
49
|
+
if (roles.users.isInited)
|
|
50
|
+
roles.updateDebounced();
|
|
51
|
+
|
|
52
|
+
roles.users.emit("roles-role-update", next);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
delete() {
|
|
59
|
+
|
|
60
|
+
const roles = this.#roles;
|
|
61
|
+
|
|
62
|
+
roles.delete(this._id);
|
|
63
|
+
|
|
64
|
+
snapshots.delete(this._id);
|
|
65
|
+
|
|
66
|
+
roles.updateDebounced();
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
}
|