You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Current »

Introduction

This article shows a way to show or hide menu based on access codes set by admin daily for users to enter.

How To

1. Create forms for admin and users

For Admin, they should be able to set up the access codes and menus for specific users.

Figure 1:  Admin Manage Access Form

  • Hidden date field is to get the current date to have daily access codes.
    • Default Value: #date.yyyy-MM-dd#

Figure 2: User Check Access Form

2. Create The Lists

Figure 3: Lists Created

3. Create The UI with Menus

Figure 3: UI Builder With 3 Menus

4. Attach BeanShell Tool to Each Menu

Figure 4: A Menu With BeanShell Tool Attached

The following code gives each menu the ability to display on certain SQL query conditions met:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.joget.apps.app.service.AppUtil;
import org.joget.commons.util.LogUtil;
import org.joget.workflow.util.WorkflowUtil;
 
public static boolean checkAccess(String currentUser) {
        Connection con = null;
        boolean hasAccess = false;
        boolean isMenuEnabled = false;
         
        // Check if the user is an admin
        if (WorkflowUtil.isCurrentUserInRole("ROLE_ADMIN")) {
            return true; // Admins have access regardless of the database check
        }
 
        try {
            // Retrieve connection from the default datasource
            DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource");
            con = ds.getConnection();
 
            // Execute SQL query
            if (!con.isClosed()) {
                // Change c_menu_1_enable to the appropriate menu id
                String query = "SELECT DISTINCT a.c_users, a.c_menu_1_enable " +
                               "FROM app_fd_mac_permission AS a " + // Manage Access Form
                               "JOIN app_fd_mac__check_access AS b " + // Check Access Form
                                // Check if User, Access Codes and Date are found and matched from the two forms
                               "ON a.c_users = b.c_User AND a.c_ac1 = b.c_ac1 AND a.c_ac2 = b.c_ac2 AND a.c_ac3 = b.c_ac3 AND a.c_daydate = b.c_daydate " +
                               "WHERE a.c_users = ?";
                PreparedStatement stmt = con.prepareStatement(query);
                stmt.setString(1, currentUser);
                ResultSet rs = stmt.executeQuery();
 
                // Check if any record matches
                if (rs.next()) {
                    hasAccess = true;
                    if (rs.getBoolean("a.c_menu_1_enable")) { // Change c_menu_1_enable to the appropriate menu id
                        isMenuEnabled = true;
                        break;  // If any of the entries have menu enabled, set it to true
                    }
                }
            }
        } catch (Exception e) {
            LogUtil.error("AccessChecker", e, "Error checking access");
        } finally {
            // Always close the connection after use
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                // Ignored
            }
        }
 
        return hasAccess && isMenuEnabled;
    }
     
return checkAccess(WorkflowUtil.getCurrentUsername());

Results

For example, Admin sets up access for user Cat, with three codes (62, 95, 21) and give the user access to specific menus if entered correctly by their side.

Figure 5: Admin Set Up For Cat Grant


Figure 6: Cat Grant Checking Access


Figure 7: Menus Displayed For Cat

Sample App

APP_kb_dx8_menuAccessCodes.jwa


  • No labels