diff options
| author | josh97josh | 2018-04-22 13:30:49 +0100 |
|---|---|---|
| committer | josh97josh | 2018-04-22 13:30:49 +0100 |
| commit | 0ee0b1e848d3c8455d615c6d942e1b013f767b0f (patch) | |
| tree | dfa2c67c7645ec6f0802e2badcef78f3fe77abf9 /src/db/model/User.ts | |
| parent | edited user functions (diff) | |
| download | medicine-0ee0b1e848d3c8455d615c6d942e1b013f767b0f.tar.gz medicine-0ee0b1e848d3c8455d615c6d942e1b013f767b0f.zip | |
added check permissions functions
Diffstat (limited to 'src/db/model/User.ts')
| -rw-r--r-- | src/db/model/User.ts | 59 |
1 files changed, 47 insertions, 12 deletions
diff --git a/src/db/model/User.ts b/src/db/model/User.ts index 581a887..a12ba50 100644 --- a/src/db/model/User.ts +++ b/src/db/model/User.ts @@ -5,6 +5,8 @@ import { BelongsToMany, Column, CreatedAt, DataType, HasMany, import { default as UserGroup } from './UserGroup'; import { default as UserHasPrivilege } from './UserHasPrivilege'; import { default as UserJoinsProject } from './UserJoinsProject'; +import { RequestError } from '../../errors/errorware'; + import ContributorGroup from './ContributorGroup'; import { truncateFile } from '../../files'; @@ -106,20 +108,49 @@ export default class User extends Model<User> { return; } - // TODO implement - public async getAccessLevel(project: Project): Promise<string> { - - return (await project.getAccessLevel(this)).join() - - } - public hasPrivilege(privilege: string): boolean { if (this.userGroups === undefined) { throw new Error('usergroups undefined'); } - return this.userGroups.some( - (ug: UserGroup): boolean => ug.name === privilege - ); + if(this.userGroups.some((ug: UserGroup): boolean => ug.name == privilege)){ + return true; + } + switch(privilege){ + case 'canCreateUsers': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canCreateUsers == true + ); + case 'canDeleteUsers': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canDeleteUsers == true + ); + case 'canEditUsers': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canEditUsers == true + ); + case 'canCreateProjects': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canCreateProjects == true + ); + case 'canDeleteProjects': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canDeleteProjects == true + ); + case 'canEditProjects': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canEditProjects == true + ); + case 'canAccessLogs': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canAccessLogs == true + ); + default: + throw new Error('privilege undefined'); + } + } + + public async getAccessLevel(project: Project): Promise<string> { + return (await project.getAccessLevel(this)).join() } public async getProjectInfo(project: Project): Promise<ProjectInfo> { @@ -159,14 +190,17 @@ export default class User extends Model<User> { }): void { if (newInfo.password !== undefined) { if (this.authenticate(newInfo.password.old)) { - this.password = newInfo.password.new; + this.passwordInternal = newInfo.password.new; + } + else{ + throw new RequestError(400, 'invalid_password') } } } public authenticate(password: string): boolean { // TODO use salting (and maybe even constant-time comparison?) - return password === this.password; + return password === this.passwordInternal; } public get password(): string { @@ -176,6 +210,7 @@ export default class User extends Model<User> { public set password(newPassword: string) { // TODO use salting this.passwordInternal = newPassword; + console.log(newPassword); } } |
