Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

Commit

Permalink
[master] 8.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Michaelpalacce authored and Michaelpalacce committed Mar 15, 2020
1 parent 4152a1d commit 13e0baf
Show file tree
Hide file tree
Showing 23 changed files with 386 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ UPLOADS_DIR=/Uploads
ADMIN_USERNAME=root
ADMIN_PASSWORD=toor
DEBUG=1
SECURITY_ENABLED=0
SECURITY_ENABLED=1
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ https://github.com/Michaelpalacce/Server/releases
- DEBUG - Whether to write logs on a debug level 1 or 0
- Others should probably not be touched

#Adding users
- If you want to add users go to the users page from the sidebar and click on the Add Users button
- You will be asked to fill in the new user's data
- Permissions currently does not work
- Route will be the path from which the user can access the FS
- When adding a user the user will persist after 5 seconds so don't stop the server

#Known Bugs:
- When handling big folders and uploading at the same time, may display some items twice or fail loading

Expand Down
5 changes: 5 additions & 0 deletions UPDATELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
8.10.0
- Added ability ot add and delete users
- Updating users is not possible yet
- Users will have different route permissions

8.9.1
- Forgot to update event_request

Expand Down
2 changes: 2 additions & 0 deletions handlers/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ require( './folder/controller/upload' );
require( './folder/controller/move' );
require( './user/controller/list' );
require( './user/controller/add' );
require( './user/controller/get' );
require( './user/controller/delete' );
4 changes: 1 addition & 3 deletions handlers/folder/controller/browse.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,5 @@ app.get( '/browse/getFiles', ( event )=>{
const { items, position, hasMore } = data;

event.send( { items, position, dir, hasMore } )
}).catch(()=>{
event.redirect( event.headers.referer );
});
}).catch( event.next );
} );
4 changes: 3 additions & 1 deletion handlers/main/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ app.add(( event )=>{

if ( ! userManager.has( process.env.ADMIN_USERNAME ) )
{
userManager.set( process.env.ADMIN_USERNAME, {
userManager.set( {
username : process.env.ADMIN_USERNAME,
password : process.env.ADMIN_PASSWORD,
isSU : true,
permissions : [],
Expand Down Expand Up @@ -79,6 +80,7 @@ if ( process.env.SECURITY_ENABLED == true )
event.session.add( 'username', user.getUsername() );
event.session.add( 'route', user.getRoute() );
event.session.add( 'authenticated', true );
event.session.add( 'SU', user.isSuperUser() );

event.redirect( '/' );
}
Expand Down
28 changes: 27 additions & 1 deletion handlers/main/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@ class User
{
this.username = userData.username || '';
this.password = userData.password || '';
this.isSU = userData.isSU || false;
this.isSU = userData.isSU === 'true' || userData.isSU === true;
this.route = userData.route || '/';
this.permissions = userData.permissions || [];
}

/**
* @brief Gets the user in an object
*
* @return Object
*/
getUserData()
{
return {
username : this.username,
password : this.password,
isSU : this.isSU,
route : this.route,
permissions : this.permissions,
}
}

/**
* @brief Checks if the user is a valid user
*
Expand Down Expand Up @@ -51,6 +67,16 @@ class User
{
return this.route;
}

/**
* @brief Returns if the user is a super user or not
*
* @return Boolean
*/
isSuperUser()
{
return this.isSU;
}
}

module.exports = User;
24 changes: 10 additions & 14 deletions handlers/main/user/user_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UserManager

setInterval(()=>{
this.flushUsers();
}, 10000 );
}, 5000 );
}

/**
Expand Down Expand Up @@ -105,21 +105,18 @@ class UserManager
/**
* @brief Sets a user in the data store
*
* @param username String
* @param userData Object
*
* @return void
*/
set( username, userData )
set( userData )
{
if ( this.has( username ) )
if ( typeof userData.username !== 'string' || this.has( userData.username ) )
{
throw new Error( `User: ${username} already exist` );
throw new Error( `User: ${userData.username} already exist` );
}

userData.username = username;

this.users[username] = new User( userData );
this.users[userData.username] = new User( userData );
}

/**
Expand All @@ -142,19 +139,18 @@ class UserManager
/**
* @brief Updates the user if it exists
*
* @param username String
* @param userData Object
* @param user User
*
* @returns void
*/
update( username, userData )
update( user )
{
if ( ! this.has( username ) )
if ( typeof user.getUsername() !== 'string' || ! this.has( user.getUsername() ) )
{
throw new Error( `User: ${username} does not exist` );
throw new Error( `User: ${user.getUsername()} does not exist` );
}

this.users[username] = userData;
this.users[user.getUsername()] = user;
}
}

Expand Down
39 changes: 37 additions & 2 deletions handlers/user/controller/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@
const { Server } = require( 'event_request' );
const app = Server();

app.post( '/user', async ( event ) =>{
// event.render( 'users', {} );
/**
* @brief Adds a new user
*/
app.post( '/users/add', ( event ) =>{
if ( event.session.get( 'SU' ) === false )
{
return event.sendError( 'Only super users can add other users', 400 );
}

const userManager = event.userManager;
const result = event.validationHandler.validate( event.body,
{
username : 'filled||string||range:3-64',
password : 'filled||string||range:3-64',
isSU : 'filled||boolean',
route : 'filled||string'
}
);

if ( result.hasValidationFailed() )
{
return event.sendError( `There is an error: ${JSON.stringify( result.getValidationResult() )}`, 400 )
}

const { username } = result.getValidationResult();

if ( userManager.has( username ) )
{
return event.sendError( `User: ${username} already exists`, 400 )
}

const userData = result.getValidationResult();
userData.route = decodeURIComponent( userData.route );

userManager.set( userData );

event.send( 'ok' );
});
39 changes: 39 additions & 0 deletions handlers/user/controller/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

const { Server } = require( 'event_request' );
const app = Server();

/**
* @brief Deletes an existing user
*
* @details Can only delete if current user is SU
* Can not delete self
*/
app.delete( '/users/:username:', async ( event ) =>{
if ( event.session.get( 'SU' ) === false )
{
return event.sendError( 'Only super users can delete other users', 400 );
}

const userManager = event.userManager;
const result = event.validationHandler.validate( event.params,
{
username : 'filled||string||range:3-64'
}
);

if ( result.hasValidationFailed() )
{
return event.sendError( `There is an error: ${JSON.stringify( result.getValidationResult() )}`, 400 )
}

const { username } = result.getValidationResult();

if ( event.session.get( 'username' ) === username )
{
return event.sendError( 'Cannot delete self!', 400 );
}

userManager.delete( username );

event.send( 'ok' );
});
24 changes: 24 additions & 0 deletions handlers/user/controller/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

const { Server } = require( 'event_request' );
const app = Server();

/**
* @brief Gets information about the user
*/
app.get( '/users/:username:', async ( event ) =>{
const userManager = event.userManager;
const result = event.validationHandler.validate( event.params,
{
username : 'filled||string||range:3-64'
}
);

if ( result.hasValidationFailed() )
{
return event.sendError( `There is an error: ${JSON.stringify( result.getValidationResult() )}`, 400 )
}

const { username } = result.getValidationResult();

event.send( userManager.get( username ).getUserData() );
});
6 changes: 6 additions & 0 deletions handlers/user/controller/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
const { Server } = require( 'event_request' );
const app = Server();

/**
* @brief Renders the Users page
*/
app.get( '/users', async ( event ) =>{
event.render( 'users', {} );
});

/**
* @brief Returns all the usernames
*/
app.get( '/users/list', async ( event ) =>{
event.send( Object.keys( event.userManager.users ) );
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "server-emulator",
"version": "8.9.1",
"version": "8.10.0",
"description": "Server emulator used to easily emulate the FS in a browser and allow streaming,downloading,deletion, etc.",
"main": "index.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion public/css/modal.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 35% !important;
width: 50% !important;
}

.bigger-modal-content{
Expand Down Expand Up @@ -74,4 +74,9 @@
.preview-item{
width: 100%;
height: auto;
}

.input-label{
margin-right: 10px;
font-size: 20px;
}
24 changes: 10 additions & 14 deletions public/js/main.js → public/js/browse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ window.Dropzone.autoDiscover = false;
/**
* @brief View Class responsible for altering the page
*/
class View
class Browse
{
constructor()
{
Expand Down Expand Up @@ -56,12 +56,12 @@ class View
});

$( document ).on( 'click', '.file-delete', ( event ) => {
this.deleteItem( $( event.target ), View.TYPE_FILE );
this.deleteItem( $( event.target ), Browse.TYPE_FILE );
return false;
});

$( document ).on( 'click', '.folder-delete', ( event ) => {
this.deleteItem( $( event.target ), View.TYPE_FOLDER );
this.deleteItem( $( event.target ), Browse.TYPE_FOLDER );
return false;
});

Expand Down Expand Up @@ -140,7 +140,7 @@ class View
*
* @return void
*/
deleteItem( element, type = View.TYPE_FILE )
deleteItem( element, type = Browse.TYPE_FILE )
{
element = element.closest( '.item' );
const itemToDelete = element.attr( 'data-item-encoded-uri' );
Expand All @@ -161,7 +161,7 @@ class View
});
};

if ( type === View.TYPE_FOLDER )
if ( type === Browse.TYPE_FOLDER )
{
modal.askConfirmation( `Are you sure you want to delete this item?` ).then(( confirmDelete )=>{
if ( ! confirmDelete )
Expand Down Expand Up @@ -266,12 +266,7 @@ class View
}

const { items, position, hasMore, dir } = JSON.parse( data );

if ( dir !== decodeURIComponent( this.currentDir ) )
{
return;
}
this.currentPosition = position;
this.currentPosition = position;

for ( const index in items )
{
Expand Down Expand Up @@ -417,9 +412,10 @@ class View
}
}

View.TYPE_FILE = 'file';
View.TYPE_FOLDER = 'folder';
Browse.TYPE_FILE = 'file';
Browse.TYPE_FOLDER = 'folder';

const view = new View();
const view = new Browse();
const modal = new Modal();

view.browse( currentDir );
Loading

0 comments on commit 13e0baf

Please sign in to comment.