mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-05 17:53:12 +02:00
fix: create tokens createByUserId (#5968)
## About the changes Applies a fix that is already in main with some modifications to adapt it to 5.8 release
This commit is contained in:
parent
228d99454d
commit
3dc12a5e1a
@ -42,7 +42,7 @@ import {
|
||||
getStandardResponses,
|
||||
} from '../../openapi/util/standard-responses';
|
||||
import { ProxyService } from '../../services/proxy-service';
|
||||
import { extractUsername } from '../../util';
|
||||
import { extractUserId, extractUsername } from '../../util';
|
||||
import { OperationDeniedError } from '../../error';
|
||||
|
||||
interface TokenParam {
|
||||
@ -312,6 +312,7 @@ export class ApiTokenController extends Controller {
|
||||
const token = await this.apiTokenService.createApiToken(
|
||||
createToken,
|
||||
extractUsername(req),
|
||||
extractUserId(req),
|
||||
);
|
||||
this.openApiService.respondWithValidation(
|
||||
201,
|
||||
|
@ -27,7 +27,7 @@ import {
|
||||
ProjectService,
|
||||
ProxyService,
|
||||
} from '../../../services';
|
||||
import { extractUsername } from '../../../util';
|
||||
import { extractUserId, extractUsername } from '../../../util';
|
||||
import { IAuthRequest } from '../../unleash-types';
|
||||
import Controller from '../../controller';
|
||||
import { Logger } from '../../../logger';
|
||||
@ -190,6 +190,7 @@ export class ProjectApiTokenController extends Controller {
|
||||
const token = await this.apiTokenService.createApiToken(
|
||||
createToken,
|
||||
extractUsername(req),
|
||||
extractUserId(req),
|
||||
);
|
||||
this.openApiService.respondWithValidation(
|
||||
201,
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { SYSTEM_USER } from '../../lib/types';
|
||||
import { IUser } from '../server-impl';
|
||||
import { extractUsernameFromUser } from './extract-user';
|
||||
import { extractUserIdFromUser, extractUsernameFromUser } from './extract-user';
|
||||
|
||||
describe('extractUsernameFromUser', () => {
|
||||
test('Should return the email if it exists', () => {
|
||||
@ -19,14 +20,16 @@ describe('extractUsernameFromUser', () => {
|
||||
expect(extractUsernameFromUser(user)).toBe(user.username);
|
||||
});
|
||||
|
||||
test('Should return "unknown" if neither email nor username exists', () => {
|
||||
test('Should return the system user if neither email nor username exists', () => {
|
||||
const user = {} as IUser;
|
||||
|
||||
expect(extractUsernameFromUser(user)).toBe('unknown');
|
||||
expect(extractUsernameFromUser(user)).toBe(SYSTEM_USER.username);
|
||||
expect(extractUserIdFromUser(user)).toBe(SYSTEM_USER.id);
|
||||
});
|
||||
|
||||
test('Should return "unknown" if user is null', () => {
|
||||
test('Should return the system user if user is null', () => {
|
||||
const user = null as unknown as IUser;
|
||||
expect(extractUsernameFromUser(user)).toBe('unknown');
|
||||
expect(extractUsernameFromUser(user)).toBe(SYSTEM_USER.username);
|
||||
expect(extractUserIdFromUser(user)).toBe(SYSTEM_USER.id);
|
||||
});
|
||||
});
|
||||
|
@ -1,14 +1,19 @@
|
||||
import { SYSTEM_USER } from '../../lib/types';
|
||||
import { IAuthRequest, IUser } from '../server-impl';
|
||||
|
||||
export function extractUsernameFromUser(user: IUser): string {
|
||||
return user?.email || user?.username || 'unknown';
|
||||
return user?.email || user?.username || SYSTEM_USER.username;
|
||||
}
|
||||
|
||||
export function extractUsername(req: IAuthRequest): string {
|
||||
return extractUsernameFromUser(req.user);
|
||||
}
|
||||
|
||||
export const extractUserId = (req: IAuthRequest) => req.user.id;
|
||||
export const extractUserIdFromUser = (user: IUser) =>
|
||||
user?.id || SYSTEM_USER.id;
|
||||
|
||||
export const extractUserId = (req: IAuthRequest) =>
|
||||
extractUserIdFromUser(req.user);
|
||||
|
||||
export const extractUserInfo = (req: IAuthRequest) => ({
|
||||
id: extractUserId(req),
|
||||
|
Loading…
Reference in New Issue
Block a user