Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Kohana]Moduł Auth - logowanie
henio
post 10.07.2009, 10:50:14
Post #1





Grupa: Zarejestrowani
Postów: 237
Pomógł: 1
Dołączył: 15.10.2004
Skąd: Lublin

Ostrzeżenie: (0%)
-----


Szukałem w google, ale niestety nigdzie nie znalazłem odpowiedniej strony i opisem.
  1. <?php
  2. class User_Controller extends Glowna_Controller
  3. {
  4.    public function __construct()
  5.    {
  6.    parent::__construct();
  7.    $this-> session = Session:: instance();
  8.    $this-> auth = Auth :: instance();
  9.    }
  10.  
  11.    public function rejestracja()
  12.    {
  13.    $post = new Validation($_POST);
  14.    $post ->pre_filter('trim')
  15.          ->pre_filter('htmlspecialchars')
  16.          ->add_rules('email', 'required', 'length[4,127]', 'valid::email')
  17.          ->add_rules('username', 'required', 'length[3,32]', 'chars[a-zA-Z0-9_.-]')
  18.          ->add_rules('password', 'required', 'length[5,42]')
  19.          ->add_rules('password2', 'required', 'matches[password]')
  20.          ->add_callbacks('email', array($this, '__email_exists'))
  21.          ->add_callbacks('username', array($this, '__user_exists'));
  22.  
  23.        if($post->validate())
  24.        {
  25.        $user = new User_Model;
  26.        $user -> load_data($post -> as_array());
  27.  
  28.            if($user->save() && $user -> add(ORM :: factory('role', 'login')))
  29.            {
  30.            $msg = Kohana :: lang('register.successful');
  31.            }
  32.            else
  33.            {
  34.            $msg = Kohana :: lang('register.error');
  35.            }
  36.        Session :: instance() -> set_flash('flash', $msg);
  37.        url :: redirect('adm/glowna');
  38.        }
  39.        else
  40.        {
  41.        $view = new View('layout/adm2');
  42.        $view->author = Kohana::config('settings.author');
  43.        $view->charset = Kohana::config('settings.charset');
  44.        $view->copyright = Kohana::config('settings.copyright');
  45.    
  46.        $view->library = new Heniek;
  47.        $view->content = new View('adm/rejestracja');
  48.        $view->content -> post = $post;
  49.        $view->content -> errors = $post -> errors();
  50.        $view->render(true);
  51.        }
  52.    }
  53.  
  54.    public function __email_exists($validator, $field)
  55.    {
  56.    $email = $validator -> $field;
  57.    $email_exists = (bool) ORM::factory('user')->where('email', $email)->count_all();
  58.        if($email_exists)
  59.        {
  60.        $validator -> add_error($field, 'email_exists');
  61.        }
  62.    }
  63.  
  64.    public function __user_exists($validator, $field)
  65.    {
  66.    $login = $validator -> $field;
  67.  
  68.    if(ORM :: factory('user') -> username_exists($login)) $validator -> add_error($field, 'user_exists');
  69.  
  70.    }
  71.  
  72.    public function loguj()
  73.    {
  74.    if($this -> auth -> logged_in()) url :: redirect('adm/glowna');
  75.    $post = new Validation($_POST);
  76.    $post -> pre_filter('trim')
  77.          -> pre_filter('htmlentities')
  78.          -> add_rules('username', 'required', 'length[3, 32]')
  79.          -> add_rules('password', 'required', 'length[3, 50]');
  80.  
  81.        if($post->validate())
  82.        {
  83.        $user = ORM :: factory('user', (string)$post['username']);
  84.        
  85.        if(!$this -> auth -> login($user, (string)$post['password'])) Session :: set_flash('login_error', Kohana :: lang('login.error'));
  86.        }
  87.        else
  88.        {
  89.        Session :: set_flash('login_error', Kohana :: lang('login.error'));
  90.        }
  91.            
  92.    url :: redirect('adm/glowna');
  93.    }
  94. }
  95. ?>

Chciałbym rozszerzyć motodę loguj o możliwość zapamiętania logowania. Chciałbym gdyby ktoś zapodał mi link do jakiejś strony z opisem albo pokazał jakiś suchy przykład. W configu ustawiłem sobie odpowiedni czas tego "pamiętania".

Ten post edytował henio 10.07.2009, 10:50:53
Go to the top of the page
+Quote Post
eS...
post 10.07.2009, 12:11:11
Post #2





Grupa: Zarejestrowani
Postów: 367
Pomógł: 2
Dołączył: 4.03.2003
Skąd: C:/Windows/Temp

Ostrzeżenie: (0%)
-----


  1. <?php
  2. if(!$this -> auth -> login($user, (string)$post['password'], TRUE)) Session :: set_flash('login_error', Kohana :: lang('login.error'));
  3. ?>


Potem metoda auto_login(), powinno zalogować automatycznie jeśli wcześniej user llogował się z 3 parametrem metody login() ustawionym na TRUE

