Leosac
0.8.0
Open Source Access Control
DoorSearch.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 "
api/search/DoorSearch.hpp
"
21
#include "
Exceptions.hpp
"
22
#include "
api/APISession.hpp
"
23
#include "
core/auth/Door.hpp
"
24
#include "core/auth/Door_odb.h"
25
#include "
tools/db/DBService.hpp
"
26
#include "
tools/log.hpp
"
27
28
using namespace
Leosac
;
29
using namespace
Leosac::Module
;
30
using namespace
Leosac::Module::WebSockAPI
;
31
32
DoorSearch::DoorSearch
(
RequestContext
ctx)
33
:
MethodHandler
(ctx)
34
{
35
}
36
37
MethodHandlerUPtr
DoorSearch::create
(
RequestContext
ctx)
38
{
39
return
std::make_unique<DoorSearch>(ctx);
40
}
41
42
struct
DoorComparator
43
{
44
bool
operator()
(
const
Auth::Door
&d1,
const
Auth::Door
&d2)
45
{
46
ASSERT_LOG
(d1.
id
(),
"d1 has no id."
);
47
ASSERT_LOG
(d2.
id
(),
"d2 has no id."
);
48
return
d1.
id
() < d2.
id
();
49
}
50
};
51
52
json
DoorSearch::process_impl
(
const
json
&req)
53
{
54
json
rep = json::array();
55
DBPtr
db =
ctx_
.
dbsrv
->db();
56
odb::transaction t(db->begin());
57
using
Query
= odb::query<Auth::Door>;
58
std::string partial_name = req.at(
"partial_name"
);
59
std::set<Auth::Door, DoorComparator> doors;
60
61
// We want a case insensitive search. However, there is no portable
62
// way to do this. So for now we'll rely on a naive, potentially slow
63
// bruteforce-style implementation.
64
// todo: fixme
65
66
for
(
auto
i = 0u; i < partial_name.length(); ++i)
67
{
68
auto
partial_name_copy = partial_name;
69
partial_name_copy[i] = std::toupper(partial_name[i]);
70
Query
q(Query::alias.like(
"%"
+ partial_name_copy +
"%"
));
71
auto
results = db->query(q);
72
for
(
const
auto
&door : results)
73
{
74
doors.insert(door);
75
}
76
}
77
78
Query
q(Query::alias.like(
"%"
+ partial_name +
"%"
));
79
auto
results = db->query(q);
80
for
(
const
auto
&door : results)
81
{
82
doors.insert(door);
83
}
84
85
for
(
const
auto
&door : doors)
86
{
87
json
result_json = {{
"id"
, door.id()}, {
"alias"
, door.alias()}};
88
rep.push_back(result_json);
89
}
90
91
return
rep;
92
}
93
94
std::vector<ActionActionParam>
DoorSearch::required_permission
(
const
json
&)
const
95
{
96
std::vector<ActionActionParam> perm_;
97
SecurityContext::ActionParam
ap{};
98
99
perm_.push_back({
SecurityContext::Action::DOOR_SEARCH
, ap});
100
return
perm_;
101
}
Exceptions.hpp
Leosac::Module::WebSockAPI::DoorSearch::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:
DoorSearch.cpp:94
Leosac::Module::WebSockAPI::DoorSearch::process_impl
virtual json process_impl(const json &req) override
The API method implementation.
Definition:
DoorSearch.cpp:52
Leosac::Module::WebSockAPI::RequestContext::dbsrv
DBServicePtr dbsrv
Definition:
RequestContext.hpp:39
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition:
log.hpp:190
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition:
db_fwd.hpp:31
Leosac::Module::WebSockAPI::DoorSearch::create
static MethodHandlerUPtr create(RequestContext)
Definition:
DoorSearch.cpp:37
DoorComparator::operator()
bool operator()(const Auth::Door &d1, const Auth::Door &d2)
Definition:
DoorSearch.cpp:44
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::Module::WebSockAPI::DoorSearch::DoorSearch
DoorSearch(RequestContext ctx)
Definition:
DoorSearch.cpp:32
Leosac::Module::WebSockAPI::MethodHandler
The base class for API method handler implementation.
Definition:
MethodHandler.hpp:46
DoorSearch.hpp
Door.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition:
APIStatusCode.hpp:22
Leosac::Auth::Door
Represent a door.
Definition:
Door.hpp:35
Leosac::Module::WebSockAPI::MethodHandler::ctx_
RequestContext ctx_
Definition:
MethodHandler.hpp:90
Leosac::Auth::Door::id
virtual DoorId id() const override
Definition:
Door.cpp:31
Leosac::Module::WebSockAPI::MethodHandlerUPtr
std::unique_ptr< MethodHandler > MethodHandlerUPtr
Definition:
WebSockFwd.hpp:36
DBService.hpp
APISession.hpp
DoorComparator
Definition:
DoorSearch.cpp:42
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::SecurityContext::Action::DOOR_SEARCH
@ DOOR_SEARCH
src
modules
websock-api
api
search
DoorSearch.cpp
Generated on Tue Mar 22 2022 10:48:28 for Leosac by
1.8.17