<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * @package    symfony
 * @subpackage plugin
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: BasesfGuardAuthActions.class.php 19772 2009-07-01 08:41:12Z fabien $
 */
class BasesfGuardAuthActions extends sfActions
{
  public function executeSignin($request)
  {
    $user = $this->getUser();
    if ($user->isAuthenticated())
    {
      return $this->redirect('@homepage');
    }

    if ($request->isXmlHttpRequest())
    {
      $this->getResponse()->setHeaderOnly(true);
      $this->getResponse()->setStatusCode(401);

      return sfView::NONE;
    }

    $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
    $this->form = new $class();

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('signin'));
      if ($this->form->isValid())
      {
        $values = $this->form->getValues();
        $this->getUser()->signin($values['user'], array_key_exists('remember', $values) ? $values['remember'] : false);

        // always redirect to a URL set in app.yml
        // or to the referer
        // or to the homepage
        //$signinUrl = sfConfig::get('app_sf_guard_plugin_success_signin_url', $user->getReferer('@homepage'));
        //echo $_SERVER["REQUEST_URI"]."".$_SERVER["REQUEST_URI"];exit;
        //echo $_SERVER["SERVER_NAME"]."/trabajadorSocial_dev.php/login";exit;
        //return $this->redirect("http://".$_SERVER["SERVER_NAME"]."/trabajadorSocial_dev.php/login");
        $urlH = "";
        if(!stripos($_SERVER["SERVER_NAME"], "http")){
          $urlH = "http://";
        }
        $urlH .= $_SERVER["SERVER_NAME"];


        if($this->getUser()->hasCredential(array('ADMIN', 'is_super_admin'),false)){
          $urlH .= "/backend";
          if(stripos($_SERVER["REQUEST_URI"], "dev")){
            $urlH .= "_dev.php";
          } else {
            $urlH .= ".php";
          }
        }
        if($this->getUser()->hasCredential("TS")){
          $urlH .= "/trabajadorSocial";
          if(stripos($_SERVER["REQUEST_URI"], "dev")){
            $urlH .= "_dev.php";
          } else {
            $urlH .= ".php";
          }
        }
        if($this->getUser()->hasCredential("AD")){
          $urlH .= "/administrativo";
          if(stripos($_SERVER["REQUEST_URI"], "dev")){
            $urlH .= "_dev.php";
          } else {
            $urlH .= ".php";
          }
        }
        
        return $this->redirect($urlH);
       }
    }
    else
    {
      // if we have been forwarded, then the referer is the current URL
      // if not, this is the referer of the current request
      $user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());

      $module = sfConfig::get('sf_login_module');
      if ($this->getModuleName() != $module)
      {
        return $this->redirect($module.'/'.sfConfig::get('sf_login_action'));
      }

      $this->getResponse()->setStatusCode(401);
    }
  }

  public function executeSignout($request)
  {
    $this->getUser()->signOut();

    $signoutUrl = sfConfig::get('app_sf_guard_plugin_success_signout_url', $request->getReferer());

    $this->redirect('' != $signoutUrl ? $signoutUrl : '@homepage');
  }

  public function executeSecure()
  {
    $this->getResponse()->setStatusCode(403);
  }

  public function executePassword()
  {
    throw new sfException('This method is not yet implemented.');
  }
}
