Leosac  0.8.0
Open Source Access Control
UserSerializer.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 "core/SecurityContext.hpp"
22 #include "core/auth/User.hpp"
24 #include "core/credentials/Credential_odb.h"
26 #include "tools/JSONUtils.hpp"
27 #include "tools/Schedule_odb.h"
28 #include "tools/log.hpp"
29 
30 using namespace Leosac;
31 using namespace Leosac::Auth;
32 
34 {
35  json memberships = {};
36  for (const auto &membership : user.group_memberships())
37  {
39  ap.membership.membership_id = membership->id();
41  {
42  json group_info = {{"id", membership->id()},
43  {"type", "user-group-membership"}};
44  memberships.push_back(group_info);
45  }
46  }
47  json credentials = {};
48  for (const Cred::CredentialLWPtr &cred : user.lazy_credentials())
49  {
51  cred.object_id()};
53  {
54  json cred_info = {
55  {"id", cred.object_id()},
56  {"type",
58  credentials.push_back(cred_info);
59  }
60  }
61  // We dont list schedule mapping to websocket client, instead we list
62  // schedules.
63  std::set<Tools::ScheduleId> schedule_ids;
64  json schedules = {};
65  for (const Tools::ScheduleMappingLWPtr &mapping : user.lazy_schedules_mapping())
66  {
67  auto loaded = mapping.load();
68  ASSERT_LOG(loaded, "Cannot load. Need to investigate.");
69  schedule_ids.insert(loaded->schedule_id());
70  }
71  for (const auto &id : schedule_ids)
72  {
73  json sched_info = {{"id", id}, {"type", "schedule"}};
74  schedules.push_back(sched_info);
75  }
76 
77  json serialized = {
78  {"id", user.id()},
79  {"type", "user"},
80  {"attributes",
81  {
82  {"version", user.odb_version()},
83  {"username", user.username()},
84  {"firstname", user.firstname()},
85  {"lastname", user.lastname()},
86  {"rank", static_cast<int>(user.rank())},
87  {"validity-enabled", user.validity().is_enabled()},
88  {"validity-start", date::format("%FT%T%z", user.validity().start())},
89  {"validity-end", date::format("%FT%T%z", user.validity().end())},
90  }},
91  {"relationships",
92  {{"memberships", {{"data", memberships}}},
93  {"credentials", {{"data", credentials}}},
94  {"schedules", {{"data", schedules}}}}}};
95 
97  ap.user.user_id = user.id();
99  {
100  serialized["attributes"]["email"] = user.email();
101  }
102  return serialized;
103 }
104 
106  const SecurityContext &sc)
107 {
108  using namespace Leosac::JSONUtil;
109 
110  out.firstname(extract_with_default(in, "firstname", out.firstname()));
111  out.lastname(extract_with_default(in, "lastname", out.lastname()));
112  out.email(extract_with_default(in, "email", out.email()));
113  if (in.find("password") != in.end() && (*in.find("password")).is_string())
114  {
115  out.password(in.at("password"));
116  }
117 
119  ap.user.user_id = out.id();
121  {
122  out.rank(extract_with_default(in, "rank", out.rank()));
123  }
125  {
126  Auth::ValidityInfo validity_default;
127  validity_default.set_enabled(out.validity().is_enabled());
128  out.validity(
129  extract_validity_with_default(in, "validity", validity_default));
130  }
131 }
132 
134  const SecurityContext &sc)
135 {
136  return UserJSONSerializer::serialize(in, sc).dump(4);
137 }
138 
139 void UserJSONStringSerializer::unserialize(Auth::User &out, const std::string &in,
140  const SecurityContext &sc)
141 {
142  json tmp = json::parse(in);
143  UserJSONSerializer::unserialize(out, tmp, sc);
144 }
Leosac::Auth::User::lastname
const std::string & lastname() const
Definition: User.cpp:88
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::JSONUtil
Add a few useful extraction functions.
Definition: JSONUtils.cpp:27
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::UserJSONSerializer::serialize
static json serialize(const Auth::User &in, const SecurityContext &sc)
Definition: UserSerializer.cpp:33
PolymorphicCredentialSerializer.hpp
Leosac::SecurityContext::check_permission
virtual bool check_permission(Action a, const ActionParam &ap) const
Check for the permission to perform action a with parameters ap.
Definition: SecurityContext.cpp:30
Leosac::Auth::User::password
void password(const std::string &pw)
Set a new password for the user.
Definition: User.cpp:138
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
User.hpp
Leosac::UserJSONStringSerializer::unserialize
static void unserialize(Auth::User &out, const std::string &in, const SecurityContext &sc)
Definition: UserSerializer.cpp:139
Leosac::Auth::User::rank
UserRank rank() const
Get the global rank of the user.
Definition: User.cpp:161
Leosac::Auth::ValidityInfo::is_enabled
bool is_enabled() const
Is the credential enabled ?
Definition: ValidityInfo.cpp:39
Leosac::Auth::User::odb_version
size_t odb_version() const
Definition: User.cpp:171
Leosac::SecurityContext::MembershipActionParam::membership_id
Auth::UserGroupMembershipId membership_id
Definition: SecurityContext.hpp:188
Leosac::SecurityContext::Action::MEMBERSHIP_READ
@ MEMBERSHIP_READ
Leosac::SecurityContext::CredentialActionParam::credential_id
Cred::CredentialId credential_id
Definition: SecurityContext.hpp:181
Leosac::SecurityContext::ActionParam
Definition: SecurityContext.hpp:231
Leosac::Tools::ScheduleMappingLWPtr
odb::lazy_weak_ptr< ScheduleMapping > ScheduleMappingLWPtr
Definition: ToolsFwd.hpp:43
Leosac::Cred::CredentialLWPtr
odb::lazy_weak_ptr< Credential > CredentialLWPtr
Definition: CredentialFwd.hpp:37
Leosac::SecurityContext::Action::USER_READ_EMAIL
@ USER_READ_EMAIL
SecurityContext.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
UserSerializer.hpp
Leosac::Auth::User::username
const std::string & username() const noexcept
Get the username of this user.
Definition: User.cpp:55
Leosac::Auth::ValidityInfo::set_enabled
void set_enabled(bool v)
Definition: ValidityInfo.cpp:93
Leosac::Auth::ValidityInfo::end
const TimePoint & end() const
Definition: ValidityInfo.cpp:103
Leosac::Auth::User::lazy_schedules_mapping
std::vector< Tools::ScheduleMappingLWPtr > lazy_schedules_mapping() const
Definition: User.cpp:181
Leosac::SecurityContext::ActionParam::membership
MembershipActionParam membership
Definition: SecurityContext.hpp:233
JSONUtils.hpp
Leosac::Auth::User::id
UserId id() const noexcept
Definition: User.cpp:128
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::Auth::User::group_memberships
const UserGroupMembershipSet & group_memberships() const
Retrieve the UserGroupMembership that this user is involved with.
Definition: User.cpp:133
Leosac::JSONUtil::extract_validity_with_default
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
Leosac::Auth::ValidityInfo::start
const TimePoint & start() const
Definition: ValidityInfo.cpp:98
Leosac::UserJSONSerializer::unserialize
static void unserialize(Auth::User &out, const json &in, const SecurityContext &sc)
Definition: UserSerializer.cpp:105
Leosac::SecurityContext::Action::USER_UPDATE_RANK
@ USER_UPDATE_RANK
Editing rank means being able to become administrator.
Leosac::Auth::User::email
const std::string & email() const
Definition: User.cpp:93
Leosac::SecurityContext::CredentialActionParam
Definition: SecurityContext.hpp:179
Leosac::Colorize::detail::format
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
log.hpp
Leosac::Auth::User::validity
const ValidityInfo & validity() const
Definition: User.cpp:113
Leosac::SecurityContext::Action::USER_MANAGE_VALIDITY
@ USER_MANAGE_VALIDITY
Can we enable/disable the user or change its validity period ?
CredentialFwd.hpp
Leosac::SecurityContext::Action::CREDENTIAL_READ
@ CREDENTIAL_READ
Leosac::Auth::User::lazy_credentials
std::vector< Cred::CredentialLWPtr > lazy_credentials() const
Definition: User.cpp:176
Leosac::SecurityContext::UserActionParam::user_id
Auth::UserId user_id
Definition: SecurityContext.hpp:174
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
Leosac::Auth::ValidityInfo
A simple class that stores (and can be queried for) the validity of some objects.
Definition: ValidityInfo.hpp:42
Leosac::Auth::User::firstname
const std::string & firstname() const
Definition: User.cpp:83
Leosac::UserJSONStringSerializer::serialize
static std::string serialize(const Auth::User &in, const SecurityContext &sc)
Definition: UserSerializer.cpp:133
Leosac::SecurityContext::ActionParam::user
UserActionParam user
Definition: SecurityContext.hpp:234