Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Controller_Auth extends Controller_Template {
- public $template = 'notebook/frontend';
- public function action_login() {
- $this->template->title = 'Logowanie';
- $form = new Form_Login();
- if ($this->request->post()) {
- if ($form->values($this->request->post())->validate()) {
- Notice::add(Notice::SUCCESS, 'Zostałeś zalogowany');
- $this->request->redirect('/');
- } else {
- Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
- }
- }
- $head = new View('auth/login');
- $head->form = $form;
- $this->template->margins = false;
- $this->template->content = $head;
- }
- public function action_logout() {
- if (Auth::instance()->logout()) {
- Notice::add(Notice::SUCCESS, 'Zostałeś wylogowany');
- } else {
- Notice::add(Notice::ERROR, 'Błąd podczas wylogowywania');
- }
- $this->request->redirect('/',401);
- }
- public function action_register() {
- $this->template->title = 'Rejestracja';
- $form = new Form_Register();
- if ($this->request->post()) {
- if ($form->values($this->request->post())->validate()) {
- //create user
- $user = ORM::factory('user')->values([
- 'username' => $this->request->post('login'),
- 'password' => $this->request->post('password_1'),
- 'email' => $this->request->post('email')
- ])->save();
- //login as user
- Auth::instance()->login(Request::current()->post('login'), Request::current()->post('password_1'),false);
- Notice::add(Notice::SUCCESS, 'Zostałeś zarejestrowany');
- $this->request->redirect('/');
- } else {
- Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
- }
- }
- $content = new View('auth/register');
- $content->form = $form;
- $this->template->margins = false;
- $this->template->content = $content;
- }
- public function action_remind() {
- $this->template->title = 'Przypominanie hasła';
- $form = new Form_Remind();
- if ($this->request->post()) {
- if ($form->values($this->request->post())->validate()) {
- $user = ORM::factory('user')
- ->where('email', '=', $this->request->post('email'))
- ->find();
- if ($user->loaded()) {
- $recovery = ORM::factory('users_recovery');
- $recovery->values([
- 'user_id' => $user->id
- ]) -> save();
- }
- Notice::add(Notice::SUCCESS, 'Wysłano maila z nowym hasłem');
- $this->request->redirect('/');
- } else {
- Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
- }
- }
- $content = new View('auth/remind');
- $content->form = $form;
- $this->template->margins = false;
- $this->template->content = $content;
- }
- public function action_restore() {
- $this->template->title = 'Ustawianie nowego hasła';
- $form = new Form_Restore();
- $token = $this->request->query('token');
- $remind = ORM::factory('users_recovery')
- ->where('token','=',$token)
- ->where('token_expire','>=', date('Y-m-d H:i:s'))
- ->where('token_used', '=', 0)
- ->find();
- if(!$remind->loaded()){
- Notice::add(Notice::ERROR, 'Nieprawidłowy token');
- $this->request->redirect('/');
- }
- if($this->request->post()){
- if ($form->values($this->request->post())->validate()) {
- $user = ORM::factory('user')
- ->where('id', '=', $remind->user_id)
- ->find();
- $user->values([
- 'password' => $this->request->post('password_1'),
- ])->save();
- //set every recovery passwords tokens as used
- $reminds = ORM::factory('users_recovery')
- ->where('token','=',$token)
- ->where('token_expire','>=', date('Y-m-d H:i:s'))
- ->where('token_used', '=', 0)
- ->find_all();
- foreach ($reminds as $_remind) {
- $remind->values([
- 'token_used' => 1
- ])->save();
- }
- Auth::instance()->force_login($user);
- Notice::add(Notice::SUCCESS, 'Hasło zostało zmienione, zostałeś zalogowany');
- $this->request->redirect('/');
- }
- else {
- Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
- }
- }
- $content = new View('auth/restore');
- $content->form = $form;
- $this->template->margins = false;
- $this->template->content = $content;
- }
- public function action_account() {
- $this->template->title = 'Ustawienia';
- $passwords_form = new Form_Change_Password();
- $email_form = new Form_Change_Email();
- if ($this->request->post()) {
- if ($passwords_form->sent()) {
- if ($passwords_form->values($this->request->post())->validate()) {
- $user = Auth::instance()->get_user();
- $user->password = $passwords_form->child('password_1')->value();
- $user->save();
- //trigger change password
- ORM::factory('user', $user->id)->events()->trigger('change_password');
- Notice::add(Notice::SUCCESS, 'Zmieniłem hasło');
- } else {
- Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
- }
- }
- if ($email_form->sent()) {
- if ($email_form->values($this->request->post())->validate()) {
- //inaczej nie chciało działać :(
- $user = ORM::factory('user', Auth::instance()->get_user()->id);
- $user->email = $email_form->child('email')->value();
- $user->save();
- //trigger change email
- ORM::factory('user', $user->id)->events()->trigger('change_email');
- Notice::add(Notice::SUCCESS, 'Zmieniłem adres email');
- } else {
- Notice::add(Notice::ERROR, 'Formularz został źle wypełniony');
- }
- }
- }
- $content = new View('auth/account');
- $content->passwords_form = $passwords_form;
- $content->email_form = $email_form;
- $this->template->margins = false;
- $this->template->content = $content;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement