Example #1
0
         $query = "SELECT LOWER(ua.`organisation_id`) as `organisation_id`, lower(ua.`group`) as `group`, lower(ua.`role`) as `role`\n\t\t\t\t\t\t\t  FROM `" . AUTH_DATABASE . "`.`user_access` ua\n\t\t\t\t\t\t\t  WHERE ua.`user_id` = " . $PROXY_ID;
         $my_orgs_groups_roles = $db->GetAll($query);
         break;
 }
 // Display Page.
 switch ($STEP) {
     case 2:
         if ($NOTICE) {
             echo display_notice();
         }
         if ($SUCCESS) {
             $query = "SELECT *\n\t\t\t\t\t\t\t\t  FROM `" . AUTH_DATABASE . "`.`user_access` a\n\t\t\t\t\t\t\t\t  WHERE a.`user_id` = " . $db->qstr($ENTRADA_USER->getID()) . "\n\t\t\t\t\t\t\t\t  AND a.`organisation_id` = " . $db->qstr($_SESSION["tmp"]["current_org"]) . "\n\t\t\t\t\t\t\t\t  AND a.`group` = " . $db->qstr($_SESSION["tmp"]["current_group"]) . "\n\t\t\t\t\t\t\t\t  AND a.`role` = " . $db->qstr($_SESSION["tmp"]["current_role"]);
             $result = $db->getRow($query);
             if ($result) {
                 $ENTRADA_USER->setAccessId($result["id"]);
                 $_SESSION["permissions"] = permissions_load();
             }
             unset($ENTRADA_ACL);
             $ENTRADA_ACL = new Entrada_Acl($_SESSION["details"]);
             $ENTRADA_CACHE->remove("acl_" . $ENTRADA_USER->getID());
             $ENTRADA_CACHE->save($ENTRADA_ACL, "acl_" . $ENTRADA_USER->getID());
             echo display_success();
         }
         break;
     case 1:
     default:
         $query = "SELECT *\n\t\t\t\t\t\t\t  FROM `" . AUTH_DATABASE . "`.`user_access` a\n\t\t\t\t\t\t\t  WHERE a.`user_id` = " . $db->qstr($ENTRADA_USER->getID()) . "\n\t\t\t\t\t\t\t  AND a.`id` = " . $db->qstr($ENTRADA_USER->getAccessId());
         $result = $db->getRow($query);
         if ($result) {
             $current_org = $result["organisation_id"];
             $current_group = $result["group"];
/**
 * Load the active organisation for the user including their permissions,
 * template, system groups, etc.
 *
 * @global type $ENTRADA_USER
 * @global type $ENTRADA_TEMPLATE
 * @global type $SYSTEM_GROUPS
 * @global object $db
 * @param type $organisation_id
 * @param type $user_access_id
 */
function load_active_organisation($organisation_id = 0, $user_access_id = 0)
{
    global $ENTRADA_USER, $ENTRADA_TEMPLATE, $SYSTEM_GROUPS, $db;
    $allow_organisation_id_set = false;
    $allow_access_id_set = false;
    $change_organisations = true;
    $organisation_id = (int) $organisation_id;
    $user_access_id = (int) $user_access_id;
    if ($ENTRADA_USER && $ENTRADA_TEMPLATE) {
        $_SESSION["permissions"] = permissions_load();
        /**
         * Load active organisation from preferences if one exists.
         */
        $active_organisation = preferences_load("organisation_switcher");
        /**
         * Check whether we are trying to set a new org and access_id or use one
         * from user preferences, or the default.
         */
        if (!$organisation_id || !$user_access_id) {
            if (isset($active_organisation["organisation_id"]) && isset($active_organisation["access_id"])) {
                $organisation_id = (int) $active_organisation["organisation_id"];
                $user_access_id = (int) $active_organisation["access_id"];
            } else {
                $organisation_id = $ENTRADA_USER->getActiveOrganisation();
                $user_access_id = $ENTRADA_USER->getAccessId();
            }
        }
        /**
         * Interate through existing permissions to ensure
         */
        foreach ($_SESSION["permissions"] as $access_id => $permission) {
            if ($permission["organisation_id"] == $organisation_id) {
                $allow_organisation_id_set = true;
                if ($access_id == $user_access_id) {
                    $allow_access_id_set = true;
                }
            }
        }
        if ($allow_organisation_id_set && $allow_access_id_set) {
            $ENTRADA_USER->setActiveOrganisation($organisation_id);
            $ENTRADA_USER->setAccessId($user_access_id);
            $_SESSION[APPLICATION_IDENTIFIER]["organisation_switcher"]["organisation_id"] = $organisation_id;
            $_SESSION[APPLICATION_IDENTIFIER]["organisation_switcher"]["access_id"] = $user_access_id;
            application_log("success", "User [" . $ENTRADA_USER->getId() . "] loaded organisation [" . $organisation_id . "] and access_id [" . $user_access_id . "] successfully.");
        } else {
            application_log("error", "User [" . $ENTRADA_USER->getId() . "] attempted to change to organisation [" . $organisation_id . "] and access_id [" . $user_access_id . "] but was unsuccessful.");
        }
        /**
         * Returns all of the system groups and roles associated with this user
         * within the active organisation.
         */
        $query = "SELECT a.*\n                  FROM `" . AUTH_DATABASE . "`.`system_groups` AS a,\n                  `" . AUTH_DATABASE . "`.`system_group_organisation` AS c\n                  WHERE a.`id` = c.`groups_id`\n                  AND c.`organisation_id` = " . $db->qstr($ENTRADA_USER->getActiveOrganisation()) . "\n                  ORDER BY a.`group_name` ASC";
        $results = $db->GetAll($query);
        if ($results) {
            foreach ($results as $result) {
                $SYSTEM_GROUPS[$result["group_name"]] = array();
                $query = "SELECT a.*\n                            FROM `" . AUTH_DATABASE . "`.`system_roles` a\n                            WHERE a.`groups_id` = " . $result["id"] . "\n                            ORDER BY a.`role_name` ASC";
                $roles = $db->GetAll($query);
                if ($roles) {
                    foreach ($roles as $role) {
                        $SYSTEM_GROUPS[$result["group_name"]][] = $role["role_name"];
                    }
                }
            }
        }
        preferences_update("organisation_switcher", $active_organisation);
        $ENTRADA_TEMPLATE->setActiveTemplate($ENTRADA_USER->getActiveOrganisation());
    }
}