Leosac  0.7.0
OpenSourceAccessControl
CredentialSerializer.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/SecurityContext.hpp"
23 #include "core/auth/User_odb.h"
26 #include "tools/JSONUtils.hpp"
27 #include "tools/Schedule_odb.h"
29 #include <date/date.h>
30 
31 using namespace Leosac;
32 using namespace Leosac::Cred;
33 
35  const SecurityContext &)
36 {
37  std::set<Tools::ScheduleId> schedule_ids;
38  json schedules = {};
39  for (const Tools::ScheduleMappingLWPtr &mapping : in.lazy_schedules_mapping())
40  {
41  auto loaded = mapping.load();
42  ASSERT_LOG(loaded, "Cannot load. Need to investigate.");
43  schedule_ids.insert(loaded->schedule_id());
44  }
45  for (const auto &id : schedule_ids)
46  {
47  json sched_info = {{"id", id}, {"type", "schedule"}};
48  schedules.push_back(sched_info);
49  }
50 
51  json serialized = {
52  {"id", in.id()},
53  {"type", "credential"},
54  {"attributes",
55  {{"version", in.odb_version()},
56  {"alias", in.alias()},
57  {"description", in.description()},
58  {"validity-enabled", in.validity().is_enabled()},
59  {"validity-start", date::format("%FT%T%z", in.validity().start())},
60  {"validity-end", date::format("%FT%T%z", in.validity().end())}}}};
61 
62  if (in.owner_id())
63  {
64  serialized["relationships"]["owner"] = {
65  {"data", {{"id", in.owner_id()}, {"type", "user"}}}};
66  }
67  serialized["relationships"]["schedules"] = {{"data", schedules}};
68  return serialized;
69 }
70 
72  const SecurityContext &)
73 {
74  using namespace JSONUtil;
75  out.alias(extract_with_default(in, "alias", out.alias()));
76  out.description(extract_with_default(in, "description", out.description()));
77 
78  // Credential validity attributes
79  Auth::ValidityInfo validity_default;
80  validity_default.set_enabled(out.validity().is_enabled());
81  out.validity(extract_validity_with_default(in, "validity", validity_default));
82 
83  // Owner
84  Auth::UserId new_owner_id = extract_with_default(in, "owner_id", out.owner_id());
85  if (new_owner_id != out.owner_id())
86  {
87  if (new_owner_id)
88  {
90  Auth::UserLPtr new_owner(*dbptr, new_owner_id);
91  out.owner(new_owner);
92  }
93  else
94  {
95  out.owner(std::shared_ptr<Auth::User>());
96  }
97  }
98 }
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
virtual Auth::UserId owner_id() const =0
Returns the id of the owner, or 0 if there is no owner (or the owner has no id).
static void unserialize(Cred::ICredential &out, const json &in, const SecurityContext &sc)
nlohmann::json json
virtual std::string description() const =0
An optional description / notes for the credential.
This is the header file for a generated source file, GitSHA1.cpp.
Base interface for credential objects.
Definition: ICredential.hpp:35
virtual std::vector< Tools::ScheduleMappingLWPtr > lazy_schedules_mapping() const =0
Retrieve the lazy_weak_ptr to ScheduleMapping that map this credential.
odb::lazy_shared_ptr< User > UserLPtr
Definition: AuthFwd.hpp:32
A simple class that stores (and can be queried for) the validity of some objects. ...
virtual void validity(const Auth::ValidityInfo &)=0
Provide the validity info object to the credential.
static json serialize(const Cred::ICredential &in, const SecurityContext &sc)
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
virtual Auth::UserLPtr owner() const =0
Retrieve the owner of the credential.
unsigned long UserId
Definition: AuthFwd.hpp:34
A SecurityContext is used to query permission while doing an operation.
Provides various database-related services to consumer.
Definition: DBService.hpp:34
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
virtual std::string alias() const =0
An alias for the credential.
odb::lazy_weak_ptr< ScheduleMapping > ScheduleMappingLWPtr
Definition: ToolsFwd.hpp:43
virtual size_t odb_version() const =0
Credentials are "optimistic" object (wrt ODB).
Auth::ValidityInfo extract_validity_with_default(const nlohmann::json &obj, const std::string &base_key, const Auth::ValidityInfo &def)
Extract fields representing a ValidityInfo object.
Definition: JSONUtils.cpp:29
virtual CredentialId id() const =0
Retrieve the identifier of the credential.
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
std::string format(const std::string &escape_code, const T &in)
Return a string containing the escape code, a string representation of T and the clear escape string...
Definition: Colorize.hpp:49