Integrate the page appearance into your AppGini application

After controlling access to your custom page, the next step is to customize its appearance so that it matches the rest of the application pages. This can be very easily achieved by including the header and footer files as follows.

Grant access to any logged user

Another case is to grant access to your page to all logged users. Here is the code for this scenario.

<?php
    define('PREPEND_PATH', '../');
    $hooks_dir = dirname(__FILE__);
    include("$hooks_dir/../defaultLang.php");
    include("$hooks_dir/../language.php");
    include("$hooks_dir/../lib.php");
 
    include_once("$hooks_dir/../header.php");
 
    /* grant access to all logged users */
    $mi = getMemberInfo();
    if(!$mi['username'] || $mi['username'] == 'guest'){
        echo "Access denied";
        exit;
    }
 
    echo "You can access this page!";
 
    include_once("$hooks_dir/../footer.php");
?>

If you’re using AppGini 5.90 or higher, you no longer need to include language files when creating a custom page. The above code would still work, but it’s recommended to change this part of the code:

include("$hooks_dir/../defaultLang.php");
include("$hooks_dir/../language.php");
include("$hooks_dir/../lib.php");

to:

include("$hooks_dir/../lib.php");

Grant access to one or more groups

In case you want all the users that belong to the “Admins” and “Data entry” groups (for example) to be able to access your custom page, let’s edit the code to read like this

<?php
    define('PREPEND_PATH', '../');
    $hooks_dir = dirname(__FILE__);
    include("$hooks_dir/../defaultLang.php");
    include("$hooks_dir/../language.php");
    include("$hooks_dir/../lib.php");
     
    /* grant access to the groups 'Admins' and 'Data entry' */
    $mi = getMemberInfo();
    if(!in_array($mi['group'], array('Admins', 'Data entry'))){
        echo "Access denied";
        exit;
    }
 
    echo "You can access this page!";
?>

Grant access to one or more users

Another case is when you want one or more specific users, rather than a whole group, to access the page. We’ll still use the getMemberInfo() function but the check will be slightly different:

<?php
    define('PREPEND_PATH', '../');
    $hooks_dir = dirname(__FILE__);
    include("$hooks_dir/../defaultLang.php");
    include("$hooks_dir/../language.php");
    include("$hooks_dir/../lib.php");
     
    /* grant access to the groups 'Admins' and 'Data entry' */
    $mi = getMemberInfo();
    if(!in_array($mi['username'], array('john.doe', 'jane.doe'))){
        echo "Access denied";
        exit;
    }
 
    echo "You can access this page!";
?>

0 Comments

Submit a Comment

Your email address will not be published.