Leosac  0.8.0
Open Source Access Control
AccessPointSearch.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 
21 #include "Exceptions.hpp"
22 #include "api/APISession.hpp"
24 #include "core/auth/AccessPoint_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 
33  : MethodHandler(ctx)
34 {
35 }
36 
38 {
39  return std::make_unique<AccessPointSearch>(ctx);
40 }
41 
43 {
44  bool operator()(const Auth::AccessPoint &ap1, const Auth::AccessPoint &ap2)
45  {
46  ASSERT_LOG(ap1.id(), "g1 has no id.");
47  ASSERT_LOG(ap2.id(), "g2 has no id.");
48  return ap1.id() < ap2.id();
49  }
50 };
51 
53 {
54  json rep = json::array();
55  DBPtr db = ctx_.dbsrv->db();
56  odb::transaction t(db->begin());
57  using Query = odb::query<Auth::AccessPoint>;
58  std::string partial_name = req.at("partial_name");
59  std::set<Auth::AccessPoint, AccessPointComparator> access_points;
60 
61  // todo fix me performance
62 
63  for (auto i = 0u; i < partial_name.length(); ++i)
64  {
65  auto partial_name_copy = partial_name;
66  partial_name_copy[i] = std::toupper(partial_name[i]);
67  Query q(Query::alias.like("%" + partial_name_copy + "%"));
68  auto results = db->query(q);
69  for (const auto &ap : results)
70  {
71  access_points.insert(ap);
72  }
73  }
74 
75  Query q(Query::alias.like("%" + partial_name + "%"));
76  auto results = db->query(q);
77  for (const auto &ap : results)
78  {
79  access_points.insert(ap);
80  }
81 
82  for (const auto &ap : access_points)
83  {
84  json result_json = {{"id", ap.id()}, {"alias", ap.alias()}};
85  rep.push_back(result_json);
86  }
87 
88  return rep;
89 }
90 
91 std::vector<ActionActionParam>
93 {
94  std::vector<ActionActionParam> perm_;
95  perm_.push_back({SecurityContext::Action::ACCESS_POINT_SEARCH, {}});
96  return perm_;
97 }
AccessPoint.hpp
Exceptions.hpp
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
All modules that provides features to Leosac shall be in this namespace.
Query
odb::query< Tools::LogEntry > Query
Definition: LogEntry.cpp:36
Leosac::Module::WebSockAPI::MethodHandler
The base class for API method handler implementation.
Definition: MethodHandler.hpp:46
Leosac::Module::WebSockAPI::AccessPointSearch::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: AccessPointSearch.cpp:92
AccessPointComparator::operator()
bool operator()(const Auth::AccessPoint &ap1, const Auth::AccessPoint &ap2)
Definition: AccessPointSearch.cpp:44
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::WebSockAPI::MethodHandler::ctx_
RequestContext ctx_
Definition: MethodHandler.hpp:90
Leosac::Module::WebSockAPI::AccessPointSearch::process_impl
virtual json process_impl(const json &req) override
The API method implementation.
Definition: AccessPointSearch.cpp:52
Leosac::Module::WebSockAPI::MethodHandlerUPtr
std::unique_ptr< MethodHandler > MethodHandlerUPtr
Definition: WebSockFwd.hpp:36
Leosac::Auth::AccessPoint::id
AccessPointId id() const override
Definition: AccessPoint.cpp:33
DBService.hpp
Leosac::Module::WebSockAPI::AccessPointSearch::create
static MethodHandlerUPtr create(RequestContext)
Definition: AccessPointSearch.cpp:37
AccessPointSearch.hpp
APISession.hpp
Leosac::Module::WebSockAPI::AccessPointSearch::AccessPointSearch
AccessPointSearch(RequestContext ctx)
Definition: AccessPointSearch.cpp:32
log.hpp
Leosac::SecurityContext::Action::ACCESS_POINT_SEARCH
@ ACCESS_POINT_SEARCH
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::Auth::AccessPoint
Definition: AccessPoint.hpp:30
Leosac::Module::WebSockAPI
Definition: ActionActionParam.hpp:28
AccessPointComparator
Definition: AccessPointSearch.cpp:42