diff options
| author | Tom Harley | 2018-04-22 22:30:54 +0100 |
|---|---|---|
| committer | Tom Harley | 2018-04-22 22:31:23 +0100 |
| commit | 1b2fe66acf4041728dcb772ee9638b6b79f34d1a (patch) | |
| tree | ecb03f6551e35336f5a654f3b3257435cbadfb6d /src/db | |
| parent | Edit file uploads to use raw data instead of forms (diff) | |
| parent | fixxed tests (diff) | |
| download | medicine-1b2fe66acf4041728dcb772ee9638b6b79f34d1a.tar.gz medicine-1b2fe66acf4041728dcb772ee9638b6b79f34d1a.zip | |
Merge branch ‘trh/gets’ of gitlab.cs.st-andrews.ac.uk:cs3099group-be-4/project-code into trh/gets
Diffstat (limited to 'src/db')
| -rw-r--r-- | src/db/.DS_Store | bin | 0 -> 6148 bytes | |||
| -rw-r--r-- | src/db/model/ContributorGroup.ts | 13 | ||||
| -rw-r--r-- | src/db/model/Project.ts | 88 | ||||
| -rw-r--r-- | src/db/model/User.ts | 73 | ||||
| -rw-r--r-- | src/db/model/UserJoinsProject.ts | 10 |
5 files changed, 149 insertions, 35 deletions
diff --git a/src/db/.DS_Store b/src/db/.DS_Store Binary files differnew file mode 100644 index 0000000..6a62f95 --- /dev/null +++ b/src/db/.DS_Store diff --git a/src/db/model/ContributorGroup.ts b/src/db/model/ContributorGroup.ts index d3a5867..da7243d 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, PrimaryKey, Table } 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,9 @@ 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..7b30d56 100644 --- a/src/db/model/Project.ts +++ b/src/db/model/Project.ts @@ -1,5 +1,6 @@ import { default as ContributorGroup } from './ContributorGroup'; import { default as File } from './File'; +import { NotVoid } from 'lodash'; import { BelongsTo, BelongsToMany, Column, CreatedAt, DataType, ForeignKey, Model, PrimaryKey, Table, UpdatedAt } from 'sequelize-typescript'; import { default as User } from './User'; @@ -92,30 +93,99 @@ export default class Project extends Model<Project> { return; } - public getAccessLevel(user: User): ContributorGroup { - throw new Error(`unimplemented, ${this}`); + 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 + ); + case 'canDeleteFiles': + return adders.some( + (cg: ContributorGroup): boolean => cg.canDeleteFiles + ); + case 'canViewFiles': + return adders.some( + (cg: ContributorGroup): boolean => cg.canViewFiles + ); + case 'canAddUsers': + return adders.some( + (cg: ContributorGroup): boolean => cg.canAddUsers + ); + case 'canRemoveUsers': + return adders.some( + (cg: ContributorGroup): boolean => cg.canRemoveUsers + ); + case 'canEditUserPermissions': + return adders.some( + (cg: ContributorGroup): boolean => cg.canEditUserPermissions + ); + case 'canDeleteProject': + return adders.some( + (cg: ContributorGroup): boolean => cg.canDeleteProject + ); + default: + throw new Error('invalid access role'); + } + } else { + return(false); + } + } + + 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 76e055d..3c0abe2 100644 --- a/src/db/model/User.ts +++ b/src/db/model/User.ts @@ -1,3 +1,4 @@ +import { RequestError } from '../../errors/errorware'; import { default as File } from './File'; import { default as Project } from './Project'; import { BelongsToMany, Column, CreatedAt, DataType, HasMany, @@ -6,6 +7,9 @@ import { default as UserGroup } from './UserGroup'; import { default as UserHasPrivilege } from './UserHasPrivilege'; import { default as UserJoinsProject } from './UserJoinsProject'; +import ContributorGroup from './ContributorGroup'; +import { truncateFile } from '../../files'; + @Table export default class User extends Model<User> { @PrimaryKey @@ -13,7 +17,7 @@ export default class User extends Model<User> { public username!: string; @Column - private readonly passwordInternal!: string; + private passwordInternal!: string; @BelongsToMany(() => UserGroup, () => UserHasPrivilege) public userGroups?: UserGroup[]; @@ -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 + ); + case 'canDeleteUsers': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canDeleteUsers + ); + case 'canEditUsers': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canEditUsers + ); + case 'canCreateProjects': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canCreateProjects + ); + case 'canDeleteProjects': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canDeleteProjects + ); + case 'canEditProjects': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canEditProjects + ); + case 'canAccessLogs': + return this.userGroups.some( + (ug: UserGroup): boolean => ug.canAccessLogs + ); + 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: {}, @@ -154,15 +189,17 @@ export default class User extends Model<User> { private_user_metadata?: Metadata; }): void { if (newInfo.password !== undefined) { - if (this.authenticate(newInfo.password.old)) { - this.password = newInfo.password.new; + if (!this.authenticate(newInfo.password.old)) { + 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 { @@ -171,7 +208,7 @@ export default class User extends Model<User> { public set password(newPassword: string) { // TODO use salting - this.password = newPassword; + this.passwordInternal = newPassword; } } diff --git a/src/db/model/UserJoinsProject.ts b/src/db/model/UserJoinsProject.ts index 0948517..c2aa33a 100644 --- a/src/db/model/UserJoinsProject.ts +++ b/src/db/model/UserJoinsProject.ts @@ -1,8 +1,8 @@ +import { default as ContributorGroup } from './ContributorGroup'; import { default as Project } from './Project'; import { BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table } from 'sequelize-typescript'; import { default as User } from './User'; -import { default as ContributorGroup } from './UserGroup'; @Table export default class UserJoinsProject extends Model<UserJoinsProject> { @@ -14,14 +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; } |
