Leosac  0.7.0
OpenSourceAccessControl
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 
40  db::OptionalTransaction t(database->begin());
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 }
virtual void target(Cred::ICredentialPtr cred) override
Set the user that is targeted by the event.
std::string after_
Optional JSON dump of the object after the event took place.
nlohmann::json json
static std::string type_name(const Cred::ICredential &in)
Returns the "type-name" of the credential.
An ODB enabled credential object.
Definition: Credential.hpp:37
This is the header file for a generated source file, GitSHA1.cpp.
Cred::CredentialId target_id() const override
virtual void author(Auth::UserPtr user) override
Set the author of the entry.
Definition: AuditEntry.cpp:89
const std::string & before() const override
std::string generate_target_description() const
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:73
std::string generate_description() const override
Generate a description for this event.
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
std::shared_ptr< ICredential > ICredentialPtr
Cred::CredentialId target_cred_id_
static std::shared_ptr< CredentialEvent > create_empty()
static std::shared_ptr< CredentialEvent > create(const DBPtr &database, Cred::ICredentialPtr target_cred, AuditEntryPtr parent)
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
const std::string & after() const override
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
Provides an implementation of ICredentialEvent.
unsigned long CredentialId
Auth::UserLPtr author_
The user at the source of the entry.
Definition: AuditEntry.hpp:116
std::string before_
Optional JSON dump of the object before the event took place.
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
std::shared_ptr< CredentialEvent > CredentialEventPtr
Definition: AuditFwd.hpp:102
Cred::CredentialLWPtr target_
void database(DBPtr db)
Set the database pointer.
Definition: AuditEntry.cpp:145
The Audit namespace provides classes and facilities to keep track of what&#39;s happening on the Leosac d...