Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

 

In this tutorial, we will following the guideline of developing a plugin to develop our Not Permission plugin. Please also refer to the very first tutorial 如何开发一个Bean Shell Hash Variable插件 for more details steps.

1. What is the problem?

I want to configure the userview permission or form permission to not in groups, department or etc. 

2. What is your idea to solve the problem?

We can develop a Userview权限/表单权限插件 to reverse the result of other permission plugins.

3. What is the input needed for your plugin?

To develop a Not Permission plugin, we can consider to provide the following as input.

  1. Permission: To configure a Permission plugin to get the result for reverse it.
  2. Must be Logged In User: The result will only valid if and only if the user is also a logged in user.

4. What is the output and expected outcome of your plugin?

The reverse value of the configured permission plugin.

5. Is there any resources/API that can be reuse?

You can refer to 如何开发甘特图用户视图菜单 on how to reuse a existing plugin.

6. Prepare your development environment

We need to always have our Joget Workflow Source Code ready and builded by following this guideline

The following of this tutorial is prepared with a Macbook Pro and Joget Source Code version 5.0.0. Please refer to 如何开发插件 for other platform command.

在本教程中,我们将遵循开发插件  开发我们的Not Permission插件的  指导原则。 有关更多详细信息步骤,请参阅第一个教程  如何开发一个Bean Shell哈希变量插件

1.什么问题?

我想配置userview权限或表单权限不在组,部门或其他 

2.你有什么想法来解决这个问题?

我们可以开发一个  Userview权限/表单权限插件  来反转其他权限插件的结果。

3.你的插件需要什么输入?

要开发一个Not Permission插件,我们可以考虑提供以下内容作为输入。

  1. 权限:配置一个权限插件来得到相反的结果。
  2. 必须登录用户:只有当用户也是登录用户时,结果才有效。

4.你的插件的输出和预期结果是什么?

配置的权限插件的反向值。

5.有没有可重用的资源/ API?

您可以参考  如何开发甘特图用户视图菜单如何重用现有的插件。

6.准备你的开发环境

我们需要始终准备好Joget Workflow Source Code,并按照这个指导方针建立起来  。 

本教程的以下内容是使用Macbook Pro和Joget源代码5.0.0版编写的。 其他平台命令请参考  如何开发插件

让我们说我们的文件夹目录如下。 Let said our folder directory as following. 

Code Block
- Home
  - joget
    - plugins
    - jw-community
      -5.0.1

The "plugins" directory is the folder we will create and store all our plugins and the "jw-community" directory is where the Joget Workflow Source code stored.

“plugins”目录是我们要创建和存储我们所有插件的文件夹,“jw-community”目录是Joget Workflow源代码存储的地方。

运行以下命令在“plugins”目录下创建一个maven项目。Run the following command to create a maven project in "plugins" directory.

Code Block
languagebash
cd joget/plugins/
~/joget/jw-community/5.0.1/wflow-plugin-archetype/create-plugin.sh org.joget.tutorial not_permission 5.0.1

Then, the shell script will ask us to key in a version for your plugin and ask us for confirmation before generate the maven project.然后,shell脚本将要求我们为您的插件输入一个版本,并在生成maven项目之前要求我们确认。

Code Block
languagebash
Define value for property 'version':  1.0-SNAPSHOT: : 5.0.0
[INFO] Using property: package = org.joget.tutorial
Confirm properties configuration:
groupId: org.joget.tutorial
artifactId: not_permission
version: 5.0.0
package: org.joget.tutorial
Y: : y

We should get "BUILD SUCCESS" message shown in our terminal and a "not_permission" folder created in "plugins" folder.

我们应该在终端上显示“BUILD SUCCESS”消息,在“plugins”文件夹中创建一个“not_permission”文件夹。

用你喜欢的IDE打开maven项目。我将使用  NetBeans。  Open the maven project with your favour IDE. I will be using NetBeans.  

7. Just code it!

a.

...

扩展插件类型的抽象类

