Leosac  0.8.0
Open Source Access Control
AccessOverview.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 "AccessOverview.hpp"
21 #include "core/auth/Door.hpp"
22 #include "core/auth/Door_odb.h"
23 #include "core/auth/User_odb.h"
24 #include "tools/JSONUtils.hpp"
25 #include "tools/db/DBService.hpp"
26 
27 using namespace Leosac;
28 using namespace Leosac::Module;
29 using namespace Leosac::Module::WebSockAPI;
30 
32  : MethodHandler(ctx)
33 {
34 }
35 
37 {
38  return std::make_unique<AccessOverview>(ctx);
39 }
40 
42 {
43  json rep;
44  DBPtr db = ctx_.dbsrv->db();
45  odb::transaction t(db->begin());
46 
47  // todo: This probably doesn't scale very well...
48 
49  auto doors = db->query<Auth::Door>();
50 
51  // Since we'll be looping over users multiple time, we cannot use
52  // an odb::result object.
53  auto users_odb = db->query<Auth::User>();
54  // So we'll have to convert this to a vector of User, instead of
55  // odb::result::iterator.
56  std::vector<Auth::UserPtr> users;
57  for (auto itr_odb(users_odb.begin()); itr_odb != users_odb.end(); ++itr_odb)
58  users.push_back(itr_odb.load());
59 
60  for (const auto &door : doors)
61  {
62  std::set<Auth::UserId> unique_user_ids;
63  json door_info = {{"door_id", door.id()}, {"user_ids", json::array()}};
64  for (const auto &lazy_mapping : door.lazy_mapping())
65  {
66  auto mapping = lazy_mapping.load();
67  for (const auto &user_ptr : users)
68  {
69  // Check the std::set in case the user is already authorized to
70  // access the door.
71  if (unique_user_ids.count(user_ptr->id()))
72  {
73  continue;
74  }
75  if (mapping->has_user_indirect(user_ptr))
76  {
77  unique_user_ids.insert(user_ptr->id());
78  }
79  }
80  }
81  for (const auto &id : unique_user_ids)
82  door_info["user_ids"].push_back(id);
83  rep.push_back(door_info);
84  }
85 
86  return rep;
87 }
88 
89 std::vector<ActionActionParam>
91 {
92  std::vector<ActionActionParam> perm_;
94 
95  perm_.push_back({SecurityContext::Action::ACCESS_OVERVIEW, ap});
96  return perm_;
97 }
Leosac::Module::WebSockAPI::AccessOverview::AccessOverview
AccessOverview(RequestContext ctx)
Definition: AccessOverview.cpp:31
Leosac::Module::WebSockAPI::RequestContext::dbsrv
DBServicePtr dbsrv
Definition: RequestContext.hpp:39
Leosac::SecurityContext::Action::ACCESS_OVERVIEW
@ ACCESS_OVERVIEW
Overview of users/doors access permission.
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.
Leosac::SecurityContext::ActionParam
Definition: SecurityContext.hpp:231
Leosac::Module::WebSockAPI::AccessOverview::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: AccessOverview.cpp:90
Leosac::Module::WebSockAPI::MethodHandler
The base class for API method handler implementation.
Definition: MethodHandler.hpp:46
Door.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Auth::User
Represent a user.
Definition: User.hpp:42
Leosac::Module::WebSockAPI::AccessOverview::create
static MethodHandlerUPtr create(RequestContext)
Definition: AccessOverview.cpp:36
Leosac::Module::WebSockAPI::AccessOverview::process_impl
virtual json process_impl(const json &req) override
The API method implementation.
Definition: AccessOverview.cpp:41
Leosac::Auth::Door
Represent a door.
Definition: Door.hpp:35
Leosac::Module::WebSockAPI::MethodHandler::ctx_
RequestContext ctx_
Definition: MethodHandler.hpp:90
JSONUtils.hpp
Leosac::Auth::User::id
UserId id() const noexcept
Definition: User.cpp:128
AccessOverview.hpp
Leosac::Module::WebSockAPI::MethodHandlerUPtr
std::unique_ptr< MethodHandler > MethodHandlerUPtr
Definition: WebSockFwd.hpp:36
DBService.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