Reszta w dokumentacji Kohany

Ten post edytował eS... 10.07.2009, 12:13:52


--------------------
Go to the top of the page
+Quote Post
henio
post 10.07.2009, 12:33:32
Post #3





Grupa: Zarejestrowani
Postów: 237
Pomógł: 1
Dołączył: 15.10.2004
Skąd: Lublin

Ostrzeżenie: (0%)
-----


Ok, tylko że dokumentacja nie wspomina gdzie konkretnie należy użyć metody auto_login.
Kod
auto_login()    auto_login() tries to login a user automatically. Only works if the user first logged in with the $remember option

Z tego co tu jest napisane wynika tylko, że ta metoda próbuje automatycznie zalogować użytkownika, jeśli ten wcześniej zalogował się z opcją zapamiętania logowania (TRUE)
Go to the top of the page
+Quote Post
eS...
post 10.07.2009, 12:37:29
Post #4





Grupa: Zarejestrowani
Postów: 367
Pomógł: 2
Dołączył: 4.03.2003
Skąd: C:/Windows/Temp

Ostrzeżenie: (0%)
-----


Hmmm, skoro stara sie zalogować automatycznie, to wypadało by ją wywołać przed metodą login(), tak na mój rozum

w skrócie:
1.auto_login() - sprawdzam czy user ma byc zalogowany automatycznie i czy istnieje cookie, jesli tak loguje i lece dalej z kodem
2. jesli nie wywoluje metode login


--------------------
Go to the top of the page
+Quote Post
henio
post 10.07.2009, 12:47:51
Post #5





Grupa: Zarejestrowani
Postów: 237
Pomógł: 1
Dołączył: 15.10.2004
Skąd: Lublin

Ostrzeżenie: (0%)
-----


Z tego co napisałeś zrozumiałem, że należy za każdym razem sprawdzać, że metodę auto_login(), czy tylko przy wciśnięciu przycisku loguj? A co gdy user ma być zapamiętany i od razu wchodzi na dowolną podstronę chcąc być już zalogowanym
Go to the top of the page
+Quote Post
eS...
post 10.07.2009, 12:51:54
Post #6





Grupa: Zarejestrowani
Postów: 367
Pomógł: 2
Dołączył: 4.03.2003
Skąd: C:/Windows/Temp

Ostrzeżenie: (0%)
-----


Za każdym razem?
Nie, tylko w kontrolerze/metodzie który służy do logowania sprawdzasz:

  1. <?php
  2. if($auth->auto_login()){
  3.  
  4.  //zalogowany, ustawiasz sessje czy co kolwiek
  5.  
  6. }else{
  7.  
  8. //wyswietlasz formularz logowania lub cos innego
  9.  
  10. }
  11. ?>


Czyli wszędzie tam gdzie byś używał Twojej metody loguj() dodaj ifa z auto_login() smile.gif


--------------------
Go to the top of the page
+Quote Post
henio
post 10.07.2009, 13:28:30
Post #7





Grupa: Zarejestrowani
Postów: 237
Pomógł: 1
Dołączył: 15.10.2004
Skąd: Lublin

Ostrzeżenie: (0%)
-----


U mnie nie ma oddzielnej strony do logowania, tylko w przypadku gdy user nie jest zalogowany to kontroler wyświetla odpowiedni widok. Dlatego nie zrozumiałem o co ci chodzi.
Go to the top of the page
+Quote Post
eS...
post 10.07.2009, 13:33:48
Post #8





Grupa: Zarejestrowani
Postów: 367
Pomógł: 2
Dołączył: 4.03.2003
Skąd: C:/Windows/Temp

Ostrzeżenie: (0%)
-----


  1. <?php
  2. public function loguj()
  3.   {
  4.   if($this -> auth -> logged_in()) {
  5.              url :: redirect('adm/glowna');
  6.   }elseif($this -> auth -> auto_login()){
  7.      // autologowanie
  8.     url::redirect('whatever');
  9.  }
  10.   $post = new Validation($_POST);
  11.   $post -> pre_filter('trim')
  12.         -> pre_filter('htmlentities')
  13.         -> add_rules('username', 'required', 'length[3, 32]')
  14.         -> add_rules('password', 'required', 'length[3, 50]');
  15.  
  16.       if($post->validate())
  17.       {
  18.       $user = ORM :: factory('user', (string)$post['username']);
  19.      
  20.       if(!$this -> auth -> login($user, (string)$post['password'])) Session :: set_flash('login_error', Kohana :: lang('login.error'));
  21.       }
  22.       else
  23.       {
  24.       Session :: set_flash('login_error', Kohana :: lang('login.error'));
  25.       }
  26.          
  27.   url :: redirect('adm/glowna');
  28.   }
  29. }
  30. ?>


Powinno pomóc


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 16.04.2024 - 09:10