mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
core: group api (#1168)
This commit is contained in:
1 parent
e654a5b7e6
commit
81cf24106b
2 files changed
+15
-13
No files matched your search
@@ -438,21 +438,15 @@ export const DomainApi = {
|
||||
),
|
||||
groups: Query(
|
||||
Schema.object({
|
||||
search: Schema.string(),
|
||||
names: Schema.array(Schema.string()),
|
||||
domainId: Schema.string().required(),
|
||||
uid: Schema.number().step(1),
|
||||
names: Schema.array(Schema.string()),
|
||||
search: Schema.string(),
|
||||
limit: Schema.number().step(1).max(100),
|
||||
}),
|
||||
async (ctx, args) => {
|
||||
if (!ctx.user.hasPerm(PERM.PERM_VIEW) && !ctx.user.hasPriv(PRIV.PRIV_VIEW_ALL_DOMAIN)) throw new PermissionError(PERM.PERM_VIEW);
|
||||
const groups = await user.listGroup(args.domainId);
|
||||
if (args.names?.length) {
|
||||
return groups.filter((g) => args.names.includes(g.name));
|
||||
}
|
||||
if (args.search) {
|
||||
const searchLower = args.search.toLowerCase();
|
||||
return groups.filter((g) => g.name.toLowerCase().includes(searchLower));
|
||||
}
|
||||
return groups;
|
||||
return user.listGroup(args.domainId, args.uid, args.names, args.search, args.limit ?? 20);
|
||||
},
|
||||
),
|
||||
'domain.group': Mutation(
|
||||
|
||||
@@ -479,8 +479,16 @@ class UserModel {
|
||||
]);
|
||||
}
|
||||
|
||||
static async listGroup(domainId: string, uid?: number) {
|
||||
const groups = await collGroup.find(typeof uid === 'number' ? { domainId, uids: uid } : { domainId }).toArray();
|
||||
static async listGroup(domainId: string, uid?: number, names?: string[], search?: string, limit?: number) {
|
||||
const filter: Filter<GDoc> = { domainId };
|
||||
if (typeof uid === 'number') filter.uids = uid;
|
||||
const $and: Filter<GDoc>[] = [];
|
||||
if (names?.length) $and.push({ name: { $in: names } });
|
||||
if (search) $and.push({ name: { $regex: escapeRegExp(search), $options: 'i' } });
|
||||
if ($and.length) filter.$and = $and;
|
||||
let cursor = collGroup.find(filter);
|
||||
if (limit) cursor = cursor.limit(limit);
|
||||
const groups = await cursor.toArray();
|
||||
if (uid) {
|
||||
groups.push({
|
||||
_id: new ObjectId(), domainId, uids: [uid], name: uid.toString(),
|
||||
|
||||
Reference in new issue
Block a user