@yongdall/organization-rbac 0.4.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/hooks.yongdall.mjs +30 -0
- package/hooks.yongdall.mjs.map +1 -0
- package/index.d.mts +87 -0
- package/index.mjs +3 -0
- package/organization-rbac-BmLEDpJH.mjs +214 -0
- package/organization-rbac-BmLEDpJH.mjs.map +1 -0
- package/package.json +23 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { a as OrganizationMember, n as useOrganizationPermissions, s as OrganizationAppointment, t as useOrganizationAllPermissions } from "./organization-rbac-BmLEDpJH.mjs";
|
|
2
|
+
import { Role } from "@yongdall/role";
|
|
3
|
+
|
|
4
|
+
//#region plugins/organization-rbac/hooks.yongdall.mjs
|
|
5
|
+
/** @import { Hooks, UserManager } from '@yongdall/core' */
|
|
6
|
+
/** @type {Hooks.Define['models']} */
|
|
7
|
+
const models = {
|
|
8
|
+
organizationAppointment: OrganizationAppointment,
|
|
9
|
+
organizationMember: OrganizationMember,
|
|
10
|
+
role: Role
|
|
11
|
+
};
|
|
12
|
+
/** @type {Hooks.Define['migrationModels']} */
|
|
13
|
+
const migrationModels = [
|
|
14
|
+
OrganizationAppointment,
|
|
15
|
+
OrganizationMember,
|
|
16
|
+
Role
|
|
17
|
+
];
|
|
18
|
+
/** @type {Partial<UserManager>} */
|
|
19
|
+
const userManager = {
|
|
20
|
+
getOrganizationAllPermissions() {
|
|
21
|
+
return useOrganizationAllPermissions();
|
|
22
|
+
},
|
|
23
|
+
getOrganizationPermissions() {
|
|
24
|
+
return useOrganizationPermissions();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
export { migrationModels, models, userManager };
|
|
30
|
+
//# sourceMappingURL=hooks.yongdall.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.yongdall.mjs","names":[],"sources":["../../plugins/organization-rbac/hooks.yongdall.mjs"],"sourcesContent":["/** @import { Hooks, UserManager } from '@yongdall/core' */\nimport { Role } from '@yongdall/role';\nimport { OrganizationAppointment, OrganizationMember, useOrganizationAllPermissions, useOrganizationPermissions } from './index.mjs';\n\n/** @type {Hooks.Define['models']} */\nexport const models = {\n\torganizationAppointment: OrganizationAppointment,\n\torganizationMember: OrganizationMember,\n\trole: Role,\n};\n\n/** @type {Hooks.Define['migrationModels']} */\nexport const migrationModels = [\n\tOrganizationAppointment,\n\tOrganizationMember,\n\tRole,\n];\n\n\n/** @type {Partial<UserManager>} */\nexport const userManager = {\n\tgetOrganizationAllPermissions() {\n\t\treturn useOrganizationAllPermissions()\n\t},\n\tgetOrganizationPermissions() {\n\t\treturn useOrganizationPermissions()\n\t},\n}\n"],"mappings":";;;;;;AAKA,MAAa,SAAS;CACrB,yBAAyB;CACzB,oBAAoB;CACpB,MAAM;CACN;;AAGD,MAAa,kBAAkB;CAC9B;CACA;CACA;CACA;;AAID,MAAa,cAAc;CAC1B,gCAAgC;AAC/B,SAAO,+BAA+B;;CAEvC,6BAA6B;AAC5B,SAAO,4BAA4B;;CAEpC"}
|
package/index.d.mts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import * as imodel15 from "imodel";
|
|
2
|
+
import * as _yongdall_model2 from "@yongdall/model";
|
|
3
|
+
|
|
4
|
+
//#region plugins/organization-rbac/models/OrganizationAppointment.d.mts
|
|
5
|
+
declare const OrganizationAppointment: _yongdall_model2.ModelTable<{
|
|
6
|
+
id: imodel15.FieldDefine<"uuid", false, false>;
|
|
7
|
+
label: imodel15.FieldDefine<"string", false, false>;
|
|
8
|
+
description: imodel15.FieldDefine<"string", false, true>;
|
|
9
|
+
createdAt: imodel15.FieldDefine<"date", false, false>;
|
|
10
|
+
updatedAt: imodel15.FieldDefine<"date", false, false>;
|
|
11
|
+
}, "organizationAppointment">;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region plugins/organization-rbac/models/OrganizationMember.d.mts
|
|
14
|
+
/**
|
|
15
|
+
* 组织用户角色模型
|
|
16
|
+
*/
|
|
17
|
+
type OrganizationUserRole = {
|
|
18
|
+
/**
|
|
19
|
+
* - 用户ID,主键第一部分
|
|
20
|
+
*/
|
|
21
|
+
userId: string;
|
|
22
|
+
/**
|
|
23
|
+
* - 组织ID,主键第二部分
|
|
24
|
+
*/
|
|
25
|
+
organizationId: string;
|
|
26
|
+
/**
|
|
27
|
+
* - 角色ID,主键第三部分
|
|
28
|
+
*/
|
|
29
|
+
roleId: string;
|
|
30
|
+
/**
|
|
31
|
+
* - 编号
|
|
32
|
+
*/
|
|
33
|
+
no: number;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* 组织用户角色模型
|
|
37
|
+
* @typedef {Object} OrganizationUserRole
|
|
38
|
+
* @property {string} userId - 用户ID,主键第一部分
|
|
39
|
+
* @property {string} organizationId - 组织ID,主键第二部分
|
|
40
|
+
* @property {string} roleId - 角色ID,主键第三部分
|
|
41
|
+
* @property {number} no - 编号
|
|
42
|
+
*/
|
|
43
|
+
declare const OrganizationUserRole: _yongdall_model2.ModelTable<{
|
|
44
|
+
userId: imodel15.FieldDefine<"uuid", false, false>;
|
|
45
|
+
organizationId: imodel15.FieldDefine<"uuid", false, false>;
|
|
46
|
+
roleId: imodel15.FieldDefine<"uuid", false, false>;
|
|
47
|
+
no: imodel15.FieldDefine<"i32", false, false>;
|
|
48
|
+
}, "organizationUserRole">;
|
|
49
|
+
declare const OrganizationMember: _yongdall_model2.ModelTable<{
|
|
50
|
+
id: imodel15.FieldDefine<"uuid", false, false>;
|
|
51
|
+
userId: imodel15.FieldDefine<"uuid", false, false>;
|
|
52
|
+
organizationId: imodel15.FieldDefine<"uuid", false, false>;
|
|
53
|
+
appointmentId: imodel15.FieldDefine<"uuid", false, true>;
|
|
54
|
+
default: imodel15.FieldDefine<"bool", false, false>;
|
|
55
|
+
createdAt: imodel15.FieldDefine<"date", false, false>;
|
|
56
|
+
updatedAt: imodel15.FieldDefine<"date", false, false>;
|
|
57
|
+
roles: imodel15.FieldDefine<_yongdall_model2.ModelTable<{
|
|
58
|
+
userId: imodel15.FieldDefine<"uuid", false, false>;
|
|
59
|
+
organizationId: imodel15.FieldDefine<"uuid", false, false>;
|
|
60
|
+
roleId: imodel15.FieldDefine<"uuid", false, false>;
|
|
61
|
+
no: imodel15.FieldDefine<"i32", false, false>;
|
|
62
|
+
}, "organizationUserRole">, true, false>;
|
|
63
|
+
}, "organizationMember">;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region plugins/organization-rbac/index.d.mts
|
|
66
|
+
/**
|
|
67
|
+
* 所在组织
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
declare const useOrganizations: () => Promise<Set<string>>;
|
|
71
|
+
/**
|
|
72
|
+
* 角色
|
|
73
|
+
* @returns {Promise<Record<string, Set<string>>>}
|
|
74
|
+
*/
|
|
75
|
+
declare const useOrganizationRoleIds: () => Promise<{
|
|
76
|
+
[k: string]: Set<string>;
|
|
77
|
+
}>;
|
|
78
|
+
/**
|
|
79
|
+
* @returns {Promise<Record<string, Set<string>>>}
|
|
80
|
+
*/
|
|
81
|
+
declare const useOrganizationPermissions: () => Promise<any>;
|
|
82
|
+
/**
|
|
83
|
+
* @returns {Promise<Set<string>>}
|
|
84
|
+
*/
|
|
85
|
+
declare const useOrganizationAllPermissions: () => Promise<Set<any>>;
|
|
86
|
+
//#endregion
|
|
87
|
+
export { OrganizationAppointment, OrganizationMember, OrganizationUserRole, useOrganizationAllPermissions, useOrganizationPermissions, useOrganizationRoleIds, useOrganizations };
|
package/index.mjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as OrganizationMember, i as useOrganizations, n as useOrganizationPermissions, o as OrganizationUserRole, r as useOrganizationRoleIds, s as OrganizationAppointment, t as useOrganizationAllPermissions } from "./organization-rbac-BmLEDpJH.mjs";
|
|
2
|
+
|
|
3
|
+
export { OrganizationAppointment, OrganizationMember, OrganizationUserRole, useOrganizationAllPermissions, useOrganizationPermissions, useOrganizationRoleIds, useOrganizations };
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { RolePermission } from "@yongdall/role";
|
|
2
|
+
import { createComputed } from "@yongdall/context";
|
|
3
|
+
import { Query, Where, createField, createModel, now, uuid } from "@yongdall/model";
|
|
4
|
+
import { getUser } from "@yongdall/core";
|
|
5
|
+
import { connect } from "@yongdall/connection";
|
|
6
|
+
|
|
7
|
+
//#region plugins/organization-rbac/models/OrganizationAppointment.mjs
|
|
8
|
+
const OrganizationAppointment = createModel("organizationAppointment", {
|
|
9
|
+
id: createField("uuid", {
|
|
10
|
+
nullable: false,
|
|
11
|
+
primary: 1,
|
|
12
|
+
default: uuid,
|
|
13
|
+
uncreatable: true
|
|
14
|
+
}),
|
|
15
|
+
label: createField("string", {
|
|
16
|
+
nullable: false,
|
|
17
|
+
label: "职务名称"
|
|
18
|
+
}),
|
|
19
|
+
description: createField("string", {
|
|
20
|
+
nullable: true,
|
|
21
|
+
label: "描述"
|
|
22
|
+
}),
|
|
23
|
+
createdAt: createField("date", {
|
|
24
|
+
nullable: false,
|
|
25
|
+
default: now,
|
|
26
|
+
creating: now,
|
|
27
|
+
label: "创建日期"
|
|
28
|
+
}),
|
|
29
|
+
updatedAt: createField("date", {
|
|
30
|
+
nullable: false,
|
|
31
|
+
default: now,
|
|
32
|
+
updating: now,
|
|
33
|
+
label: "最后更新日期"
|
|
34
|
+
})
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region plugins/organization-rbac/models/OrganizationMember.mjs
|
|
39
|
+
/**
|
|
40
|
+
* 组织用户角色模型
|
|
41
|
+
* @typedef {Object} OrganizationUserRole
|
|
42
|
+
* @property {string} userId - 用户ID,主键第一部分
|
|
43
|
+
* @property {string} organizationId - 组织ID,主键第二部分
|
|
44
|
+
* @property {string} roleId - 角色ID,主键第三部分
|
|
45
|
+
* @property {number} no - 编号
|
|
46
|
+
*/
|
|
47
|
+
const OrganizationUserRole = createModel("organizationUserRole", {
|
|
48
|
+
userId: createField("uuid", {
|
|
49
|
+
nullable: false,
|
|
50
|
+
index: true,
|
|
51
|
+
label: "用户",
|
|
52
|
+
model: "user",
|
|
53
|
+
renderer: "document",
|
|
54
|
+
immutable: true,
|
|
55
|
+
primary: 1
|
|
56
|
+
}),
|
|
57
|
+
organizationId: createField("uuid", {
|
|
58
|
+
nullable: false,
|
|
59
|
+
index: true,
|
|
60
|
+
label: "组织",
|
|
61
|
+
model: "organization",
|
|
62
|
+
renderer: "document",
|
|
63
|
+
immutable: true,
|
|
64
|
+
primary: 2
|
|
65
|
+
}),
|
|
66
|
+
roleId: createField("uuid", {
|
|
67
|
+
nullable: false,
|
|
68
|
+
index: true,
|
|
69
|
+
label: "角色",
|
|
70
|
+
model: "role",
|
|
71
|
+
renderer: "document",
|
|
72
|
+
immutable: true,
|
|
73
|
+
primary: 3
|
|
74
|
+
}),
|
|
75
|
+
no: createField("i32", {
|
|
76
|
+
nullable: false,
|
|
77
|
+
default: 0
|
|
78
|
+
})
|
|
79
|
+
});
|
|
80
|
+
const OrganizationMember = createModel("organizationMember", {
|
|
81
|
+
id: createField("uuid", {
|
|
82
|
+
nullable: false,
|
|
83
|
+
index: true,
|
|
84
|
+
primary: 1,
|
|
85
|
+
default: uuid
|
|
86
|
+
}),
|
|
87
|
+
userId: createField("uuid", {
|
|
88
|
+
nullable: false,
|
|
89
|
+
index: true,
|
|
90
|
+
label: "用户",
|
|
91
|
+
model: "user",
|
|
92
|
+
renderer: "document",
|
|
93
|
+
immutable: true,
|
|
94
|
+
layout: { rowSpan: 6 }
|
|
95
|
+
}),
|
|
96
|
+
organizationId: createField("uuid", {
|
|
97
|
+
nullable: false,
|
|
98
|
+
index: true,
|
|
99
|
+
label: "组织",
|
|
100
|
+
model: "organization",
|
|
101
|
+
renderer: "document",
|
|
102
|
+
immutable: true,
|
|
103
|
+
layout: { rowSpan: 6 }
|
|
104
|
+
}),
|
|
105
|
+
appointmentId: createField("uuid", {
|
|
106
|
+
nullable: true,
|
|
107
|
+
index: true,
|
|
108
|
+
label: "职务",
|
|
109
|
+
model: "appointment",
|
|
110
|
+
renderer: "document",
|
|
111
|
+
layout: { rowSpan: 6 }
|
|
112
|
+
}),
|
|
113
|
+
default: createField("bool", {
|
|
114
|
+
nullable: false,
|
|
115
|
+
default: false,
|
|
116
|
+
label: "默认职务"
|
|
117
|
+
}),
|
|
118
|
+
createdAt: createField("date", {
|
|
119
|
+
nullable: false,
|
|
120
|
+
default: now,
|
|
121
|
+
creating: now,
|
|
122
|
+
label: "创建日期"
|
|
123
|
+
}),
|
|
124
|
+
updatedAt: createField("date", {
|
|
125
|
+
nullable: false,
|
|
126
|
+
default: now,
|
|
127
|
+
updating: now,
|
|
128
|
+
label: "最后更新日期"
|
|
129
|
+
}),
|
|
130
|
+
roles: createField(OrganizationUserRole, {
|
|
131
|
+
array: true,
|
|
132
|
+
constraints: {
|
|
133
|
+
organizationId: { field: "organizationId" },
|
|
134
|
+
userId: { field: "userId" },
|
|
135
|
+
no: { noField: true }
|
|
136
|
+
},
|
|
137
|
+
label: "角色列表"
|
|
138
|
+
})
|
|
139
|
+
}, {
|
|
140
|
+
indexes: [{
|
|
141
|
+
fields: [
|
|
142
|
+
"userId",
|
|
143
|
+
"organizationId",
|
|
144
|
+
"appointmentId"
|
|
145
|
+
],
|
|
146
|
+
unique: true
|
|
147
|
+
}],
|
|
148
|
+
hooks: {
|
|
149
|
+
async afterCreate(conn, Model, record) {
|
|
150
|
+
if (!record.default) return;
|
|
151
|
+
await conn.update(Model, { default: false }, Where.and("organizationId", "=", record.organizationId, true).and("userId", record.userId));
|
|
152
|
+
},
|
|
153
|
+
async afterUpdate(conn, Model, record, oldRecord) {
|
|
154
|
+
if (!record.default) return;
|
|
155
|
+
if (oldRecord.default) return;
|
|
156
|
+
await conn.update(Model, { default: false }, Where.and("organizationId", "=", record.organizationId, true).and("userId", record.userId));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region plugins/organization-rbac/index.mjs
|
|
163
|
+
/**
|
|
164
|
+
* 所在组织
|
|
165
|
+
* @returns
|
|
166
|
+
*/
|
|
167
|
+
const useOrganizations = createComputed(async () => {
|
|
168
|
+
const id = await getUser();
|
|
169
|
+
/** @type {{organizationId: string}[]} */
|
|
170
|
+
const relations = await connect("rdb").select(new Query(OrganizationMember).select(["organizationId"]).where("userId", id));
|
|
171
|
+
return new Set(relations.map((v) => v.organizationId));
|
|
172
|
+
});
|
|
173
|
+
/**
|
|
174
|
+
* 角色
|
|
175
|
+
* @returns {Promise<Record<string, Set<string>>>}
|
|
176
|
+
*/
|
|
177
|
+
const useOrganizationRoleIds = createComputed(async () => {
|
|
178
|
+
const organizations = await useOrganizations();
|
|
179
|
+
if (!organizations.size) return {};
|
|
180
|
+
const id = await getUser();
|
|
181
|
+
/** @type {{organizationId: number; roleId: string;}[]} */
|
|
182
|
+
const list = await connect("rdb").select(new Query(OrganizationUserRole).select(["organizationId", "roleId"]).where("organizationId", "in", [...organizations]).where("userId", id));
|
|
183
|
+
return Object.fromEntries(Object.entries(Object.groupBy(list, (v) => v.organizationId)).map(([organizationId, list]) => [organizationId, new Set(list?.map((v) => v.roleId))]));
|
|
184
|
+
});
|
|
185
|
+
/**
|
|
186
|
+
* @returns {Promise<Record<string, Set<string>>>}
|
|
187
|
+
*/
|
|
188
|
+
const useOrganizationPermissions = createComputed(async () => {
|
|
189
|
+
const roleIds = await useOrganizationRoleIds();
|
|
190
|
+
const allRoleIds = [...new Set(Object.values(roleIds).flatMap((v) => [...v]))];
|
|
191
|
+
if (!allRoleIds.length) return Object.create(null);
|
|
192
|
+
/** @type {{permission: string; roleId: string}[]} */
|
|
193
|
+
const values = await connect("rdb").select(new Query(RolePermission).select(["permission", "roleId"]).where("roleId", "in", allRoleIds));
|
|
194
|
+
const list = Object.fromEntries(Object.entries(Object.groupBy(values, (l) => l.roleId)).map(([k, v]) => [k, v?.map((v) => v.permission)]));
|
|
195
|
+
/** @type {Record<string, Set<string>>} */
|
|
196
|
+
const permissions = Object.create(null);
|
|
197
|
+
for (const [organization, roleSet] of Object.entries(roleIds)) {
|
|
198
|
+
const s = new Set([...roleSet].flatMap((v) => list[v] || []));
|
|
199
|
+
if (!s.size) continue;
|
|
200
|
+
permissions[organization] = s;
|
|
201
|
+
}
|
|
202
|
+
return permissions;
|
|
203
|
+
});
|
|
204
|
+
/**
|
|
205
|
+
* @returns {Promise<Set<string>>}
|
|
206
|
+
*/
|
|
207
|
+
const useOrganizationAllPermissions = createComputed(async () => {
|
|
208
|
+
const v = await useOrganizationPermissions();
|
|
209
|
+
return new Set(Object.values(v).flatMap((v) => [...v]));
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
//#endregion
|
|
213
|
+
export { OrganizationMember as a, useOrganizations as i, useOrganizationPermissions as n, OrganizationUserRole as o, useOrganizationRoleIds as r, OrganizationAppointment as s, useOrganizationAllPermissions as t };
|
|
214
|
+
//# sourceMappingURL=organization-rbac-BmLEDpJH.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"organization-rbac-BmLEDpJH.mjs","names":[],"sources":["../../plugins/organization-rbac/models/OrganizationAppointment.mjs","../../plugins/organization-rbac/models/OrganizationMember.mjs","../../plugins/organization-rbac/index.mjs"],"sourcesContent":["import { createField, createModel, now, uuid } from '@yongdall/model';\n\n\n\n// export const OrganizationAppointmentRole = createModel('organizationAppointmentRole', {\n// \tappointmentId: createField('uuid', { nullable: false, index: true, label: '职务', model: 'appointment', renderer: 'document', immutable: true, primary: 1 }),\n// \troleId: createField('uuid', { nullable: false, index: true, label: '角色', model: 'role', renderer: 'document', immutable: true, primary: 2 }),\n// \tno: createField('i32', { nullable: false, default: 0 }),\n// });\n\nexport const OrganizationAppointment = createModel('organizationAppointment', {\n\tid: createField('uuid', { nullable: false, primary: 1, default: uuid, uncreatable: true }),\n\t\n\tlabel: createField('string', { nullable: false, label: '职务名称' }),\n\tdescription: createField('string', { nullable: true, label: '描述' }),\n\t\n\tcreatedAt: createField('date', { nullable: false, default: now, creating: now, label: '创建日期' }),\n\tupdatedAt: createField('date', { nullable: false, default: now, updating: now, label: '最后更新日期' }),\n\t\n\t// roles: createField(OrganizationAppointmentRole, { \n\t// \tarray: true, \n\t// \tconstraints: { appointmentId: { field: 'id' }, no: { noField: true } }, \n\t// \tlabel: '角色列表' \n\t// }),\n});\n","import { createField, createModel, now, uuid, Where} from '@yongdall/model';\n\n/**\n * 组织用户角色模型\n * @typedef {Object} OrganizationUserRole\n * @property {string} userId - 用户ID,主键第一部分\n * @property {string} organizationId - 组织ID,主键第二部分\n * @property {string} roleId - 角色ID,主键第三部分\n * @property {number} no - 编号\n */\nexport const OrganizationUserRole = createModel('organizationUserRole', {\n\tuserId: createField('uuid', { nullable: false, index: true, label: '用户', model: 'user', renderer: 'document', immutable: true, primary: 1 }),\n\torganizationId: createField('uuid', { nullable: false, index: true, label: '组织', model: 'organization', renderer: 'document', immutable: true, primary: 2 }),\n\troleId: createField('uuid', { nullable: false, index: true, label: '角色', model: 'role', renderer: 'document', immutable: true, primary: 3 }),\n\n\tno: createField('i32', { nullable: false, default: 0 }),\n});\n\n\nexport const OrganizationMember = createModel('organizationMember', {\n\tid: createField('uuid', { nullable: false, index: true, primary: 1, default: uuid }),\n\n\tuserId: createField('uuid', { nullable: false, index: true, label: '用户', model: 'user', renderer: 'document', immutable: true, layout: { rowSpan: 6 } }),\n\torganizationId: createField('uuid', { nullable: false, index: true, label: '组织', model: 'organization', renderer: 'document', immutable: true, layout: { rowSpan: 6 } }),\n\tappointmentId: createField('uuid', { nullable: true, index: true, label: '职务', model: 'appointment', renderer: 'document', layout: { rowSpan: 6 } }),\n\n\tdefault: createField('bool', { nullable: false, default: false, label: '默认职务' }),\n\n\tcreatedAt: createField('date', { nullable: false, default: now, creating: now, label: '创建日期' }),\n\tupdatedAt: createField('date', { nullable: false, default: now, updating: now, label: '最后更新日期' }),\n\n\troles: createField(OrganizationUserRole, {\n\t\tarray: true,\n\t\tconstraints: {\n\t\t\torganizationId: { field: 'organizationId' },\n\t\t\tuserId: { field: 'userId' },\n\t\t\tno: { noField: true }\n\t\t},\n\t\tlabel: '角色列表'\n\t}),\n}, {\n\tindexes: [\n\t\t{ fields: ['userId', 'organizationId', 'appointmentId'], unique: true }\n\t],\n\thooks: {\n\t\tasync afterCreate(conn, Model, record) {\n\t\t\tif (!record.default) { return }\n\t\t\tawait conn.update(\n\t\t\t\tModel,\n\t\t\t\t{ default: false },\n\t\t\t\tWhere.and('organizationId', '=', record.organizationId, true)\n\t\t\t\t\t.and('userId', record.userId),\n\t\t\t);\n\t\t},\n\n\t\tasync afterUpdate(conn, Model, record, oldRecord) {\n\t\t\tif (!record.default) { return }\n\t\t\tif (oldRecord.default) { return }\n\t\t\tawait conn.update(\n\t\t\t\tModel,\n\t\t\t\t{ default: false },\n\t\t\t\tWhere.and('organizationId', '=', record.organizationId, true)\n\t\t\t\t\t.and('userId', record.userId),\n\t\t\t);\n\t\t},\n\t}\n});\n","import { createComputed } from '@yongdall/context';\nimport { Query } from '@yongdall/model';\nimport { getUser } from '@yongdall/core';\nimport { connect } from '@yongdall/connection';\nimport { RolePermission } from '@yongdall/role';\nimport { OrganizationMember, OrganizationUserRole } from './models/index.mjs';\n\nexport * from './models/index.mjs';\n\n\n/**\n * 所在组织\n * @returns \n */\nexport const useOrganizations = createComputed(async () => {\n\tconst id = await getUser();\n\t/** @type {{organizationId: string}[]} */\n\tconst relations = await connect('rdb').select(\n\t\tnew Query(OrganizationMember).select(['organizationId']).where('userId', id)\n\t)\n\treturn new Set(relations.map(v => v.organizationId));\n});\n\n\n/**\n * 角色\n * @returns {Promise<Record<string, Set<string>>>}\n */\nexport const useOrganizationRoleIds = createComputed(async () => {\n\tconst organizations = await useOrganizations()\n\tif (!organizations.size) { return {}; }\n\tconst id = await getUser();\n\t/** @type {{organizationId: number; roleId: string;}[]} */\n\tconst list = await connect('rdb').select(\n\t\tnew Query(OrganizationUserRole).select(['organizationId', 'roleId'])\n\t\t\t.where('organizationId', 'in', [...organizations])\n\t\t\t.where('userId', id)\n\t);\n\treturn Object.fromEntries((Object.entries(Object.groupBy(list, v => v.organizationId))\n\t\t.map(([organizationId, list]) => [organizationId, new Set(list?.map(v => v.roleId))])))\n});\n\n\n/**\n * @returns {Promise<Record<string, Set<string>>>}\n */\nexport const useOrganizationPermissions = createComputed(async () => {\n\tconst roleIds = await useOrganizationRoleIds()\n\tconst allRoleIds = [...new Set(Object.values(roleIds).flatMap(v =>[...v]))];\n\tif (!allRoleIds.length) { return Object.create(null); }\n\t/** @type {{permission: string; roleId: string}[]} */\n\tconst values = await connect('rdb').select(\n\t\tnew Query(RolePermission).select(['permission', 'roleId'])\n\t\t\t.where('roleId', 'in', allRoleIds)\n\t);\n\tconst list = Object.fromEntries(Object.entries(Object.groupBy(values, l => l.roleId)).map(([k,v]) => [k, v?.map(v => v.permission)]));\n\t/** @type {Record<string, Set<string>>} */\n\tconst permissions = Object.create(null);\n\tfor (const [organization, roleSet] of Object.entries(roleIds)) {\n\t\tconst s = new Set([...roleSet].flatMap(v => list[v] || []));\n\t\tif (!s.size) { continue; }\n\t\tpermissions[organization] = s;\n\t}\n\treturn permissions;\n});\n\n\n\n/**\n * @returns {Promise<Set<string>>}\n */\nexport const useOrganizationAllPermissions = createComputed(async () => {\n\tconst v = await useOrganizationPermissions()\n\treturn new Set(Object.values(v).flatMap(v => [...v]));\n});\n"],"mappings":";;;;;;;AAUA,MAAa,0BAA0B,YAAY,2BAA2B;CAC7E,IAAI,YAAY,QAAQ;EAAG,UAAU;EAAQ,SAAS;EAAG,SAAS;EAAM,aAAa;EAAM,CAAC;CAE5F,OAAO,YAAY,UAAU;EAAE,UAAU;EAAO,OAAO;EAAQ,CAAC;CAChE,aAAa,YAAY,UAAU;EAAE,UAAU;EAAM,OAAO;EAAM,CAAC;CAEnE,WAAW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IAAI,CAAC,GAAG,QAAQ,CAAC,SAAQ,MAAK,KAAK,MAAM,EAAE,CAAC,CAAC;AAC3D,MAAI,CAAC,EAAE,KAAQ;AACf,cAAY,gBAAgB;;AAE7B,QAAO;EACN;;;;AAOF,MAAa,gCAAgC,eAAe,YAAY;CACvE,MAAM,IAAI,MAAM,4BAA4B;AAC5C,QAAO,IAAI,IAAI,OAAO,OAAO,EAAE,CAAC,SAAQ,MAAK,CAAC,GAAG,EAAE,CAAC,CAAC;EACpD"}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yongdall/organization-rbac",
|
|
3
|
+
"version": "0.4.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.mjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.mjs"
|
|
8
|
+
},
|
|
9
|
+
"description": "",
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "",
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@yongdall/connection": "^0.4.0",
|
|
15
|
+
"@yongdall/context": "^0.4.0",
|
|
16
|
+
"@yongdall/core": "^0.4.0",
|
|
17
|
+
"@yongdall/model": "^0.4.0",
|
|
18
|
+
"@yongdall/role": "^0.4.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@yongdall/types": "^0.4.0"
|
|
22
|
+
}
|
|
23
|
+
}
|