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 
20 
22 #include "PFGPIO.hpp"
24 #include "modules/pifacedigital/PFGPIO_odb.h"
26 #include "tools/JSONUtils.hpp"
27 #include "tools/db/DBService.hpp"
29 
30 namespace Leosac
31 {
32 namespace Module
33 {
34 namespace Piface
35 {
36 std::vector<WebSockAPI::ICRUDResourceHandler::ActionActionParam>
38  const json &req) const
39 {
40  std::vector<CRUDResourceHandler::ActionActionParam> ret;
41 
43  try
44  {
45  gpio_action_param.device_id = req.at("gpio_id").get<Hardware::DeviceId>();
46  }
47  catch (json::out_of_range &e)
48  {
49  gpio_action_param.device_id = UUID{};
50  }
51 
52  switch (verb)
53  {
54  case Verb::READ:
55  ret.emplace_back(SecurityContext::Action::HARDWARE_READ, gpio_action_param);
56  break;
57  case Verb::CREATE:
59  gpio_action_param);
60  break;
61  case Verb::UPDATE:
63  gpio_action_param);
64  break;
65  case Verb::DELETE:
67  gpio_action_param);
68  break;
69  }
70  return ret;
71 }
72 
73 boost::optional<json> CRUDHandler::create_impl(const json &req)
74 {
75  json rep;
76  DBPtr db = ctx_.dbsrv->db();
77  odb::transaction t(db->begin());
78 
79  auto new_gpio = std::make_shared<PFGPIO>();
80  PFGPIOSerializer::unserialize(*new_gpio, req.at("attributes"),
82  db->persist(new_gpio);
83 
84  rep["data"] = PFGPIOSerializer::serialize(*new_gpio, security_context());
85  t.commit();
86  return rep;
87 }
88 
90 {
91  db::OptionalTransaction t(db->begin());
92  auto gpio = db->find<PFGPIO>(id);
93  t.commit();
94  if (!gpio)
95  throw EntityNotFound(id, "pfdigital.gpio");
96  return gpio;
97 }
98 
99 boost::optional<json> CRUDHandler::read_impl(const json &req)
100 {
101  json rep;
102 
103  using Result = odb::result<PFGPIO>;
104  DBPtr db = ctx_.dbsrv->db();
105  odb::transaction t(db->begin());
106  auto gpio_id = req.at("gpio_id").get<Hardware::DeviceId>();
107 
108  if (!gpio_id.is_nil())
109  {
110  auto gpio = find_gpio_by_id(gpio_id, db);
111  rep["data"] = PFGPIOSerializer::serialize(*gpio, security_context());
112  }
113  else
114  {
115  Result result = db->query<PFGPIO>();
116  rep["data"] = json::array();
117  auto current_user = ctx_.session->current_user();
118 
119  // fixme: may be rather slow.
120  for (const auto &gpio : result)
121  {
123  if (ctx_.session->security_context().check_permission(
125  {
126  rep["data"].push_back(
128  }
129  }
130  }
131  t.commit();
132  return rep;
133 }
134 
135 boost::optional<json> CRUDHandler::update_impl(const json &req)
136 {
137  json rep;
138  DBPtr db = ctx_.dbsrv->db();
139  odb::transaction t(db->begin());
140  auto gpio_id = req.at("gpio_id").get<Hardware::DeviceId>();
141  auto gpio = find_gpio_by_id(gpio_id, db);
142 
143  PFGPIOSerializer::unserialize(*gpio, req.at("attributes"), security_context());
144 
145  db->update(gpio);
146  rep["data"] = PFGPIOSerializer::serialize(*gpio, security_context());
147  t.commit();
148  return rep;
149 }
150 
151 boost::optional<json> CRUDHandler::delete_impl(const json &req)
152 {
153  auto did = req.at("gpio_id").get<Hardware::DeviceId>();
154  DBPtr db = ctx_.dbsrv->db();
155  odb::transaction t(db->begin());
156 
157  auto gpio = find_gpio_by_id(did, db);
158  db->erase(gpio);
159  t.commit();
160 
161  return json{};
162 }
163 
165  : CRUDResourceHandler(ctx)
166 {
167 }
168 
171 {
172  auto instance = WebSockAPI::CRUDResourceHandlerUPtr(new CRUDHandler(ctx));
173  return instance;
174 }
175 }
176 }
177 }
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
Leosac::SecurityContext::Action::HARDWARE_CREATE
@ HARDWARE_CREATE
Leosac::Module::Piface::CRUDHandler::instanciate
static WebSockAPI::CRUDResourceHandlerUPtr instanciate(WebSockAPI::RequestContext)
Definition: CRUDHandler.cpp:170
Leosac::SecurityContext::Action::HARDWARE_DELETE
@ HARDWARE_DELETE
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::WebSockAPI::ICRUDResourceHandler::Verb::DELETE
@ DELETE
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Module::Piface::find_gpio_by_id
auto find_gpio_by_id(const Hardware::DeviceId &id, DBPtr db)
Definition: CRUDHandler.cpp:89
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::CREATE
@ CREATE
Leosac::EntityNotFound
Definition: EntityNotFound.hpp:27
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::READ
@ READ
Leosac::Module::Piface::PFGPIO
Piface Module GPIO descriptor.
Definition: PFGPIO.hpp:44
Leosac::Module::Piface::CRUDHandler::CRUDHandler
CRUDHandler(const WebSockAPI::RequestContext &ctx)
Definition: CRUDHandler.cpp:164
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::Piface::PFGPIOSerializer::serialize
static json serialize(const PFGPIO &in, const SecurityContext &sc)
Definition: PFGPIO.cpp:37
Leosac::Module::Piface::CRUDHandler::update_impl
boost::optional< WebSockAPI::json > update_impl(const WebSockAPI::json &req) override
Definition: CRUDHandler.cpp:135
Leosac::SecurityContext::Action::HARDWARE_UPDATE
@ HARDWARE_UPDATE
CRUDHandler.hpp
Leosac::Module::WebSockAPI::CRUDResourceHandler::ctx_
RequestContext ctx_
Definition: CRUDResourceHandler.hpp:95
JSONUtils.hpp
DBService.hpp
Leosac::Module::Piface::CRUDHandler::read_impl
boost::optional< WebSockAPI::json > read_impl(const WebSockAPI::json &req) override
Definition: CRUDHandler.cpp:99
APISession.hpp
Leosac::Module::Piface::CRUDHandler::required_permission
std::vector< ActionActionParam > required_permission(Verb verb, const WebSockAPI::json &req) const override
Definition: CRUDHandler.cpp:37
Leosac::Module::WebSockAPI::RequestContext
Holds valuable pointer to provide context to a request.
Definition: RequestContext.hpp:36
PFGPIO.hpp
Leosac::Module::Piface::CRUDHandler::delete_impl
boost::optional< WebSockAPI::json > delete_impl(const WebSockAPI::json &req) override
Definition: CRUDHandler.cpp:151
Leosac::Module::Piface::CRUDHandler::create_impl
boost::optional< WebSockAPI::json > create_impl(const WebSockAPI::json &req) override
Definition: CRUDHandler.cpp:73
Leosac::Module::Piface::json
nlohmann::json json
Definition: PFGPIO.hpp:36
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb
Verb
Definition: CRUDResourceHandler.hpp:43
Leosac::Module::Piface::PFGPIOSerializer::unserialize
static void unserialize(PFGPIO &out, const json &in, const SecurityContext &sc)
Definition: PFGPIO.cpp:49
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