Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.22 KB | None | 0 0
  1. <?php
  2.  
  3. class Controller_Auth extends Controller_Template {
  4.     public $template = 'notebook/frontend';
  5.    
  6.     public function action_login() {
  7.         $this->template->title = 'Logowanie';
  8.         $form = new Form_Login();
  9.        
  10.         if ($this->request->post()) {
  11.             if ($form->values($this->request->post())->validate()) {
  12.                 Notice::add(Notice::SUCCESS, 'Zostałeś zalogowany');
  13.                 $this->request->redirect('/');
  14.             } else {
  15.                 Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
  16.             }
  17.         }
  18.        
  19.         $head = new View('auth/login');
  20.         $head->form = $form;
  21.         $this->template->margins = false;
  22.         $this->template->content = $head;
  23.     }
  24.    
  25.     public function action_logout() {
  26.         if (Auth::instance()->logout()) {
  27.             Notice::add(Notice::SUCCESS, 'Zostałeś wylogowany');
  28.         } else {
  29.             Notice::add(Notice::ERROR, 'Błąd podczas wylogowywania');
  30.         }
  31.         $this->request->redirect('/',401);
  32.     }
  33.    
  34.     public function action_register() {
  35.         $this->template->title = 'Rejestracja';
  36.         $form = new Form_Register();
  37.        
  38.         if ($this->request->post()) {
  39.             if ($form->values($this->request->post())->validate()) {
  40.                 //create user
  41.                 $user = ORM::factory('user')->values([
  42.                     'username' => $this->request->post('login'),
  43.                     'password' => $this->request->post('password_1'),
  44.                     'email'    => $this->request->post('email')
  45.                 ])->save();
  46.                
  47.                 //login as user
  48.                 Auth::instance()->login(Request::current()->post('login'), Request::current()->post('password_1'),false);
  49.                 Notice::add(Notice::SUCCESS, 'Zostałeś zarejestrowany');
  50.                 $this->request->redirect('/');
  51.             } else {
  52.                 Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
  53.             }
  54.         }
  55.  
  56.         $content = new View('auth/register');
  57.         $content->form = $form;
  58.         $this->template->margins = false;
  59.         $this->template->content = $content;
  60.     }
  61.    
  62.     public function action_remind() {
  63.         $this->template->title = 'Przypominanie hasła';
  64.         $form = new Form_Remind();
  65.  
  66.         if ($this->request->post()) {
  67.             if ($form->values($this->request->post())->validate()) {
  68.                     $user = ORM::factory('user')
  69.                                 ->where('email', '=', $this->request->post('email'))
  70.                                 ->find();
  71.                     if ($user->loaded()) {
  72.                         $recovery = ORM::factory('users_recovery');
  73.                         $recovery->values([
  74.                             'user_id' => $user->id
  75.                         ]) -> save();
  76.                     }
  77.                     Notice::add(Notice::SUCCESS, 'Wysłano maila z nowym hasłem');
  78.                     $this->request->redirect('/');
  79.             } else {
  80.                 Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
  81.             }
  82.         }
  83.  
  84.         $content = new View('auth/remind');
  85.         $content->form = $form;
  86.         $this->template->margins = false;
  87.         $this->template->content = $content;
  88.     }
  89.    
  90.     public function action_restore() {
  91.         $this->template->title = 'Ustawianie nowego hasła';
  92.         $form = new Form_Restore();
  93.  
  94.         $token = $this->request->query('token');
  95.         $remind = ORM::factory('users_recovery')
  96.                 ->where('token','=',$token)
  97.                 ->where('token_expire','>=',  date('Y-m-d H:i:s'))
  98.                 ->where('token_used', '=', 0)
  99.                 ->find();
  100.  
  101.         if(!$remind->loaded()){
  102.             Notice::add(Notice::ERROR, 'Nieprawidłowy token');
  103.             $this->request->redirect('/');
  104.         }
  105.  
  106.         if($this->request->post()){
  107.             if ($form->values($this->request->post())->validate()) {
  108.                 $user = ORM::factory('user')
  109.                     ->where('id', '=', $remind->user_id)
  110.                     ->find();
  111.                 $user->values([
  112.                     'password' => $this->request->post('password_1'),
  113.                 ])->save();
  114.  
  115.                 //set every recovery passwords tokens as used
  116.                 $reminds = ORM::factory('users_recovery')
  117.                 ->where('token','=',$token)
  118.                 ->where('token_expire','>=',  date('Y-m-d H:i:s'))
  119.                 ->where('token_used', '=', 0)
  120.                 ->find_all();
  121.                 foreach ($reminds as $_remind) {
  122.                     $remind->values([
  123.                         'token_used' => 1
  124.                     ])->save();
  125.                 }
  126.  
  127.                 Auth::instance()->force_login($user);
  128.                 Notice::add(Notice::SUCCESS, 'Hasło zostało zmienione, zostałeś zalogowany');
  129.                 $this->request->redirect('/');
  130.             }
  131.             else {
  132.                 Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
  133.             }
  134.         }
  135.  
  136.  
  137.         $content = new View('auth/restore');
  138.         $content->form = $form;
  139.         $this->template->margins = false;
  140.         $this->template->content = $content;
  141.     }
  142.    
  143.     public function action_account() {
  144.         $this->template->title = 'Ustawienia';
  145.        
  146.         $passwords_form = new Form_Change_Password();
  147.         $email_form = new Form_Change_Email();
  148.        
  149.         if ($this->request->post()) {
  150.             if ($passwords_form->sent()) {
  151.                 if ($passwords_form->values($this->request->post())->validate()) {
  152.                     $user = Auth::instance()->get_user();
  153.                     $user->password = $passwords_form->child('password_1')->value();
  154.                     $user->save();
  155.                     //trigger change password
  156.                     ORM::factory('user', $user->id)->events()->trigger('change_password');
  157.                    
  158.                     Notice::add(Notice::SUCCESS, 'Zmieniłem hasło');
  159.                 } else {
  160.                     Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
  161.                 }
  162.                
  163.             }
  164.            
  165.             if ($email_form->sent()) {
  166.                 if ($email_form->values($this->request->post())->validate()) {
  167.  
  168.                     //inaczej nie chciało działać :(
  169.                     $user = ORM::factory('user', Auth::instance()->get_user()->id);
  170.                     $user->email = $email_form->child('email')->value();
  171.                     $user->save();
  172.                     //trigger change email
  173.                     ORM::factory('user', $user->id)->events()->trigger('change_email');
  174.                    
  175.                     Notice::add(Notice::SUCCESS, 'Zmieniłem adres email');
  176.                 } else {
  177.                     Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
  178.                 }
  179.             }
  180.         }
  181.        
  182.         $content = new View('auth/account');
  183.         $content->passwords_form = $passwords_form;
  184.         $content->email_form = $email_form;
  185.         $this->template->margins = false;
  186.         $this->template->content = $content;
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement