Leosac  0.7.0
OpenSourceAccessControl
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 }
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
The base class for API method handler implementation.
This is the header file for a generated source file, GitSHA1.cpp.
virtual json process_impl(const json &req) override
The API method implementation.
Definition: LogGet.cpp:41
boost::posix_time::ptime timestamp_
Definition: LogEntry.hpp:49
std::unique_ptr< MethodHandler > MethodHandlerUPtr
Definition: WebSockFwd.hpp:36
static MethodHandlerUPtr create(RequestContext)
Definition: LogGet.cpp:36
LogGet(RequestContext ctx)
Definition: LogGet.cpp:31
All modules that provides features to Leosac shall be in this namespace.
unsigned long id_
Definition: LogEntry.hpp:46
Holds valuable pointer to provide context to a request.
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
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