Leosac  0.8.0
Open Source Access Control
UserEvent.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 "UserEvent.hpp"
21 #include "core/audit/UserEvent_odb.h"
22 #include "core/auth/User.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<UserEvent> UserEvent::create(const DBPtr &database,
31  Auth::UserPtr target_user,
32  AuditEntryPtr parent)
33 {
34  ASSERT_LOG(database, "Database cannot be null.");
35  ASSERT_LOG(target_user, "Target user must be non null.");
36  ASSERT_LOG(target_user->id(), "Target user 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  Audit::UserEventPtr audit =
42  std::shared_ptr<Audit::UserEvent>(new Audit::UserEvent());
43  audit->database_ = database;
44  audit->target_ = target_user;
45  database->persist(audit);
46 
47  // Now that we are persisted, set parent.
48  // This make sure that if something bad happens, the parent is not left
49  // with an invalid foreign key for its children.
50  audit->set_parent(parent);
51  database->update(audit);
52 
53  t.commit();
54  return audit;
55 }
56 
58 {
59  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
60  target_ = user;
61 }
62 
63 void UserEvent::before(const std::string &repr)
64 {
65  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
66  before_ = repr;
67 }
68 
69 void UserEvent::after(const std::string &repr)
70 {
71  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
72  after_ = repr;
73 }
74 
76 {
77  if (target_.lock())
78  return target_.object_id();
79  return 0;
80 }
81 
82 const std::string &UserEvent::before() const
83 {
84  return before_;
85 }
86 
87 const std::string &UserEvent::after() const
88 {
89  return after_;
90 }
91 
93 {
94  using namespace FlagSetOperator;
95  std::stringstream ss;
96 
98  ss << "User " << generate_target_description() << " has been created.";
100  ss << "User " << generate_target_description() << " has been edited.";
102  ss << "User " << generate_target_description() << " has been deleted.";
103 
104  return ss.str();
105 }
106 
108 {
109  Leosac::json desc;
110 
111  desc["id"] = target_id();
112  auto t = target_.load();
113  if (t)
114  desc["username"] = t->username();
115  return desc.dump();
116 }
117 
118 std::shared_ptr<UserEvent> UserEvent::create_empty()
119 {
120  return UserEventPtr(new UserEvent());
121 }
122 
124 {
125  return target_.lock();
126 }
Leosac::Audit::EventType::USER_CREATED
@ USER_CREATED
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::UserEvent::generate_description
std::string generate_description() const override
Generate a description for this event.
Definition: UserEvent.cpp:92
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::UserEventPtr
std::shared_ptr< UserEvent > UserEventPtr
Definition: AuditFwd.hpp:93
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::Audit::UserEvent::create_empty
static std::shared_ptr< UserEvent > create_empty()
Definition: UserEvent.cpp:118
Leosac::Audit::UserEvent::UserEvent
UserEvent()=default
Leosac::Audit::AuditEntryPtr
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
Leosac::Audit::UserEvent::after_
std::string after_
Optional JSON dump of the object after the event took place.
Definition: UserEvent.hpp:82
Leosac::Audit::UserEvent::create
static std::shared_ptr< UserEvent > create(const DBPtr &database, Auth::UserPtr target_user, AuditEntryPtr parent)
Definition: UserEvent.cpp:30
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::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
User.hpp
Leosac::Audit::UserEvent::before
const std::string & before() const override
Definition: UserEvent.cpp:82
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::UserEvent
Provides an implementation of IUserEvent.
Definition: UserEvent.hpp:33
Leosac::Audit::UserEvent::target_
Auth::UserLWPtr target_
Definition: UserEvent.hpp:72
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::UserEvent::after
const std::string & after() const override
Definition: UserEvent.cpp:87
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::EventType::USER_DELETED
@ USER_DELETED
JSONUtils.hpp
Leosac::Audit::UserEvent::target
virtual Auth::UserLPtr target() const override
Definition: UserEvent.cpp:123
UserEvent.hpp
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::UserEvent::target_id
Auth::UserId target_id() const override
Retrieve the user_id that was targeted by this event.
Definition: UserEvent.cpp:75
Leosac::Audit::EventType::USER_EDITED
@ USER_EDITED
log.hpp
Leosac::Audit::UserEvent::before_
std::string before_
Optional JSON dump of the object before the event took place.
Definition: UserEvent.hpp:77
Leosac::Auth::UserId
unsigned long UserId
Definition: AuthFwd.hpp:34
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::UserEvent::generate_target_description
std::string generate_target_description() const
Generate a small json-string to describe the target user.
Definition: UserEvent.cpp:107