Leosac  0.8.0
Open Source Access Control
ScheduleEvent.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/ScheduleEvent_odb.h"
22 #include "tools/AssertCast.hpp"
23 #include "tools/JSONUtils.hpp"
24 #include "tools/Schedule.hpp"
25 #include "tools/Schedule_odb.h"
27 #include "tools/log.hpp"
28 
29 using namespace Leosac;
30 using namespace Leosac::Audit;
31 
32 std::shared_ptr<ScheduleEvent>
33 ScheduleEvent::create(const DBPtr &database, Tools::ISchedulePtr target_sched,
34  AuditEntryPtr parent)
35 {
36  ASSERT_LOG(database, "Database cannot be null.");
37  ASSERT_LOG(target_sched, "Credential shall not be null.");
38  ASSERT_LOG(target_sched->id(), "Credential must be already persisted.");
39 
41 
43  std::shared_ptr<Audit::ScheduleEvent>(new Audit::ScheduleEvent());
44  audit->database_ = database;
45  audit->target(target_sched);
46  database->persist(audit);
47 
48  audit->set_parent(parent);
49  database->update(audit);
50 
51  t.commit();
52  return audit;
53 }
54 
55 std::shared_ptr<ScheduleEvent> ScheduleEvent::create_empty()
56 {
57  return std::shared_ptr<ScheduleEvent>(new Audit::ScheduleEvent());
58 }
59 
61 {
62  ASSERT_LOG(sched, "sched must not be null.");
63  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
64  if (sched)
65  ASSERT_LOG(sched->id(), "Schedule has no id.");
66 
67  auto sched_odb = assert_cast<Tools::SchedulePtr>(sched);
68 
69  target_ = sched_odb;
70  target_sched_id_ = sched_odb->id();
71 }
72 
73 void ScheduleEvent::before(const std::string &repr)
74 {
75  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
76  before_ = repr;
77 }
78 
79 void ScheduleEvent::after(const std::string &repr)
80 {
81  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
82  after_ = repr;
83 }
84 
86 {
87  if (target_.lock())
88  {
89  return target_.object_id();
90  }
91  return target_sched_id_;
92 }
93 
94 const std::string &ScheduleEvent::before() const
95 {
96  return before_;
97 }
98 
99 const std::string &ScheduleEvent::after() const
100 {
101  return after_;
102 }
103 
105 {
106  using namespace FlagSetOperator;
107  std::stringstream ss;
108 
109  auto target = target_.load();
110  auto author = author_.load();
111 
113  ss << "Schedule " << generate_target_description() << " has been created.";
115  ss << "Schedule " << generate_target_description() << " has been edited.";
117  ss << "Schedule " << generate_target_description() << " has been deleted.";
118 
119  return ss.str();
120 }
121 
123 {
124  Leosac::json desc;
125 
126  desc["id"] = target_id();
127  auto t = target_.load();
128  if (t)
129  desc["name"] = t->name();
130 
131  return desc.dump();
132 }
Leosac::Audit::AuditEntry::author
virtual void author(Auth::UserPtr user) override
Set the author of the entry.
Definition: AuditEntry.cpp:88
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::Tools::ISchedulePtr
std::shared_ptr< ISchedule > ISchedulePtr
Definition: ToolsFwd.hpp:37
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::Audit::ScheduleEvent::target
virtual void target(Tools::ISchedulePtr sch) override
Set the user that is targeted by the event.
Definition: ScheduleEvent.cpp:60
Leosac::Audit::ScheduleEvent::after
const std::string & after() const override
Definition: ScheduleEvent.cpp:99
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::Audit::EventType::SCHEDULE_UPDATED
@ SCHEDULE_UPDATED
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::ScheduleEvent::before_
std::string before_
Optional JSON dump of the object before the event took place.
Definition: ScheduleEvent.hpp:78
Leosac::Audit::ScheduleEvent::target_id
Tools::ScheduleId target_id() const override
Definition: ScheduleEvent.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::ScheduleEvent::create_empty
static std::shared_ptr< ScheduleEvent > create_empty()
A factory function for usage in tests.
Definition: ScheduleEvent.cpp:55
Leosac::Audit::AuditEntry::finalized
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:72
Leosac::Audit::ScheduleEvent::generate_target_description
std::string generate_target_description() const
Definition: ScheduleEvent.cpp:122
Leosac::Tools::ScheduleId
unsigned long ScheduleId
Definition: ToolsFwd.hpp:33
JSONUtils.hpp
Leosac::Audit::EventType::SCHEDULE_DELETED
@ SCHEDULE_DELETED
Leosac::Audit::ScheduleEvent::generate_description
std::string generate_description() const override
Generate a description for this event.
Definition: ScheduleEvent.cpp:104
ScheduleEvent.hpp
Leosac::Audit::ScheduleEventPtr
std::shared_ptr< ScheduleEvent > ScheduleEventPtr
Definition: AuditFwd.hpp:105
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::ScheduleEvent::create
static std::shared_ptr< ScheduleEvent > create(const DBPtr &database, Tools::ISchedulePtr target_sched, AuditEntryPtr parent)
Definition: ScheduleEvent.cpp:33
Leosac::Audit::EventType::SCHEDULE_CREATED
@ SCHEDULE_CREATED
Leosac::Audit::ScheduleEvent::after_
std::string after_
Optional JSON dump of the object after the event took place.
Definition: ScheduleEvent.hpp:83
log.hpp
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::ScheduleEvent::target_
Tools::ScheduleLWPtr target_
Definition: ScheduleEvent.hpp:71
Schedule.hpp
Leosac::Audit::ScheduleEvent::target_sched_id_
Tools::ScheduleId target_sched_id_
Definition: ScheduleEvent.hpp:73
AssertCast.hpp
Leosac::Audit::ScheduleEvent
Provides an implementation of IScheduleEvent.
Definition: ScheduleEvent.hpp:34
Leosac::Audit::ScheduleEvent::before
const std::string & before() const override
Definition: ScheduleEvent.cpp:94
Leosac::Audit::AuditEntry::author_
Auth::UserLPtr author_
The user at the source of the entry.
Definition: AuditEntry.hpp:116