在“org.joget.tutorial”包下创建一个“NotPermission”类。然后,用Create a "NotPermission" class under "org.joget.tutorial" package. Then, extend the class with org.joget.apps.userview.model.UserviewPermission abstract class and implement   抽象类扩展  该类并实现org.joget.apps.form.model.FormPermission interface. Please refer to 接口。请参阅  Userview权限/表单权限插件.

b.

...

实现所有的抽象方法

像往常一样,我们必须执行所有的抽象方法。我们将使用AppPluginUtil.getMessage方法来支持i18n,并使用常量变量MESSAGE_PATH作为消息资源包目录。As usual, we have to implement all the abstract methods. We will using AppPluginUtil.getMessage method to support i18n and using constant variable MESSAGE_PATH for message resource bundle directory.

Code Block
languagejava
titleImplementation of all basic abstract methods
collapsetrue
package org.joget.tutorial;
 
import org.joget.apps.app.service.AppPluginUtil;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.FormPermission;
import org.joget.apps.userview.model.UserviewPermission;
 
public class NotPermission extends UserviewPermission implements FormPermission {
    
    private final static String MESSAGE_PATH = "messages/NotPermission";
 
    public String getName() {
        return "Not Permission";
    }
 
    public String getVersion() {
        return "5.0.0";
    }
 
    public String getDescription() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.tutorial.NotPermission.pluginDesc", getClassName(), MESSAGE_PATH);
    }
 
    public String getLabel() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.tutorial.NotPermission.pluginLabel", getClassName(), MESSAGE_PATH);
    }
 
    public String getClassName() {
        return getClass().getName();
    }
 
    public String getPropertyOptions() {
        return AppUtil.readPluginResource(getClassName(), "/properties/notPermission.json", null, true, MESSAGE_PATH);
    }
    
    @Override
    public boolean isAuthorize() {
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}

Then, we have to do a UI for admin user to provide inputs for our plugin. In getPropertyOptions method, we already specify our 插件属性选项与配置 definition file is locate at "然后,我们必须为管理员用户提供一个UI来为我们的插件提供输入。在getPropertyOptions方法中,我们已经指定了  插件属性选项和配置  定义文件位于“/properties/notPermission.json". Let us create a directory "resources/properties" under "notjson”。让我们在“not_permission / src / main”目录下创建一个“resources / main" directory. After create the directory, create a file named "notPermission.json" in the "properties" folder.In the properties definition options file, we will need to provide options as below. Please note that we can use "@@message.key@@" syntax to support i18n in our properties options.properties”目录。创建目录后,在“properties”文件夹中创建一个名为“notPermission.json”的文件。

在属性定义选项文件中,我们需要提供如下的选项。请注意,我们可以在我们的属性选项中使用“@@ message.key @@”语法来支持i18n。

Code Block
languagejs
[{
    title : '@@userview.notpermission.config@@',
    properties : [{
        name : 'permission',
        label : '@@userview.notpermission.permission@@',
        type : 'elementselect',
        options_ajax : '[CONTEXT_PATH]/web/property/json/getElements?classname=org.joget.apps.userview.model.UserviewPermission',
        url : '[CONTEXT_PATH]/web/property/json[APP_PATH]/getPropertyOptions'
    },
    {
        name : 'loggedIn',
        label : '@@userview.notpermission.loggedIn@@',
        type : 'checkbox',
        value : 'true',
        options : [{
            value : 'true',
            label : ''
        }]
    }]
}]

After done the properties option to collect input, we can work on the main method of the plugin which is isAuthorize method.在完成收集输入的属性选项后,我们可以使用isAuthorize方法。

