mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-20 18:49:27 -08:00
⚡ Add GET /users route
This commit is contained in:
parent
53d88b33ce
commit
bbab96cacb
49
packages/cli/src/PublicApi/helpers.ts
Normal file
49
packages/cli/src/PublicApi/helpers.ts
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
import * as querystring from 'querystring';
|
||||||
|
|
||||||
|
interface IPaginationOffsetDecoded {
|
||||||
|
offset: number;
|
||||||
|
limit: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const decodeCursor = (cursor: string)
|
||||||
|
: IPaginationOffsetDecoded => {
|
||||||
|
const data = JSON.parse(Buffer.from(cursor, 'base64').toString());
|
||||||
|
const unserializedData = querystring.decode(data) as { offset: string, limit: string };
|
||||||
|
return {
|
||||||
|
offset: parseInt(unserializedData.offset, 10),
|
||||||
|
limit: parseInt(unserializedData.limit, 10),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getNextCursor = (offset: number, limit: number, numberOfRecords: number): string | null => {
|
||||||
|
const retrieveRecordsLength = offset + limit;
|
||||||
|
|
||||||
|
if (retrieveRecordsLength < numberOfRecords) {
|
||||||
|
return Buffer.from(JSON.stringify(querystring.encode({
|
||||||
|
limit,
|
||||||
|
offset: offset + limit,
|
||||||
|
}))).toString('base64');
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getSelectableProperties = (table: string) => {
|
||||||
|
return {
|
||||||
|
user: [
|
||||||
|
'id',
|
||||||
|
'email',
|
||||||
|
'firstName',
|
||||||
|
'lastName',
|
||||||
|
'createdAt',
|
||||||
|
'updatedAt',
|
||||||
|
],
|
||||||
|
role: [
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
'scope',
|
||||||
|
'createdAt',
|
||||||
|
'updatedAt',
|
||||||
|
],
|
||||||
|
}[table];
|
||||||
|
};
|
|
@ -43,6 +43,22 @@ export const getRoutes = (): express.Router => {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if (!config.get('userManagement.isInstanceOwnerSetUp')) {
|
||||||
|
// Logger.debug(
|
||||||
|
// 'Request to send email invite(s) to user(s) failed because the owner account is not set up',
|
||||||
|
// );
|
||||||
|
// throw new ResponseHelper.ResponseError(
|
||||||
|
// 'You must set up your own account before inviting others',
|
||||||
|
// undefined,
|
||||||
|
// 400,
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (req.user.globalRole.name === 'owner') {
|
||||||
|
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
req.user = user[0];
|
req.user = user[0];
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -59,6 +59,14 @@ paths:
|
||||||
schema:
|
schema:
|
||||||
type: string
|
type: string
|
||||||
example: MTIzZTQ1NjctZTg5Yi0xMmQzLWE0NTYtNDI2NjE0MTc0MDA
|
example: MTIzZTQ1NjctZTg5Yi0xMmQzLWE0NTYtNDI2NjE0MTc0MDA
|
||||||
|
- name: includeRole
|
||||||
|
in: query
|
||||||
|
required: false
|
||||||
|
style: form
|
||||||
|
explode: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
example: true
|
||||||
responses:
|
responses:
|
||||||
"200":
|
"200":
|
||||||
description: Successful operation
|
description: Successful operation
|
||||||
|
|
|
@ -1,10 +1,16 @@
|
||||||
import express = require('express');
|
import express = require('express');
|
||||||
|
import { getConnection } from 'typeorm';
|
||||||
|
|
||||||
import { UserRequest } from '../../../../requests';
|
import { UserRequest } from '../../../../requests';
|
||||||
|
|
||||||
|
import { User } from '../../../../databases/entities/User';
|
||||||
|
import { decodeCursor, getNextCursor, getSelectableProperties } from '../../../helpers';
|
||||||
|
|
||||||
|
import * as config from '../../../../../config';
|
||||||
|
|
||||||
export = {
|
export = {
|
||||||
createUsers: async (req: UserRequest.Invite, res: express.Response) => {
|
createUsers: async (req: UserRequest.Invite, res: express.Response) => {
|
||||||
res.json({ success: true});
|
res.json({ success: true });
|
||||||
},
|
},
|
||||||
deleteUser: async (req: UserRequest.Delete, res: express.Response) => {
|
deleteUser: async (req: UserRequest.Delete, res: express.Response) => {
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
|
@ -13,6 +19,32 @@ export = {
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
},
|
},
|
||||||
getUsers: async (req: UserRequest.Get, res: express.Response) => {
|
getUsers: async (req: UserRequest.Get, res: express.Response) => {
|
||||||
res.json({ success: true });
|
let offset = 0;
|
||||||
|
let limit = parseInt(req.query.limit as string, 10) || 10;
|
||||||
|
const includeRole = req.query?.includeRole?.toLowerCase() === 'true' || false;
|
||||||
|
|
||||||
|
if (req.query.cursor) {
|
||||||
|
const cursor = req.query.cursor as string;
|
||||||
|
({ offset, limit } = decodeCursor(cursor));
|
||||||
|
}
|
||||||
|
|
||||||
|
const query = getConnection()
|
||||||
|
.getRepository(User)
|
||||||
|
.createQueryBuilder()
|
||||||
|
.leftJoinAndSelect('User.globalRole', 'Role')
|
||||||
|
.select(getSelectableProperties('user')!.map(property => `User.${property}`))
|
||||||
|
.limit(limit)
|
||||||
|
.offset(offset);
|
||||||
|
|
||||||
|
if (includeRole) {
|
||||||
|
query.addSelect(getSelectableProperties('role')!.map(property => `Role.${property}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
const [users, count] = await query.getManyAndCount();
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
users,
|
||||||
|
nextCursor: getNextCursor(offset, limit, count),
|
||||||
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
2
packages/cli/src/requests.d.ts
vendored
2
packages/cli/src/requests.d.ts
vendored
|
@ -198,7 +198,7 @@ export declare namespace UserRequest {
|
||||||
|
|
||||||
export type Delete = AuthenticatedRequest<{ id: string, email: string }, {}, {}, { transferId?: string }>;
|
export type Delete = AuthenticatedRequest<{ id: string, email: string }, {}, {}, { transferId?: string }>;
|
||||||
|
|
||||||
export type Get = AuthenticatedRequest<{ id: string, email: string }, {}, {}, { limit: string, cursor: string }>;
|
export type Get = AuthenticatedRequest<{ id: string, email: string }, {}, {}, { limit: string, cursor: string, includeRole: string }>;
|
||||||
|
|
||||||
export type Reinvite = AuthenticatedRequest<{ id: string }>;
|
export type Reinvite = AuthenticatedRequest<{ id: string }>;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue