Leosac  0.7.0
OpenSourceAccessControl
CRUDHandler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2017 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 
24 #include "modules/wiegand/WiegandConfig_odb.h"
25 #include "tools/JSONUtils.hpp"
26 #include "tools/db/DBService.hpp"
28 
29 namespace Leosac
30 {
31 namespace Module
32 {
33 namespace Wiegand
34 {
35 
36 std::vector<WebSockAPI::ICRUDResourceHandler::ActionActionParam>
38  const json &req) const
39 {
40  std::vector<CRUDResourceHandler::ActionActionParam> ret;
41 
42  SecurityContext::HardwareDeviceActionParam hardware_action_param{};
43  try
44  {
45  hardware_action_param.device_id =
46  req.at("reader_id").get<Hardware::DeviceId>();
47  }
48  catch (json::out_of_range &e)
49  {
50  hardware_action_param.device_id = Hardware::DeviceId{};
51  }
52 
53  switch (verb)
54  {
55  case Verb::READ:
57  hardware_action_param);
58  break;
59  case Verb::CREATE:
61  hardware_action_param);
62  break;
63  case Verb::UPDATE:
65  hardware_action_param);
66  break;
67  case Verb::DELETE:
69  hardware_action_param);
70  break;
71  }
72  return ret;
73 }
74 
75 boost::optional<json> CRUDHandler::create_impl(const json &req)
76 {
77  json rep;
78  DBPtr db = ctx_.dbsrv->db();
79  odb::transaction t(db->begin());
80 
81  auto new_reader = std::make_shared<WiegandReaderConfig>();
82  WiegandReaderConfigSerializer::unserialize(*new_reader, req.at("attributes"),
84  db->persist(new_reader);
85 
86  rep["data"] =
88  t.commit();
89  return rep;
90 }
91 
93 {
94  db::OptionalTransaction t(db->begin());
95  auto reader = db->find<WiegandReaderConfig>(id);
96  t.commit();
97  if (!reader)
98  throw EntityNotFound(id, "wiegand-reader");
99  return reader;
100 }
101 
102 boost::optional<json> CRUDHandler::read_impl(const json &req)
103 {
104  json rep;
105 
106  using Result = odb::result<WiegandReaderConfig>;
107  DBPtr db = ctx_.dbsrv->db();
108  odb::transaction t(db->begin());
109  auto reader_id = req.at("reader_id").get<Hardware::DeviceId>();
110 
111  if (!reader_id.is_nil())
112  {
113  auto reader = find_reader_by_id(reader_id, db);
114  rep["data"] =
116  }
117  else
118  {
119  Result result = db->query<WiegandReaderConfig>();
120  rep["data"] = json::array();
121  auto current_user = ctx_.session->current_user();
122 
123  // fixme: may be rather slow.
124  for (const auto &reader : result)
125  {
127  if (ctx_.session->security_context().check_permission(
129  {
130  rep["data"].push_back(WiegandReaderConfigSerializer::serialize(
131  reader, security_context()));
132  }
133  }
134  }
135  t.commit();
136  return rep;
137 }
138 
139 boost::optional<json> CRUDHandler::update_impl(const json &req)
140 {
141  json rep;
142  DBPtr db = ctx_.dbsrv->db();
143  odb::transaction t(db->begin());
144  auto reader_id = req.at("reader_id").get<Hardware::DeviceId>();
145  auto reader = find_reader_by_id(reader_id, db);
146 
147  WiegandReaderConfigSerializer::unserialize(*reader, req.at("attributes"),
148  security_context());
149 
150  db->update(reader);
151  rep["data"] =
153  t.commit();
154  return rep;
155 }
156 
157 boost::optional<json> CRUDHandler::delete_impl(const json &req)
158 {
159  auto did = req.at("reader_id").get<Hardware::DeviceId>();
160  DBPtr db = ctx_.dbsrv->db();
161  odb::transaction t(db->begin());
162 
163  auto reader = find_reader_by_id(did, db);
164  db->erase(reader);
165  t.commit();
166 
167  return json{};
168 }
169 
171  : CRUDResourceHandler(ctx)
172 {
173 }
174 
177 {
178  auto instance = WebSockAPI::CRUDResourceHandlerUPtr(new CRUDHandler(ctx));
179  return instance;
180 }
181 }
182 }
183 }
An instance of this class represents the configuration of one Wiegand reader.
static void unserialize(WiegandReaderConfig &out, const json &in, const SecurityContext &sc)
This is the header file for a generated source file, GitSHA1.cpp.
boost::optional< WebSockAPI::json > delete_impl(const WebSockAPI::json &req) override
std::vector< ActionActionParam > required_permission(Verb verb, const WebSockAPI::json &req) const override
Definition: CRUDHandler.cpp:37
virtual UserSecurityContext & security_context() const override
Helper function that returns the security context.
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
std::unique_ptr< CRUDResourceHandler > CRUDResourceHandlerUPtr
Definition: WebSockFwd.hpp:39
CRUDHandler(const WebSockAPI::RequestContext &ctx)
boost::optional< WebSockAPI::json > update_impl(const WebSockAPI::json &req) override
boost::optional< WebSockAPI::json > read_impl(const WebSockAPI::json &req) override
Thin wrapper around boost::uuids::uuid.
Definition: Uuid.hpp:35
Permissions for hardware devices.
auto find_reader_by_id(const Hardware::DeviceId &id, DBPtr db)
Definition: CRUDHandler.cpp:92
odb::result< Tools::LogEntry > Result
Definition: LogEntry.cpp:37
static WebSockAPI::CRUDResourceHandlerUPtr instanciate(WebSockAPI::RequestContext)
Holds valuable pointer to provide context to a request.
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
static json serialize(const WiegandReaderConfig &in, const SecurityContext &sc)
boost::optional< WebSockAPI::json > create_impl(const WebSockAPI::json &req) override
Definition: CRUDHandler.cpp:75