Scalr\Acl\Acl::setUserRoles PHP Method

setUserRoles() public method

Set roles for specified user for specified team.
public setUserRoles ( integer $teamId, integer $userId, array $accountRoles, integer $accountId = null )
$teamId integer The identifier of the team
$userId integer The identifier of the user
$accountRoles array The list of the identifiers of the roles of account level
$accountId integer optional The identifier of the account
    public function setUserRoles($teamId, $userId, $accountRoles, $accountId = null)
    {
        $accountId = intval($accountId);
        //Verify that team and user are from the same acount
        if (!empty($accountId)) {
            $check = $this->db->GetOne("\n                SELECT 1 FROM account_users WHERE id = ? AND account_id = ? LIMIT 1\n            ", array($userId, $accountId)) && $this->db->GetOne("\n                SELECT 1 FROM account_teams WHERE id = ? AND account_id = ? LIMIT 1\n            ", array($teamId, $accountId));
            if (!$check) {
                throw new Exception\AclException(sprintf('Cannot find the team "%d" or user "%d" in the account "%d"', $teamId, $userId, $accountId));
            }
        } else {
            //Retrieves identifier of the account
            $accountId = $this->db->GetOne("\n                SELECT u.account_id\n                FROM account_users u\n                JOIN account_teams t ON t.account_id = u.account_id\n                WHERE u.user_id = ? AND t.team_id = ?\n                LIMIT 1\n            ", array($userId, $accountId));
            if (!$accountId) {
                throw new Exception\AclException(sprintf('Cannot find the team "%d" or user "%d" in the account "%d"', $teamId, $userId, $accountId));
            }
        }
        $teamUserId = $this->db->GetOne("\n           SELECT tu.id\n           FROM `account_team_users` tu\n           WHERE tu.`team_id` = ? AND tu.`user_id` = ?\n           LIMIT 1\n        ", array($teamId, $userId));
        if (empty($teamUserId)) {
            $this->db->Execute("\n                INSERT IGNORE `account_team_users`\n                SET team_id = ?,\n                    user_id = ?\n            ", array($teamId, $userId));
            $teamUserId = $this->db->Insert_ID();
        } else {
            //Removes previous relations
            $this->db->Execute("\n                DELETE FROM `account_team_user_acls` WHERE account_team_user_id = ?\n            ", array($teamUserId));
        }
        if ($c = count($accountRoles)) {
            //Creates new relations
            $this->db->Execute("\n                INSERT IGNORE `account_team_user_acls` (account_team_user_id, account_role_id)\n                SELECT ?, r.account_role_id\n                FROM `acl_account_roles` r\n                WHERE r.account_id = ?\n                AND r.account_role_id IN (" . rtrim(str_repeat("?,", $c), ',') . ")\n            ", array_merge(array($teamUserId, $accountId), array_values($accountRoles)));
        }
    }