Leosac  0.8.0
Open Source Access Control
GroupEvent.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 "GroupEvent.hpp"
21 #include "core/audit/GroupEvent_odb.h"
22 #include "core/auth/Group.hpp"
23 #include "tools/JSONUtils.hpp"
25 #include "tools/log.hpp"
26 
27 using namespace Leosac;
28 using namespace Leosac::Audit;
29 
30 std::shared_ptr<GroupEvent> GroupEvent::create(const DBPtr &database,
31  Auth::GroupPtr target_group,
32  AuditEntryPtr parent)
33 {
34  ASSERT_LOG(database, "Database cannot be null.");
35  ASSERT_LOG(target_group, "Target group must be non null.");
36  ASSERT_LOG(target_group->id(), "Target group must be already persisted.");
37  ASSERT_LOG(parent, "Parent must be non null.");
38  ASSERT_LOG(parent->id(), "Parent must be already persisted.");
39 
41 
42  Audit::GroupEventPtr audit =
43  std::shared_ptr<Audit::GroupEvent>(new Audit::GroupEvent());
44  audit->database_ = database;
45  audit->target(target_group);
46  database->persist(audit);
47 
48  audit->set_parent(parent);
49  database->update(audit);
50 
51  t.commit();
52  return audit;
53 }
54 
56 {
57  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
58  if (grp)
59  ASSERT_LOG(grp->id(), "Group has no id.");
60  target_ = grp;
61  target_group_id_ = grp->id();
62 }
63 
64 void GroupEvent::before(const std::string &repr)
65 {
66  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
67  before_ = repr;
68 }
69 
70 void GroupEvent::after(const std::string &repr)
71 {
72  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
73  after_ = repr;
74 }
75 
77 {
78  if (target_.lock())
79  {
80  return target_.object_id();
81  }
82  return target_group_id_;
83 }
84 
85 const std::string &GroupEvent::before() const
86 {
87  return before_;
88 }
89 
90 const std::string &GroupEvent::after() const
91 {
92  return after_;
93 }
94 
96 {
97  using namespace FlagSetOperator;
98  std::stringstream ss;
99 
101  ss << "Group " << generate_target_description() << " has been created.";
103  ss << "Group " << generate_target_description() << " has been edited.";
105  ss << "Group " << generate_target_description() << " has been deleted.";
106 
107  return ss.str();
108 }
109 
111 {
112  Leosac::json desc;
113 
114  desc["id"] = target_id();
115  auto t = target_.load();
116  if (t)
117  desc["name"] = t->name();
118 
119  return desc.dump();
120 }
121 
122 std::shared_ptr<GroupEvent> GroupEvent::create_empty()
123 {
124  return GroupEventPtr(new GroupEvent());
125 }
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::Audit::EventType::GROUP_CREATED
@ GROUP_CREATED
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::AuditEntryPtr
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Audit::AuditEntry::database
void database(DBPtr db)
Set the database pointer.
Definition: AuditEntry.cpp:144
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::GroupEvent::generate_description
std::string generate_description() const override
Generate a description for this event.
Definition: GroupEvent.cpp:95
Leosac::Audit::GroupEventPtr
std::shared_ptr< GroupEvent > GroupEventPtr
Definition: AuditFwd.hpp:96
Leosac::Audit::GroupEvent::before
const std::string & before() const override
Definition: GroupEvent.cpp:85
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::EventType::GROUP_DELETED
@ GROUP_DELETED
Leosac::Audit::GroupEvent::create
static std::shared_ptr< GroupEvent > create(const DBPtr &database, Auth::GroupPtr target_group, AuditEntryPtr parent)
Definition: GroupEvent.cpp:30
Leosac::Audit::GroupEvent::GroupEvent
GroupEvent()=default
Leosac::Audit::AuditEntry::finalized
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:72
Group.hpp
Leosac::Auth::GroupPtr
std::shared_ptr< Group > GroupPtr
Definition: AuthFwd.hpp:37
JSONUtils.hpp
Leosac::Audit::GroupEvent::target_id
Auth::GroupId target_id() const override
Definition: GroupEvent.cpp:76
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::GroupEvent
Provides an implementation of IGroupEvent.
Definition: GroupEvent.hpp:33
Leosac::Audit::GroupEvent::target_group_id_
Auth::GroupId target_group_id_
This is equals to target_->id().
Definition: GroupEvent.hpp:78
Leosac::Audit::GroupEvent::create_empty
static std::shared_ptr< GroupEvent > create_empty()
Definition: GroupEvent.cpp:122
Leosac::Audit::EventType::GROUP_UPDATED
@ GROUP_UPDATED
Leosac::Audit::GroupEvent::generate_target_description
std::string generate_target_description() const
Generate a small json-string description about the targeted group.
Definition: GroupEvent.cpp:110
Leosac::Auth::GroupId
unsigned long GroupId
Definition: AuthFwd.hpp:41
log.hpp
Leosac::Audit::GroupEvent::target
virtual void target(Auth::GroupPtr grp) override
Set the group that is targeted by the event.
Definition: GroupEvent.cpp:55
GroupEvent.hpp
Leosac::Audit::GroupEvent::after_
std::string after_
Optional JSON dump of the object after the event took place.
Definition: GroupEvent.hpp:88
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::GroupEvent::target_
Auth::GroupLWPtr target_
Definition: GroupEvent.hpp:70
Leosac::Audit::GroupEvent::after
const std::string & after() const override
Definition: GroupEvent.cpp:90
Leosac::Audit::GroupEvent::before_
std::string before_
Optional JSON dump of the object before the event took place.
Definition: GroupEvent.hpp:83