Leosac  0.8.0
Open Source Access Control
LogGet.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "LogGet.hpp"
21 #include "Exceptions.hpp"
22 #include "api/APISession.hpp"
23 #include "tools/JSONUtils.hpp"
24 #include "tools/LogEntry_odb.h"
25 #include "tools/db/DBService.hpp"
26 
27 using namespace Leosac;
28 using namespace Leosac::Module;
29 using namespace Leosac::Module::WebSockAPI;
30 
32  : MethodHandler(ctx)
33 {
34 }
35 
37 {
38  return std::make_unique<LogGet>(ctx);
39 }
40 
42 {
43  json rep;
44  DBPtr db = ctx_.dbsrv->db();
45  if (db)
46  {
47  using namespace Tools;
48  using namespace JSONUtil;
49 
50  rep["data"] = json::array();
51  std::string sort = extract_with_default(req, "sort", "desc");
52  int p = extract_with_default(req, "p", 0); // page
53  int ps = extract_with_default(req, "ps", 20); // page size
54  if (ps <= 0)
55  ps = 1;
56 
57  LogEntry::QueryResult result = LogEntry::retrieve(db, p, ps, sort == "asc");
58  for (Tools::LogEntry &entry : result.entries)
59  {
60  auto timestamp = boost::posix_time::to_time_t(entry.timestamp_);
61  rep["data"].push_back(
62  {{"id", entry.id_},
63  {"type", "log-message"},
64  {"attributes",
65  {{"message", entry.msg_}, {"timestamp", timestamp}}}});
66  }
67 
68  rep["meta"] = {
69  {"total", result.total}, {"last", result.last}, {"first", result.first}};
70  rep["status"] = 0;
71  }
72  else
73  {
74  rep["status"] = -1;
75  }
76  return rep;
77 }
78 
79 std::vector<ActionActionParam> LogGet::required_permission(const json &) const
80 {
81  std::vector<ActionActionParam> perm_;
83 
84  perm_.push_back({SecurityContext::Action::LOG_READ, ap});
85  return perm_;
86 }
Leosac::JSONUtil::extract_with_default
std::chrono::system_clock::time_point extract_with_default(const nlohmann::json &obj, const std::string &key, const std::chrono::system_clock::time_point &tp)
Extract an ISO 8601 datetime string from a json object.
Definition: JSONUtils.cpp:45
Exceptions.hpp
Leosac::Module::WebSockAPI::RequestContext::dbsrv
DBServicePtr dbsrv
Definition: RequestContext.hpp:39
Leosac::Module::WebSockAPI::LogGet::LogGet
LogGet(RequestContext ctx)
Definition: LogGet.cpp:31
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Module
All modules that provides features to Leosac shall be in this namespace.
Leosac::SecurityContext::ActionParam
Definition: SecurityContext.hpp:231
Leosac::Module::WebSockAPI::MethodHandler
The base class for API method handler implementation.
Definition: MethodHandler.hpp:46
Leosac::Tools::LogEntry::msg_
std::string msg_
Definition: LogEntry.hpp:52
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::WebSockAPI::MethodHandler::ctx_
RequestContext ctx_
Definition: MethodHandler.hpp:90
JSONUtils.hpp
Leosac::Tools::LogEntry::id_
unsigned long id_
Definition: LogEntry.hpp:46
Leosac::Tools::LogEntry::timestamp_
boost::posix_time::ptime timestamp_
Definition: LogEntry.hpp:49
Leosac::Module::WebSockAPI::LogGet::required_permission
std::vector< ActionActionParam > required_permission(const json &req) const override
Return a list of "Action" / "ActionParam" that must pass before the request is processed.
Definition: LogGet.cpp:79
Leosac::Module::WebSockAPI::MethodHandlerUPtr
std::unique_ptr< MethodHandler > MethodHandlerUPtr
Definition: WebSockFwd.hpp:36
DBService.hpp
Leosac::Tools::LogEntry
A log entry.
Definition: LogEntry.hpp:40
APISession.hpp
LogGet.hpp
Leosac::Module::WebSockAPI::json
nlohmann::json json
Definition: AccessOverview.hpp:30
Leosac::Module::WebSockAPI::RequestContext
Holds valuable pointer to provide context to a request.
Definition: RequestContext.hpp:36
Leosac::Module::WebSockAPI
Definition: ActionActionParam.hpp:28
Leosac::Module::WebSockAPI::LogGet::create
static MethodHandlerUPtr create(RequestContext)
Definition: LogGet.cpp:36
Leosac::Module::WebSockAPI::LogGet::process_impl
virtual json process_impl(const json &req) override
The API method implementation.
Definition: LogGet.cpp:41
Leosac::SecurityContext::Action::LOG_READ
@ LOG_READ