Leosac  0.8.0
Open Source Access Control
HardwareSearch.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 
21 #include "Exceptions.hpp"
22 #include "api/APISession.hpp"
24 #include "hardware/GPIO_odb.h"
25 #include "tools/db/DBService.hpp"
26 #include "tools/log.hpp"
28 
29 using namespace Leosac;
30 using namespace Leosac::Module;
31 using namespace Leosac::Module::WebSockAPI;
32 
34  : MethodHandler(ctx)
35 {
36 }
37 
39 {
40  return std::make_unique<HardwareSearch>(ctx);
41 }
42 
44 {
46  {
47  ASSERT_LOG(c1, "Hardware c1 is null");
48  ASSERT_LOG(c2, "Hardware c2 is null");
49  ASSERT_LOG(!c1->id().is_nil(), "c1 has no id.");
50  ASSERT_LOG(!c2->id().is_nil(), "c2 has no id.");
51  return c1->id() < c2->id();
52  }
53 };
54 
56 {
57  json rep = json::array();
58  DBPtr db = ctx_.dbsrv->db();
59  odb::transaction t(db->begin());
60  using Query = odb::query<Hardware::Device>;
61  std::string partial_name = req.at("partial_name");
62  std::set<Hardware::DevicePtr, HardwareComparator> hardware_devices;
63 
64  // We want a case insensitive search. However, there is no portable
65  // way to do this. So for now we'll rely on a naive, potentially slow
66  // bruteforce-style implementation.
67  // todo: fixme
68 
69  for (auto i = 0u; i < partial_name.length(); ++i)
70  {
71  auto partial_name_copy = partial_name;
72  partial_name_copy[i] = std::toupper(partial_name[i]);
73  Query q(Query::name.like("%" + partial_name_copy + "%"));
74  auto results = db->query(q);
75  for (const auto &device : results)
76  {
77  // We want shared_ptr to store in set. Call load, this should load from
78  // the cache anyway.
79  hardware_devices.insert(db->load<Hardware::Device>(device.id()));
80  }
81  }
82 
83  Query q(Query::name.like("%" + partial_name + "%"));
84  auto results = db->query(q);
85  for (const auto &dev : results)
86  {
87  // We want shared_ptr to store in set. Call load, this should load from the
88  // cache anyway.
89  hardware_devices.insert(db->load<Hardware::Device>(dev.id()));
90  }
91 
92  auto hardware_service =
94  ASSERT_LOG(hardware_service, "Failed to retrieve hardware service");
95  for (const auto &dev : hardware_devices)
96  {
97  ASSERT_LOG(dev, "Hardware is null.");
98  json result_json = {{"id", dev->id()},
99  {"name", dev->name()},
100  {"device-class", dev->device_class()},
101  {"type", hardware_service->hardware_device_type(*dev)}};
102  rep.push_back(result_json);
103  }
104 
105  return rep;
106 }
107 
108 std::vector<ActionActionParam>
110 {
111  std::vector<ActionActionParam> perm_;
113 
114  perm_.emplace_back(SecurityContext::Action::HARDWARE_SEARCH, ap);
115  return perm_;
116 }
HardwareComparator::operator()
bool operator()(const Hardware::DevicePtr &c1, const Hardware::DevicePtr &c2)
Definition: HardwareSearch.cpp:45
HardwareSearch.hpp
Leosac::Hardware::HardwareService
Database aware Hardware Service.
Definition: HardwareService.hpp:39
Exceptions.hpp
Leosac::Module::WebSockAPI::HardwareSearch::process_impl
virtual json process_impl(const json &req) override
The API method implementation.
Definition: HardwareSearch.cpp:55
Leosac::Module::WebSockAPI::HardwareSearch::create
static MethodHandlerUPtr create(RequestContext)
Definition: HardwareSearch.cpp:38
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
Leosac::Module::WebSockAPI::RequestContext::dbsrv
DBServicePtr dbsrv
Definition: RequestContext.hpp:39
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Module::WebSockAPI::HardwareSearch::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: HardwareSearch.cpp:109
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.
Query
odb::query< Tools::LogEntry > Query
Definition: LogEntry.cpp:36
Leosac::SecurityContext::ActionParam
Definition: SecurityContext.hpp:231
Leosac::Hardware::Device
Base class for hardware devices.
Definition: Device.hpp:44
Leosac::Module::WebSockAPI::MethodHandler
The base class for API method handler implementation.
Definition: MethodHandler.hpp:46
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
GetServiceRegistry.hpp
Leosac::Module::WebSockAPI::MethodHandler::ctx_
RequestContext ctx_
Definition: MethodHandler.hpp:90
Leosac::SecurityContext::Action::HARDWARE_SEARCH
@ HARDWARE_SEARCH
Leosac::Hardware::DevicePtr
std::shared_ptr< Device > DevicePtr
Definition: HardwareFwd.hpp:55
Leosac::Module::WebSockAPI::MethodHandlerUPtr
std::unique_ptr< MethodHandler > MethodHandlerUPtr
Definition: WebSockFwd.hpp:36
HardwareService.hpp
DBService.hpp
HardwareComparator
Definition: HardwareSearch.cpp:43
APISession.hpp
Leosac::ServiceRegistry::get_service
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
Definition: ServiceRegistry.hpp:290
log.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::HardwareSearch::HardwareSearch
HardwareSearch(RequestContext ctx)
Definition: HardwareSearch.cpp:33