/** * @ApiDoc( * description = "returns the URLs for Tables and Users" * ) * @return JsonResponse */ public function urlsAction() { //return the URLs for using the user admin $roles = $this->getParameter('qcharts.user_roles'); $authChecker = $this->get("security.authorization_checker"); $allow_demo_users = $this->getParameter('qcharts.allow_demo_users'); try { ApiController::checkCredentials($authChecker, $roles, ApiController::USER, $allow_demo_users); $tableInfo = $this->generateUrl('qcharts.api.table_info'); $tablesUrl = $this->generateUrl('qcharts.api.tables'); $connections = $this->generateUrl('qcharts.api.connection_names'); $connectionSchemas = $this->generateUrl('qcharts.api.connection_schemas'); //$roleUrl = $this->generateUrl('qcharts.api.user_promote'); $roleUrl = ""; $frontendTableInfo = $this->generateUrl('qcharts.frontend.table_information'); $frontendHomepage = $this->generateUrl('qcharts.frontend.homepage'); $frontendBase = $this->generateUrl('qcharts.frontend.base'); $directoryUrl = $this->generateUrl('qcharts.api.directory_get'); $directoryQuery = $this->generateUrl('qcharts.api.directory_query'); $snapshots = $this->generateUrl('qcharts.api.snapshot.get'); $snapshotDownload = $this->generateUrl('qcharts.api.snapshot.snapshot_download'); $favoritesGet = $this->generateUrl('qcharts.api.favorite_get'); $options = ["status" => 200, "textStatus" => "Urls returned", "urls" => ["frontend" => ["tableInfo" => $frontendTableInfo, "homepage" => $frontendHomepage, "base" => $frontendBase], "query" => $this->generateUrl('qcharts.api.query_get'), "database" => ["tableInfo" => $tableInfo, "tables" => $tablesUrl, "connections" => $connections, "schemas" => $connectionSchemas], "snapshots" => ["snapshots" => $snapshots, "download" => $snapshotDownload], "favorite" => ["base" => $favoritesGet], "directory" => ["base" => $directoryUrl, "query" => $directoryQuery], "user" => ["role" => $roleUrl], "chart" => $this->generateUrl('qcharts.api.chart_data')]]; return new JsonResponse($options); } catch (InvalidCredentialsException $e) { return new JsonResponse(ApiController::getNotValidCredentials()); } }
/** * @ApiDoc( * description = "Removes the given query request from the favorites list of the user", * parameters = { * { * "name" = "q", * "required" = true, * "dataType" = "integer", * "description" = "The query Id to remove from the user's list" * } * }, * statusCodes = { * 202 = "The query was removed from the user's favorite list", * 500 = "Error while attempting to remove the query form the user's favorite list" * } * ) * @param Request $request * @return JsonResponse */ public function removeFavoriteAction(Request $request) { $roles = $this->getParameter("qcharts.user_roles"); $authService = $this->get("security.authorization_checker"); $options = []; try { ApiController::checkCredentials($authService, $roles, "user"); $user = $this->getUser(); $queryId = $request->request->get("q", null); $queryService = $this->get("qcharts.query"); $favService = $this->get("qcharts.core.favorite_service"); $queryRequest = $queryService->getQueryRequestById($queryId); $favService->removeFavourite($user, $queryRequest); $options = ["status" => 202, "textStatus" => "The favorite was removed from the user's list"]; } catch (InstanceNotFoundException $e) { $options = ["status" => $e->getCode(), "textStatus" => $e->getMessage()]; } finally { return new JsonResponse($options); } }
/** * @ApiDoc( * description = "Deletes the requested snapshot file", * parameters = { * { * "name" = "snapshot", * "dataType" = "integer", * "description" = "The snapshot name to delete", * "required" = true * }, * { * "name" = "q", * "dataType" = "integer", * "description" = "Query request that has the snapshot", * "required" = true * } * }, * statusCodes = { * 202 = "Snapshot deleted", * 402 = "Credentials invalid" * } * ) * @param Request $request * @return JsonResponse */ public function deleteSnapshotAction(Request $request) { $options = []; $authChecker = $this->get("security.authorization_checker"); $roles = $this->getParameter("qcharts.user_roles"); try { ApiController::checkCredentials($authChecker, $roles, ApiController::SUPER_ADMIN); $snapshotService = $this->get("qcharts.core.snapshot_service"); $queryService = $this->get("qcharts.query"); $snapshotId = $request->request->get("snapshot", null); $queryId = $request->request->get("q", null); $queryRequest = $queryService->getQueryRequestById($queryId); $snapshot = $snapshotService->getSnapshotFile($queryRequest, $snapshotId); $snapshotService->deleteSnapshot($queryRequest, $snapshot); $options = ["status" => 202, "textStatus" => "The snapshot was deleted"]; } catch (InvalidCredentialsException $e) { $options = ApiController::getNotValidCredentials(); } catch (SnapshotException $e) { $options = ["status" => $e->getCode(), "textStatus" => $e->getMessage()]; } catch (InstanceNotFoundException $e) { $options = ["status" => $e->getCode(), "textStatus" => $e->getMessage()]; } finally { return new JsonResponse($options); } }
/** * * @ApiDoc( * description = "Demotes the given User with username", * parameters = { * { * "name" = "username", * "dataType" = "string", * "required" = true, * "description" = "Username to demote" * } * }, * statusCodes = { * 200 = "Success", * 404 = "No users with the given query was found", * 405 = "User does not had the given role", * 500 = "" * } * ) * @param Request $request * @return JsonResponse */ public function demoteAction(Request $request) { $authChecker = $this->get("security.authorization_checker"); $roles = $this->getParameter('qcharts.user_roles'); if (!$authChecker->isGranted($roles["super_admin"])) { return new JsonResponse(ApiController::getNotValidCredentials()); } $options = []; $username = $request->request->get("username"); try { $userService = $this->get("qcharts.user_service"); $userService->demoteUser($username); $options = ["status" => 200, "textStatus" => "User was successfully demoted", "username" => $username]; } catch (InstanceNotFoundException $e) { $options = ["status" => $e->getCode(), "textStatus" => $e->getMessage(), "username" => $username]; } catch (UserRoleException $e) { $options = ["status" => $e->getCode(), "textStatus" => $e->getMessage()]; } finally { return new JsonResponse($options); } }
/** * @ApiDoc( * description="Run Action, returns the results directly from database", * parameters={ * { * "name"="query", * "dataType"="string", * "required"=true, * "description"="The query to run in the database" * }, * { * "name"="limit", * "dataType"="integer", * "required"=false, * "description"="The limit to use" * }, * { * "name"="connection", * "dataType"="string", * "required"=false, * "description"="Connection to query from, default connection is used if not passed" * } * }, * statusCodes = { * 200 = "Returned successfully", * 402 = "Credentials not valid", * 500 = "Exception found and returned with error" * } * ) * @param Request $request * @return JsonResponse */ public function runAction(Request $request) { $authChecker = $this->get('security.authorization_checker'); /** @var QueryService $queryService */ $queryService = $this->get('qcharts.query'); $chartValidator = $this->get("qcharts.chart_validator"); $roles = $this->getParameter('qcharts.user_roles'); if (!$authChecker->isGranted($roles["admin"])) { return new JsonResponse(ApiController::getNotValidCredentials()); } $query = $request->request->get('query'); try { $queryLimit = $request->request->get('limit', 0); $connection = $request->request->get('connection', 'default'); $result = $queryService->getResultsFromQuery($query, $queryLimit, $connection); $isPieCompatible = false; try { $isPieCompatible = $chartValidator->resultsArePieCompatible($result["results"]); $isPieCompatible = $isPieCompatible && $chartValidator->resultsAreNumeric($result["results"], 'pie'); } catch (ValidationFailedException $e) { $isPieCompatible = false; } finally { return new JsonResponse(["status" => 200, "originalQuery" => $query, "textStatus" => "Results are returned", "results" => $result["results"], "lengthResults" => count($result["results"]), "queryDuration" => $result["duration"], "limit" => $queryLimit, "isPieChartCompatible" => $isPieCompatible]); } } catch (ParameterNotPassedException $e) { //return the error as an error return new JsonResponse(["status" => 500, "textStatus" => $e->getMessage(), "originalQuery" => $query]); } catch (DatabaseException $e) { return new JsonResponse(["status" => $e->getCode(), "textStatus" => $e->getMessage()]); } catch (ValidationFailedException $e) { return new JsonResponse(["status" => $e->getCode(), "textStatus" => $e->getMessage(), "originalQuery" => $query]); } catch (\InvalidArgumentException $e) { return new JsonResponse(["status" => 500, "textStatus" => $e->getMessage(), "originalQuery" => $query]); } }
/** * @ApiDoc( * description = "Returns the Queries in the requested directory", * parameters = { * { * "name" = "dir", * "dataType" = "integer", * "required" = true, * "description" = "The directory Id" * }, * { * "name" = "_format", * "dataType" = "string", * "required" = false, * "description" = "The encoding type" * } * }, * statusCodes = { * 200 = "The queries were returned" * } * ) * @param Request $request * @return JsonResponse|Response */ public function queriesInDirectoryAction(Request $request) { $authChecker = $this->get("security.authorization_checker"); $roles = $this->getParameter("qcharts.user_roles"); $serializer = $this->get("qcharts.serializer"); $allow_demo_users = $this->getParameter('qcharts.allow_demo_users'); if (!$authChecker->isGranted($roles["user"]) && !$allow_demo_users) { return new JsonResponse(ApiController::getNotValidCredentials()); } $encodingType = $request->query->get("_format", "json"); $options = []; try { $directoryId = $request->query->get("dir", null); $queryService = $this->get("qcharts.query"); $results = $queryService->getQueriesInDirectory($directoryId); $options = ["queries" => $results, "count" => count($results), "status" => 200, "textStatus" => "Queries returned"]; } catch (TypeNotValidException $e) { $options = ["status" => $e->getCode(), "textStatus" => $e->getMessage()]; } finally { $options = $serializer->serialize($options, $encodingType); $response = new Response($options); $response->headers->set("Content-type", "application/json"); return $response; } }