diff --git a/src/client/containers/Settings/Server/index.jsx b/src/client/containers/Settings/Server/index.jsx index e50ccae2b..7f75c98eb 100644 --- a/src/client/containers/Settings/Server/index.jsx +++ b/src/client/containers/Settings/Server/index.jsx @@ -23,11 +23,20 @@ import SettingItem from 'components/Settings/SettingItem' import helpers from 'lib/helpers' import axios from 'axios' import Log from '../../../logger' +import EnableSwitch from 'components/Settings/EnableSwitch' +import { observer } from 'mobx-react' +import { makeObservable, observable } from 'mobx' +import UIKit from 'uikit' +@observer class ServerSettingsController extends React.Component { + @observable maintenanceModeEnabled = false + constructor (props) { super(props) + makeObservable(this) + this.state = { restarting: false } @@ -39,23 +48,14 @@ class ServerSettingsController extends React.Component { // helpers.UI.inputs() } - componentDidUpdate () { + componentDidUpdate (prevProps) { // helpers.UI.reRenderInputs() + if (prevProps.settings !== this.props.settings) { + if (this.maintenanceModeEnabled !== this.getSetting('maintenanceMode')) + this.maintenanceModeEnabled = this.getSetting('maintenanceMode') + } } - // static getDerivedStateFromProps (nextProps, state) { - // if (nextProps.settings) { - // let stateObj = { ...state } - // if (!state.tpsUsername) - // stateObj.tpsUsername = nextProps.settings.getIn(['settings', 'tpsUsername', 'value']) || '' - // if (!state.tpsApiKey) stateObj.tpsApiKey = nextProps.settings.getIn(['settings', 'tpsApiKey', 'value']) || '' - // - // return stateObj - // } - // - // return null - // } - restartServer () { this.setState({ restarting: true }) @@ -78,6 +78,43 @@ class ServerSettingsController extends React.Component { : '' } + onMaintenanceModeChange (e) { + const self = this + const val = e.target.checked + + if (val === true) { + UIKit.modal.confirm( + `

Are you sure?

+

+ This will force logout every user and prevent non-administrators from logging in. +

+ `, + () => { + this.props + .updateSetting({ + name: 'maintenanceMode:enable', + value: val, + stateName: 'maintenanceMode', + noSnackbar: true + }) + .then(() => { + self.maintenanceModeEnabled = val + }) + }, + { + labels: { Ok: 'Yes', Cancel: 'No' }, + confirmButtonClass: 'md-btn-danger' + } + ) + } else { + this.props + .updateSetting({ name: 'maintenanceMode:enable', value: val, stateName: 'maintenanceMode', noSnackbar: true }) + .then(() => { + self.maintenanceModeEnabled = val + }) + } + } + render () { const { active } = this.props return ( @@ -97,6 +134,18 @@ class ServerSettingsController extends React.Component { /> } /> + this.onMaintenanceModeChange(e)} + /> + } + /> ) } diff --git a/src/middleware/index.js b/src/middleware/index.js index b3e64d38c..c7724d073 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -96,6 +96,24 @@ module.exports = function (app, db, callback) { // CORS app.use(allowCrossDomain) + + // Maintenance Mode + app.use(function (req, res, next) { + var settings = require('../settings/settingsUtil') + settings.getSettings(function (err, setting) { + if (err) return winston.warn(err) + var maintenanceMode = setting.data.settings.maintenanceMode + + if (req.user) { + if (maintenanceMode.value === true && !req.user.role.isAdmin) { + return res.render('maintenance', { layout: false }) + } + } + + return next() + }) + }) + // Mobile app.use('/mobile', express.static(path.join(__dirname, '../../', 'mobile'))) diff --git a/src/routes/index.js b/src/routes/index.js index 10c568607..307c2fe29 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -27,13 +27,6 @@ function mainRoutes (router, middleware, controllers) { router.get('/install', function (req, res) { return res.redirect('/') }) - router.get( - '/dashboard', - middleware.redirectToLogin, - middleware.redirectIfUser, - middleware.loadCommonData, - controllers.main.dashboard - ) router.get('/login', function (req, res) { return res.redirect('/') @@ -81,6 +74,19 @@ function mainRoutes (router, middleware, controllers) { }) }) + // Maintenance + router.get('/maintenance', function (req, res) { + return res.redirect('/') + }) + + router.get( + '/dashboard', + middleware.redirectToLogin, + middleware.redirectIfUser, + middleware.loadCommonData, + controllers.main.dashboard + ) + // Tickets router.get( '/tickets', @@ -449,8 +455,8 @@ function handleErrors (err, req, res) { var status = err.status || 500 res.status(err.status) - if (status === 404) { - res.render('404', { layout: false }) + if (status === 500) { + res.render('500', { layout: false }) return } diff --git a/src/settings/defaults.js b/src/settings/defaults.js index 511893613..e1db014d7 100644 --- a/src/settings/defaults.js +++ b/src/settings/defaults.js @@ -674,6 +674,23 @@ function installationID (callback) { }) } +function maintenanceModeDefault (callback) { + SettingsSchema.getSettingByName('maintenanceMode:enable', function (err, setting) { + if (err) return callback(err) + if (!setting) { + SettingsSchema.create( + { + name: 'maintenanceMode:enable', + value: false + }, + callback + ) + } else { + return callback() + } + }) +} + settingsDefaults.init = function (callback) { winston.debug('Checking Default Settings...') async.series( @@ -714,6 +731,9 @@ settingsDefaults.init = function (callback) { function (done) { return elasticSearchConfToDB(done) }, + function (done) { + return maintenanceModeDefault(done) + }, function (done) { return installationID(done) } diff --git a/src/settings/settingsUtil.js b/src/settings/settingsUtil.js index 39edfee34..5dc324c9c 100644 --- a/src/settings/settingsUtil.js +++ b/src/settings/settingsUtil.js @@ -123,6 +123,8 @@ util.getSettings = function (callback) { s.privacyPolicy = parseSetting(settings, 'legal:privacypolicy', '') s.privacyPolicy.value = jsStringEscape(s.privacyPolicy.value) + s.maintenanceMode = parseSetting(settings, 'maintenanceMode:enable', false) + async.parallel( [ function (done) { diff --git a/src/views/500.hbs b/src/views/500.hbs new file mode 100644 index 000000000..2a780d1cc --- /dev/null +++ b/src/views/500.hbs @@ -0,0 +1,120 @@ + + + + Trudesk + + + + +Trudesk Logo +
+

500

+

Unknown Server Error

+
+ + \ No newline at end of file diff --git a/src/views/maintenance.hbs b/src/views/maintenance.hbs new file mode 100644 index 000000000..72365faf7 --- /dev/null +++ b/src/views/maintenance.hbs @@ -0,0 +1,119 @@ + + + + Trudesk + + + + +
+

Maintenance

+

Server is current in maintenance.
Check back later.

+
+ + \ No newline at end of file