From fbef411bb1a0987a753024fc7822683fd3dc1df8 Mon Sep 17 00:00:00 2001 From: josh97josh Date: Mon, 16 Apr 2018 18:03:59 +0100 Subject: minor errors fixed --- src/FileRouter.ts | 2 ++ src/db/model/User.ts | 4 ++-- src/db/model/UserJoinsProject.ts | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 9a7dcac..ff8f5c1 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -367,6 +367,7 @@ const postCurUser: Middleware = async( const postUsername: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { + //if (req.query.action === 'create') let user: User | null | undefined = res.locals.user; res.locals.modified = true; if (user == null) { @@ -382,6 +383,7 @@ const postUsername: Middleware = async( if (req.body) { user.metadata = req.body; } + await user.save() next(); }; diff --git a/src/db/model/User.ts b/src/db/model/User.ts index 3a0ef26..b8dfcc0 100644 --- a/src/db/model/User.ts +++ b/src/db/model/User.ts @@ -13,7 +13,7 @@ export default class User extends Model { public username!: string; @Column - private readonly passwordInternal!: string; + private passwordInternal!: string; @BelongsToMany(() => UserGroup, () => UserHasPrivilege) public userGroups?: UserGroup[]; @@ -173,7 +173,7 @@ export default class User extends Model { 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..7ddb99e 100644 --- a/src/db/model/UserJoinsProject.ts +++ b/src/db/model/UserJoinsProject.ts @@ -2,7 +2,7 @@ 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'; +import { default as ContributorGroup } from './ContributorGroup'; @Table export default class UserJoinsProject extends Model { -- cgit v1.2.1 From c2f1e602e9f1e4483af0c8c2224a907475d2eb07 Mon Sep 17 00:00:00 2001 From: josh97josh Date: Wed, 18 Apr 2018 15:53:30 +0100 Subject: User privileges added on post --- src/FileRouter.ts | 71 ++++++++++++++++++++++++++++++++-------- src/db/model/ContributorGroup.ts | 4 +++ src/db/model/User.ts | 1 - src/index.ts | 71 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index ff8f5c1..746a13f 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -12,6 +12,8 @@ import { View, files, views } from './files'; import { logger } from './logger'; import { Property, default as serverConfig } from './serverConfig'; import { uuid } from './uuid'; +import UserHasPrivilege from './db/model/UserHasPrivilege'; +import { Contains } from 'sequelize-typescript'; // TODO: figure this out const BASE_FILE_STORAGE: string = 'TODO: figure this out'; @@ -367,23 +369,63 @@ const postCurUser: Middleware = async( const postUsername: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise => { - //if (req.query.action === 'create') let user: User | null | undefined = res.locals.user; res.locals.modified = true; - if (user == null) { - logger.debug('Adding new user'); - user = new User({ - username: req.params.username, - password: req.params.password - }); - } else { - logger.debug('Editing user'); - user.password = req.params.password; + + + 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 + } + }) + user.password = req.params.password; + if (req.body) { + user.metadata = req.body; + } + await user.save(); + await user.$set('userGroups', priv); + } + else{ + next(new RequestError(404, 'user_not_found')); + return; + } } - if (req.body) { - user.metadata = req.body; + else if( req.query.action === 'delete'){ + logger.debug('Deleting a user'); + if (user!= null){ + await user.destroy(); + } + else{ + next(new RequestError(404, 'user_not_found')); + return; + } + } + else{ + if (user == null) { + logger.debug('Adding new user'); + const priv: UserGroup[] | null = await UserGroup.findAll({ + include: [{all: true}], + where: { + name: req.body.privileges + } + }) + user = new User({ + username: req.params.username, + password: req.params.password, + }); + await user.save(); + await user.$set('userGroups', priv); + } + else{ + next(new RequestError(400, 'user_already_exists')); + return; + } } - await user.save() + next(); }; @@ -611,14 +653,17 @@ 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/ContributorGroup.ts b/src/db/model/ContributorGroup.ts index d3a5867..637fc05 100644 --- a/src/db/model/ContributorGroup.ts +++ b/src/db/model/ContributorGroup.ts @@ -31,6 +31,10 @@ export default class ContributorGroup extends Model { @Column public canEditUserPermissions!: boolean; + @Column + public canDeleteProject!: boolean; + + @AllowNull @Column(DataType.TEXT) public description!: string | null; diff --git a/src/db/model/User.ts b/src/db/model/User.ts index b8dfcc0..506614c 100644 --- a/src/db/model/User.ts +++ b/src/db/model/User.ts @@ -133,7 +133,6 @@ export default class User extends Model { } else if (this.projects === undefined) { this.projects = []; } - console.log("2") return { username : this.username, privileges: this.userGroups! diff --git a/src/index.ts b/src/index.ts index 7e21c35..0b2cec9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import { default as seq } from './db/orm'; import { default as User } from './db/model/User'; import { default as UserGroup } from './db/model/UserGroup'; +import { default as ContributorGroup } from './db/model/ContributorGroup'; const DEFAULT_PORT: number = 3000; @@ -59,8 +60,78 @@ logger.info(`Port ${port} chosen`); description: 'Allows Post to Log' }); + const userManagement: UserGroup = new UserGroup({ + name: 'userManagement', + canCreateUsers: true, + canDeleteUsers: true, + canEditUsers: true, + isInternal: false, + description: 'Allows management of other users' + }); + + const projectAdmin: UserGroup = new UserGroup({ + name: 'projectAdmin', + canDeleteProjects: true, + canEditProjects: true, + isInternal: false, + description: "Admin privileges on other projects " + }) + + const standardUser: UserGroup = new UserGroup({ + name: 'standardUser', + canCreateProjects: true, + isInternal: true, + description: "Allows creation of new projects" + }) + await logging.save(); await admin.save(); + await userManagement.save(); + await projectAdmin.save(); + await standardUser.save(); + + const projectOwner: ContributorGroup = new ContributorGroup({ + name: 'projectOwner', + canCreateFiles: true, + canAddUsers: true, + canRemoveUsers: true, + canDeleteFiles: true, + canDeleteProject: true, + canEditUserPermissions: true, + canViewFiles: true, + isInternal: false, + description: "Project Admin" + }); + + const standardProjectUser: ContributorGroup = new ContributorGroup({ + name: 'standardProjectUser', + canCreateFiles: true, + canViewFiles: true, + isInternal: false, + description: "Allows working on a project" + }); + + const readOnlyProjectUser: ContributorGroup = new ContributorGroup({ + name: 'readOnlyProjectUser', + canViewFiles: true, + isInternal: false, + description: "Allows viewing of files in a project " + }) + + const projectUserManagement: ContributorGroup = new ContributorGroup({ + name: 'projectUserManagement', + canAddUsers: true, + canEditUserPermissions: true, + canRemoveUsers: true, + isInternal: false, + description: "Allows managament of users in a Project" + }) + + await projectOwner.save(); + await standardProjectUser.save(); + await readOnlyProjectUser.save() + await projectUserManagement.save() + logger.info('Adding admin user'); -- cgit v1.2.1 From d201b19a53117afea306b0a2d96e28660f9f17d4 Mon Sep 17 00:00:00 2001 From: josh97josh Date: Thu, 19 Apr 2018 14:35:27 +0100 Subject: Project Owners added --- src/FileRouter.ts | 87 +++++++++++++++++++++++++++++++--------- src/db/model/ContributorGroup.ts | 12 ++++-- src/db/model/Project.ts | 24 +++++++++-- src/db/model/UserJoinsProject.ts | 7 ++-- 4 files changed, 101 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 746a13f..61f8262 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -14,6 +14,7 @@ import { Property, default as serverConfig } from './serverConfig'; import { uuid } from './uuid'; import UserHasPrivilege from './db/model/UserHasPrivilege'; import { Contains } from 'sequelize-typescript'; +import UserJoinsProject from './db/model/UserJoinsProject'; // TODO: figure this out const BASE_FILE_STORAGE: string = 'TODO: figure this out'; @@ -382,7 +383,9 @@ const postUsername: Middleware = async( name: req.body.privileges } }) - user.password = req.params.password; + if(user.password != null){ + user.password = req.params.password; + } if (req.body) { user.metadata = req.body; } @@ -537,25 +540,73 @@ const postProjectName: Middleware = async( let project: Project | null = res.locals.project; res.locals.modified = true; let promise: PromiseLike | null = null; - if (project == null) { + + if( req.query.action === 'update'){ logger.debug('Adding new project'); - const file: File = new File({ - uuid: uuid.generate(), - mimetype: 'inode/directory' - }); - promise = file.save(); - project = new Project({ - name: req.params.project_name, - rootFolder: file - }); - } else { - logger.debug('Updating project properties'); + if(project != null){ + project.metadata = req.body; + await project.save() + } + else{ + next(new RequestError(400, 'project_not_found')); + } + } - project.metadata = req.body; - // tslint:disable-next-line:await-promise - await promise; - await project.save(); - next(); + else if( req.query.action === 'delete'){ + if(project != null){ + project.destroy() + } + else{ + next(new RequestError(400, 'project_not_found')); + } + } + else if( req.query.action === 'update_grant'){ + + } + else{ + if(project == null){ + logger.debug('Adding new project'); + const file: File = new File({ + uuid: uuid.generate(), + mimetype: 'inode/directory' + }); + promise = file.save(); + project = new Project({ + 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 + await promise; + await project.save() + const contribute: UserJoinsProject = new UserJoinsProject({ + username: req.user.username, + projectName: req.params.project_name, + contributorGroupName: 'projectOwner' + + }) + contribute.save() + + console.log(contribute.contributorGroupName); + + // if (owner != undefined){ + // await contribute.$set('contributorGroupId', [owner]); + // } + } + else{ + next(new RequestError(400, 'project_already_exists')); + } + } + + next() }; /** diff --git a/src/db/model/ContributorGroup.ts b/src/db/model/ContributorGroup.ts index 637fc05..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 { - @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"." */ @@ -33,7 +37,7 @@ export default class ContributorGroup extends Model { @Column public canDeleteProject!: boolean; - + @AllowNull @Column(DataType.TEXT) diff --git a/src/db/model/Project.ts b/src/db/model/Project.ts index d57eaaa..41a70c9 100644 --- a/src/db/model/Project.ts +++ b/src/db/model/Project.ts @@ -88,15 +88,31 @@ export default class Project extends Model { return; } - public getAccessLevel(user: User): ContributorGroup { - throw new Error(`unimplemented, ${this}`); + public async getAccessLevel(user: User): Promise { + const ujps: UserJoinsProject[] | null = await UserJoinsProject.findAll({ + include: [ ContributorGroup], + where: { + username: user.username, + projectName: this.name + } + }) + const people: String[] = ujps.map((ujp: UserJoinsProject): String => { + if(ujp.contributorGroup != undefined){ + console.log(ujp.contributorGroupName) + return ujp.contributorGroupName + } + else{ + throw new Error(`unimplemented, ${this}`); + } + }) + //console.log(people); + return await people; } public getUserInfo(user: User): UserInfo { return { username: user.username, - access_level: this.getAccessLevel(user) - .toString() + access_level: this.getAccessLevel(user).toString() }; } 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 { @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; -- cgit v1.2.1 From 9ea66944ccb2c7c4dfa5f80bf1ef4637636d9a9f Mon Sep 17 00:00:00 2001 From: josh97josh Date: Fri, 20 Apr 2018 23:47:19 +0100 Subject: get project fixed --- src/FileRouter.ts | 9 +++++---- src/db/model/Project.ts | 20 +++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 0882272..dc57ab2 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -14,6 +14,7 @@ import { uuid } from './uuid'; import UserHasPrivilege from './db/model/UserHasPrivilege'; import { Contains } from 'sequelize-typescript'; import UserJoinsProject from './db/model/UserJoinsProject'; +import { appendFileSync } from 'fs'; /** @@ -481,9 +482,9 @@ const getProjects: Middleware = async( User ] }); - res.locals.data = projects.map( - (p: Project): ProjectFullInfo => p.fullInfo - ); + res.locals.data = Promise.all(projects.map( + (p: Project): Promise => p.getFullInfo() + )); next(); }; @@ -513,7 +514,7 @@ const getProjectName: Middleware = async( if (project == null) { return next(new RequestError(404, 'project_not_found')); } else { - res.locals.data = project.fullInfo; + res.locals.data = await project.getFullInfo(); next(); } }; diff --git a/src/db/model/Project.ts b/src/db/model/Project.ts index a7f1199..fa49fea 100644 --- a/src/db/model/Project.ts +++ b/src/db/model/Project.ts @@ -100,41 +100,39 @@ export default class Project extends Model { projectName: this.name } }) - const people: String[] = ujps.map((ujp: UserJoinsProject): String => { + const people: String[] = await ujps.map((ujp: UserJoinsProject): String => { if(ujp.contributorGroup != undefined){ - console.log(ujp.contributorGroupName) return ujp.contributorGroupName } else{ throw new Error(`unimplemented, ${this}`); } }) - //console.log(people); - return await people; + return people; } - public getUserInfo(user: User): UserInfo { + public async getUserInfo(user: User): Promise { 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 { if (this.contributors === undefined) { throw new Error('contributors undefined'); } + //console.log(await this.getUserInfo(this.contributors[0])) 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 => this.getUserInfo(u))), public_metadata: { creation_date: this.creationDate }, - // TODO: Check if we have anything to use these for yet? private_metadata: {}, admin_metadata: {} - }; + } } } -- cgit v1.2.1 From 2d1b8a8752419ac87efce76769db8a19cf8f9176 Mon Sep 17 00:00:00 2001 From: josh97josh Date: Sat, 21 Apr 2018 08:46:30 +0100 Subject: edited user functions --- src/FileRouter.ts | 61 ++++++++++++++++++++++++++++++++++++++-------------- src/db/model/User.ts | 18 ++++++++++------ 2 files changed, 56 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index dc57ab2..1bf562f 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -248,14 +248,14 @@ const getUsers: Middleware = async( Project ] }); - res.locals.data = users.map((u: User): UserFullInfo => { - const info: UserFullInfo = u.fullInfo; - if (!req.user.hasPrivilege('admin')) { - delete info.private_user_metadata; - delete info.private_admin_metadata; - } + res.locals.data = Promise.all(users.map((u: User): Promise => { + const info: Promise = u.getFullInfo(); return info; - }); + })); + if (!req.user.hasPrivilege('admin')) { + await delete res.locals.data.private_user_metadata; + await delete res.locals.data.private_admin_metadata; + } next(); }; @@ -295,24 +295,25 @@ const getUserProperties: Middleware = ( * "private_admin_metadata": metadata * } */ -const getUsername: Middleware = ( +const getUsername: Middleware = async ( req: Request, res: Response, next: NextFunction -): void => { +): Promise => { logger.debug(`Listing user ${res.locals.user}`); res.locals.modified = true; if (res.locals.user == null) { next(new RequestError(404, 'user_not_found')); return; } - res.locals.data = res.locals.user.fullInfo; + res.locals.data = await res.locals.user.getFullInfo(); + console.log(await res.locals.user.hasPrivilege("standardUser")) next(); }; -const getCurUser: Middleware = ( +const getCurUser: Middleware = async( req: Request, res: Response, next: NextFunction -): void => { +): Promise => { logger.debug('Listing current user'); - res.locals.data = req.user.fullInfo; + res.locals.data = await req.user.getFullInfo(); next(); }; @@ -370,7 +371,6 @@ const postUsername: Middleware = async( ): Promise => { let user: User | null | undefined = res.locals.user; res.locals.modified = true; - if( req.query.action === 'update'){ if (user!= null){ @@ -559,7 +559,36 @@ const postProjectName: Middleware = async( } } else if( req.query.action === 'update_grant'){ - + if (project == null){ + 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(); + } + + if(req.body.access_level != 'none'){ + try{ + let ujp: UserJoinsProject = new UserJoinsProject({ + username: req.body.username, + projectName: project.name, + contributorGroupName: req.body.access_level + }) + await ujp.save(); + } + catch(err){ + next(new RequestError(400, 'invalid_access_level')); + } + } + + } } else{ if(project == null){ @@ -889,7 +918,7 @@ export class FileRouter { } }); if (user == null) { - return next(new RequestError(404, 'user_not_found')); + //return next(new RequestError(404, 'user_not_found')); } else { res.locals.user = user; } diff --git a/src/db/model/User.ts b/src/db/model/User.ts index 8c27012..581a887 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 ContributorGroup from './ContributorGroup'; +import { truncateFile } from '../../files'; @Table export default class User extends Model { @@ -105,8 +107,10 @@ export default class User extends Model { } // TODO implement - public getAccessLevel(project: Project): string { - throw new Error(`unimplemented, ${this}`); + public async getAccessLevel(project: Project): Promise { + + return (await project.getAccessLevel(this)).join() + } public hasPrivilege(privilege: string): boolean { @@ -118,14 +122,14 @@ export default class User extends Model { ); } - public getProjectInfo(project: Project): ProjectInfo { + public async getProjectInfo(project: Project): Promise { return { project_name: project.name, - access_level: this.getAccessLevel(project) + access_level: await this.getAccessLevel(project) }; } - public get fullInfo(): UserFullInfo { + public async getFullInfo(): Promise { // FIXME: Fetch these things if they aren't already present if (this.userGroups === undefined) { this.userGroups = []; @@ -137,8 +141,8 @@ export default class User extends Model { 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 => this.getProjectInfo(p)) ), // TODO: Check if we have anything to use these for yet? public_user_metadata: {}, -- cgit v1.2.1 From 0ee0b1e848d3c8455d615c6d942e1b013f767b0f Mon Sep 17 00:00:00 2001 From: josh97josh Date: Sun, 22 Apr 2018 13:30:49 +0100 Subject: added check permissions functions --- src/FileRouter.ts | 59 ++++++++++++++++--------------------------- src/db/model/Project.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++++--- src/db/model/User.ts | 59 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 131 insertions(+), 54 deletions(-) (limited to 'src') 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 { @@ -91,6 +92,65 @@ export default class Project extends Model { public set adminMetadata(newMetadata: Metadata) { return; } + + public async hasAccessLevel(user:User, accessLevel: string): Promise{ + 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 { const ujps: UserJoinsProject[] | null = await UserJoinsProject.findAll({ @@ -100,15 +160,15 @@ export default class Project extends Model { 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 { @@ -122,7 +182,6 @@ export default class Project extends Model { 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 { return; } - // TODO implement - public async getAccessLevel(project: Project): Promise { - - 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 { + return (await project.getAccessLevel(this)).join() } public async getProjectInfo(project: Project): Promise { @@ -159,14 +190,17 @@ export default class User extends Model { }): 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 { public set password(newPassword: string) { // TODO use salting this.passwordInternal = newPassword; + console.log(newPassword); } } -- cgit v1.2.1