예제 #1
0
 /**
  * POST method: create new session.
  *
  * @param Request $request
  * @throws Exception
  * @return mixed
  */
 public function post($request)
 {
     switch (count($request->url_elements)) {
         case 1:
             // Empty request data - throw Exception
             if (empty($request->json)) {
                 throw new Exception("Missing required data.", 400);
             }
             // No login or password - throw Exception
             if (!$request->json->login || !$request->json->password) {
                 throw new Exception("Missing required parameter.", 400);
             }
             // Failed authorisation - throw Exception
             if (!Common::doAuthorisation($request->json->login, $request->json->password)) {
                 throw new Exception("Incorrect login or password.", 403);
             }
             // Like success - create session & return
             $session = Session::find_by_id($_SESSION['session']);
             if ($session) {
                 return json_decode($session->to_json());
             } else {
                 throw new Exception("Internal error.", 500);
             }
         default:
             throw new Exception("Unknown request.", 500);
     }
 }