Leosac  0.8.0
Open Source Access Control
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 
43  Auth::UserPtr target_user, AuditEntryPtr parent)
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 
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 }
Leosac::db::OptionalTransaction
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
Definition: OptionalTransaction.hpp:43
Leosac::db::OptionalTransaction::commit
void commit()
Commit the transaction, if there was no currently active transaction at the time of this object's cre...
Definition: OptionalTransaction.cpp:38
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::Audit::UserGroupMembershipEvent::target_user_id_
Auth::UserId target_user_id_
Set to target_user_->id()
Definition: UserGroupMembershipEvent.hpp:80
Leosac::Audit::AuditEntryPtr
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
Leosac::Audit::EventType::GROUP_MEMBERSHIP_JOINED
@ GROUP_MEMBERSHIP_JOINED
Someone joined the group.
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Audit::UserGroupMembershipEvent::generate_target_group_description
std::string generate_target_group_description() const
Definition: UserGroupMembershipEvent.cpp:110
Leosac::Audit::AuditEntry::database
void database(DBPtr db)
Set the database pointer.
Definition: AuditEntry.cpp:144
Leosac::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::UserGroupMembershipEvent::generate_description
std::string generate_description() const override
Generate a description for this event.
Definition: UserGroupMembershipEvent.cpp:83
Leosac::Audit::UserGroupMembershipEvent::target_group_
Auth::GroupLWPtr target_group_
Definition: UserGroupMembershipEvent.hpp:64
Leosac::Audit::UserGroupMembershipEvent::target_user_
Auth::UserLWPtr target_user_
Definition: UserGroupMembershipEvent.hpp:67
Leosac::Audit::UserGroupMembershipEvent::target_group
virtual void target_group(Auth::GroupPtr grp) override
The group that gained or lost an user.
Definition: UserGroupMembershipEvent.cpp:35
Leosac::Audit::UserGroupMembershipEvent
Provides an implementation of IUserGroupMembershipEvent.
Definition: UserGroupMembershipEvent.hpp:33
Leosac::Audit
The Audit namespace provides classes and facilities to keep track of what's happening on the Leosac d...
Definition: AccessPointEvent.hpp:27
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
OptionalTransaction.hpp
Leosac::Audit::UserGroupMembershipEvent::create
static UserGroupMembershipEventPtr create(const DBPtr &database, Auth::GroupPtr target_group, Auth::UserPtr target_user, AuditEntryPtr parent)
Definition: UserGroupMembershipEvent.cpp:42
Leosac::Auth::GroupPtr
std::shared_ptr< Group > GroupPtr
Definition: AuthFwd.hpp:37
JSONUtils.hpp
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::UserGroupMembershipEvent::target_group_id
Auth::GroupId target_group_id() const override
Definition: UserGroupMembershipEvent.cpp:76
Leosac::Audit::UserGroupMembershipEvent::target_group_id_
Auth::GroupId target_group_id_
This is equals to target_group_->id().
Definition: UserGroupMembershipEvent.hpp:75
Leosac::Audit::UserGroupMembershipEvent::target_user_id
Auth::UserId target_user_id() const override
Definition: UserGroupMembershipEvent.cpp:69
Leosac::Audit::EventType::GROUP_MEMBERSHIP_LEFT
@ GROUP_MEMBERSHIP_LEFT
Someone left the group.
Leosac::Auth::GroupId
unsigned long GroupId
Definition: AuthFwd.hpp:41
log.hpp
Leosac::Audit::UserGroupMembershipEvent::generate_target_user_description
std::string generate_target_user_description() const
Definition: UserGroupMembershipEvent.cpp:98
Leosac::Auth::UserId
unsigned long UserId
Definition: AuthFwd.hpp:34
UserGroupMembershipEvent.hpp
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::UserGroupMembershipEventPtr
std::shared_ptr< UserGroupMembershipEvent > UserGroupMembershipEventPtr
Definition: AuditFwd.hpp:99
Leosac::Audit::UserGroupMembershipEvent::target_user
virtual void target_user(Auth::UserPtr user) override
The user that joined or left the group.
Definition: UserGroupMembershipEvent.cpp:29