Skip to content
Snippets Groups Projects
Auth.php 1.4 KiB
Newer Older
<?php

class Controller_Api_V0_Auth extends Controller_Handler_Api
{
    /**
Artem Konenko's avatar
Artem Konenko committed
     * @api {get} api/v0/auth/userinfo?token=:token&login=:login&password=:password Check authorisation and get user info
     * @apiName Get user info
     * @apiGroup Auth
     * @apiVersion 0.1.0
     * @apiParam {String} token Api key
     * @apiParam {String} login User's login
     * @apiParam {String} password User's password
     */
    public function action_get_userinfo() {
        if ( !$this->user->isAdmin() ) // ToDo: we should use apikey mask for checking rights
            $this->fail();

        $login = $this->request->query('login');
        $password = $this->request->query('password');

        $id = (int) Model_Account::checkAuth($login, $password);
        if ($id < 0)
            $this->notFoundError('Login or password are incorrect.');

        $info = (object)Model_Account::with($id);

        $res = (object)['LastName' => $info->LastName,
                'FirstName' => $info->FirstName,
                'SecondName' => $info->SecondName,
                'IsEnabled' => $info->IsEnabled,
                'Type' => $info->Type];

        switch($info->Type )
        {
            case 'teacher':
                $res->TeacherID = $info->TeacherID;
                break;
            case 'student':
                $res->StudentID = $info->StudentID;
                break;
        }

        return $res;
    }

}