Scalr\Acl\Acl::setAllRolesForUser PHP Method

setAllRolesForUser() public method

This method modifies resords of two tables account_team_users and account_team_user_acls. Attention! It expects full list of the ACL roles relations for user. All missing relations will be removed.
public setAllRolesForUser ( integer $userId, array $data = [], integer $accountId = null )
$userId integer The ID of the user
$data array ACL roles array which looks like array(teamId => array(accountRoleId1, accountRoleId2, ...))
$accountId integer optional The ID of the account. Restricts queries to the specified account.
    public function setAllRolesForUser($userId, array $data = array(), $accountId = null)
    {
        $tu = array();
        $rs = $this->db->Execute("\n            SELECT tu.`id`, tu.`team_id` FROM `account_team_users` tu WHERE tu.`user_id` = ?\n        ", array($userId));
        while ($rec = $rs->FetchRow()) {
            $tu[$rec['team_id']] = $rec['id'];
        }
        //Useless relations between teems
        $toRemove = array_diff(array_keys($tu), array_keys($data));
        if (!empty($toRemove)) {
            $this->db->Execute("\n                DELETE FROM `account_team_users`\n                WHERE `user_id` = ?\n                AND `team_id` IN (" . rtrim(str_repeat("?,", count($toRemove)), ',') . ")\n            ", array_merge(array($userId), $toRemove));
        }
        foreach ($data as $teamId => $roles) {
            if (empty($roles)) {
                $roles = array();
            }
            if (!isset($tu[$teamId])) {
                //Relation between user and team has to be created
                $this->db->Execute("\n                    INSERT IGNORE `account_team_users` (`user_id`, `team_id`) VALUES (?, ?)\n                ", array($userId, $teamId));
                $tu[$teamId] = $this->db->Insert_ID();
                $tua = array();
            } else {
                $tua = array_map(function ($value) {
                    return $value['account_role_id'];
                }, $this->db->GetAll("\n                    SELECT account_role_id FROM `account_team_user_acls` WHERE `account_team_user_id` = ?\n                ", array($tu[$teamId])));
            }
            //Unnecessary relations with roles
            $toRemove = array_diff($tua, array_values($roles));
            if (!empty($toRemove)) {
                $this->db->Execute("\n                    DELETE FROM `account_team_user_acls`\n                    WHERE `account_team_user_id` = ?\n                    AND `account_role_id` IN (" . rtrim(str_repeat("?,", count($toRemove)), ',') . ")\n                ", array_merge(array($tu[$teamId]), $toRemove));
            }
            if ($c = count($roles)) {
                //INSERT-SELECT approach avoids missing foreign keys assertions
                $this->db->Execute("\n                    INSERT IGNORE `account_team_user_acls` (`account_team_user_id`, `account_role_id`)\n                    SELECT '" . $tu[$teamId] . "', `account_role_id` FROM `acl_account_roles`\n                    WHERE `account_role_id` IN (" . rtrim(str_repeat("?,", $c), ',') . ")\n                    " . (!empty($accountId) ? " AND `account_id` = " . intval($accountId) : "") . "\n                ", array_values($roles));
            }
        }
    }