Leosac  0.8.0
Open Source Access Control
AuditEntry.hpp
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 #pragma once
21 
22 #include "core/audit/AuditFwd.hpp"
24 #include "core/auth/AuthFwd.hpp"
26 #include "tools/db/database.hpp"
27 #include <boost/date_time/posix_time/posix_time.hpp>
28 #include <memory>
29 #include <odb/callback.hxx>
30 #include <odb/core.hxx>
31 
32 namespace Leosac
33 {
34 namespace Audit
35 {
44 #pragma db object polymorphic optimistic callback(odb_callback)
45 class AuditEntry : virtual public IAuditEntry,
46  public std::enable_shared_from_this<AuditEntry>
47 {
48  protected:
49  AuditEntry();
50 
51  public:
52  AuditEntry(const AuditEntry &) = delete;
53  virtual ~AuditEntry() = default;
54 
55  virtual AuditEntryId id() const override;
56 
57  virtual void finalize() override;
58 
59  virtual bool finalized() const override;
60 
61  virtual void event_mask(const EventMask &mask) override;
62 
63  virtual const EventMask &event_mask() const override;
64 
65  virtual void author(Auth::UserPtr user) override;
66 
67  virtual Auth::UserId author_id() const override;
68 
69  virtual void set_parent(IAuditEntryPtr parent) override;
70 
71  virtual IAuditEntryPtr parent() const override;
72 
73  virtual void remove_parent() override;
74 
75  virtual size_t children_count() const override;
76 
77  virtual size_t version() const override;
78 
79  virtual void reload() override;
80 
81  virtual boost::posix_time::ptime timestamp() const override;
82 
88  void database(DBPtr db);
89 
94 
95  private:
96 #pragma db id auto
98 
99 #pragma db not_null
100  boost::posix_time::ptime timestamp_;
101 
102 #pragma db value_not_null
103  std::vector<AuditEntryPtr> children_;
104 
105  protected:
106 #pragma db inverse(children_)
108 
109 #pragma db not_null
110  std::string msg_;
111 
117 
118 #pragma db type("TEXT")
120 
124  size_t duration_;
125 
134 
141 #pragma db transient
143 
148 #pragma db transient
150 
151  private:
152 #pragma db version
153  const size_t version_;
154 
155  friend class odb::access;
156 
167  void odb_callback(odb::callback_event e, odb::database &) const;
168 };
169 
170 #pragma db view object(AuditEntry)
172 {
173 #pragma db column("count(" + AuditEntry::id_ + ")")
174  std::size_t count;
175 };
176 }
177 }
178 
179 #ifdef ODB_COMPILER
180 #include "core/auth/User.hpp"
181 #endif
Leosac::Audit::AuditEntry::~AuditEntry
virtual ~AuditEntry()=default
Leosac::Audit::AuditEntry::author
virtual void author(Auth::UserPtr user) override
Set the author of the entry.
Definition: AuditEntry.cpp:88
Leosac::Audit::AuditEntry::version_
const size_t version_
Definition: AuditEntry.hpp:153
Leosac::Audit::AuditEntryCount
Definition: AuditEntry.hpp:171
Leosac::Audit::AuditEntry::parent_
AuditEntryWPtr parent_
Definition: AuditEntry.hpp:107
Leosac::Audit::AuditEntry::children_count
virtual size_t children_count() const override
Returns the number of children that this entry has.
Definition: AuditEntry.cpp:123
Leosac::Audit::AuditEntryPtr
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
Leosac::Audit::AuditEntry::msg_
std::string msg_
Definition: AuditEntry.hpp:110
database.hpp
Leosac::Audit::AuditEntry::odb_callback
void odb_callback(odb::callback_event e, odb::database &) const
Implementation of an ODB callback.
Definition: AuditEntry.cpp:44
Leosac::Audit::AuditEntry::id_
AuditEntryId id_
Definition: AuditEntry.hpp:97
AuthFwd.hpp
ElapsedTimeCounter.hpp
Leosac::Audit::AuditEntry::author_id
virtual Auth::UserId author_id() const override
Retrieve the user id of the author of this entry.
Definition: AuditEntry.cpp:149
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::Audit::AuditEntry::children_
std::vector< AuditEntryPtr > children_
Definition: AuditEntry.hpp:103
User.hpp
Leosac::Audit::AuditEntryId
unsigned long AuditEntryId
Definition: AuditFwd.hpp:31
Leosac::Tools::ElapsedTimeCounter
This class provide a simple to get the elapsed time since it's creation.
Definition: ElapsedTimeCounter.hpp:36
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::AuditEntry::event_mask
virtual const EventMask & event_mask() const override
Retrieve the current event mask.
Definition: AuditEntry.cpp:83
Leosac::Audit::IAuditEntryPtr
std::shared_ptr< IAuditEntry > IAuditEntryPtr
Definition: AuditFwd.hpp:40
Leosac::Audit::AuditEntry::remove_parent
virtual void remove_parent() override
Remove the parent-child relationship between this entry and its parent.
Definition: AuditEntry.cpp:111
Leosac::Audit::AuditEntryWPtr
std::weak_ptr< AuditEntry > AuditEntryWPtr
Definition: AuditFwd.hpp:82
Leosac::Audit::AuditEntry::timestamp_
boost::posix_time::ptime timestamp_
Definition: AuditEntry.hpp:100
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Audit::AuditEntry::duration_
size_t duration_
How long did it take for the Audit object to be finalized.
Definition: AuditEntry.hpp:124
Leosac::Auth::UserLPtr
odb::lazy_shared_ptr< User > UserLPtr
Definition: AuthFwd.hpp:32
Leosac::Audit::AuditEntry::finalized
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:72
Leosac::Audit::AuditEntry::finalize
virtual void finalize() override
Mark the entry as finalized, and update it wrt the database.
Definition: AuditEntry.cpp:57
Leosac::Audit::AuditEntry::get_last_audit
static AuditEntryPtr get_last_audit(DBPtr db)
Retrieve the last (finalized) audit entry.
Definition: AuditEntry.cpp:161
Leosac::Audit::AuditEntry::etc_
Tools::ElapsedTimeCounter etc_
Keep track of how long the object has been alive.
Definition: AuditEntry.hpp:149
Leosac::Audit::EventMask
FlagSet< EventType > EventMask
Definition: AuditFwd.hpp:179
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::AuditEntry::set_parent
virtual void set_parent(IAuditEntryPtr parent) override
Set parent as the parent audit entry for this entry.
Definition: AuditEntry.cpp:95
Leosac::Audit::AuditEntry::timestamp
virtual boost::posix_time::ptime timestamp() const override
Retrieve unix timestamp.
Definition: AuditEntry.cpp:156
Leosac::Audit::AuditEntry::database_
DBPtr database_
Pointer to the database.
Definition: AuditEntry.hpp:142
Leosac::Audit::IAuditEntry
Base interface to Audit object.
Definition: IAuditEntry.hpp:47
Leosac::Audit::AuditEntry::finalized_
bool finalized_
Audit entry are sometime persisted multiple time before reaching their final state.
Definition: AuditEntry.hpp:133
IAuditEntry.hpp
Leosac::Audit::AuditEntry::id
virtual AuditEntryId id() const override
Retrieve the identifier of the entry.
Definition: AuditEntry.cpp:39
Leosac::Auth::UserId
unsigned long UserId
Definition: AuthFwd.hpp:34
Leosac::Audit::AuditEntry
Implementation of IAuditEntry, backed by ODB.
Definition: AuditEntry.hpp:45
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::AuditEntryCount::count
std::size_t count
Definition: AuditEntry.hpp:174
AuditFwd.hpp
Leosac::Audit::AuditEntry::reload
virtual void reload() override
Reload the object from the database.
Definition: AuditEntry.cpp:133
Leosac::Audit::AuditEntry::access
friend class odb::access
Definition: AuditEntry.hpp:155
Leosac::Audit::AuditEntry::version
virtual size_t version() const override
Returns the ODB version of the object.
Definition: AuditEntry.cpp:128
Leosac::Audit::AuditEntry::AuditEntry
AuditEntry()
Definition: AuditEntry.cpp:31
Leosac::Audit::AuditEntry::author_
Auth::UserLPtr author_
The user at the source of the entry.
Definition: AuditEntry.hpp:116