Scalr\Acl\Acl::setAllRolesForTeam PHP Method

setAllRolesForTeam() public method

Set all relations between all users of this team and ACL roles
public setAllRolesForTeam ( integer $teamId, array $data = [], integer $accountId = null )
$teamId integer The ID of the team
$data array Roles array should look like array(user_id => array(account_role_id, ...))
$accountId integer optional Restricts queries to the specified account
    public function setAllRolesForTeam($teamId, array $data = array(), $accountId = null)
    {
        if ($c = count($data)) {
            //Creates required relations between users and team
            //INSERT-SELECT approach helps to avoid foreign key assertion
            $this->db->Execute("\n                INSERT IGNORE `account_team_users` (`team_id`, `user_id`)\n                SELECT " . intval($teamId) . ", `id` FROM `account_users`\n                WHERE `id` IN (" . rtrim(str_repeat('?,', $c), ',') . ")\n            ", array_keys($data));
        }
        $cur = array();
        //Fetches current relations between users and roles of this team
        $rs = $this->db->Execute("\n            SELECT atu.`id`, atu.`user_id`, tua.`account_role_id` as `role_id`\n            FROM `account_team_users` atu\n            LEFT JOIN `account_team_user_acls` tua ON tua.`account_team_user_id` = atu.`id`\n            WHERE `team_id` = ?\n        ", array($teamId));
        while ($rec = $rs->FetchRow()) {
            if (!isset($cur[$rec['user_id']])) {
                $cur[$rec['user_id']] = array('id' => $rec['id'], 'roles' => isset($rec['role_id']) ? array($rec['role_id']) : array());
            } elseif (isset($rec['role_id'])) {
                $cur[$rec['user_id']]['roles'][] = $rec['role_id'];
            }
        }
        //Removes unnecessary relations
        $toRemove = array_diff(array_keys($cur), array_keys($data));
        if (!empty($toRemove)) {
            $this->db->Execute("\n                DELETE FROM `account_team_users`\n                WHERE `team_id` = ? AND `user_id` IN(" . rtrim(str_repeat('?,', count($toRemove)), ',') . ")\n            ", array_merge(array($teamId), $toRemove));
        }
        foreach ($data as $userId => $roles) {
            //It's unbelievable
            if (!isset($cur[$userId])) {
                continue;
            }
            if (empty($roles)) {
                $roles = array();
            } elseif ($c = count($roles)) {
                //Creates all relations to ACL roles
                $this->db->Execute("\n                    INSERT IGNORE `account_team_user_acls` (`account_team_user_id`, `account_role_id`)\n                    SELECT " . intval($cur[$userId]['id']) . ", `account_role_id`\n                    FROM `acl_account_roles`\n                    WHERE `account_role_id` IN (" . rtrim(str_repeat('?,', $c), ',') . ")\n                    " . (!empty($accountId) ? " AND `account_id` = " . intval($accountId) : "") . "\n                ", $roles);
            }
            //Removes unnecessary relations to roles
            $toRemove = array_diff($cur[$userId]['roles'], $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($cur[$userId]['id']), $toRemove));
            }
        }
    }