Fixed getting all permissions for users

ref #79
This commit is contained in:
Nicolai Ort 2021-01-08 20:17:05 +01:00
parent 3bd4948c43
commit aa0337ea33
3 changed files with 12 additions and 9 deletions

View File

@ -29,7 +29,7 @@ export class UserController {
@OpenAPI({ description: 'Lists all users. <br> This includes their groups and permissions directly granted to them (if existing/associated).' }) @OpenAPI({ description: 'Lists all users. <br> This includes their groups and permissions directly granted to them (if existing/associated).' })
async getAll() { async getAll() {
let responseUsers: ResponseUser[] = new Array<ResponseUser>(); let responseUsers: ResponseUser[] = new Array<ResponseUser>();
const users = await this.userRepository.find({ relations: ['permissions', 'groups'] }); const users = await this.userRepository.find({ relations: ['permissions', 'groups', 'groups.permissions'] });
users.forEach(user => { users.forEach(user => {
responseUsers.push(new ResponseUser(user)); responseUsers.push(new ResponseUser(user));
}); });
@ -43,7 +43,7 @@ export class UserController {
@OnUndefined(UserNotFoundError) @OnUndefined(UserNotFoundError)
@OpenAPI({ description: 'Lists all information about the user whose id got provided. <br> Please remember that only permissions granted directly to the user will show up here, not permissions inherited from groups.' }) @OpenAPI({ description: 'Lists all information about the user whose id got provided. <br> Please remember that only permissions granted directly to the user will show up here, not permissions inherited from groups.' })
async getOne(@Param('id') id: number) { async getOne(@Param('id') id: number) {
let user = await this.userRepository.findOne({ id: id }, { relations: ['permissions', 'groups'] }) let user = await this.userRepository.findOne({ id: id }, { relations: ['permissions', 'groups', 'groups.permissions'] })
if (!user) { throw new UserNotFoundError(); } if (!user) { throw new UserNotFoundError(); }
return new ResponseUser(user); return new ResponseUser(user);
} }

View File

@ -132,18 +132,20 @@ export class User extends Principal {
* Resolves all permissions granted to this user through groups or directly to the string enum format. * Resolves all permissions granted to this user through groups or directly to the string enum format.
*/ */
public get allPermissions(): string[] { public get allPermissions(): string[] {
let allPermissions = new Array<string>(); let returnPermissions: string[] = new Array<string>();
for (let permission in this.permissions) {
allPermissions.push(permission.toString()); if (!this.permissions) { return returnPermissions; }
for (let permission of this.permissions) {
returnPermissions.push(permission.toString());
} }
if (!this.groups) { return returnPermissions; }
for (let group of this.groups) { for (let group of this.groups) {
for (let permission in group.permissions) { for (let permission of group.permissions) {
allPermissions.push(permission.toString()); returnPermissions.push(permission.toString());
} }
} }
console.log(allPermissions) return Array.from(new Set(returnPermissions));
return Array.from(new Set(allPermissions));
} }
/** /**

View File

@ -91,5 +91,6 @@ export class ResponseUser extends ResponsePrincipal {
this.profilePic = user.profilePic; this.profilePic = user.profilePic;
this.groups = user.groups; this.groups = user.groups;
this.permissions = user.allPermissions; this.permissions = user.allPermissions;
this.groups.forEach(function (g) { delete g.permissions });
} }
} }