Leosac  0.7.0
OpenSourceAccessControl
UserGroupMembershipEvent.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/audit/UserGroupMembershipEvent_odb.h"
23 #include "tools/log.hpp"
24 #include <tools/JSONUtils.hpp>
25 
26 using namespace Leosac;
27 using namespace Leosac::Audit;
28 
30 {
31  target_user_ = user;
32  target_user_id_ = user->id();
33 }
34 
36 {
37  target_group_ = group;
38  target_group_id_ = group->id();
39 }
40 
44 {
45  ASSERT_LOG(database, "Database cannot be null.");
46  ASSERT_LOG(target_user, "Target user must be non null.");
47  ASSERT_LOG(target_user->id(), "Target user must be already persisted.");
48  ASSERT_LOG(target_group, "Target group must be non null.");
49  ASSERT_LOG(target_group->id(), "Target group must be already persisted.");
50  ASSERT_LOG(parent, "Parent must be non null.");
51  ASSERT_LOG(parent->id(), "Parent must be already persisted.");
52 
53  db::OptionalTransaction t(database->begin());
55  std::shared_ptr<Audit::UserGroupMembershipEvent>(
57  audit->database_ = database;
58  audit->target_user(target_user);
59  audit->target_group(target_group);
60  database->persist(audit);
61 
62  audit->set_parent(parent);
63  database->update(audit);
64 
65  t.commit();
66  return audit;
67 }
68 
70 {
71  if (target_user_.lock())
72  return target_user_.object_id();
73  return target_user_id_;
74 }
75 
77 {
78  if (target_group_.lock())
79  return target_group_.object_id();
80  return target_group_id_;
81 }
82 
84 {
85  using namespace FlagSetOperator;
86  std::stringstream ss;
87 
89  ss << "User " << generate_target_user_description()
90  << " has joined the group " << generate_target_group_description();
92  ss << "User " << generate_target_user_description() << " has left the group "
94 
95  return ss.str();
96 }
97 
99 {
100  Leosac::json desc;
101 
102  desc["id"] = target_user_id();
103  auto t = target_user_.load();
104  if (t)
105  desc["username"] = t->username();
106 
107  return desc.dump();
108 }
109 
111 {
112  Leosac::json desc;
113 
114  desc["id"] = target_group_id();
115  auto t = target_group_.load();
116  if (t)
117  desc["name"] = t->name();
118 
119  return desc.dump();
120 }
nlohmann::json json
static UserGroupMembershipEventPtr create(const DBPtr &database, Auth::GroupPtr target_group, Auth::UserPtr target_user, AuditEntryPtr parent)
This is the header file for a generated source file, GitSHA1.cpp.
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:140
unsigned long GroupId
Definition: AuthFwd.hpp:41
Auth::GroupId target_group_id_
This is equals to target_group_->id().
unsigned long UserId
Definition: AuthFwd.hpp:34
std::shared_ptr< Group > GroupPtr
Definition: AuthFwd.hpp:37
std::shared_ptr< UserGroupMembershipEvent > UserGroupMembershipEventPtr
Definition: AuditFwd.hpp:99
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
Auth::GroupId target_group_id() const override
Auth::UserId target_user_id_
Set to target_user_->id()
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
virtual void target_user(Auth::UserPtr user) override
The user that joined or left the group.
std::string generate_description() const override
Generate a description for this event.
Provides an implementation of IUserGroupMembershipEvent.
virtual void target_group(Auth::GroupPtr grp) override
The group that gained or lost an user.
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
void database(DBPtr db)
Set the database pointer.
Definition: AuditEntry.cpp:145
The Audit namespace provides classes and facilities to keep track of what&#39;s happening on the Leosac d...