From f50e7f0b3ac49b6f728099e9ed1f1ccb0d9e78ff Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Wed, 2 Dec 2020 19:29:40 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=9A=A7=20UserAction=20relation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/User.ts | 15 +++++++++++---- src/models/UserAction.ts | 12 ++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/models/User.ts b/src/models/User.ts index fce74e8..b039d20 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -2,6 +2,7 @@ import { Entity, Column, OneToMany, ManyToOne, PrimaryGeneratedColumn, Generated import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsString, isUUID, } from "class-validator"; import { UserGroup } from './UserGroup'; import { Permission } from './Permission'; +import { UserAction } from './UserAction'; /** * Defines a admin user. @@ -11,10 +12,10 @@ export class User { /** * autogenerated unique id (primary key). */ - @PrimaryGeneratedColumn() - @IsOptional() - @IsInt() - id: number; + @PrimaryGeneratedColumn() + @IsOptional() + @IsInt() + id: number; /** * autogenerated uuid @@ -96,6 +97,12 @@ export class User { @IsString() profilepic: string; + /** + * actions + */ + @OneToMany(() => UserAction, action => action.user) + actions: UserAction + /** * calculate all permissions */ diff --git a/src/models/UserAction.ts b/src/models/UserAction.ts index ea794a5..c9caf60 100644 --- a/src/models/UserAction.ts +++ b/src/models/UserAction.ts @@ -1,10 +1,11 @@ -import { PrimaryGeneratedColumn, Column, OneToMany, Entity } from "typeorm"; +import { PrimaryGeneratedColumn, Column, OneToMany, Entity, ManyToOne } from "typeorm"; import { IsInt, IsNotEmpty, IsOptional, IsString, } from "class-validator"; +import { User } from './User'; /** * Defines the UserAction interface. @@ -19,8 +20,11 @@ export class UserAction { @IsInt() id: number; - // TODO: - // user: relation + /** + * user + */ + @ManyToOne(() => User, user => user.actions) + user: User /** * The actions's target (e.g. Track#2) @@ -41,7 +45,7 @@ export class UserAction { /** * The description of change (before-> after; e.g. distance:15->17) */ - @Column({nullable: true}) + @Column({ nullable: true }) @IsOptional() @IsString() changed: string; From 1cf35f016b0aeb0f1224648b301044b2ea76dc60 Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Wed, 2 Dec 2020 19:34:43 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=9A=A7=20Permissions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/User.ts | 67 +++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/models/User.ts b/src/models/User.ts index b039d20..5f153b7 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -10,90 +10,90 @@ import { UserAction } from './UserAction'; @Entity() export class User { /** - * autogenerated unique id (primary key). - */ + * autogenerated unique id (primary key). + */ @PrimaryGeneratedColumn() @IsOptional() @IsInt() id: number; /** - * autogenerated uuid - */ + * autogenerated uuid + */ @IsOptional() @IsInt() @Generated("uuid") uuid: string; /** - * user email - */ + * user email + */ @IsEmail() email: string; /** - * username - */ + * username + */ @IsString() username: string; /** - * firstname - */ + * firstname + */ @IsString() @IsNotEmpty() firstname: string; /** - * middlename - */ + * middlename + */ @IsString() @IsOptional() middlename: string; /** - * lastname - */ + * lastname + */ @IsString() @IsNotEmpty() lastname: string; /** - * password - */ + * password + */ @IsString() @IsNotEmpty() password: string; /** - * permissions - */ + * permissions + */ @ManyToOne(() => Permission, permission => permission.users) permissions: Permission[]; /** - * groups - */ + * groups + */ @ManyToMany(() => UserGroup) @JoinTable() groups: UserGroup[]; /** - * is user enabled? - */ + * is user enabled? + */ @IsBoolean() enabled: boolean; /** - * jwt refresh count - */ + * jwt refresh count + */ @IsInt() @Column({ default: 1 }) refreshTokenCount: number; /** - * profilepic - */ + * profilepic + */ @IsString() profilepic: string; @@ -107,10 +107,17 @@ export class User { * calculate all permissions */ public get calc_permissions(): Permission[] { - let final_permissions = this.groups.forEach((permission) => { - console.log(permission); + let final_permissions = [] + this.groups.forEach((permission) => { + if (!final_permissions.includes(permission)) { + final_permissions.push(permission) + } }) - // TODO: add user permissions on top of group permissions + return - return [] + this.permissions.forEach((permission) => { + if (!final_permissions.includes(permission)) { + final_permissions.push(permission) + } + }) + return final_permissions } } From 4c80ab1516d11ec069fd79e06cdf050712647090 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 2 Dec 2020 19:45:02 +0100 Subject: [PATCH 3/3] Updated relationships to be nullable ref #11 #13 --- src/models/Address.ts | 4 ++-- src/models/DistanceDonation.ts | 2 +- src/models/Donation.ts | 2 +- src/models/GroupContact.ts | 6 +++--- src/models/Participant.ts | 4 ++-- src/models/Permission.ts | 4 ++-- src/models/Runner.ts | 8 ++++---- src/models/RunnerCard.ts | 4 ++-- src/models/RunnerGroup.ts | 4 ++-- src/models/RunnerOrganisation.ts | 4 ++-- src/models/RunnerTeam.ts | 2 +- src/models/Scan.ts | 2 +- src/models/ScanStation.ts | 4 ++-- src/models/Track.ts | 4 ++-- src/models/TrackScan.ts | 6 +++--- src/models/User.ts | 2 +- src/models/UserGroup.ts | 2 +- 17 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/models/Address.ts b/src/models/Address.ts index ccc2c70..735ecd3 100644 --- a/src/models/Address.ts +++ b/src/models/Address.ts @@ -76,12 +76,12 @@ export class Address { /** * Used to link the address to participants. */ - @OneToMany(() => Participant, participant => participant.address) + @OneToMany(() => Participant, participant => participant.address, { nullable: true }) participants: Participant[]; /** * Used to link the address to runner groups. */ - @OneToMany(() => RunnerOrganisation, group => group.address) + @OneToMany(() => RunnerOrganisation, group => group.address, { nullable: true }) groups: RunnerOrganisation[]; } diff --git a/src/models/DistanceDonation.ts b/src/models/DistanceDonation.ts index 878f8cb..79bf0b8 100644 --- a/src/models/DistanceDonation.ts +++ b/src/models/DistanceDonation.ts @@ -13,7 +13,7 @@ export class DistanceDonation extends Donation { * The runner associated. */ @IsNotEmpty() - @ManyToOne(() => Runner, runner => runner.distanceDonations) + @ManyToOne(() => Runner, runner => runner.distanceDonations, { nullable: true }) runner: Runner; /** diff --git a/src/models/Donation.ts b/src/models/Donation.ts index 7143c99..b94e182 100644 --- a/src/models/Donation.ts +++ b/src/models/Donation.ts @@ -24,7 +24,7 @@ export abstract class Donation { * The donations's donor. */ @IsNotEmpty() - @ManyToOne(() => Participant, donor => donor.donations) + @ManyToOne(() => Participant, donor => donor.donations, { nullable: true }) donor: Participant; /** diff --git a/src/models/GroupContact.ts b/src/models/GroupContact.ts index 745174f..5d12066 100644 --- a/src/models/GroupContact.ts +++ b/src/models/GroupContact.ts @@ -55,7 +55,7 @@ export class GroupContact { * Optional */ @IsOptional() - @ManyToOne(() => Address, address => address.participants) + @ManyToOne(() => Address, address => address.participants, { nullable: true }) address?: Address; /** @@ -79,12 +79,12 @@ export class GroupContact { /** * Used to link the contact as the donor of a donation. */ - @OneToMany(() => Donation, donation => donation.donor) + @OneToMany(() => Donation, donation => donation.donor, { nullable: true }) donations: Donation[]; /** * Used to link runners to donations. */ - @OneToMany(() => RunnerGroup, group => group.contact) + @OneToMany(() => RunnerGroup, group => group.contact, { nullable: true }) groups: RunnerGroup[]; } \ No newline at end of file diff --git a/src/models/Participant.ts b/src/models/Participant.ts index bcbf5ca..e80d348 100644 --- a/src/models/Participant.ts +++ b/src/models/Participant.ts @@ -53,7 +53,7 @@ export abstract class Participant { * The participant's address. * Optional */ - @ManyToOne(() => Address, address => address.participants) + @ManyToOne(() => Address, address => address.participants, { nullable: true }) address?: Address; /** @@ -77,6 +77,6 @@ export abstract class Participant { /** * Used to link the participant as the donor of a donation. */ - @OneToMany(() => Donation, donation => donation.donor) + @OneToMany(() => Donation, donation => donation.donor, { nullable: true }) donations: Donation[]; } \ No newline at end of file diff --git a/src/models/Permission.ts b/src/models/Permission.ts index e9b049b..751efe3 100644 --- a/src/models/Permission.ts +++ b/src/models/Permission.ts @@ -23,13 +23,13 @@ export abstract class Permission { /** * users */ - @OneToMany(() => User, user => user.permissions) + @OneToMany(() => User, user => user.permissions, { nullable: true }) users: User[] /** * groups */ - @OneToMany(() => UserGroup, group => group.permissions) + @OneToMany(() => UserGroup, group => group.permissions, { nullable: true }) groups: UserGroup[] /** diff --git a/src/models/Runner.ts b/src/models/Runner.ts index 3a8532a..7423c22 100644 --- a/src/models/Runner.ts +++ b/src/models/Runner.ts @@ -15,25 +15,25 @@ export class Runner extends Participant { * The runner's associated group. */ @IsNotEmpty() - @ManyToOne(() => RunnerGroup, group => group.runners) + @ManyToOne(() => RunnerGroup, group => group.runners, { nullable: true }) group: RunnerGroup; /** * Used to link runners to donations. */ - @OneToMany(() => DistanceDonation, distanceDonation => distanceDonation.runner) + @OneToMany(() => DistanceDonation, distanceDonation => distanceDonation.runner, { nullable: true }) distanceDonations: DistanceDonation[]; /** * Used to link runners to cards. */ - @OneToMany(() => RunnerCard, card => card.runner) + @OneToMany(() => RunnerCard, card => card.runner, { nullable: true }) cards: RunnerCard[]; /** * Used to link runners to a scans */ - @OneToMany(() => Scan, scan => scan.runner) + @OneToMany(() => Scan, scan => scan.runner, { nullable: true }) scans: Scan[]; @IsInt() diff --git a/src/models/RunnerCard.ts b/src/models/RunnerCard.ts index f978d0c..8b0e05b 100644 --- a/src/models/RunnerCard.ts +++ b/src/models/RunnerCard.ts @@ -26,7 +26,7 @@ export class RunnerCard { * The runner that is currently associated with this card. */ @IsOptional() - @ManyToOne(() => Runner, runner => runner.cards) + @ManyToOne(() => Runner, runner => runner.cards, { nullable: true }) runner: Runner; /** @@ -51,6 +51,6 @@ export class RunnerCard { /** * Used to link cards to a track scans. */ - @OneToMany(() => TrackScan, scan => scan.track) + @OneToMany(() => TrackScan, scan => scan.track, { nullable: true }) scans: TrackScan[]; } diff --git a/src/models/RunnerGroup.ts b/src/models/RunnerGroup.ts index a7c199b..2cd3694 100644 --- a/src/models/RunnerGroup.ts +++ b/src/models/RunnerGroup.ts @@ -35,12 +35,12 @@ export abstract class RunnerGroup { * Optional */ @IsOptional() - @ManyToOne(() => GroupContact, contact => contact.groups) + @ManyToOne(() => GroupContact, contact => contact.groups, { nullable: true }) contact?: GroupContact; /** * Used to link runners to a runner group. */ - @OneToMany(() => Runner, runner => runner.group) + @OneToMany(() => Runner, runner => runner.group, { nullable: true }) runners: Runner[]; } \ No newline at end of file diff --git a/src/models/RunnerOrganisation.ts b/src/models/RunnerOrganisation.ts index 958b363..4245fa0 100644 --- a/src/models/RunnerOrganisation.ts +++ b/src/models/RunnerOrganisation.ts @@ -15,12 +15,12 @@ export class RunnerOrganisation extends RunnerGroup { * Optional */ @IsOptional() - @ManyToOne(() => Address, address => address.groups) + @ManyToOne(() => Address, address => address.groups, { nullable: true }) address?: Address; /** * Used to link teams to runner groups. */ - @OneToMany(() => RunnerTeam, team => team.parentGroup) + @OneToMany(() => RunnerTeam, team => team.parentGroup, { nullable: true }) teams: RunnerTeam[]; } \ No newline at end of file diff --git a/src/models/RunnerTeam.ts b/src/models/RunnerTeam.ts index f12c5eb..5adc60d 100644 --- a/src/models/RunnerTeam.ts +++ b/src/models/RunnerTeam.ts @@ -14,6 +14,6 @@ export class RunnerTeam extends RunnerGroup { * Optional */ @IsNotEmpty() - @ManyToOne(() => RunnerOrganisation, org => org.teams) + @ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: true }) parentGroup?: RunnerOrganisation; } \ No newline at end of file diff --git a/src/models/Scan.ts b/src/models/Scan.ts index 055e94b..3fd64ed 100644 --- a/src/models/Scan.ts +++ b/src/models/Scan.ts @@ -25,7 +25,7 @@ export abstract class Scan { * The associated runner. */ @IsNotEmpty() - @ManyToOne(() => Runner, runner => runner.scans) + @ManyToOne(() => Runner, runner => runner.scans, { nullable: true }) runner: Runner; /** diff --git a/src/models/ScanStation.ts b/src/models/ScanStation.ts index fd30a34..c529b25 100644 --- a/src/models/ScanStation.ts +++ b/src/models/ScanStation.ts @@ -34,7 +34,7 @@ export class ScanStation { * The track this station is associated with. */ @IsNotEmpty() - @ManyToOne(() => Track, track => track.stations) + @ManyToOne(() => Track, track => track.stations, { nullable: true }) track: Track; /** @@ -56,6 +56,6 @@ export class ScanStation { /** * Used to link track scans to a scan station. */ - @OneToMany(() => TrackScan, scan => scan.track) + @OneToMany(() => TrackScan, scan => scan.track, { nullable: true }) scans: TrackScan[]; } diff --git a/src/models/Track.ts b/src/models/Track.ts index ce4693c..ad6f476 100644 --- a/src/models/Track.ts +++ b/src/models/Track.ts @@ -41,12 +41,12 @@ export class Track { /** * Used to link scan stations to track. */ - @OneToMany(() => ScanStation, station => station.track) + @OneToMany(() => ScanStation, station => station.track, { nullable: true }) stations: ScanStation[]; /** * Used to link track scans to a track. */ - @OneToMany(() => TrackScan, scan => scan.track) + @OneToMany(() => TrackScan, scan => scan.track, { nullable: true }) scans: TrackScan[]; } diff --git a/src/models/TrackScan.ts b/src/models/TrackScan.ts index ce35ee8..ae0467c 100644 --- a/src/models/TrackScan.ts +++ b/src/models/TrackScan.ts @@ -22,21 +22,21 @@ export class TrackScan extends Scan { * The associated track. */ @IsNotEmpty() - @ManyToOne(() => Track, track => track.scans) + @ManyToOne(() => Track, track => track.scans, { nullable: true }) track: Track; /** * The associated card. */ @IsNotEmpty() - @ManyToOne(() => RunnerCard, card => card.scans) + @ManyToOne(() => RunnerCard, card => card.scans, { nullable: true }) card: RunnerCard; /** * The scanning station. */ @IsNotEmpty() - @ManyToOne(() => ScanStation, station => station.scans) + @ManyToOne(() => ScanStation, station => station.scans, { nullable: true }) station: ScanStation; /** diff --git a/src/models/User.ts b/src/models/User.ts index fce74e8..3f9a1db 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -67,7 +67,7 @@ export class User { /** * permissions */ - @ManyToOne(() => Permission, permission => permission.users) + @ManyToOne(() => Permission, permission => permission.users, { nullable: true }) permissions: Permission[]; /** diff --git a/src/models/UserGroup.ts b/src/models/UserGroup.ts index ae014e2..c3b2f0c 100644 --- a/src/models/UserGroup.ts +++ b/src/models/UserGroup.ts @@ -23,7 +23,7 @@ export abstract class UserGroup { /** * permissions */ - @ManyToOne(() => Permission, permission => permission.groups) + @ManyToOne(() => Permission, permission => permission.groups, { nullable: true }) permissions: Permission[]; /**