updated init with empty config fix, added site restore by verifying backup file

This commit is contained in:
Ro 2020-08-18 15:47:04 -07:00
parent efb20eeeb3
commit 126361fca8
8 changed files with 248 additions and 56 deletions

View file

@ -8,6 +8,7 @@ const auth = new Auth();
const utils = new Utils();
var backup_upload = multer().array('backup_upload');
var backup_restore = multer().any();
/***
CREATE BACK UP
@ -87,4 +88,20 @@ router.post('/restore', backup_upload, (req, res) => {
});
});
router.post('/init-restore', backup_restore, (req, res) => {
utils
.verifyBackup(req.files[0], req.body)
.then(response => {
res.json({
type: response.type,
message: response.message
});
})
.catch(err => {
res.json({
type: err.type,
message: err.message
});
});
});
module.exports = router;

View file

@ -1,6 +1,7 @@
import * as DataEvent from '../../src/com/events/DataEvent';
const bCrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const _ = require('lodash');
export default class Auth {
//--------------------------
@ -64,6 +65,32 @@ export default class Auth {
});
}
verifyCredentials(config, credentials) {
return new Promise((resolve, reject) => {
var found = _.find(config, { handle: credentials.handle });
var response;
if (found) {
if (!this.isValidPassword(found, credentials.pass)) {
response = {
type: DataEvent.REQUEST_LAME,
message: 'CHECK YOUR PASSWORD'
};
reject(response);
}
response = { type: DataEvent.REQUEST_GOOD, message: 'Backup Verified. Restoring' };
resolve(response);
} else {
response = { type: DataEvent.REQUEST_LAME, message: 'Handle not found, boss' };
reject(response);
}
});
}
isValidPassword(user, password) {
return bCrypt.compareSync(password, user.password);
}
/**
* Checks to make sure received token matches
* @parameter token: created token

View file

@ -2,12 +2,15 @@ import Settings, { SETTINGS_FILE } from './Settings';
import Render from './Render';
import StringUtils from '../../src/com/utils/StringUtils';
import _ from 'lodash';
import Auth from '../data/Auth';
import DataEvent from '../../src/com/events/DataEvent';
const settings = new Settings();
const render = new Render();
const stringUtils = new StringUtils();
const moment = require('moment');
const fs = require('fs-extra');
const AdmZip = require('adm-zip');
const auth = new Auth();
export default class Utils {
constructor() {}
@ -157,9 +160,10 @@ export default class Utils {
}
restoreBackup(file) {
//var response;
var response;
return new Promise((resolve, reject) => {
var zip = new AdmZip(file.buffer);
try {
zip.extractEntryTo('settings/settings.json', 'site', false, true);
zip.extractEntryTo('settings/folks.json', 'site', false, true);
zip.extractEntryTo('settings/tags.json', 'site', false, true);
@ -190,6 +194,53 @@ export default class Utils {
});
resolve();
} catch (error) {
response = {
type: error,
message: 'ERROR READING BACKUP'
};
reject(response);
}
});
}
verifyBackup(file, body) {
var response;
var zip = new AdmZip(file.buffer);
var credentials = { handle: body.restore_member_handle, pass: body.restore_member_pass };
var self = this;
return new Promise((resolve, reject) => {
try {
let folks = JSON.parse(zip.readAsText('settings/folks.json'));
auth.verifyCredentials(folks, credentials)
.then(r => {
//resolve(r);
self.restoreBackup(file)
.then(() => {
response = {
type: '',
message: 'RESTORE COMPLETE'
};
resolve(response);
})
.catch(err => {
response = {
type: 'error',
message: 'ERROR RESTORING BACKUP'
};
});
})
.catch(err => {
reject(err);
});
} catch (error) {
response = {
type: 'error',
message: 'ERROR READING BACKUP FILE'
};
reject(response);
}
});
}
}

View file

@ -28,4 +28,26 @@ block main-content
input.large(type='text', name='new_member_title' id='new_member_title', placeholder="Site Title Please")
br
button#init-blog(data-action='blog-init' type='submit') SET IT UP
.option
button.init-option#init-switch-restore OR RESTORE FROM BACKUP
.dash-restore#dash-restore
form#init-restore
h1 Restore backup.
p Let's verify your backup
label What's your handle?
br
input.large(type='text', name='restore_member_handle' id='restore_member_handle', placeholder="What\'s your handle?")
br
label Let's get a password
br
input.large(type='password', name='restore_member_pass' id='restore_member_pass', placeholder="Password Please")
br
label Backup File
br
input(id="backup-upload" type="file" name="backup-upload")
br
button#blog-restore(data-action='blog-restore' type='submit') RESTORE
.option
button.init-option#init-switch-fresh OR INSTALL FRESH SITE

View file

@ -7,13 +7,18 @@
var app = require('./brain/app');
var debug = require('debug')('fipamo:server');
var http = require('http');
var config = require('./site/settings.json');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || config.global.port);
try {
var configPort = require('./site/settings.json').global.port;
} catch (err) {
console.log('settings.json not found, assuming this is a first run...');
}
var port = normalizePort(configPort || process.env.PORT || 2314);
app.set('port', port);
/**

View file

@ -1,10 +1,12 @@
import FipamoApi from '../libraries/FipamoAPI';
import FipamoAdminAPI from '../libraries/FipamoAdminAPI';
import DataUitls from './utils/DataUtils';
import * as DataEvent from './events/DataEvent';
import DashManager from './controllers/DashManager';
import Notfications from './ui/Notifications';
const api = new FipamoApi();
const admin = new FipamoAdminAPI();
const data = new DataUitls();
const notify = new Notfications();
@ -21,6 +23,10 @@ export default class Base {
//--------------------------
start() {
if (document.getElementById('dash-form') || document.getElementById('dash-init')) {
var options = document.getElementsByClassName('init-option');
for (let index = 0; index < options.length; index++) {
options[index].addEventListener('click', e => this.handleOptions(e));
}
if (document.getElementById('dash-form')) {
document
.getElementById('login-btn')
@ -29,6 +35,9 @@ export default class Base {
document
.getElementById('init-blog')
.addEventListener('click', e => this.handleSetup(e));
document
.getElementById('blog-restore')
.addEventListener('click', e => this.handleRestore(e));
}
} else {
new DashManager();
@ -76,4 +85,43 @@ export default class Base {
notify.alert(err, false);
});
}
handleRestore(e) {
e.stopPropagation();
e.preventDefault();
var form = document.getElementById('init-restore');
admin
.handleInitRestore(form)
.then(response => {
if (response.type === DataEvent.REQUEST_LAME) {
notify.alert(response.message, false);
} else {
notify.alert(response.message, true);
setTimeout(() => {
//window.location = '/@/dashboard';
}, 700);
}
})
.catch(err => {
notify.alert(err, false);
});
}
handleOptions(e) {
e.stopPropagation();
e.preventDefault();
let init = document.getElementById('dash-init');
let restore = document.getElementById('dash-restore');
if (e.target.id === 'init-switch-restore') {
init.style.display = 'none';
init.style.visibility = 'hidden';
restore.style.display = 'block';
restore.style.visibility = 'visible';
} else {
init.style.display = 'block';
init.style.visibility = 'visible';
restore.style.display = 'none';
restore.style.visibility = 'hidden';
}
}
}

View file

@ -21,6 +21,7 @@ export const API_NAV_SYNC = '/api/v1/settings/nav-sync';
export const API_CREATE_BACKUP = '/api/v1/backup/create';
export const API_DOWNLOAD_BACKUP = '/api/v1/backup/download';
export const API_RESTORE_BACKUP = '/api/v1/backup/restore';
export const API_INIT_RESTORE_BACKUP = '/api/v1/backup/init-restore';
export const API_SEND_MAIL = '/api/v1/mailer';
import * as DataEvent from '../com/events/DataEvent';
export default class APIUtils {
@ -225,6 +226,24 @@ export default class APIUtils {
});
});
}
handleInitRestore(form) {
return new Promise((resolve, reject) => {
var url, event, method, type, data;
url = API_INIT_RESTORE_BACKUP;
event = DataEvent.API_BACKUP_RESTORE;
method = REQUEST_TYPE_POST;
type = CONTENT_TYPE_FORM;
data = new FormData(form);
this._request(url, event, method, type, data)
.then(result => {
resolve(result);
})
.catch(err => {
reject(err);
});
});
}
//--------------------------
// private
//--------------------------

View file

@ -14,7 +14,7 @@
height 100%
margin 0 auto
.dash-init
.dash-init, .dash-restore
width 100%
max-width 900px
margin 0 auto
@ -40,6 +40,9 @@
width 120px
margin 0 auto
display block
.dash-restore
display: none;
visibility hidden;
#dash-login
width 100%