Leosac  0.8.0
Open Source Access Control
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 }
Leosac::Module::Wiegand::CRUDHandler::delete_impl
boost::optional< WebSockAPI::json > delete_impl(const WebSockAPI::json &req) override
Definition: CRUDHandler.cpp:157
Leosac::db::OptionalTransaction
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
Definition: OptionalTransaction.hpp:43
Leosac::db::OptionalTransaction::commit
void commit()
Commit the transaction, if there was no currently active transaction at the time of this object's cre...
Definition: OptionalTransaction.cpp:38
WiegandConfigSerializer.hpp
Leosac::SecurityContext::Action::HARDWARE_CREATE
@ HARDWARE_CREATE
Leosac::Module::Wiegand::CRUDHandler::read_impl
boost::optional< WebSockAPI::json > read_impl(const WebSockAPI::json &req) override
Definition: CRUDHandler.cpp:102
Leosac::Module::Wiegand::CRUDHandler::CRUDHandler
CRUDHandler(const WebSockAPI::RequestContext &ctx)
Definition: CRUDHandler.cpp:170
Leosac::Module::Wiegand::CRUDHandler::update_impl
boost::optional< WebSockAPI::json > update_impl(const WebSockAPI::json &req) override
Definition: CRUDHandler.cpp:139
Leosac::Module::Wiegand::WiegandReaderConfigSerializer::serialize
static json serialize(const WiegandReaderConfig &in, const SecurityContext &sc)
Definition: WiegandConfigSerializer.cpp:36
Leosac::SecurityContext::Action::HARDWARE_DELETE
@ HARDWARE_DELETE
Leosac::Module::Wiegand::CRUDHandler::required_permission
std::vector< ActionActionParam > required_permission(Verb verb, const WebSockAPI::json &req) const override
Definition: CRUDHandler.cpp:37
Leosac::Module::WebSockAPI::CRUDResourceHandlerUPtr
std::unique_ptr< CRUDResourceHandler > CRUDResourceHandlerUPtr
Definition: WebSockFwd.hpp:39
Leosac::Module::WebSockAPI::RequestContext::dbsrv
DBServicePtr dbsrv
Definition: RequestContext.hpp:39
Leosac::SecurityContext::HardwareDeviceActionParam::device_id
Hardware::DeviceId device_id
Definition: SecurityContext.hpp:226
Leosac::Module::Wiegand::json
nlohmann::json json
Definition: WiegandConfigSerializer.hpp:32
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::DELETE
@ DELETE
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Module::Wiegand::CRUDHandler::instanciate
static WebSockAPI::CRUDResourceHandlerUPtr instanciate(WebSockAPI::RequestContext)
Definition: CRUDHandler.cpp:176
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::CREATE
@ CREATE
Leosac::EntityNotFound
Definition: EntityNotFound.hpp:27
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::READ
@ READ
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::UPDATE
@ UPDATE
Leosac::SecurityContext::Action::HARDWARE_READ
@ HARDWARE_READ
Permissions for hardware devices.
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
OptionalTransaction.hpp
Leosac::Module::Wiegand::find_reader_by_id
auto find_reader_by_id(const Hardware::DeviceId &id, DBPtr db)
Definition: CRUDHandler.cpp:92
CRUDHandler.hpp
Leosac::Module::Wiegand::CRUDHandler::create_impl
boost::optional< WebSockAPI::json > create_impl(const WebSockAPI::json &req) override
Definition: CRUDHandler.cpp:75
Leosac::SecurityContext::Action::HARDWARE_UPDATE
@ HARDWARE_UPDATE
Leosac::Module::WebSockAPI::CRUDResourceHandler::ctx_
RequestContext ctx_
Definition: CRUDResourceHandler.hpp:95
JSONUtils.hpp
DBService.hpp
APISession.hpp
Leosac::Module::Wiegand::WiegandReaderConfigSerializer::unserialize
static void unserialize(WiegandReaderConfig &out, const json &in, const SecurityContext &sc)
Definition: WiegandConfigSerializer.cpp:65
Leosac::Module::WebSockAPI::RequestContext
Holds valuable pointer to provide context to a request.
Definition: RequestContext.hpp:36
Leosac::Module::Wiegand::WiegandReaderConfig
An instance of this class represents the configuration of one Wiegand reader.
Definition: WiegandConfig.hpp:46
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb
Verb
Definition: CRUDResourceHandler.hpp:43
EntityNotFound.hpp
Result
odb::result< Tools::LogEntry > Result
Definition: LogEntry.cpp:37
Leosac::SecurityContext::HardwareDeviceActionParam
Definition: SecurityContext.hpp:224
Leosac::UUID
Thin wrapper around boost::uuids::uuid.
Definition: Uuid.hpp:35
Leosac::Module::WebSockAPI::RequestContext::session
APIPtr session
Definition: RequestContext.hpp:38
Leosac::Module::WebSockAPI::CRUDResourceHandler::security_context
virtual UserSecurityContext & security_context() const override
Helper function that returns the security context.
Definition: CRUDResourceHandler.cpp:96