Leosac  0.8.0
Open Source Access Control
DBService.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 "DBService.hpp"
21 #include "DatabaseTracer.hpp"
22 #include "OptionalTransaction.hpp"
24 #include "core/audit/AuditEntry_odb.h"
25 #include "core/auth/AccessPoint_odb.h"
26 #include "core/auth/Door_odb.h"
27 #include "core/auth/Group_odb.h"
28 #include "core/auth/User_odb.h"
29 #include "core/auth/Zone_odb.h"
30 #include "core/credentials/Credential_odb.h"
32 #include "tools/AssertCast.hpp"
33 #include "tools/Schedule_odb.h"
34 #include "tools/log.hpp"
35 #include <odb/database.hxx>
36 
37 using namespace Leosac;
38 
39 
41  : database_(db)
42 {
43  ASSERT_LOG(database_, "Not valid database pointer for DBService.");
44 }
45 
47 {
48  return database_;
49 }
50 
52 {
53  if (database_->tracer())
54  {
55  auto tracer = assert_cast<db::DatabaseTracer *>(database_->tracer());
56  return tracer->count();
57  }
58  return 0;
59 }
60 
62 {
64  auto grp = database_->find<Auth::Group>(id);
65  t.commit();
66  if (!grp && flags & Flag::THROW_IF_NOT_FOUND)
67  throw EntityNotFound(id, "group");
68  return grp;
69 }
70 
72 {
74  auto user = database_->find<Auth::User>(id);
75  t.commit();
76  if (!user && flags & Flag::THROW_IF_NOT_FOUND)
77  throw EntityNotFound(id, "user");
78  return user;
79 }
80 
83 {
85  auto ugm = database_->find<Auth::UserGroupMembership>(id);
86  t.commit();
87  if (!ugm && flags & Flag::THROW_IF_NOT_FOUND)
88  throw EntityNotFound(id, "user-group-membership");
89  return ugm;
90 }
91 
93  DBService::Flag flags)
94 {
96  auto cred = database_->find<Cred::Credential>(id);
97  t.commit();
98  if (!cred && flags & Flag::THROW_IF_NOT_FOUND)
99  throw EntityNotFound(id, "credential");
100  return cred;
101 }
102 
104  DBService::Flag flags)
105 {
107  auto sched = database_->find<Tools::Schedule>(id);
108  t.commit();
109  if (!sched && flags & Flag::THROW_IF_NOT_FOUND)
110  throw EntityNotFound(id, "schedule");
111  return sched;
112 }
113 
115  DBService::Flag flags)
116 {
118  auto door = database_->find<Auth::Door>(id);
119  t.commit();
120  if (!door && flags & Flag::THROW_IF_NOT_FOUND)
121  throw EntityNotFound(id, "door");
122  return door;
123 }
124 
126  DBService::Flag flags)
127 {
129  auto zone = database_->find<Auth::Zone>(id);
130  t.commit();
131  if (!zone && flags & Flag::THROW_IF_NOT_FOUND)
132  throw EntityNotFound(id, "zone");
133  return zone;
134 }
135 
136 
138  Flag flags)
139 {
141  auto audit = database_->find<Audit::AuditEntry>(id);
142  t.commit();
143  if (!audit && flags & Flag::THROW_IF_NOT_FOUND)
144  throw EntityNotFound(id, "audit-entry");
145  audit->database(database_);
146  return audit;
147 }
148 
149 
152  DBService::Flag flags)
153 {
155  auto ap = database_->find<Auth::AccessPoint>(id);
156  t.commit();
157  if (!ap && flags & Flag::THROW_IF_NOT_FOUND)
158  throw EntityNotFound(id, "door");
159  return ap;
160 }
161 
163  DBService::Flag flags)
164 {
166  auto ap = database_->find<update::Update>(id);
167  t.commit();
168  if (!ap && flags & Flag::THROW_IF_NOT_FOUND)
169  throw EntityNotFound(id, "update");
170  return ap;
171 }
172 
173 template <typename T>
174 static void persist_impl(const DBPtr db, T &&obj)
175 {
176  db::OptionalTransaction t(db->begin());
177  ASSERT_LOG(obj.id() == 0, "Object is probably already persisted.");
178  db->persist(std::forward<T>(obj));
179  t.commit();
180 }
181 
182 template <typename T>
183 static void update_impl(const DBPtr db, T &&obj)
184 {
185  db::OptionalTransaction t(db->begin());
186  db->update(std::forward<T>(obj));
187  t.commit();
188 }
189 
191 {
192  persist_impl(database_, assert_cast<Audit::AuditEntry &>(ientry));
193 }
194 
196 {
197  update_impl(database_, assert_cast<Audit::AuditEntry &>(ientry));
198 }
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::Tools::ISchedulePtr
std::shared_ptr< ISchedule > ISchedulePtr
Definition: ToolsFwd.hpp:37
Leosac::Auth::ZoneId
unsigned long ZoneId
Definition: AuthFwd.hpp:119
Leosac::DBService::find_update_by_id
update::IUpdatePtr find_update_by_id(const update::UpdateId &id, Flag f=Flag::DEFAULT)
Definition: DBService.cpp:162
AuditEntry.hpp
Leosac::DBService::database_
const DBPtr database_
Definition: DBService.hpp:106
DatabaseTracer.hpp
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::DBService::find_door_by_id
Auth::IDoorPtr find_door_by_id(const Auth::DoorId &id, Flag f=Flag::DEFAULT)
Definition: DBService.cpp:114
Leosac::DBService::operation_count
size_t operation_count() const
Return the number of operation against the database.
Definition: DBService.cpp:51
Leosac::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
Leosac::update::IUpdatePtr
std::shared_ptr< IUpdate > IUpdatePtr
Definition: UpdateFwd.hpp:42
Leosac::Audit::AuditEntryId
unsigned long AuditEntryId
Definition: AuditFwd.hpp:31
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::IAuditEntryPtr
std::shared_ptr< IAuditEntry > IAuditEntryPtr
Definition: AuditFwd.hpp:40
Leosac::EntityNotFound
Definition: EntityNotFound.hpp:27
Leosac::Auth::IAccessPointPtr
std::shared_ptr< IAccessPoint > IAccessPointPtr
Definition: AuthFwd.hpp:127
Leosac::DBService::update
void update(Audit::IAuditEntry &)
Update the object.
Definition: DBService.cpp:195
Leosac::DBService::Flag
Flag
Definition: DBService.hpp:37
Leosac::DBService::find_audit_by_id
Audit::IAuditEntryPtr find_audit_by_id(const Audit::AuditEntryId &id, Flag f=Flag::DEFAULT)
Definition: DBService.cpp:137
Leosac::DBService::find_schedule_by_id
Tools::ISchedulePtr find_schedule_by_id(const Tools::ScheduleId &id, Flag f=Flag::DEFAULT)
Definition: DBService.cpp:103
Leosac::DBService::DBService
DBService(DBPtr db)
Definition: DBService.cpp:40
Leosac::Auth::Zone
A Zone is a container for doors and other zone.
Definition: Zone.hpp:60
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
OptionalTransaction.hpp
Leosac::Auth::User
Represent a user.
Definition: User.hpp:42
Leosac::DBService::find_credential_by_id
Cred::ICredentialPtr find_credential_by_id(const Cred::CredentialId &id, Flag f=Flag::DEFAULT)
Definition: DBService.cpp:92
Leosac::update::Update
Represent an update.
Definition: Update.hpp:39
Leosac::Auth::DoorId
unsigned long DoorId
Definition: AuthFwd.hpp:107
Leosac::Auth::UserGroupMembershipId
unsigned long UserGroupMembershipId
Definition: AuthFwd.hpp:82
Leosac::Auth::Door
Represent a door.
Definition: Door.hpp:35
Leosac::Auth::IDoorPtr
std::shared_ptr< IDoor > IDoorPtr
Definition: AuthFwd.hpp:104
Leosac::Tools::ScheduleId
unsigned long ScheduleId
Definition: ToolsFwd.hpp:33
Leosac::Auth::GroupPtr
std::shared_ptr< Group > GroupPtr
Definition: AuthFwd.hpp:37
Leosac::Auth::Group
A authentication group regroup users that share permissions.
Definition: Group.hpp:57
Leosac::Cred::Credential
An ODB enabled credential object.
Definition: Credential.hpp:37
Leosac::DBService::find_membership_by_id
Auth::UserGroupMembershipPtr find_membership_by_id(const Auth::UserGroupMembershipId &id, Flag f=Flag::DEFAULT)
Definition: DBService.cpp:82
Leosac::Auth::UserGroupMembershipPtr
std::shared_ptr< UserGroupMembership > UserGroupMembershipPtr
Definition: AuthFwd.hpp:81
Leosac::DBService::find_group_by_id
Auth::GroupPtr find_group_by_id(const Auth::GroupId &id, Flag f=Flag::DEFAULT)
Retrieve a group by its id.
Definition: DBService.cpp:61
DBService.hpp
Leosac::DBService::find_access_point_by_id
Auth::IAccessPointPtr find_access_point_by_id(const Auth::AccessPointId &id, Flag f=Flag::DEFAULT)
Definition: DBService.cpp:151
Leosac::Audit::IAuditEntry
Base interface to Audit object.
Definition: IAuditEntry.hpp:47
Leosac::Cred::ICredentialPtr
std::shared_ptr< ICredential > ICredentialPtr
Definition: CredentialFwd.hpp:32
Leosac::Cred::CredentialId
unsigned long CredentialId
Definition: CredentialFwd.hpp:35
Leosac::Auth::GroupId
unsigned long GroupId
Definition: AuthFwd.hpp:41
Leosac::DBService::db
DBPtr db() const
Simply returns the underlying database pointer.
Definition: DBService.cpp:46
Leosac::DBService::find_user_by_id
Auth::UserPtr find_user_by_id(const Auth::UserId &id, Flag f=Flag::DEFAULT)
Find a user by its id.
Definition: DBService.cpp:71
log.hpp
Leosac::Auth::IZonePtr
std::shared_ptr< IZone > IZonePtr
Definition: AuthFwd.hpp:116
Leosac::DBService::persist
void persist(Audit::IAuditEntry &)
Persist an audit entry object.
Definition: DBService.cpp:190
Leosac::Auth::UserId
unsigned long UserId
Definition: AuthFwd.hpp:34
Leosac::Audit::AuditEntry
Implementation of IAuditEntry, backed by ODB.
Definition: AuditEntry.hpp:45
Leosac::Tools::Schedule
A schedule is simply a list of time frame (SingleTimeFrame) with a name.
Definition: Schedule.hpp:41
Leosac::Auth::AccessPoint
Definition: AccessPoint.hpp:30
Leosac::DBService::find_zone_by_id
Auth::IZonePtr find_zone_by_id(const Auth::ZoneId &id, Flag f=Flag::DEFAULT)
Definition: DBService.cpp:125
EntityNotFound.hpp
Leosac::update::UpdateId
unsigned long UpdateId
Definition: UpdateFwd.hpp:56
AssertCast.hpp
Leosac::Auth::AccessPointId
unsigned long AccessPointId
Definition: AuthFwd.hpp:128
Leosac::Auth::UserGroupMembership
Describe the membership of an User with regroup to a Group.
Definition: UserGroupMembership.hpp:39