Leosac  0.8.0
Open Source Access Control
ScheduleMappingSerializer.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 
22 #include "core/auth/Door_odb.h"
23 #include "core/auth/User_odb.h"
25 #include "tools/JSONUtils.hpp"
27 #include "tools/db/DBService.hpp"
30 
31 using namespace Leosac;
32 using namespace Leosac::Tools;
33 
35  const SecurityContext &)
36 {
37  // general
38  json serialized = {
39  {"id", in.id()},
40  {"type", "schedule-mapping"},
41  {"attributes", {{"alias", in.alias()}, {"version", in.odb_version()}}}};
42 
43  json users = json::array();
44  for (const auto &user : in.users())
45  {
46  json json_user = {{"id", user.object_id()}, {"type", "user"}};
47  users.push_back(json_user);
48  }
49 
50  json groups = json::array();
51  for (const auto &group : in.groups())
52  {
53  json json_group = {{"id", group.object_id()}, {"type", "group"}};
54  groups.push_back(json_group);
55  }
56 
57  json creds = json::array();
58  for (const auto &cred : in.credentials())
59  {
60  // Credentials needs to be loaded to determine the underlying type.
61  // This is done silently by the serializer.
62  json json_cred = {
63  {"id", cred.object_id()},
64  {"type", PolymorphicCredentialJSONSerializer::type_name(*cred.load())}};
65  creds.push_back(json_cred);
66  }
67 
68  json doors = json::array();
69  for (const auto &door : in.doors())
70  {
71  json json_door = {{"id", door.object_id()}, {"type", "door"}};
72  doors.push_back(json_door);
73  }
74 
75  serialized["relationships"]["users"] = {{"data", users}};
76  serialized["relationships"]["groups"] = {{"data", groups}};
77  serialized["relationships"]["credentials"] = {{"data", creds}};
78  serialized["relationships"]["doors"] = {{"data", doors}};
79 
80  return serialized;
81 }
82 
84  const json &in,
85  const SecurityContext &)
86 {
87  // We need to database to build Lazy pointer from object's identifier.
88  auto db = get_service_registry().get_service<DBService>()->db();
89  using namespace JSONUtil;
90  out.alias(extract_with_default(in, "alias", out.alias()));
91 
92  auto group_ids = in.at("groups");
93  out.clear_groups();
94  for (const auto &group_id : group_ids)
95  {
96  auto group = Auth::GroupLPtr(*db, group_id.get<Tools::ScheduleMappingId>());
97  out.add_group(group);
98  }
99 
100  auto user_ids = in.at("users");
101  out.clear_users();
102  for (const auto &user_id : user_ids)
103  {
104  Auth::UserLPtr user(*db, user_id.get<Auth::UserId>());
105  out.add_user(user);
106  }
107 
108  auto credential_ids = in.at("credentials");
109  out.clear_credential();
110  for (const auto &credential_id : credential_ids)
111  {
112  Cred::CredentialLPtr credential(*db,
113  credential_id.get<Cred::CredentialId>());
114  out.add_credential(credential);
115  }
116 
117  auto door_ids = in.at("doors");
118  out.clear_doors();
119  for (const auto &door_id : door_ids)
120  {
121  Auth::DoorLPtr door(*db, door_id.get<Auth::DoorId>());
122  out.add_door(door);
123  }
124 }
125 
126 std::string
128  const SecurityContext &sc)
129 {
130  return ScheduleMappingJSONSerializer::serialize(in, sc).dump(4);
131 }
132 
134  const std::string &in,
135  const SecurityContext &sc)
136 {
138 }
Leosac::Tools::ScheduleMapping
Represent one of the mapping of a schedule.
Definition: ScheduleMapping.hpp:37
Leosac::Tools::ScheduleMapping::add_group
void add_group(const Auth::GroupLPtr &group)
Add a group to the mapping.
Definition: ScheduleMapping.cpp:142
Leosac::JSONUtil::extract_with_default
std::chrono::system_clock::time_point extract_with_default(const nlohmann::json &obj, const std::string &key, const std::chrono::system_clock::time_point &tp)
Extract an ISO 8601 datetime string from a json object.
Definition: JSONUtils.cpp:45
Leosac::Tools::ScheduleMapping::add_user
void add_user(const Auth::UserLPtr &user)
Add a user to the mapping.
Definition: ScheduleMapping.cpp:133
PolymorphicCredentialSerializer.hpp
Leosac::Auth::GroupLPtr
odb::lazy_shared_ptr< Group > GroupLPtr
Definition: AuthFwd.hpp:40
Leosac::Tools::ScheduleMapping::doors
const std::vector< Auth::DoorLWPtr > & doors() const
Definition: ScheduleMapping.cpp:175
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
ServiceRegistry.hpp
ScheduleMappingSerializer.hpp
Leosac::Tools::ScheduleMappingJSONStringSerializer::unserialize
static void unserialize(Tools::ScheduleMapping &out, const std::string &in, const SecurityContext &sc)
Definition: ScheduleMappingSerializer.cpp:133
Leosac::Tools::ScheduleMapping::credentials
const std::vector< Cred::CredentialLWPtr > & credentials() const
Definition: ScheduleMapping.cpp:170
Leosac::Tools::ScheduleMappingJSONSerializer::unserialize
static void unserialize(Tools::ScheduleMapping &out, const json &in, const SecurityContext &sc)
Definition: ScheduleMappingSerializer.cpp:83
Leosac::Tools::ScheduleMappingJSONStringSerializer::serialize
static std::string serialize(const Tools::ScheduleMapping &in, const SecurityContext &sc)
Definition: ScheduleMappingSerializer.cpp:127
Leosac::Tools::ScheduleMapping::odb_version
size_t odb_version() const
Definition: ScheduleMapping.cpp:200
Leosac::Tools::json
nlohmann::json json
Definition: ScheduleMappingSerializer.hpp:31
Leosac::Tools::ScheduleMappingJSONSerializer::serialize
static json serialize(const Tools::ScheduleMapping &in, const SecurityContext &sc)
Definition: ScheduleMappingSerializer.cpp:34
Leosac::Tools::ScheduleMapping::alias
const std::string & alias() const
Definition: ScheduleMapping.cpp:40
Leosac::DBService
Provides various database-related services to consumer.
Definition: DBService.hpp:34
Leosac::Tools::ScheduleMapping::clear_users
void clear_users()
Definition: ScheduleMapping.cpp:185
Leosac::Tools::ScheduleMapping::users
const std::vector< Auth::UserLWPtr > & users() const
Definition: ScheduleMapping.cpp:160
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
GetServiceRegistry.hpp
Leosac::Tools::ScheduleMapping::add_door
void add_door(const Auth::DoorLPtr &door)
Add a door to the mapping.
Definition: ScheduleMapping.cpp:124
Leosac::Auth::UserLPtr
odb::lazy_shared_ptr< User > UserLPtr
Definition: AuthFwd.hpp:32
Leosac::Tools::ScheduleMapping::clear_doors
void clear_doors()
Definition: ScheduleMapping.cpp:180
Leosac::Auth::DoorId
unsigned long DoorId
Definition: AuthFwd.hpp:107
JSONUtils.hpp
Leosac::PolymorphicCredentialJSONSerializer::type_name
static std::string type_name(const Cred::ICredential &in)
Returns the "type-name" of the credential.
Definition: PolymorphicCredentialSerializer.cpp:50
Leosac::Tools::ScheduleMapping::groups
const std::vector< Auth::GroupLWPtr > & groups() const
Definition: ScheduleMapping.cpp:165
ThreadLocalRegistry.hpp
DBService.hpp
Leosac::Tools::ScheduleMappingId
unsigned long ScheduleMappingId
Definition: ToolsFwd.hpp:44
Leosac::Cred::CredentialId
unsigned long CredentialId
Definition: CredentialFwd.hpp:35
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
Leosac::Cred::CredentialLPtr
odb::lazy_shared_ptr< Credential > CredentialLPtr
Definition: CredentialFwd.hpp:38
Leosac::Tools::ScheduleMapping::clear_credential
void clear_credential()
Definition: ScheduleMapping.cpp:195
Leosac::Tools::ScheduleMapping::id
ScheduleMappingId id() const
Definition: ScheduleMapping.cpp:35
Leosac::Auth::UserId
unsigned long UserId
Definition: AuthFwd.hpp:34
Leosac::Tools::ScheduleMapping::clear_groups
void clear_groups()
Definition: ScheduleMapping.cpp:190
ScheduleMapping.hpp
Leosac::Auth::DoorLPtr
odb::lazy_shared_ptr< Door > DoorLPtr
Definition: AuthFwd.hpp:111
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
Leosac::Tools::ScheduleMapping::add_credential
void add_credential(const Cred::CredentialLPtr &cred)
Add a credential to the mapping.
Definition: ScheduleMapping.cpp:151