diff options
| author | josh97josh | 2018-04-22 17:33:13 +0100 |
|---|---|---|
| committer | josh97josh | 2018-04-22 17:33:13 +0100 |
| commit | 68bd207cbf6e6d5bb1580841aa874c85e584752e (patch) | |
| tree | efb4c8699f86e8f23259410d6bfbd0aa95fc2cdc /src/db/model | |
| parent | Merge branch 'trh/gets' of ssh://localhost:2222/cs3099group-be-4/project-code... (diff) | |
| parent | added check permissions functions (diff) | |
| download | medicine-68bd207cbf6e6d5bb1580841aa874c85e584752e.tar.gz medicine-68bd207cbf6e6d5bb1580841aa874c85e584752e.zip | |
Merge branch ‘jl247/permissions’ into trh/gets
Diffstat (limited to 'src/db/model')
| -rw-r--r-- | src/db/model/ContributorGroup.ts | 14 | ||||
| -rw-r--r-- | src/db/model/Project.ts | 93 | ||||
| -rw-r--r-- | src/db/model/User.ts | 68 | ||||
| -rw-r--r-- | src/db/model/UserJoinsProject.ts | 7 |
4 files changed, 151 insertions, 31 deletions
diff --git a/src/db/model/ContributorGroup.ts b/src/db/model/ContributorGroup.ts index d3a5867..73bc558 100644 --- a/src/db/model/ContributorGroup.ts +++ b/src/db/model/ContributorGroup.ts @@ -1,12 +1,16 @@ -import { AllowNull, Column, DataType, HasMany, Model, Table +import { AllowNull, Column, DataType, HasMany, Model, Table, PrimaryKey } from 'sequelize-typescript'; import { default as UserJoinsProject } from './UserJoinsProject'; @Table export default class ContributorGroup extends Model<ContributorGroup> { - @AllowNull + @PrimaryKey @Column(DataType.TEXT) - public name!: string | null; + public name!: string; + + // @PrimaryKey + // @Column + // public username!: string; /* "Error: Foreign key for "ContributorGroup" * is missing on "UserJoinsProject"." */ @@ -31,6 +35,10 @@ export default class ContributorGroup extends Model<ContributorGroup> { @Column public canEditUserPermissions!: boolean; + @Column + public canDeleteProject!: boolean; + + @AllowNull @Column(DataType.TEXT) public description!: string | null; diff --git a/src/db/model/Project.ts b/src/db/model/Project.ts index 6cda34e..c937004 100644 --- a/src/db/model/Project.ts +++ b/src/db/model/Project.ts @@ -4,6 +4,7 @@ import { BelongsTo, BelongsToMany, Column, CreatedAt, DataType, ForeignKey, Model, PrimaryKey, Table, UpdatedAt } from 'sequelize-typescript'; import { default as User } from './User'; import { default as UserJoinsProject } from './UserJoinsProject'; +import { NotVoid } from 'lodash'; @Table export default class Project extends Model<Project> { @@ -91,34 +92,106 @@ export default class Project extends Model<Project> { public set adminMetadata(newMetadata: Metadata) { return; } + + public async hasAccessLevel(user:User, accessLevel: string): Promise<boolean>{ + const ujps: UserJoinsProject[] | null = await UserJoinsProject.findAll({ + include: [ ContributorGroup], + where: { + username: user.username, + projectName: this.name + } + }) + if(ujps != null){ + const adders:ContributorGroup[] = await ujps.map((ujp:UserJoinsProject): ContributorGroup =>{ + if (ujp.contributorGroup != null){ + return ujp.contributorGroup + } + else{ + throw new Error(`Missing contributor group`); + } + }) + if(adders.some((cg: ContributorGroup): boolean => cg.name == accessLevel)){ + return true; + } + + switch(accessLevel){ + case 'canCreateFiles': + return adders.some( + (cg: ContributorGroup): boolean => cg.canCreateFiles == true + ); + case 'canDeleteFiles': + return adders.some( + (cg: ContributorGroup): boolean => cg.canDeleteFiles == true + ); + case 'canViewFiles': + return adders.some( + (cg: ContributorGroup): boolean => cg.canViewFiles == true + ); + case 'canAddUsers': + return adders.some( + (cg: ContributorGroup): boolean => cg.canAddUsers == true + ); + case 'canRemoveUsers': + return adders.some( + (cg: ContributorGroup): boolean => cg.canRemoveUsers == true + ); + case 'canEditUserPermissions': + return adders.some( + (cg: ContributorGroup): boolean => cg.canEditUserPermissions == true + ); + case 'canDeleteProject': + return adders.some( + (cg: ContributorGroup): boolean => cg.canDeleteProject == true + ); + default: + throw new Error(`invalid access role`); + } + } + else{ + return(false) + } + } - public getAccessLevel(user: User): ContributorGroup { - throw new Error(`unimplemented, ${this}`); + public async getAccessLevel(user: User): Promise<String[]> { + const ujps: UserJoinsProject[] | null = await UserJoinsProject.findAll({ + include: [ ContributorGroup], + where: { + username: user.username, + projectName: this.name + } + }) + const listContributors: String[] = await ujps.map((ujp: UserJoinsProject): String => { + if(ujp.contributorGroup != undefined){ + return ujp.contributorGroupName + } + else{ + throw new Error(`Invalid access`); + } + }) + return listContributors; } - public getUserInfo(user: User): UserInfo { + public async getUserInfo(user: User): Promise<UserInfo> { return { username: user.username, - access_level: this.getAccessLevel(user) - .toString() + access_level: (await this.getAccessLevel(user)).join() }; } - public get fullInfo(): ProjectFullInfo { + public async getFullInfo(): Promise<ProjectFullInfo> { if (this.contributors === undefined) { throw new Error('contributors undefined'); } return { project_name: this.name, - users: this.contributors - .map((u: User): UserInfo => this.getUserInfo(u)), + users: await Promise.all(this.contributors + .map( (u: User): Promise<UserInfo> => this.getUserInfo(u))), public_metadata: { creation_date: this.creationDate }, - // TODO: Check if we have anything to use these for yet? private_metadata: {}, admin_metadata: {} - }; + } } } diff --git a/src/db/model/User.ts b/src/db/model/User.ts index 8c27012..2b67a34 100644 --- a/src/db/model/User.ts +++ b/src/db/model/User.ts @@ -5,6 +5,10 @@ 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'; @Table export default class User extends Model<User> { @@ -104,28 +108,59 @@ export default class User extends Model<User> { return; } - // TODO implement - public getAccessLevel(project: Project): string { - throw new Error(`unimplemented, ${this}`); - } - 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 getProjectInfo(project: Project): ProjectInfo { + public async getProjectInfo(project: Project): Promise<ProjectInfo> { return { project_name: project.name, - access_level: this.getAccessLevel(project) + access_level: await this.getAccessLevel(project) }; } - public get fullInfo(): UserFullInfo { + public async getFullInfo(): Promise<UserFullInfo> { // FIXME: Fetch these things if they aren't already present if (this.userGroups === undefined) { this.userGroups = []; @@ -137,8 +172,8 @@ export default class User extends Model<User> { privileges: this.userGroups! .filter((ug: UserGroup): boolean => ug.name !== null) .map((ug: UserGroup): string => ug.name as string), - projects : this.projects!.map( - (p: Project): ProjectInfo => this.getProjectInfo(p) + projects : await Promise.all( this.projects!.map( + (p: Project): Promise<ProjectInfo> => this.getProjectInfo(p)) ), // TODO: Check if we have anything to use these for yet? public_user_metadata: {}, @@ -155,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 { diff --git a/src/db/model/UserJoinsProject.ts b/src/db/model/UserJoinsProject.ts index 7ddb99e..7fe1bd6 100644 --- a/src/db/model/UserJoinsProject.ts +++ b/src/db/model/UserJoinsProject.ts @@ -14,13 +14,14 @@ export default class UserJoinsProject extends Model<UserJoinsProject> { @Column public projectName!: string; + @ForeignKey(() => ContributorGroup) + @Column + public contributorGroupName!: string; + @CreatedAt @Column(DataType.DATE) public joinDate!: Date; - @ForeignKey(() => ContributorGroup) - @Column - public contributorGroupId!: number; @BelongsTo(() => ContributorGroup) public contributorGroup?: ContributorGroup; |
