Fixed the user->Group relation

ref #6
This commit is contained in:
Nicolai Ort 2020-12-18 21:42:43 +01:00
parent 1a9c860188
commit d670b814a4
3 changed files with 6 additions and 36 deletions

View File

@ -27,7 +27,7 @@ export class CreateAuth {
if (!this.password) {
throw new PasswordNeededError();
}
const found_user = await getConnectionManager().get().getRepository(User).findOne({ relations: ['groups', 'permissions'], where: [{ username: this.username }, { email: this.email }] });
const found_user = await getConnectionManager().get().getRepository(User).findOne({ relations: ['groups', 'permissions', 'groups.permissions'], where: [{ username: this.username }, { email: this.email }] });
if (!found_user) {
throw new UserNotFoundError();
}

View File

@ -101,22 +101,11 @@ export class CreateUser {
if (!Array.isArray(this.group)) {
this.group = [this.group]
}
const groupIDs: number[] = this.group
let errors = 0
const validateusergroups = async () => {
for (const g of groupIDs) {
const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g });
if (found.length === 0) {
errors++
} else {
groups.push(found[0]);
}
}
return groups;
}
await validateusergroups()
if (errors !== 0) {
throw new UserGroupNotFoundError();
for (let group of this.group) {
let found = await getConnectionManager().get().getRepository(UserGroup).findOne({ id: group });
if (!found) { throw new UserGroupNotFoundError(); }
groups.push(found);
}
return groups;
}
}

View File

@ -3,7 +3,6 @@ import { ChildEntity, Column, JoinTable, ManyToMany, OneToMany } from "typeorm";
import { config } from '../../config';
import { ResponsePrincipal } from '../responses/ResponsePrincipal';
import { ResponseUser } from '../responses/ResponseUser';
import { Permission } from './Permission';
import { Principal } from './Principal';
import { UserAction } from './UserAction';
import { UserGroup } from './UserGroup';
@ -111,24 +110,6 @@ export class User extends Principal {
@OneToMany(() => UserAction, action => action.user, { nullable: true })
actions: UserAction[]
/**
* calculate all permissions
*/
public get calc_permissions(): Permission[] {
let final_permissions = []
this.groups.forEach((permission) => {
if (!final_permissions.includes(permission)) {
final_permissions.push(permission)
}
})
this.permissions.forEach((permission) => {
if (!final_permissions.includes(permission)) {
final_permissions.push(permission)
}
})
return final_permissions
}
/**
* Turn this into a response.
*/