diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/FileRouter.ts | 71 | ||||
| -rw-r--r-- | src/db/model/ContributorGroup.ts | 4 | ||||
| -rw-r--r-- | src/db/model/User.ts | 1 | ||||
| -rw-r--r-- | src/index.ts | 71 |
4 files changed, 133 insertions, 14 deletions
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<void> => { - //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<ContributorGroup> { @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<User> { } 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'); |
