Leosac  0.8.0
Open Source Access Control
UpdateEvent.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 "UpdateEvent.hpp"
21 #include "core/audit/UpdateEvent_odb.h"
22 #include "tools/AssertCast.hpp"
24 #include "tools/log.hpp"
25 
26 namespace Leosac
27 {
28 namespace Audit
29 {
30 
31 std::shared_ptr<UpdateEvent> UpdateEvent::create(const DBPtr &database,
32  update::IUpdatePtr target_update,
33  AuditEntryPtr parent)
34 {
35  ASSERT_LOG(database, "Database cannot be null.");
36  ASSERT_LOG(target_update, "Target update must be non null.");
37  ASSERT_LOG(target_update->id(), "Target update must be already persisted.");
38  ASSERT_LOG(parent, "Parent must be non null.");
39  ASSERT_LOG(parent->id(), "Parent must be already persisted.");
40 
42 
43  UpdateEventPtr audit(new UpdateEvent());
44  audit->database_ = database;
45  audit->target(target_update);
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  auto update_odb = assert_cast<update::UpdatePtr>(u);
58  target_ = update_odb;
59 }
60 
62 {
63  if (target_.lock())
64  return target_.object_id();
65  return 0;
66 }
67 
68 std::shared_ptr<UpdateEvent> UpdateEvent::create_empty()
69 {
70  return UpdateEventPtr(new UpdateEvent());
71 }
72 
74 {
75  std::stringstream ss;
76 
78  {
79  ss << "Update " << target_id() << " has been created.";
80  }
82  {
83  ss << "Update " << target_id() << " has been acknowledged.";
84  }
86  {
87  ss << "Update " << target_id() << " has been cancelled.";
88  }
89 
90  return ss.str();
91 }
92 }
93 }
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::Audit::UpdateEvent::generate_description
std::string generate_description() const override
Generate a description for this event.
Definition: UpdateEvent.cpp:73
Leosac::Audit::EventType::UPDATE_CANCELLED
@ UPDATE_CANCELLED
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::update::IUpdatePtr
std::shared_ptr< IUpdate > IUpdatePtr
Definition: UpdateFwd.hpp:42
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::UpdateEventPtr
std::shared_ptr< UpdateEvent > UpdateEventPtr
Definition: AuditFwd.hpp:114
UpdateEvent.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
OptionalTransaction.hpp
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::UpdateEvent::create
static std::shared_ptr< UpdateEvent > create(const DBPtr &database, update::IUpdatePtr target_update, AuditEntryPtr parent)
Definition: UpdateEvent.cpp:31
Leosac::Audit::EventType::UPDATE_ACKED
@ UPDATE_ACKED
Leosac::Audit::UpdateEvent::UpdateEvent
UpdateEvent()=default
log.hpp
Leosac::Audit::UpdateEvent::target_
update::UpdateLWPtr target_
Definition: UpdateEvent.hpp:57
Leosac::Audit::EventType::UPDATE_CREATED
@ UPDATE_CREATED
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::UpdateEvent::target
void target(update::IUpdatePtr u) override
Set the update that is targeted by the event.
Definition: UpdateEvent.cpp:55
Leosac::update::UpdateId
unsigned long UpdateId
Definition: UpdateFwd.hpp:56
AssertCast.hpp
Leosac::Audit::UpdateEvent::create_empty
static std::shared_ptr< UpdateEvent > create_empty()
Definition: UpdateEvent.cpp:68
Leosac::Audit::UpdateEvent::target_id
update::UpdateId target_id() const override
Retrieve the user_id that was targeted by this event.
Definition: UpdateEvent.cpp:61