コード例 #1
0
 /**
  * Disable issued token to a user
  *
  * @param int $user_id ID of a user
  * @param Slim $app
  * @return string
  */
 public static function logout($user_id, Slim $app)
 {
     $app->response->headers->set('Content-Type', 'application/json');
     try {
         $user = User::find($user_id);
     } catch (RecordNotFoundException $e) {
         $app->halt(404, json_encode(['message' => 'Not Registered']));
     }
     if (Setup::unsetToken($user) === 1) {
         return json_encode(['message' => 'Logged out']);
     } else {
         $app->halt3(503);
     }
 }
コード例 #2
0
 /**
  * Get user instance with supplied token
  *
  * @param Slim $app
  * @return object|string
  */
 public static function getUserWithToken(Slim $app)
 {
     $token = $app->request->headers('Authorization');
     if (isset($token)) {
         try {
             $user = User::where('token', $token);
         } catch (RecordNotFoundException $e) {
             $app->response->headers->set('Content-Type', 'application/json');
             $app->halt(401, json_encode(['message' => 'Invalid Token']));
         }
         return $user;
     } else {
         $app->response->headers->set('Content-Type', 'application/json');
         $app->halt(401, json_encode(['message' => 'Empty Token']));
     }
 }
コード例 #3
0
 /**
  * Delete user's account
  *
  * @param string $username username of a user
  * @param Slim $app
  * @return string
  */
 public static function delete($username, Slim $app)
 {
     $app->response->headers->set('Content-Type', 'application/json');
     try {
         $user = User::where('username', $username);
     } catch (RecordNotFoundException $e) {
         $app->halt(404, json_encode(['message' => 'Not Found']));
     }
     if (is_object($user)) {
         $fields = $app->request->isPut() ? $app->request->put() : $app->request->patch();
         if (md5($fields['password']) == $user->getRecord()['dbData']['password']) {
             $check = User::destroy($user->getRecord()['dbData']['id']);
             if ($check === 1) {
                 return json_encode(['message' => 'Account Deleted']);
             } else {
                 $app->halt(304);
             }
         } else {
             $app->halt(401);
         }
     } else {
         $app->halt(503);
     }
 }