Leosac
0.8.0
Open Source Access Control
GroupSearch.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/GroupSearch.hpp
"
21
#include "
Exceptions.hpp
"
22
#include "
api/APISession.hpp
"
23
#include "
core/auth/Group.hpp
"
24
#include "core/auth/Group_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
GroupSearch::GroupSearch
(
RequestContext
ctx)
33
:
MethodHandler
(ctx)
34
{
35
}
36
37
MethodHandlerUPtr
GroupSearch::create
(
RequestContext
ctx)
38
{
39
return
std::make_unique<GroupSearch>(ctx);
40
}
41
42
struct
GroupComparator
43
{
44
bool
operator()
(
const
Auth::Group
&g1,
const
Auth::Group
&g2)
45
{
46
ASSERT_LOG
(g1.
id
(),
"g1 has no id."
);
47
ASSERT_LOG
(g2.
id
(),
"g2 has no id."
);
48
return
g1.
id
() < g2.
id
();
49
}
50
};
51
52
json
GroupSearch::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::Group>;
58
std::string partial_name = req.at(
"partial_name"
);
59
std::set<Auth::Group, GroupComparator> groups;
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::name.like(
"%"
+ partial_name_copy +
"%"
));
71
auto
results = db->query(q);
72
for
(
const
auto
&group : results)
73
{
74
groups.insert(group);
75
}
76
}
77
78
Query
q(Query::name.like(
"%"
+ partial_name +
"%"
));
79
auto
results = db->query(q);
80
for
(
const
auto
&group : results)
81
{
82
groups.insert(group);
83
}
84
85
for
(
const
auto
&group : groups)
86
{
87
json
result_json = {{
"id"
, group.id()}, {
"name"
, group.name()}};
88
rep.push_back(result_json);
89
}
90
91
return
rep;
92
}
93
94
std::vector<ActionActionParam>
GroupSearch::required_permission
(
const
json
&)
const
95
{
96
std::vector<ActionActionParam> perm_;
97
SecurityContext::ActionParam
ap{};
98
99
perm_.push_back({
SecurityContext::Action::GROUP_SEARCH
, ap});
100
return
perm_;
101
}
Leosac::Module::WebSockAPI::GroupSearch::create
static MethodHandlerUPtr create(RequestContext)
Definition:
GroupSearch.cpp:37
Exceptions.hpp
Leosac::Auth::Group::id
GroupId id() const
Retrieve the unique identifier of the group.
Definition:
Group.cpp:83
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::GroupSearch::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:
GroupSearch.cpp:94
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::Action::GROUP_SEARCH
@ GROUP_SEARCH
Leosac::SecurityContext::ActionParam
Definition:
SecurityContext.hpp:231
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
Group.hpp
Leosac::Module::WebSockAPI::MethodHandler::ctx_
RequestContext ctx_
Definition:
MethodHandler.hpp:90
Leosac::Auth::Group
A authentication group regroup users that share permissions.
Definition:
Group.hpp:57
GroupComparator::operator()
bool operator()(const Auth::Group &g1, const Auth::Group &g2)
Definition:
GroupSearch.cpp:44
Leosac::Module::WebSockAPI::MethodHandlerUPtr
std::unique_ptr< MethodHandler > MethodHandlerUPtr
Definition:
WebSockFwd.hpp:36
DBService.hpp
GroupSearch.hpp
APISession.hpp
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::GroupSearch::process_impl
virtual json process_impl(const json &req) override
The API method implementation.
Definition:
GroupSearch.cpp:52
GroupComparator
Definition:
GroupSearch.cpp:42
Leosac::Module::WebSockAPI::GroupSearch::GroupSearch
GroupSearch(RequestContext ctx)
Definition:
GroupSearch.cpp:32
src
modules
websock-api
api
search
GroupSearch.cpp
Generated on Tue Mar 22 2022 10:48:28 for Leosac by
1.8.17