A
A
Alexander2016-02-14 22:57:12
ORM
Alexander, 2016-02-14 22:57:12

How to solve the problem with ORM authorization in Kohana 3.3.4?

There was a problem with ORM authorization in Kohana 3.3.4 Created a base controller for authorization Controller_Index_Auth which is an inheritor of the Controller_Index class, which in turn is from the base controller Controller_Base . In the base controller, the $auth field stores the Auth::instance() framework object .
base controller code

class Controller_Base extends Controller_Template {

    public $user;
    public $auth;
    public $cache;
    public $session;

    public function before() {
        parent::before();

        I18n::lang('ru');
        $settings = Kohana::$config->load('settings');
        Cookie::$salt = 'asd12d2';
        Session::$default = 'cookie';
        
        $this->cache = Cache::instance('file');
        $this->auth = Auth::instance();
        $this->user = $this->auth->get_user();
        $this->session = Session::instance();
    }
}

In the controller for processing authorization requests, Controller_Index_Auth , I created several methods. During authorization, data is received from the form and processed in the action_login() method, if everything goes well, redirection to the controller Controller_Index_Account method action_index() which should display a page with the user's personal data.
Authorization works and redirects to the page with user data, but there is a problem, already in the controller Controller_Index_Account I can’t get any information about the authorized user, the logged_in () framework method for some reason returns false? Although when I am in the controllerController_Index_Auth method $this->auth->logged_in () returns true and I can get information about the authorized user using the $this->auth->get_user() method of the framework , it’s strange, according to the idea, all data after authorization should be saved in the field $auth and I can get them in any application controller, but they are not saved!
controller code Controller_Index_Auth
class Controller_Index_Auth extends Controller_Index {

    public function action_index() {
        $this->action_login();
    }

    public function action_login() {

        if(Auth::instance()->logged_in()) {
            $this->redirect('account');
        }

        if (isset($_POST['submit'])){
            $data = Arr::extract($_POST, array('username', 'password', 'remember'));
            $status = $this->auth->login($data['username'], $data['password'], (bool) $data['remember']);


            //$role = $this->auth->logged_in();
            // var_dump($role);
            // $user =  $this->auth->get_user();
            // var_dump($user);
              
            if ($status){
                if($this->auth->logged_in('admin')) {
                    $this->redirect('admin');
                }
                
                $this->redirect('account');
            }
            else {
                $errors = array(Kohana::message('auth/user', 'no_user'));
            }
        }

        // ...
    }

    //...
}

In the controller Controller_Index_Account in the before() method there is an authorization check, but it does not work $this->auth->logged_in() returns false although it should return true.
controller code Controller_Index_Account
class Controller_Index_Account extends Controller_Index {

    public function before(){
        parent::before();
        
        if (!$this->auth->logged_in()) { // здесь идет проверка
            $this->redirect('login'); 
  
        }


        $account_menu = Widget::load('menuaccount');

         // Выводим в шаблон
        $this->template->block_right = null;
        $this->template->block_left = array($account_menu);
    }

    // ...

}

In the settings file, if you change the orm driver to file, then the authorization works fine, what could be the reason?
application/config/auth.php file code
return array(

  //'driver'       => 'file',
   'driver'       => 'orm',
  'hash_method'  => 'sha256',
  'hash_key'     => 'asjdhas6f2e12kas',
  'lifetime'     => 1209600,
  'session_key'  => 'auth_user',

  // Username/password combinations for the Auth File driver
  'users' => array(
    // 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
    'user'=>'857c303e1102231b7e4a27aaa0a2dd6495aa2510c0290781614581f483efb11b'
  ),

);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
entermix, 2016-02-23
@entermix

1. Initialize session:
before you do:

$this->auth = Auth::instance();
$this->user = $this->auth->get_user();

2. Use $this->request->query()/$this->request->post() instead of $_GET/$_POST
On the question, check if the user has the login role
And why are you using Auth: in the Controller_Index_Auth class: instance()->logged_in() if it can be accessed via $this->auth ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question