<?php

class userBlockedFilter extends sfFilter
{
    public function execute($filterChain)
    {
        if(sfContext::hasInstance()){
            $context = sfContext::getInstance();
            $user = $context->getUser();
            if($user->isAuthenticated()){
                $account = $user->getAccount();
                if($account->getDeletedAt()){
                    if ($session = $user->getSession()) {
                        $session->setUserId(null);
                        $session->save();
                    }
                    $user->signOut();
                    echo json_encode(array(
                        'state' => 'error',
                        'code' => 'user_deleted',
                        'message' => 'Пользователь удалён'
                    ));
                    die();
                }
                if($account->getIsBlocked()){
                    echo json_encode(array(
                        'state' => 'error',
                        'code' => 'client_blocking',
                        'message' => 'Ваш аккаунт заблокирован',
                        'phone' => csSettings::get('phone')
                    ));
                    die();
                }
            }
        }

        $filterChain->execute();
    }
}