Kohana 3: модуль Auth - PullRequest
       24

Kohana 3: модуль Auth

1 голос
/ 09 июня 2010

Я пытаюсь изучить модуль Auth Kohana, но метод входа всегда возвращает false.

Контроллер:

<?php defined('SYSPATH') OR die('No Direct Script Access');
class Controller_Auth extends Controller {
    public function action_index() {
        if($_POST) {
            $this->login();
        }

        $this->template = View::factory('login');
        echo $this->template;
    }

    private function login() {
    $user = ORM::factory('user');

        $data = array('username' => 'wilson', 'password' => '123');
        if(!$user->login($data)) {
            echo 'FAILED!';
        }
    }

    private function logout() {

    }
}    
?>

Модель:

<?php defined('SYSPATH')  or die('No direct script access.');
class Model_User extends Model_Auth_User {
}
?>

Ответы [ 2 ]

1 голос
/ 11 июня 2010

Я не добавил правила для пользователя.Для получения дополнительной информации, [см. Эту ссылку] [1].

Спасибо, ребята, и извините за это:)

http://webcache.googleusercontent.com/search?q=cache:kXKFWswjDogJ:kerkness.ca/kowiki/doku.php%3Fid%3Dusing_the_auth_module_in_your_controllers+&cd=1&hl=en&ct=clnk&gl=us

public function action_signin()
{
    #If user already signed-in
    if(Auth::instance()->logged_in()!= 0){
        #redirect to the user account
        Request::instance()->redirect('account/myaccount');     
    }

    $content = $this->template->content = View::factory('signin');  

    #If there is a post and $_POST is not empty
    if ($_POST)
    {
        #Instantiate a new user
        $user = ORM::factory('user');

        #Check Auth
        $status = $user->login($_POST);

        #If the post data validates using the rules setup in the user model
        if ($status)
        {       
            #redirect to the user account
            Request::instance()->redirect('account/myaccount');
        }else
        {
                            #Get errors for display in view
            $content->errors = $_POST->errors('signin');
        }

    }
}
1 голос
/ 10 июня 2010

Вы делаете это неправильно.

Чтобы войти в систему пользователя, сделайте что-то вроде этого:

$auth = Auth::instance();
if ($auth->login($_POST['username'], $_POST['password']))
{
      echo 'hello, '.$auth->$_POST['username'];
}
else
{
      echo 'login failed!';
}

Кроме того, раскомментируйте строку модуля auth в загрузчике вашего приложения:

'auth'       => MODPATH.'auth',       // Basic authentication
...