Scalr\Acl\Acl::deleteAccountRole PHP Method

deleteAccountRole() public method

Deletes account role
public deleteAccountRole ( string $accountRoleId, string $accountId ) : boolean
$accountRoleId string The ID of account role
$accountId string The ID of account
return boolean Returns true on success or throws an exception
    public function deleteAccountRole($accountRoleId, $accountId)
    {
        try {
            $this->db->Execute('START TRANSACTION');
            $this->db->Execute("\n                DELETE FROM `acl_account_roles` WHERE `account_role_id` = ? AND account_id = ?\n            ", array($accountRoleId, $accountId));
            $this->db->Execute('COMMIT');
        } catch (\Exception $e) {
            //There are one or more users which are associated with this ACL
            $this->db->Execute('ROLLBACK');
            if ($e instanceof \ADODB_Exception && $e->getCode() == 1451) {
                try {
                    $cnt = 0;
                    $users = '';
                    $r = $this->db->Execute("\n                        SELECT SQL_CALC_FOUND_ROWS COALESCE(IF(au.fullname != '', au.fullname, NULL), au.email) as name\n                        FROM `account_team_user_acls` tua\n                        JOIN `account_team_users` tu ON tu.`id` = tua.`account_team_user_id`\n                        JOIN `account_users` au ON au.`id` = tu.`user_id`\n                        WHERE tua.`account_role_id` = ?\n                        GROUP BY au.`id`\n                        LIMIT 3\n                    ", array($accountRoleId));
                    while ($rec = $r->FetchRow()) {
                        $users .= $rec['name'] . " ,";
                    }
                    $users = rtrim($users, " ,");
                    $cnt = $this->db->GetOne('SELECT FOUND_ROWS()');
                    if ($cnt == 0) {
                        $cntTeams = 0;
                        $teams = '';
                        $r = $this->db->Execute("\n                            SELECT SQL_CALC_FOUND_ROWS at.name\n                            FROM `account_teams` at\n                            WHERE at.`account_role_id` = ? AND at.`account_id` = ?\n                            LIMIT 3\n                        ", array($accountRoleId, $accountId));
                        while ($rec = $r->FetchRow()) {
                            $teams .= $rec['name'] . " ,";
                        }
                        $teams = rtrim($teams, " ,");
                        $cntTeams = $this->db->GetOne('SELECT FOUND_ROWS()');
                    }
                } catch (\Exception $sub) {
                }
                if ($cnt > 0) {
                    throw new Exception\AclException(sprintf("This ACL cannot be removed because there %s %d user%s (%s) to whom it is applied.", $cnt > 1 ? 'are' : 'is', $cnt, $cnt > 1 ? 's' : '', $users . ($cnt > 3 ? ' ...' : '')));
                } else {
                    throw new Exception\AclException(sprintf("This ACL cannot be removed because there %s %d team%s (%s) to which it is applied.", $cntTeams > 1 ? 'are' : 'is', $cntTeams, $cntTeams > 1 ? 's' : '', $teams . ($cntTeams > 3 ? ' ...' : '')));
                }
            } else {
                throw $e;
            }
        }
        return true;
    }