Skip to content

Commit

Permalink
Merge pull request #79 from openjournalteam/allow-jm-toggle-plugin
Browse files Browse the repository at this point in the history
Allow Journal Manager to enable/disable plugin
  • Loading branch information
rahmanramsi authored Oct 31, 2024
2 parents 9682ab5 + aeca848 commit cc192dd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 2.0.9.1 : 31 October 2024
- Allow Journal Manager to enable/disable plugin

### 2.0.9.0 : 02 September 2024
- Add protection to access the control panel
- only allow admin to enable/disable plugin
Expand Down
7 changes: 7 additions & 0 deletions OjtPageHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,13 @@ public function toggleInstalledPlugin($args, $request)
{
$plugin = $this->ojtPlugin;

if(!$plugin->getCanDisable()) {
$json['error'] = 1;
$json['msg'] = 'User does not have permission to disable/enable plugin';
showJson($json);
return;
}

$pluginFolder = $request->getUserVar('pluginFolder');
$isEnabled = ($request->getUserVar('enabled') == 'true') ? true : false;

Expand Down
21 changes: 20 additions & 1 deletion OjtPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,28 @@ function getCanEnable() {
* @copydoc Plugin::getCanDisable()
*/
function getCanDisable() {
if($this->isCurrentUserAreJournalManager()) return true;

return $this->getRequest()->getUser()->hasRole([ROLE_ID_SITE_ADMIN], CONTEXT_SITE);
}

public function isCurrentUserAreJournalManager()
{
$currentUser = $this->getRequest()->getUser();
if(!$currentUser) return false;

$userGroupDao = DAORegistry::getDAO('UserGroupDAO');
$currentUserGroups = $userGroupDao->getByUserId($currentUser->getId(), $this->getCurrentContextId());

$currentUserGroupNameLocaleKeys = collect($currentUserGroups->toArray())->map(function ($userGroup) {
return $userGroup->getData('nameLocaleKey');
})->toArray();

if(in_array('default.groups.name.manager', $currentUserGroupNameLocaleKeys)) return true;

return false;
}

public function apiUrl()
{
return static::API;
Expand Down Expand Up @@ -347,14 +366,14 @@ public function registerModules()
$data['className'] = $plugin->getName();
$data['description'] = $plugin->getDescription();
$data['enabled'] = $plugin->getEnabled();
$data['canEnable'] = $this->getCanEnable();
$data['open'] = false;
$data['icon'] = method_exists($plugin, 'getPageIcon') ? $plugin->getPageIcon() : $this->getDefaultPluginIcon();
$data['documentation'] = method_exists($plugin, 'getDocumentation') ? $plugin->getDocumentation() : null;
$data['page'] = method_exists($plugin, 'getPage') ? $plugin->getPage() : null;

$plugins[] = $data;
}

// HookRegistry::call('PluginRegistry::categoryLoaded::themes');


Expand Down
7 changes: 7 additions & 0 deletions assets/js/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ Spruce.store("plugins", {
this.type = "all";
},
async togglePlugin(currentPlugin) {
if(!currentPlugin.canEnable){
return Toast.fire({
title: "User does not have permission to enable this plugin",
icon: "warning",
});
}

const formData = new FormData();
formData.append("className", currentPlugin.className);
formData.append("productType", currentPlugin.productType);
Expand Down
4 changes: 2 additions & 2 deletions templates/plugininstalled.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@
<div class="ojt-flex ojt-place-items-center">
<div class="lg:ojt-mx-auto">
<input type="checkbox" x-model="plugin.enabled" x-on:click.prevent class="cbx ojt-hidden"
style="display: none;" />
<label for="cbx" class="toggle" @click="$store.plugins.togglePlugin(plugin);"
style="display: none;" disabled />
<label for="cbx" class="toggle" @click="plugin.canEnable && $store.plugins.togglePlugin(plugin);"
:id="'toggleplugin' + plugin.product">
<span>
<svg width="10px" height="10px" viewBox="0 0 10 10">
Expand Down
4 changes: 2 additions & 2 deletions version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<version>
<application>ojtPlugin</application>
<type>plugins.generic</type>
<release>2.0.9.0</release>
<date>2024-09-02</date>
<release>2.0.9.1</release>
<date>2024-10-31</date>
<lazy-load>0</lazy-load>
<class>OjtPlugin</class>
</version>

0 comments on commit cc192dd

Please sign in to comment.