Skip to content
Snippets Groups Projects
Forked from it-lab / grade
Source project has a limited visibility.
Settings.php 2.94 KiB
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Handler_Settings extends Controller_Handler
{

    public function before() {
        parent::before();

        $this->user->checkAccess(User::RIGHTS_AUTHORIZED);
    }

    public function action_changeLogin() {
        $config = Kohana::$config->load('security.securityPolicy');
        $this->post->rule('login', $config['login']['allowedSymbols'])->rule('login', 'not_empty');
        $res['success'] = true;

        if ($this->post->check() && $_POST['login'] !== $this->user->Login) {
            Model_Account::changeLogin($this->user->ID, $this->post['login'])
                ? $this->user->Login = $this->post['login']
                : $this->post->error('login', 'already_exists');
        }

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

        $this->response->body(json_encode($res));
    }

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

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

        $res['success'] = true;
        $old = $this->post['old_password'];
        $new = $this->post['password'];

        if (!$this->post->check()) {
            $res['success'] = false;
            $res['errors'] = $this->post->errors();
            $this->response->body(json_encode($res));
            return;
        }

        if (!User::instance()->changePassword($old, $new)) {
            $res['success'] = false;
            $this->post->error('login', 'invalid_password');
            $res['errors'] = $this->post->errors();
            $this->response->body(json_encode($res));
            return;
        }

        $this->response->body(json_encode($res));
    }

    public function action_editProfile() {
        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $this->post
            ->rule('firstName', 'not_empty')
            ->rule('lastName', 'not_empty')
            ->rule('jobPositionID', 'not_empty')
            ->rule('jobPositionID', 'digit')
            ->rule('departmentID', 'not_empty')
            ->rule('departmentID', 'digit');
        if ($this->post->check()) {
            User::instance()->changeProfile($this->post->data());
        }
    }

    public function action_getDepartmentsList() {
        $faculty = Model_Faculty::with($this->post['facultyID']);
        $departments = $faculty->getDepartments();
        $this->response->body(json_encode($departments));
    }

    public function action_setSemesterID() {
        $this->post
            ->rule('semesterID', 'not_empty')
            ->rule('semesterID', 'digit');
        if ($this->post->check()) {
            $this->user->SemesterID = (int) $this->post['semesterID'];
        }
    }
}