Leosac  0.7.0
OpenSourceAccessControl
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 
40  db::OptionalTransaction t(database->begin());
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 }
nlohmann::json json
virtual void target(Auth::GroupPtr grp) override
Set the group that is targeted by the event.
Definition: GroupEvent.cpp:55
const std::string & before() const override
Definition: GroupEvent.cpp:85
const std::string & after() const override
Definition: GroupEvent.cpp:90
Provides an implementation of IGroupEvent.
Definition: GroupEvent.hpp:33
This is the header file for a generated source file, GitSHA1.cpp.
static std::shared_ptr< GroupEvent > create(const DBPtr &database, Auth::GroupPtr target_group, AuditEntryPtr parent)
Definition: GroupEvent.cpp:30
std::string before_
Optional JSON dump of the object before the event took place.
Definition: GroupEvent.hpp:83
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:73
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
Auth::GroupId target_id() const override
Definition: GroupEvent.cpp:76
unsigned long GroupId
Definition: AuthFwd.hpp:41
std::shared_ptr< Group > GroupPtr
Definition: AuthFwd.hpp:37
static std::shared_ptr< GroupEvent > create_empty()
Definition: GroupEvent.cpp:122
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
std::string generate_description() const override
Generate a description for this event.
Definition: GroupEvent.cpp:95
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
Auth::GroupLWPtr target_
Definition: GroupEvent.hpp:70
std::string after_
Optional JSON dump of the object after the event took place.
Definition: GroupEvent.hpp:88
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
std::string generate_target_description() const
Generate a small json-string description about the targeted group.
Definition: GroupEvent.cpp:110
Auth::GroupId target_group_id_
This is equals to target_->id().
Definition: GroupEvent.hpp:78
std::shared_ptr< GroupEvent > GroupEventPtr
Definition: AuditFwd.hpp:96
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...