예제 #1
0
 public function auth_save(Context $context)
 {
     $response = new JsonResponse();
     try {
         $auth = Configuracao::get($this->em(), AuthenticationProvider::KEY);
         $value = $auth->getValor();
         $type = $context->request()->post('type');
         $value['type'] = $type;
         if (!isset($value[$type])) {
             $value[$type] = array();
         }
         foreach ($_POST as $k => $v) {
             $value[$type][$k] = $v;
         }
         $auth = App::authenticationFactory()->create($context, $value);
         if (!$auth) {
             throw new \Exception(_('Opção inválida'));
         }
         $auth->test();
         Configuracao::set($this->em(), AuthenticationProvider::KEY, $value);
         $response->success = true;
     } catch (\Exception $e) {
         $response->message = $e->getMessage();
     }
     return $response;
 }
예제 #2
0
 public function validate(Context $context)
 {
     $username = $context->request()->post('username');
     $password = $context->request()->post('password');
     $error = null;
     if (!empty($username) && !empty($password)) {
         $em = $context->database()->createEntityManager();
         $config = \Novosga\Model\Configuracao::get($em, \Novosga\Auth\AuthenticationProvider::KEY);
         $auth = $config ? $config->getValor() : array();
         $provider = App::authenticationFactory()->create($context, $auth);
         $user = $provider->auth($username, $password);
         if ($user) {
             // atualizando o session id
             $user->setSessionId(session_id());
             $user->setUltimoAcesso(new \DateTime());
             $em->merge($user);
             $em->flush();
             // caso o usuario so tenha acesso a uma unica unidade, ja define como atual
             $us = new \Novosga\Model\Util\UsuarioSessao($user);
             $unidades = $this->app()->getAcessoService()->unidades($context, $us);
             if (sizeof($unidades) == 1) {
                 $us->setUnidade($unidades[0]);
             }
             $context->setUser($us);
         } else {
             $error = _('Usuário ou senha inválido');
         }
     }
     if (!$error) {
         $this->app()->gotoHome();
     } else {
         $this->app()->flash('error', $error);
         $this->app()->gotoLogin();
     }
 }