1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-14 00:19:16 +01:00

feat: expose user permissions

This commit is contained in:
Ivar Conradi Østhus 2021-04-19 14:55:04 +02:00
parent b55c85783b
commit e21cfdc4bc
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,52 @@
'use strict';
import { Response } from 'express';
import { IUnleashConfig } from '../../types/core';
import { IAuthRequest } from '../unleash-types';
import Controller from '../controller';
import { AccessService } from '../../services/access-service';
interface IService {
accessService: AccessService;
}
class UserController extends Controller {
private accessService: AccessService;
constructor(config: IUnleashConfig, { accessService }: IService) {
super(config);
this.accessService = accessService;
this.get('/', this.getUser);
this.get('/logout', this.logout);
}
async getUser(req: IAuthRequest, res: Response): Promise<void> {
const { user } = req;
if (user) {
const permissions = await this.accessService.getPermissionsForUser(
user,
);
delete user.permissions; // TODO: remove
return res
.status(200)
.json({ user, permissions })
.end();
}
return res.status(404).end();
}
// Deprecated, use "/logout" instead. Will be removed in v4.
logout(req: IAuthRequest, res: Response): void {
if (req.session) {
req.session = null;
}
if (req.logout) {
req.logout();
}
res.redirect(`${this.config.baseUriPath}/`);
}
}
module.exports = UserController;
export default UserController;

View File

@ -3,4 +3,6 @@ import User from '../user';
export interface IAuthRequest extends Request {
user: User;
logout: () => void;
session: any;
}