This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added 馃殾 permission checks on *some* routes, tested 50%
- Loading branch information
1 parent
cd97289
commit add9733
Showing
11 changed files
with
161 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,4 @@ npm-debug.log | |
|
||
# keep only the data folder | ||
data/* | ||
!data/.gitkeep | ||
|
||
!data/.gitkeep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
const winston = require( 'winston' ) | ||
const chalk = require( 'chalk' ) | ||
const url = require( 'url' ) | ||
const User = require( '../../../models/User' ) | ||
|
||
module.exports = ( user, operation, resource ) => { | ||
return new Promise( ( resolve, reject ) => { | ||
|
||
if ( user == null ) user = { role: 'guest', _id: '' } | ||
winston.debug( chalk.bgRed( 'checking perms' ), user.role, user._id.toString( ) ) | ||
|
||
// admin or owner | ||
if ( user.role === 'admin' || user._id.toString( ) === resource.owner.toString( ) ) { | ||
winston.debug( chalk.bgGreen( 'checking perms' ), 'user is admin or owner' ) | ||
return resolve( ) | ||
} | ||
|
||
if ( operation == null ) { | ||
winston.debug( chalk.bgRed( 'checking perms' ), 'no operation specified' ) | ||
return reject( new Error( "You are not authorised." ) ) | ||
} | ||
|
||
switch ( operation ) { | ||
case 'write': | ||
if ( resource.canWrite.indexOf( user._id ) >= 0 ) { | ||
winston.debug( chalk.bgGreen( 'checking perms' ), `user has ${operation} access` ) | ||
return resolve( ) | ||
} | ||
winston.debug( chalk.bgRed( 'checking perms' ), `user has NO ${operation} access` ) | ||
return reject( new Error( "You are not authorised." ) ) | ||
|
||
case 'read': | ||
if ( !resource.private ) { | ||
winston.debug( chalk.bgGreen( 'checking perms' ), `${operation} ok, resource is public` ) | ||
return resolve( ) | ||
} | ||
if ( resource.canWrite.indexOf( user._id ) >= 0 ) { | ||
winston.debug( chalk.bgGreen( 'checking perms' ), `user has write & ${operation} access` ) | ||
return resolve( ) | ||
} | ||
if ( resource.canRead.indexOf( user._id ) >= 0 ) { | ||
winston.debug( chalk.bgGreen( 'checking perms' ), `user has ${operation} access` ) | ||
return resolve( ) | ||
} | ||
winston.debug( chalk.bgRed( 'checking perms' ), `user has NO ${operation} access` ) | ||
return reject( new Error( "You are not authorised." ) ) | ||
|
||
default: | ||
winston.debug( chalk.bgRed( 'checking perms' ), `operation ${operation} not defined` ) | ||
return reject( new Error( "You are not authorised." ) ) | ||
} | ||
} ) | ||
} |
Oops, something went wrong.