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

xamgore's avatar
xamgore committed
class Controller_Handler_Sign extends Controller_Handler
{
    public function before() {
        parent::before();
    }

    public function action_in() {
        $ok = User::instance()->signIn($_POST['login'], $_POST['password']);
        if (!$ok) $this->fail();
xamgore's avatar
xamgore committed
    }

    public function action_up() {
        $config = Kohana::$config->load('security.securityPolicy');

        $this->post
xamgore's avatar
xamgore committed
            ->rule('login', 'not_empty')
            ->rule('activation_code', 'alpha_numeric')
xamgore's avatar
xamgore committed
            ->rule('password', 'min_length', [':value', $config['password']['length']])
            ->rule('confirm_password', 'matches', [':validation', 'confirm_password', 'password'])
            ->rule('email', 'not_empty')
            ->rule('email', 'email');

        if (!$this->post->check())
            $this->fail($this->post->errors('signin'));
        if ($err = User::instance()->signUp($_POST['activation_code'], $_POST['email'], $_POST['login'], $_POST['password']))
            $this->fail(I18n::get($err));
xamgore's avatar
xamgore committed
    }

    public function action_remindPassword() {
        $this->post->rule('email', 'not_empty')->rule('email', 'email');
        
        if (!$this->post->check())
             $this->fail('Введенная строка не является <span>e‑mail</span> адресом!');
        
        if (!Account::doesEmailExist($_POST['email']))
            $this->fail('Пользователь с таким <span>e-mail</span> адресом не зарегистрирован в системе!');
        
        Account::createRecoveryRequest($_POST['email']);
xamgore's avatar
xamgore committed
    }

    public function action_changePassword() {
        $res['success'] = false;

        $config = Kohana::$config->load('security.securityPolicy');

        $this->post->rule('token', 'alpha_numeric')
            ->rule('password', 'min_length', [':value', $config['password']['length']])
            ->rule('confirm_password', 'matches', [':validation', 'confirm_password', 'password']);

        if ($this->post->check()) {
            $token = $_POST['token'];
            if (Account::checkToken($token)) {
                Account::changePasswordByToken($token, $_POST['password']);
                $res['success'] = true;
            }
        }

        if (!$res['success']) {
            $res['errors'] = $this->post->errors('signin');
        }

        $this->response->body(json_encode($res));
    }
xamgore's avatar
xamgore committed
}