Code Block
languagejava
    @Override
    public boolean isAuthorize() {
        boolean isAuthorize = false;
        try {
            if ("true".equals(getPropertyString("loggedIn")) && WorkflowUtil.isCurrentUserAnonymous()) {
                return false;
            }
            
            //get the binder
            Object permissionData = getProperty("permission");
            if (permissionData != null && permissionData instanceof Map) {
                Map pMap = (Map) permissionData;
                if (pMap != null && pMap.containsKey("className") && !pMap.get("className").toString().isEmpty()) {
                    PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
                    UserviewPermission permission = (UserviewPermission) pluginManager.getPlugin(pMap.get("className").toString());
                    if (permission != null) {
                        Map pProps = (Map) pMap.get("properties");
                        permission.setProperties(pProps);
                        permission.setCurrentUser(getCurrentUser());
                        permission.setRequestParameters(getRequestParameters());
                        
                        isAuthorize = !permission.isAuthorize();
                    }
                }
            }
        } catch (Exception e) {
            LogUtil.error(getClassName(), e, "");
        }
        return isAuthorize;
    }

c.

...

管理你的插件的依赖库

这个插件没有额外的依赖。There are no extra dependency for this plugin.

d.

...

让你的插件国际化(国际化)准备就绪

我们在getLabel和getDescription方法中使用i18n消息密钥。我们还在我们的属性选项定义中使用了i18n消息密钥。所以,我们需要为我们的插件创建一个消息资源包属性文件。

在“not

We are using i18n message key in getLabel and getDescription method. We also used i18n message key in our properties options definition as well. So, we will need to create a message resource bundle properties file for our plugin.

Create directory "resources/messages" under "not_permission / src / main”目录下创建目录“resources / main" directory. Then, create a "NotPermission.properties" file in the folder. In the properties file, let add all the message keys and its label as below.messages”。然后,在该文件夹中创建一个“NotPermission.properties”文件。在属性文件中,让我们添加所有的消息键和它的标签如下。

Code Block
org.joget.tutorial.NotPermission.pluginLabel=Not Permission
org.joget.tutorial.NotPermission.pluginDesc=Used to reverse the result of other permission plugin
userview.notpermission.config=Configure Not Permission
userview.notpermission.permission=Permission
userview.notpermission.loggedIn=Must be Logged In User

e.

...

注册你的插件在Felix框架中

我们将不得不在Activator类(在同一个类包中自动生成)中注册我们的插件类,以告诉Felix框架这是一个插件。We will have to register our plugin class in Activator class (Auto generated in the same class package) to tell Felix Framework that this is a plugin.

Code Block
languagejava
    public void start(BundleContext context) {
        registrationList = new ArrayList<ServiceRegistration>();
        //Register plugin here
        registrationList.add(context.registerService(NotPermission.class.getName(), new NotPermission(), null));
    }

f.

...

建设和测试

让我们建立我们的插件。一旦构建过程完成,我们将在“not_permission / target”目录下创建一个“notLet build our plugin. Once the building process is done, we will found a "not_permission-5.0.0.jar" file is created under "not_permission/target" directory.

Then, let upload the plugin jar to Manage Plugins. After upload the jar file, double check the plugin is uploaded and activated correctly.

Image Removed

jar”文件。

然后,让插件jar上传到  管理插件。上传jar文件后,仔细检查插件是否正确上传和激活。

Image Added

让我们打开一个用户视图,并将其中一个类别权限更改为不允许。我们希望当前用户不在“Managers”组中。Let us open an userview and change one of the category permission to Not Permission. We will want the current user not in a "Managers" group.

After done the configuration and save the userview. Let us test it. First, check the admin user is not in "Managers" group.

Image Removed

Check the userview, the "Personal" category configured with not in "Managers" group is shown correctly.

Image Removed

Now, assign the "admin" user to "Managers" group.

Image Removed

完成配置并保存用户视图。让我们来测试它。首先检查管理员用户是否在“Managers”组中。

Image Added

检查用户视图,在“管理员”组中未配置的“Personal”类别显示正确。

Image Added

现在,将“admin”用户分配给“Mangers”组。

Image Added

“Personal”类别现在消失了。The "Personal" category is now disappeared.

8.

...

再进一步,分享或出售

您可以从You can download the source code from not_permission.zip.下载源代码  。

要下载现成的插件jar,请在To download the ready-to-use plugin jar, please find it in http://marketplace.joget.org/.找到它  。