@ -13,6 +13,16 @@ class v1 {
async main ( req , res ) {
async main ( req , res ) {
const projects = await Project . find ( { archived : false , user _id : req . session . auth . uuid } )
const projects = await Project . find ( { archived : false , user _id : req . session . auth . uuid } )
let find = {
shared _user _ids : {
$elemMatch : {
$eq : req . session . auth . uuid
}
}
}
const shared _projects = await Project . find ( find )
/ *
/ *
* Return the main view .
* Return the main view .
@ -20,7 +30,7 @@ class v1 {
* View parameters can be passed as an optional third
* View parameters can be passed as an optional third
* argument to the view ( ) method .
* argument to the view ( ) method .
* /
* /
return _flitter . view ( res , 'dash_v1:main' , { projects } )
return _flitter . view ( res , 'dash_v1:main' , { projects , shared _projects , user : req . session . auth . user } )
}
}
new _project _show ( req , res , next ) {
new _project _show ( req , res , next ) {
@ -30,20 +40,30 @@ class v1 {
async project _edit _show ( req , res , next ) {
async project _edit _show ( req , res , next ) {
const project = await Project . findById ( req . params . id )
const project = await Project . findById ( req . params . id )
if ( ! project ) {
if ( ! project ) {
return _flitter . error ( res , 404 , 'Project not found with the specified ID.' )
return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
}
// check access perms
if ( ! ( project . user _id === req . session . auth . uuid ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permissions to edit this project.' } )
}
}
return _flitter . view ( res , 'dash_v1:project' , { show _back : true , title : 'Update Project' , project _name : project . name } )
return _flitter . view ( res , 'dash_v1:project' , { show _back : true , title : 'Update Project' , project _name : project . name , user : req . session . auth . user })
}
}
async project _edit _do ( req , res , next ) {
async project _edit _do ( req , res , next ) {
const project = await Project . findById ( req . params . id )
const project = await Project . findById ( req . params . id )
if ( ! project ) {
if ( ! project ) {
return _flitter . error ( res , 404 , 'Project not found with the specified ID.' )
return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
}
}
if ( ! req . body || ! req . body . name ) {
if ( ! req . body || ! req . body . name ) {
return _flitter . view ( res , 'dash_v1:project' , { show _back : true , title : 'Update Project' , project _name : project . name , errors : [ 'Project name is required.' ] } )
return _flitter . view ( res , 'dash_v1:project' , { user : req . session . auth . user , show _back : true , title : 'Update Project' , project _name : project . name , errors : [ 'Project name is required.' ] } )
}
// check access perms
if ( ! ( project . user _id === req . session . auth . uuid ) ) {
return _flitter . error ( res , 401 , { reason : 'Project not found with the specified ID.' } )
}
}
project . name = req . body . name
project . name = req . body . name
@ -54,7 +74,7 @@ class v1 {
async new _project _do ( req , res , next ) {
async new _project _do ( req , res , next ) {
if ( ! req . body . name ) {
if ( ! req . body . name ) {
return _flitter . view ( res , 'dash_v1:project' , { show_back : true , title : 'Create Project' , errors : [ 'Project name is required.' ] } )
return _flitter . view ( res , 'dash_v1:project' , { user: req . session . auth . user , show_back : true , title : 'Create Project' , errors : [ 'Project name is required.' ] } )
}
}
const project = new Project ( {
const project = new Project ( {
@ -76,31 +96,78 @@ class v1 {
const project = await Project . findById ( req . params . id )
const project = await Project . findById ( req . params . id )
if ( ! project ) {
if ( ! project ) {
_flitter . error ( res , 404 , 'Project not found .')
return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID .'} )
}
}
const outs = await Out . find ( { project _id : project . id } ) . sort ( '-created' )
const outs = await Out . find ( { project _id : project . id } ) . sort ( '-created' )
return _flitter . view ( res , 'dash_v1:view' , { project , outs , show _back : true , title : 'View: ' + project . name } )
if ( ! ( project . user _id === req . session . auth . uuid ) && ! ( project . shared _user _ids . includes ( req . session . auth . uuid ) ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to view this project.' } )
}
return _flitter . view ( res , 'dash_v1:view' , { user : req . session . auth . user , project , outs , show _back : true , title : 'View: ' + project . name } )
}
}
async out _view ( req , res , next ) {
async out _view ( req , res , next ) {
const out = await Out . findById ( req . params . id )
const out = await Out . findById ( req . params . id )
if ( ! out ) {
return _flitter . error ( res , 404 , { reason : 'Output not found with the specified ID.' } )
}
let pretty
try {
pretty = JSON . stringify ( JSON . parse ( out . data ) , null , 4 )
}
catch ( e ) {
return _flitter . error ( res , 500 , { reason : 'Unable to parse output data. Data contains invalid JSON.' } )
}
const project = await Project . findById ( out . project _id )
console . log ( out . data )
if ( ! project || ( ! ( project . user _id === req . session . auth . uuid ) && ! ( project . shared _user _ids . includes ( req . session . auth . uuid ) ) ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to view this project.' } )
const pretty = JSON . stringify ( JSON . parse ( out . data ) , null , 4 )
}
// TODO permission access check
// TODO permission access check
return _flitter . view ( res , 'dash_v1:out' , { out , prettyd : pretty , show _back : true , title : out . brief , title _small : true } ) ;
return _flitter . view ( res , 'dash_v1:out' , { user: req . session . auth . user , out, prettyd : pretty , show _back : true , title : out . brief , title _small : true } ) ;
}
}
async out _delete ( req , res , next ) {
const out = await Out . findById ( req . params . id )
project _delete _show ( req , res , next ) {
const project = await Project . findById ( req . params . project )
return _flitter . view ( res , 'dash_v1:confirm' , { show _back : true , title : 'Are you sure?' , text : 'Deleting this project will remove all stored breakpoint data. This action cannot be undone.' , destination : '/dash/v1/project/delete/' + req . params . id } )
if ( ! project || ( ! ( project . user _id === req . session . auth . uuid ) ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
}
if ( out ) {
await out . delete ( )
}
return res . redirect ( '/dash/v1/project/view/' + req . params . project )
}
async project _delete _show ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) {
return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
}
if ( ! ( project . user _id === req . session . auth . uuid ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
}
return _flitter . view ( res , 'dash_v1:confirm' , { user : req . session . auth . user , project , show _back : true , title : 'Are you sure?' , text : 'Deleting this project will remove all stored breakpoint data. This action cannot be undone.' , destination : '/dash/v1/project/delete/' + req . params . id } )
}
}
async project _delete _do ( req , res , next ) {
async project _delete _do ( req , res , next ) {
const project = await Project . findById ( req . params . id )
const project = await Project . findById ( req . params . id )
if ( project && ( ! ( project . user _id === req . session . auth . uuid ) ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
}
if ( project ) {
if ( project ) {
const outs = await Out . find ( { project _id : project . id } )
const outs = await Out . find ( { project _id : project . id } )
@ -113,6 +180,90 @@ class v1 {
return res . redirect ( '/dash/v1' )
return res . redirect ( '/dash/v1' )
}
}
view _code ( req , res , next ) {
return _flitter . view ( res , 'dash_v1:code' , { user : req . session . auth . user , title : 'Inline Code Snippets' } )
}
async project _share _show ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
if ( ! ( project . user _id === req . session . auth . uuid ) ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
let find = {
uuid : { $nin : [ ] }
}
find . uuid . $nin . push ( req . session . auth . uuid )
find . uuid . $nin = find . uuid . $nin . concat ( project . shared _user _ids )
const to _share = await _flitter . model ( 'User' ) . find ( find )
find = {
uuid : { $in : find . uuid . $nin }
}
const shared = await _flitter . model ( 'User' ) . find ( find )
return _flitter . view ( res , 'dash_v1:share' , { user : req . session . auth . user , sharing : { to _share , shared } , project , title : 'Share Project: ' + project . name , show _back : true } )
}
async project _share _do ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
const target _user = await _flitter . model ( 'User' ) . findOne ( { uuid : req . params . user } )
if ( ! target _user ) return _flitter . error ( res , 404 , { reason : 'User not found with the specified ID.' } )
if ( ! ( project . user _id === req . session . auth . uuid ) ) return _flitter . error ( res , 401 , { reason : "You do not have permission to edit this project." } )
if ( ! ( project . user _id === target _user . uuid ) && ! ( project . shared _user _ids . includes ( target _user . uuid ) ) ) {
project . shared _user _ids . push ( target _user . uuid )
await project . save ( )
}
return res . redirect ( '/dash/v1/project/share/' + project . id )
}
async project _share _revoke ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
const target _user = await _flitter . model ( 'User' ) . findOne ( { uuid : req . params . user } )
if ( ! target _user ) return _flitter . error ( res , 404 , { reason : 'User not found with the specified ID.' } )
if ( ! ( project . user _id === req . session . auth . uuid || project . shared _user _ids . includes ( req . session . auth . uuid ) ) ) return _flitter . error ( res , 401 , { reason : "You do not have permission to edit this project." } )
const to _dash = project . shared _user _ids . includes ( req . session . auth . uuid )
if ( ! ( target _user . uuid === project . user _id ) && ( project . shared _user _ids . includes ( target _user . uuid ) ) ) {
project . shared _user _ids . splice ( project . shared _user _ids . indexOf ( target _user . uuid ) , 1 )
await project . save ( )
}
if ( to _dash ) return res . redirect ( '/dash/v1' )
return res . redirect ( '/dash/v1/project/share/' + project . id )
}
async project _share _transfer ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
const target _user = await _flitter . model ( 'User' ) . findOne ( { uuid : req . params . user } )
if ( ! target _user ) return _flitter . error ( res , 404 , { reason : 'User not found with the specified ID.' } )
if ( ! project . user _id === req . session . auth . uuid ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
project . user _id = target _user . uuid
project . shared _user _ids . push ( req . session . auth . uuid )
await project . save ( )
return res . redirect ( '/dash/v1' )
}
}
}
module . exports = exports = v1
module . exports = exports = v1