diff --git a/app/css/openmrs-addonmanager.css b/app/css/openmrs-addonmanager.css index 477f56d..b095b83 100644 --- a/app/css/openmrs-addonmanager.css +++ b/app/css/openmrs-addonmanager.css @@ -826,4 +826,9 @@ body.loading .waiting-modal { .main-row { width: 30vw; -} \ No newline at end of file +} + +.none-user-page { + text-align: center; + padding: 4%; +} diff --git a/app/js/components/manageApps/Addon.jsx b/app/js/components/manageApps/Addon.jsx index 208d2ea..56dacc8 100644 --- a/app/js/components/manageApps/Addon.jsx +++ b/app/js/components/manageApps/Addon.jsx @@ -20,7 +20,9 @@ class Addon extends Component { starting: false, isOpen: false, showMessageDetail: false, + isAdmin: false, loadingComplete: false, + enableDeleteAndStart: true, messageBody: '', messageType: 'success', showMessage: false, @@ -32,6 +34,9 @@ class Addon extends Component { this.handleAction = this.handleAction.bind(this); this.actionRunning = this.actionRunning.bind(this); this.handleAddonAction = this.handleAddonAction.bind(this); + this.checkIsAdmin = this.checkIsAdmin.bind(this); + this.fetchCurrentUser = this.fetchCurrentUser.bind(this); + this.enableAdminDeleteAndStart = this.enableAdminDeleteAndStart.bind(this); this.hideModal = this.hideModal.bind(this); this.handleDelete = this.handleDelete.bind(this); this.getAffectedModules = this.getAffectedModules.bind(this); @@ -41,6 +46,8 @@ class Addon extends Component { componentWillMount(){ this.props.checkLoginStatus(); + this.checkIsAdmin(); + this.enableAdminDeleteAndStart(); } componentDidMount() { @@ -80,6 +87,61 @@ class Addon extends Component { } + fetchCurrentUser(){ + const apiHelper = new ApiHelper(null); + const getUserData = new Promise(function(resolve, reject) { + apiHelper.get('/v1/session').then(response => { + resolve(response); + }); + }); + return getUserData; + } + + checkIsAdmin() { + this.fetchCurrentUser().then((response) => { + if (response.user) { + const user = response.user; + let userPrivileges = []; + let userCanManageModules; + userPrivileges = userPrivileges.concat(user.privileges); + if (userPrivileges.length > 0) { + const managePrivilege = userPrivileges.find((managemodules) => { + if (managemodules.display === "Manage Modules") { + return userCanManageModules = true; + } + return userCanManageModules = false; + }); + } + if (user.systemId === "admin" || user.display === "admin" || userCanManageModules){ + this.setState((prevState, props) => { + return { + isAdmin : true, + }; + }); + } + } + }); + } + + enableAdminDeleteAndStart () { + const applicationDistribution = location.href.split('/')[2]; + const urlPrefix = location.href.substr(0, location.href.indexOf('//')); + const url = location.href.split('/')[3]; + const apiBaseUrl = `/${applicationDistribution}/${url}/ws/rest`; + const requestUrl = '/owa/allowModuleWebUpload'; + axios.get(`${urlPrefix}/${apiBaseUrl}${requestUrl}`) + .then(response => { + this.setState((prevState, props) => { + return { + enableDeleteAndStart : response.data, + }; + }); + }) + .catch(error => { + toastr.error(error.message); + }); + } + fetchOwa(fileName) { this.state.loadingComplete === true ? this.setState({ loadingComplete: false }) : null; const owaName = fileName.substr(fileName.indexOf('-') + 1, fileName.length - 1); @@ -126,14 +188,14 @@ class Addon extends Component { } axios.post(`${urlPrefix}/${apiBaseUrl}${this.requestUrl}`, postData) - .then(response => { + .then(response => { const moduleName = response.data.modules[0].display; this.setState({ stopping: false, starting: false, - messageBody: response.data.action == 'START' ? - `${moduleName} module has started` - : + messageBody: response.data.action == 'START' ? + `${moduleName} module has started` + : `${moduleName} module has stopped`, messageType: 'success', showMessage: true, @@ -268,16 +330,18 @@ class Addon extends Component { render() { const { - app, - isOpen, - showMessageDetail, - affectedModules, - action, + app, + isOpen, + isAdmin, + showMessageDetail, + affectedModules, + action, loadingComplete, messageBody, + enableDeleteAndStart, messageType, showMessage } = this.state; - + const message = app.startupErrorMessage && app.startupErrorMessage.length > 0 ? 'Error starting ' : null; @@ -380,34 +444,38 @@ class Addon extends Component { - - - { - app.uuid ? - app.started ? + { enableDeleteAndStart ? + isAdmin ? + - : - - : - + + { app.uuid ? + app.started ? + + : + + : null} + + + : null :null } {isOpen ? ( { + resolve(response); + }).catch(error => { + toastr.error(error.message); + }); + }); + return getUserData; + } + + fetchAdminProperties () { + const applicationDistribution = location.href.split('/')[2]; + const urlPrefix = location.href.substr(0, location.href.indexOf('//')); + const url = location.href.split('/')[3]; + const apiBaseUrl = `/${applicationDistribution}/${url}/ws/rest`; + const requestUrl = '/owa/allowModuleWebUpload'; + axios.get(`${urlPrefix}/${apiBaseUrl}${requestUrl}`) + .then(response => { + this.setState((prevState, props) => { + return { + allowWebAdmin : response.data, + }; + }); + }) + .catch(error => { + toastr.error(error.message); + }); + } + + checkIsAdmin() { + this.fetchCurrentUser().then((response) => { + const user = response.user; + if (user) { + let userPrivileges = []; + let userCanManageModules; + userPrivileges = userPrivileges.concat(user.privileges); + if (userPrivileges.length > 0) { + const managePrivilege = userPrivileges.find((managemodules) => { + if (managemodules.display === "Manage Modules") { + return userCanManageModules = true; + } + return userCanManageModules = false; + }); + } + if (user.systemId === "admin" || user.display === "admin" || userCanManageModules){ + this.setState((prevState, props) => { + return { + isAdmin : true, + }; + }); + } + if (!user.systemId === "admin" && !user.display === "admin" && !userCanManageModules){ + this.setState((prevState, props) => { + return { + cannotViewAddons : true, + }; + }); + } + } + }); + } + handleDrop(files) { const fileName = files[0].name; const fileExtension = fileName.substr(fileName.lastIndexOf('.') + 1, fileName.length - 1).toLowerCase(); @@ -193,7 +265,7 @@ export default class ManageApps extends React.Component { appList: installedAddons, staticAppList: installedAddons, searchComplete: true - })); + })); return data; }); })); @@ -858,12 +930,18 @@ export default class ManageApps extends React.Component { msgBody, appList, searchedAddons, + checkUpdates, + isOpen, + isAdmin, + allowWebAdmin, + selectedApp, searchComplete, isSearched, progressMsg, upgradeAddon, upgradeVersion, openUpgradeConfirmation, + cannotViewAddons, displayInvalidZip, } = this.state; if (showMsg === true) { @@ -903,27 +981,52 @@ export default class ManageApps extends React.Component { ); + if (cannotViewAddons){ + return ( +
+

+ You cannot View or Manage the Add-ons since you dont have the priviledges + and you are not an admin.
+
+ + To Gain the permissions contact the system administrator. +
+ + Go back Home + +

+
+ ); + } return (
-
-
- - {updatesAvailable ? 'Back to all Add-ons': 'Check for Updates'} - - - Start all Modules - - {buttonsInstance} - -
-
+ {allowWebAdmin ? + isAdmin ? +
+
+ + {updatesAvailable ? 'Back to all Addons': 'Check for Updates'} + + + Start all Modules + + {buttonsInstance} + +
+
+ : null + :

+ Settings, upload and check updates are disabled from the website at this time. The runtime property module.allow_web_admin must be set to true. +
+

+ } @@ -934,13 +1037,14 @@ export default class ManageApps extends React.Component { {showMsg && alert}
- + {allowWebAdmin ? + isAdmin ? : null :null}
', () => { // simulate a selected file in order to render upload and clear buttons renderedComponent.setState({ isSearched: true, + isAdmin: true, files: [{ name: "test.zip", type: "application/zip" @@ -51,7 +52,7 @@ describe('', () => { it('should mount the AddAddon component in itself', () => { expect(renderedComponent.find("AddAddon")).to.have.length(1); }); - + it('Should render clear and upload buttons when file is selected', () => { const AddAddon = renderedComponent.find("AddAddon").first(); expect(AddAddon.find("#upload-btn").first().length).to.equal(1);