Skip to content
Snippets Groups Projects
Sign.php 4.71 KiB
Newer Older
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Handler_Sign extends Controller_Handler {
        
        public function before() {
        }

        public function action_in()
        {
            $checkLogin = true;
            $response['success'] = false;
                $this->post = Validation::factory($this->post->data());
                $this->post->rule('login', 'alpha_dash')->rule('login', 'not_empty');
                if(!$this->post->check())
                {
xamgore's avatar
xamgore committed
                    // Data is not safe, don't even try to authorize
                    $checkLogin = false;
            if($checkLogin)
                        ->signIn($this->post['login'],
                                $this->post['password']);
        public function action_up()
        {
            $config = Kohana::$config->load('security.securityPolicy');
            $response['success'] = false;
                ->rule('login', 'not_empty')
                ->rule('password', 'min_length', array(':value', $config['password']['length']))
                ->rule('confirm_password', 'matches', array(':validation', 'confirm_password', 'password'))
                ->rule('email', 'not_empty')
                        ->signUp($this->post['activation_code'],
                                $this->post['email'],
                                $this->post['login'],
                                $this->post['password']);
                        case 'login_exists':
                            $this->post->error('login', 'already_exists');
                        break;
                        case 'mail_exists':
                            $this->post->error('email', 'already_exists');
                        break;
                        case 'invalid_code':
                            $this->post->error('activation_code', 'invalid_code');
                        break;
                    }
                }
            }
            if(!$response['success'])
            {
                $response['errors'] = $this->post->errors ('signin');
            }
            $this->response->body(json_encode($response));
        }        
        public function action_remindPassword()
        {
            $response['success'] = false;
                $email = $this->post['email'];
                if(Account::isMailValid($email))
                    Account::createRecoveryRequest($this->post['email']);
                else
                    $response['error'] = 'Пользователь с таким e-mail адресом не зарегистрирован в системе!';
            else
                $response['error'] = 'Введенная строка не является e-mail адресом!';

            $this->response->body(json_encode($response));
        }
        
        public function action_changePassword()
            $config = Kohana::$config->load('security.securityPolicy');
            $response['success'] = false;
            $this->post->rule('token', 'alpha_numeric')
                ->rule('password', 'min_length', array(':value', $config['password']['length']))
                ->rule('confirm_password', 'matches', array(':validation', 'confirm_password', 'password'));
            if($this->post->check())
            {
                $token = $this->post['token'];
                if(Account::checkToken($token))
                    Account::changePasswordByToken($token, $this->post['password']);
                    $response['success'] = true;
                }
            }
            if(!$response['success'])
            {
                $response['errors'] = $this->post->errors ('signin');
            }
            $this->response->body(json_encode($response));