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 How to develop a Bean Shell Hash Variable for more details steps.

ในบทช่วยสอนนี้เราจะทำตามแนวทาง guideline of developing a plugin ที่ไม่ได้รับอนุญาต โปรดอ้างอิงถึง How to develop a Bean Shell Hash Variable สำหรับขั้นตอนรายละเอียดเพิ่มเติม

1. What is the problem?

อะไรคือปัญหา?

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

ฉันต้องการกำหนดค่าการอนุญาต userview หรือการอนุญาตแบบฟอร์มไม่ให้อยู่ในกลุ่มแผนกหรืออื่น ๆ

2. What is your idea to solve the problem?

ความคิดของคุณในการแก้ปัญหาคืออะไร?

We can develop a Userview Permission/ Form Permission Plugin to reverse the result of other permission plugins.

เราสามารถพัฒนา Userview Permission/ Form Permission Plugin เพื่อย้อนกลับผลลัพธ์ของปลั๊กอินการอนุญาตอื่น ๆ

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?

มีทรัพยากร / API ที่สามารถนำกลับมาใช้ใหม่ได้หรือไม่?

You can refer to How to develop a Gantt Chart Userview Menu on how to reuse a existing plugin.

คุณสามารถอ้างถึง How to develop a Gantt Chart Userview Menu เกี่ยวกับวิธีการนำปลั๊กอินที่มีอยู่กลับมาใช้ใหม่

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 Guideline for Developing a Plugin for other platform command.

Let said our folder directory as following. 

เราจำเป็นต้องให้ซอร์สโค้ด Joget Workflow ของเราพร้อมและสร้างโดยปฏิบัติตาม this guideline

บทช่วยสอนต่อไปนี้จัดทำขึ้นด้วย Macbook Pro และ Joget Source Code เวอร์ชั่น 5.0.0 โปรดอ้างอิง Guideline for Developing a Plugin สำหรับคำสั่งแพลตฟอร์มอื่น ๆ

ให้กล่าวว่าไดเรกทอรีโฟลเดอร์ของเราดังต่อไปนี้

- 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.

Run the following command to create a maven project in "plugins" directory.

ไดเรกทอรี "ปลั๊กอิน" คือโฟลเดอร์ที่เราจะสร้างและจัดเก็บปลั๊กอินทั้งหมดของเราและไดเรกทอรี "jw-community" เป็นที่เก็บซอร์สโค้ด Joget Workflow เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างโครงการ maven ในไดเรกทอรี "ปลั๊กอิน"

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.

จากนั้นเชลล์สคริปต์จะขอให้เราป้อนเวอร์ชันสำหรับปลั๊กอินของคุณและขอให้เรายืนยันก่อนที่จะสร้างโครงการ Maven

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.

Open the maven project with your favour IDE. I will be using NetBeans.  

เราควรได้รับข้อความ "BUILD SUCCESS" ที่แสดงในเครื่องของเราและโฟลเดอร์ "not_permission" ที่สร้างในโฟลเดอร์ "plugins" เปิดโครงการ maven ด้วย IDE ที่คุณโปรดปราน ใช้ NetBeans

7. Just code it!

เพียงแค่รหัส!

a. Extending the abstract class of a plugin type

การขยายคลาสนามธรรมของประเภทปลั๊กอิน

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 Permission/ Form Permission Plugin.

สร้างคลาส "NotPermission" ภายใต้แพ็คเกจ "org.joget.tutorial" จากนั้นขยายคลาสด้วย org.joget.apps.userview.model.UserviewPermission คลาสนามธรรมและใช้อินเตอร์เฟส org.joget.apps.form.model.FormPermission โปรดอ้างอิงถึง Userview Permission/ Form Permission Plugin

b. Implement all the abstract methods

ใช้วิธีนามธรรมทั้งหมด

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.

ตามปกติเราต้องใช้วิธีนามธรรมทั้งหมด เราจะใช้วิธี AppPluginUtil.getMessage เพื่อสนับสนุน i18n และใช้ตัวแปร MESSAGE_PATH คงที่สำหรับไดเรกทอรีท

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 Plugin Properties Options definition file is locate at "/properties/notPermission.json". Let us create a directory "resources/properties" under "not_permission/src/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.

