Ejemplo n.º 1
0
 /**
  * POST method: Create new user
  *
  * @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);
             }
             // Login already exists - throw Exception
             if (count(User::find('all', array('conditions' => array('login=?', $request->json->login)))) > 0) {
                 throw new Exception("User with this login already exists.", 500);
             }
             // Like success - create user & return
             $user = User::create(array('login' => $request->json->login, 'password' => Common::getPasswordHash($request->json->password, $request->json->login)));
             return json_decode($user->to_json(array('except' => 'password')));
         default:
             throw new Exception("Unknown request.", 500);
     }
 }