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 | |
| parent | edited user functions (diff) | |
| download | medicine-0ee0b1e848d3c8455d615c6d942e1b013f767b0f.tar.gz medicine-0ee0b1e848d3c8455d615c6d942e1b013f767b0f.zip | |
added check permissions functions
| -rw-r--r-- | src/FileRouter.ts | 59 | ||||
| -rw-r--r-- | src/db/model/Project.ts | 67 | ||||
| -rw-r--r-- | src/db/model/User.ts | 59 |
3 files changed, 131 insertions, 54 deletions
diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 1bf562f..5b8f98e 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -305,7 +305,6 @@ const getUsername: Middleware = async ( return; } res.locals.data = await res.locals.user.getFullInfo(); - console.log(await res.locals.user.hasPrivilege("standardUser")) next(); }; @@ -375,20 +374,23 @@ const postUsername: Middleware = async( if( req.query.action === 'update'){ if (user!= null){ logger.debug('Editing user'); - const priv: UserGroup[] | null = await UserGroup.findAll({ - include: [{all: true}], - where: { - name: req.body.privileges - } - }) - if(user.password != null){ - user.password = req.params.password; + + if(req.body.password != null){ + user.password = req.body.password; } if (req.body) { user.metadata = req.body; } await user.save(); - await user.$set('userGroups', priv); + if(req.body.privileges != null){ + const priv: UserGroup[] | null = await UserGroup.findAll({ + include: [{all: true}], + where: { + name: req.body.privileges + } + }) + await user.$set('userGroups', priv); + } } else{ next(new RequestError(404, 'user_not_found')); @@ -548,7 +550,6 @@ const postProjectName: Middleware = async( else{ next(new RequestError(400, 'project_not_found')); } - } else if( req.query.action === 'delete'){ if(project != null){ @@ -563,17 +564,16 @@ const postProjectName: Middleware = async( next(new RequestError(400, 'project_not_found')); } else{ - let ujp: UserJoinsProject | null = await UserJoinsProject.findOne({ - include: [{all:true}], - where: { - username: req.body.username, - projectName: project.name - } - }) - if(ujp != null){ - await ujp.destroy(); + let ujp: UserJoinsProject | null = await UserJoinsProject.findOne({ + include: [{all:true}], + where: { + username: req.body.username, + projectName: project.name } - + }) + if(ujp != null){ + await ujp.destroy(); + } if(req.body.access_level != 'none'){ try{ let ujp: UserJoinsProject = new UserJoinsProject({ @@ -587,7 +587,6 @@ const postProjectName: Middleware = async( next(new RequestError(400, 'invalid_access_level')); } } - } } else{ @@ -602,13 +601,6 @@ const postProjectName: Middleware = async( name: req.params.project_name, rootFolder: file }); - const owner: ContributorGroup | null = await ContributorGroup.findOne({ - include: [{all: true}], - where: { - name: 'projectOwner' - } - }) - //console.log(owner); project.metadata = req.body; // tslint:disable-next-line:await-promise @@ -621,12 +613,6 @@ const postProjectName: Middleware = async( }) contribute.save() - - console.log(contribute.contributorGroupName); - - // if (owner != undefined){ - // await contribute.$set('contributorGroupId', [owner]); - // } } else{ next(new RequestError(400, 'project_already_exists')); @@ -874,17 +860,14 @@ export class FileRouter { this.router.get ('/users', getUsers); this.router.get ('/users/:username', getUsername); this.router.post('/users/:username', postUsername); - // create, update, delete this.router.get ('/users/:username/properties', getUserProperties); this.router.get ('/current_user', getCurUser); this.router.post('/current_user', postCurUser); - // update // Projects this.router.get ('/project_roles', getProjectRoles); this.router.get ('/projects', getProjects); this.router.get ('/projects/:project_name', getProjectName); this.router.post('/projects/:project_name', postProjectName); - // create, update, delete, update_grant this.router.get ('/projects/:project_name/properties', getProjectProperties); // File access this.router.get ('/projects/:project_name/files/:path', getFilePath); diff --git a/src/db/model/Project.ts b/src/db/model/Project.ts index fa49fea..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,6 +92,65 @@ 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 async getAccessLevel(user: User): Promise<String[]> { const ujps: UserJoinsProject[] | null = await UserJoinsProject.findAll({ @@ -100,15 +160,15 @@ export default class Project extends Model<Project> { projectName: this.name } }) - const people: String[] = await ujps.map((ujp: UserJoinsProject): String => { + const listContributors: String[] = await ujps.map((ujp: UserJoinsProject): String => { if(ujp.contributorGroup != undefined){ return ujp.contributorGroupName } else{ - throw new Error(`unimplemented, ${this}`); + throw new Error(`Invalid access`); } }) - return people; + return listContributors; } public async getUserInfo(user: User): Promise<UserInfo> { @@ -122,7 +182,6 @@ export default class Project extends Model<Project> { if (this.contributors === undefined) { throw new Error('contributors undefined'); } - //console.log(await this.getUserInfo(this.contributors[0])) return { project_name: this.name, users: await Promise.all(this.contributors 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); } } |