จากนั้นเราต้องทำ UI สำหรับผู้ใช้ผู้ดูแลระบบเพื่อให้อินพุตสำหรับปลั๊กอินของเรา ในเมธอด getPropertyOptions เราได้ระบุไฟล์ข้อกำหนด Plugin Properties Options ไว้ที่ "/properties/notPermission.json" แล้ว ให้เราสร้างไดเรกทอรี "resources / properties" ภายใต้ไดเรกทอรี "not_permission / src / main" หลังจากสร้างไดเรกทอรีให้สร้างไฟล์ชื่อ "notPermission.json" ในโฟลเดอร์ "properties" ในไฟล์ตัวเลือกคำจำกัดความของคุณสมบัติเราจะต้องระบุตัวเลือกดังต่อไปนี้ โปรดทราบว่าเราสามารถใช้ไวยากรณ์ "@@ message.key @@" เพื่อรองรับ i18n ในตัวเลือกคุณสมบัติของเรา

[{
    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

    @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. Manage the dependency libraries of your plugin

จัดการไลบรารีของปลั๊กอินของคุณ

There are no extra dependency for this plugin.

ไม่มีอะไรพิเศษสำหรับปลั๊กอินนี้

d. Make your plugin internationalization (i18n) ready

เตรียมปลั๊กอินสากลให้พร้อม (i18n)

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.

เรากำลังใช้คีย์ข้อความ i18n ในวิธี getLabel และ getDescription นอกจากนี้เรายังใช้คีย์ข้อความ i18n ในการกำหนดตัวเลือกคุณสมบัติของเราเช่นกัน ดังนั้นเราจะต้องสร้างไฟล์คุณสมบัติสำหรับปลั๊กอินของเรา

Create directory "resources/messages" under "not_permission/src/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.

สร้างไดเรกทอรี "ทรัพยากร / ข้อความ" ภายใต้ไดเรกทอรี "not_permission / src / main" จากนั้นสร้างไฟล์ "NotPermission.properties" ในโฟลเดอร์ ในไฟล์คุณสมบัติให้เพิ่มคีย์ข้อความทั้งหมดและป้ายกำกับของมันดังต่อไปนี้

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. Register your plugin to Felix Framework

ลงทะเบียนปลั๊กอินของคุณไปที่ Felix Framework

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.

เราจะต้องลงทะเบียนคลาสปลั๊กอินของเราในคลาส Activator (สร้างอัตโนมัติในแพ็คเกจคลาสเดียวกัน) เพื่อบอก Felix Framework ว่านี่คือปลั๊กอิน

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

f. Build it and testing

สร้างและทดสอบ

Let 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.

ให้สร้างปลั๊กอินของเรา เมื่อกระบวนการสร้างเสร็จสิ้นเราจะพบไฟล์ "not_permission-5.0.0.jar" ถูกสร้างขึ้นภายใต้ไดเรกทอรี "not_permission / target" จากนั้นให้อัปโหลด jar ปลั๊กอินไปที่ Manage Plugins หลังจากอัปโหลดไฟล์ jar ตรวจสอบอีกครั้งว่าปลั๊กอินถูกอัปโหลดและเปิดใช้งานอย่างถูกต้อง


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.

ให้เราเปิดมุมมองผู้ใช้และเปลี่ยนหนึ่งในหมวดหมู่การอนุญาตเป็นไม่อนุญาต เราต้องการให้ผู้ใช้ปัจจุบันไม่อยู่ในกลุ่ม "Managers"


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

หลังจากเสร็จสิ้นการกำหนดค่าและบันทึก userview ให้เราทดสอบมัน ก่อนอื่นให้ตรวจสอบผู้ใช้ผู้ดูแลระบบไม่ได้อยู่ในกลุ่ม "Managers"


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

ตรวจสอบ userview หมวดหมู่ "ส่วนบุคคล" ที่กำหนดค่าโดยไม่ได้อยู่ในกลุ่ม "ผู้จัดการ" จะแสดงขึ้นอย่างถูกต้อง


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

ตอนนี้กำหนดผู้ใช้ "admin" ให้กับกลุ่ม "ผู้จัดการ"


The "Personal" category is now disappeared.

หมวดหมู่ "ส่วนบุคคล" หายไปแล้ว


8. Take a step further, share it or sell it

ก้าวไปอีกขั้นแบ่งปันหรือขายมัน

You can download the source code from not_permission.zip.

To download the ready-to-use plugin jar, please find it in http://marketplace.joget.org/.

คุณสามารถดาวน์โหลดซอร์สโค้ดจาก not_permission.zip

หากต้องการดาวน์โหลด jar ปลั๊กอินที่พร้อมใช้งานโปรดค้นหาได้ที่ http://marketplace.joget.org/