Leosac  0.8.0
Open Source Access Control
GroupSerializer.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 "GroupSerializer.hpp"
21 #include "core/auth/Group.hpp"
22 #include "tools/JSONUtils.hpp"
23 #include "tools/Schedule_odb.h"
24 #include "tools/log.hpp"
25 
26 using namespace Leosac;
27 using namespace Leosac::Auth;
28 
30  const SecurityContext &sc)
31 {
32  json memberships = {};
33 
34  for (const auto &membership : group.user_memberships())
35  {
37  ap.membership.membership_id = membership->id();
39  {
40  json group_info = {{"id", membership->id()},
41  {"type", "user-group-membership"}};
42  memberships.push_back(group_info);
43  }
44  }
45  std::set<Tools::ScheduleId> schedule_ids;
46  json schedules = {};
47  for (const Tools::ScheduleMappingLWPtr &mapping : group.lazy_schedules_mapping())
48  {
49  auto loaded = mapping.load();
50  ASSERT_LOG(loaded, "Cannot load. Need to investigate.");
51  schedule_ids.insert(loaded->schedule_id());
52  }
53  for (const auto &id : schedule_ids)
54  {
55  json sched_info = {{"id", id}, {"type", "schedule"}};
56  schedules.push_back(sched_info);
57  }
58 
59  json serialized = {
60  {"id", group.id()},
61  {"type", "group"},
62  {"attributes",
63  {
64  {"name", group.name()}, {"description", group.description()},
65  }},
66  {"relationships",
67  {{"memberships", {{"data", memberships}}},
68  {"schedules", {{"data", schedules}}}}}};
69 
70  return serialized;
71 }
72 
74  const SecurityContext &)
75 {
76  using namespace Leosac::JSONUtil;
77 
78  out.name(extract_with_default(in, "name", out.name()));
79  out.description(extract_with_default(in, "description", out.description()));
80 }
81 
83  const SecurityContext &sc)
84 {
85  return GroupJSONSerializer::serialize(in, sc).dump(4);
86 }
87 
88 void GroupJSONStringSerializer::unserialize(Auth::Group &out, const std::string &in,
89  const SecurityContext &sc)
90 {
91  auto tmp = json::parse(in);
93 }
Leosac::Auth::Group::user_memberships
const UserGroupMembershipSet & user_memberships() const
Retrieve the UserGroupMembership that this group is involved with.
Definition: Group.cpp:128
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
Leosac::GroupJSONSerializer::unserialize
static void unserialize(Auth::Group &out, const json &in, const SecurityContext &sc)
Definition: GroupSerializer.cpp:73
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::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::Group::id
GroupId id() const
Retrieve the unique identifier of the group.
Definition: Group.cpp:83
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::SecurityContext::MembershipActionParam::membership_id
Auth::UserGroupMembershipId membership_id
Definition: SecurityContext.hpp:188
Leosac::SecurityContext::Action::MEMBERSHIP_READ
@ MEMBERSHIP_READ
Leosac::Auth::Group::lazy_schedules_mapping
std::vector< Tools::ScheduleMappingLWPtr > lazy_schedules_mapping() const
Returns the vector of lazy_weak_ptr to schedule mapping.
Definition: Group.cpp:157
Leosac::SecurityContext::ActionParam
Definition: SecurityContext.hpp:231
Leosac::Tools::ScheduleMappingLWPtr
odb::lazy_weak_ptr< ScheduleMapping > ScheduleMappingLWPtr
Definition: ToolsFwd.hpp:43
Leosac::GroupJSONStringSerializer::serialize
static std::string serialize(const Auth::Group &in, const SecurityContext &sc)
Definition: GroupSerializer.cpp:82
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Group.hpp
Leosac::SecurityContext::ActionParam::membership
MembershipActionParam membership
Definition: SecurityContext.hpp:233
Leosac::Auth::Group
A authentication group regroup users that share permissions.
Definition: Group.hpp:57
JSONUtils.hpp
Leosac::Auth::Group::name
const std::string & name() const
Definition: Group.cpp:41
Leosac::GroupJSONSerializer::serialize
static json serialize(const Auth::Group &group, const SecurityContext &sc)
Definition: GroupSerializer.cpp:29
log.hpp
GroupSerializer.hpp
Leosac::GroupJSONStringSerializer::unserialize
static void unserialize(Auth::Group &out, const std::string &in, const SecurityContext &sc)
Definition: GroupSerializer.cpp:88
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
Leosac::Auth::Group::description
const std::string & description() const
Definition: Group.cpp:133