Leosac  0.8.0
Open Source Access Control
CredentialEvent.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/CredentialEvent_odb.h"
23 #include "core/credentials/Credential_odb.h"
25 #include "tools/JSONUtils.hpp"
27 #include "tools/log.hpp"
28 
29 using namespace Leosac;
30 using namespace Leosac::Audit;
31 
32 std::shared_ptr<CredentialEvent>
33 CredentialEvent::create(const DBPtr &database, Cred::ICredentialPtr target_cred,
34  AuditEntryPtr parent)
35 {
36  ASSERT_LOG(database, "Database cannot be null.");
37  ASSERT_LOG(target_cred, "Credential shall not be null.");
38  ASSERT_LOG(target_cred->id(), "Credential must be already persisted.");
39 
41 
43  std::shared_ptr<Audit::CredentialEvent>(new Audit::CredentialEvent());
44  audit->database_ = database;
45  audit->target(target_cred);
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 (cred)
59  ASSERT_LOG(cred->id(), "Credential has no id.");
60 
61  auto cred_odb = std::dynamic_pointer_cast<Cred::Credential>(cred);
62  ASSERT_LOG(cred_odb, "ICredential is not of type Credential.");
63 
64  target_ = cred_odb;
65  target_cred_id_ = cred->id();
66 }
67 
68 void CredentialEvent::before(const std::string &repr)
69 {
70  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
71  before_ = repr;
72 }
73 
74 void CredentialEvent::after(const std::string &repr)
75 {
76  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
77  after_ = repr;
78 }
79 
81 {
82  if (target_.lock())
83  {
84  return target_.object_id();
85  }
86  return 0;
87 }
88 
89 const std::string &CredentialEvent::before() const
90 {
91  return before_;
92 }
93 
94 const std::string &CredentialEvent::after() const
95 {
96  return after_;
97 }
98 
100 {
101  using namespace FlagSetOperator;
102  std::stringstream ss;
103 
104  auto target = target_.load();
105  auto author = author_.load();
106 
108  ss << "Credential " << generate_target_description() << " has been created.";
110  ss << "Credential " << generate_target_description() << " has been edited.";
112  ss << "Credential " << generate_target_description() << " has been deleted.";
113 
114  return ss.str();
115 }
116 
118 {
119  Leosac::json desc;
120 
121  desc["id"] = target_id();
122  auto t = target_.load();
123  if (t)
124  {
125  desc["alias"] = t->alias();
127  }
128  return desc.dump();
129 }
130 
131 std::shared_ptr<CredentialEvent> CredentialEvent::create_empty()
132 {
133  return std::shared_ptr<CredentialEvent>(new CredentialEvent());
134 }
Leosac::Audit::CredentialEvent::generate_target_description
std::string generate_target_description() const
Definition: CredentialEvent.cpp:117
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::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::Audit::CredentialEvent::target_cred_id_
Cred::CredentialId target_cred_id_
Definition: CredentialEvent.hpp:71
Leosac::Audit::AuditEntryPtr
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
PolymorphicCredentialSerializer.hpp
Leosac::Audit::EventType::CREDENTIAL_CREATED
@ CREDENTIAL_CREATED
Leosac::Audit::EventType::CREDENTIAL_UPDATED
@ CREDENTIAL_UPDATED
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::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::CredentialEvent::CredentialEvent
CredentialEvent()=default
CredentialEvent.hpp
Leosac::Audit::CredentialEvent::target
virtual void target(Cred::ICredentialPtr cred) override
Set the user that is targeted by the event.
Definition: CredentialEvent.cpp:55
Credential.hpp
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
Leosac::Audit::CredentialEvent
Provides an implementation of ICredentialEvent.
Definition: CredentialEvent.hpp:35
OptionalTransaction.hpp
Leosac::Audit::CredentialEvent::target_id
Cred::CredentialId target_id() const override
Definition: CredentialEvent.cpp:80
Leosac::Audit::AuditEntry::finalized
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:72
Leosac::Audit::EventType::CREDENTIAL_DELETED
@ CREDENTIAL_DELETED
JSONUtils.hpp
Leosac::PolymorphicCredentialJSONSerializer::type_name
static std::string type_name(const Cred::ICredential &in)
Returns the "type-name" of the credential.
Definition: PolymorphicCredentialSerializer.cpp:50
Leosac::Audit::CredentialEvent::before_
std::string before_
Optional JSON dump of the object before the event took place.
Definition: CredentialEvent.hpp:76
Leosac::Audit::CredentialEvent::target_
Cred::CredentialLWPtr target_
Definition: CredentialEvent.hpp:69
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::CredentialEvent::before
const std::string & before() const override
Definition: CredentialEvent.cpp:89
Leosac::Audit::CredentialEvent::generate_description
std::string generate_description() const override
Generate a description for this event.
Definition: CredentialEvent.cpp:99
Leosac::Audit::CredentialEvent::after_
std::string after_
Optional JSON dump of the object after the event took place.
Definition: CredentialEvent.hpp:81
Leosac::Cred::ICredentialPtr
std::shared_ptr< ICredential > ICredentialPtr
Definition: CredentialFwd.hpp:32
Leosac::Audit::CredentialEventPtr
std::shared_ptr< CredentialEvent > CredentialEventPtr
Definition: AuditFwd.hpp:102
Leosac::Cred::CredentialId
unsigned long CredentialId
Definition: CredentialFwd.hpp:35
log.hpp
Leosac::Audit::CredentialEvent::create_empty
static std::shared_ptr< CredentialEvent > create_empty()
Definition: CredentialEvent.cpp:131
Leosac::Audit::CredentialEvent::create
static std::shared_ptr< CredentialEvent > create(const DBPtr &database, Cred::ICredentialPtr target_cred, AuditEntryPtr parent)
Definition: CredentialEvent.cpp:33
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::CredentialEvent::after
const std::string & after() const override
Definition: CredentialEvent.cpp:94
Leosac::Audit::AuditEntry::author_
Auth::UserLPtr author_
The user at the source of the entry.
Definition: AuditEntry.hpp:116