Leosac  0.8.0
Open Source Access Control
ScheduleMapping.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 "AssertCast.hpp"
22 #include "core/auth/Door.hpp"
23 #include "core/auth/Group.hpp"
24 #include "core/auth/Group_odb.h"
25 #include "core/auth/User.hpp"
26 #include "core/auth/User_odb.h"
28 #include "core/credentials/Credential_odb.h"
29 #include "tools/ScheduleMapping_odb.h"
30 #include "tools/log.hpp"
31 
32 using namespace Leosac;
33 using namespace Leosac::Tools;
34 
36 {
37  return id_;
38 }
39 
40 const std::string &ScheduleMapping::alias() const
41 {
42  return alias_;
43 }
44 
45 void ScheduleMapping::alias(const std::string &a)
46 {
47  alias_ = a;
48 }
49 
51 {
52  if (schedule_.lock())
53  return schedule_.object_id();
54  return 0;
55 }
56 
58 {
59  return schedule_;
60 }
61 
63 {
64  for (const auto &lazy_weak_user : users_)
65  {
66  if (lazy_weak_user.object_id() == uid)
67  return true;
68  }
69  return false;
70 }
71 
73 {
74  for (const auto &lazy_weak_group : groups_)
75  {
76  if (lazy_weak_group.object_id() == gid)
77  return true;
78  }
79  return false;
80 }
81 
83 {
84  for (const auto &lazy_weak_cred : creds_)
85  {
86  if (lazy_weak_cred.object_id() == cid)
87  return true;
88  }
89  return false;
90 }
91 
93 {
94  for (const auto &lazy_weak_door : doors_)
95  {
96  if (lazy_weak_door.object_id() == did)
97  return true;
98  }
99  return false;
100 }
101 
103 {
104  // Has user
105  if (has_user(user->id()))
106  return true;
107 
108  // Has group
109  for (const auto &group_membership : user->group_memberships())
110  {
111  if (has_group(group_membership->group_id()))
112  return true;
113  }
114 
115  // Has credential
116  for (const auto &lazy_credentials : user->lazy_credentials())
117  {
118  if (has_cred(lazy_credentials.object_id()))
119  return true;
120  }
121  return false;
122 }
123 
125 {
126  ASSERT_LOG(door, "Cannot add a null door to a ScheduleMapping.");
127 
128  doors_.push_back(door);
129  if (door.get_eager())
130  door->schedule_mapping_added(shared_from_this());
131 }
132 
134 {
135  ASSERT_LOG(user, "Cannot add a null user to a ScheduleMapping.");
136 
137  users_.push_back(user);
138  if (user.get_eager())
139  user->schedule_mapping_added(shared_from_this());
140 }
141 
143 {
144  ASSERT_LOG(group, "Cannot add a null group to a ScheduleMapping.");
145 
146  groups_.push_back(group);
147  if (group.get_eager())
148  group->schedule_mapping_added(shared_from_this());
149 }
150 
152 {
153  ASSERT_LOG(cred, "Cannot add a null credential to a ScheduleMapping.");
154 
155  creds_.push_back(cred);
156  if (cred.get_eager())
157  cred->schedule_mapping_added(shared_from_this());
158 }
159 
160 const std::vector<Auth::UserLWPtr> &ScheduleMapping::users() const
161 {
162  return users_;
163 }
164 
165 const std::vector<Auth::GroupLWPtr> &ScheduleMapping::groups() const
166 {
167  return groups_;
168 }
169 
170 const std::vector<Cred::CredentialLWPtr> &ScheduleMapping::credentials() const
171 {
172  return creds_;
173 }
174 
175 const std::vector<Auth::DoorLWPtr> &ScheduleMapping::doors() const
176 {
177  return doors_;
178 }
179 
181 {
182  doors_.clear();
183 }
184 
186 {
187  users_.clear();
188 }
189 
191 {
192  groups_.clear();
193 }
194 
196 {
197  creds_.clear();
198 }
199 
201 {
202  return odb_version_;
203 }
Leosac::Tools::ScheduleMapping::schedule_id
ScheduleId schedule_id() const
Retrieve the id of the owning schedule.
Definition: ScheduleMapping.cpp:50
Leosac::Tools::ScheduleMapping::add_group
void add_group(const Auth::GroupLPtr &group)
Add a group to the mapping.
Definition: ScheduleMapping.cpp:142
Leosac::Tools::ScheduleMapping::add_user
void add_user(const Auth::UserLPtr &user)
Add a user to the mapping.
Definition: ScheduleMapping.cpp:133
Leosac::Auth::GroupLPtr
odb::lazy_shared_ptr< Group > GroupLPtr
Definition: AuthFwd.hpp:40
Leosac::Tools::ScheduleMapping::doors
const std::vector< Auth::DoorLWPtr > & doors() const
Definition: ScheduleMapping.cpp:175
Leosac::Tools::ScheduleMapping::schedule
ScheduleLWPtr schedule() const
Definition: ScheduleMapping.cpp:57
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Tools::ScheduleMapping::credentials
const std::vector< Cred::CredentialLWPtr > & credentials() const
Definition: ScheduleMapping.cpp:170
Leosac::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
Leosac::Tools::ScheduleMapping::alias_
std::string alias_
A non unique, optional alias.
Definition: ScheduleMapping.hpp:161
User.hpp
Leosac::Tools::ScheduleMapping::odb_version
size_t odb_version() const
Definition: ScheduleMapping.cpp:200
Leosac::Tools::ScheduleMapping::schedule_
ScheduleLWPtr schedule_
The schedule that owns the mapping.
Definition: ScheduleMapping.hpp:156
Leosac::Tools::ScheduleMapping::has_user
bool has_user(Auth::UserId) const
Check if the user with id uid is directly part of the mapping.
Definition: ScheduleMapping.cpp:62
Leosac::Tools::ScheduleMapping::alias
const std::string & alias() const
Definition: ScheduleMapping.cpp:40
Credential.hpp
Door.hpp
Leosac::Tools::ScheduleMapping::clear_users
void clear_users()
Definition: ScheduleMapping.cpp:185
Leosac::Tools::ScheduleMapping::users
const std::vector< Auth::UserLWPtr > & users() const
Definition: ScheduleMapping.cpp:160
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Tools::ScheduleMapping::add_door
void add_door(const Auth::DoorLPtr &door)
Add a door to the mapping.
Definition: ScheduleMapping.cpp:124
Leosac::Auth::UserLPtr
odb::lazy_shared_ptr< User > UserLPtr
Definition: AuthFwd.hpp:32
Leosac::Tools::ScheduleMapping::clear_doors
void clear_doors()
Definition: ScheduleMapping.cpp:180
Leosac::Tools::ScheduleMapping::id_
ScheduleMappingId id_
Definition: ScheduleMapping.hpp:129
Leosac::Auth::DoorId
unsigned long DoorId
Definition: AuthFwd.hpp:107
Group.hpp
Leosac::Tools::ScheduleId
unsigned long ScheduleId
Definition: ToolsFwd.hpp:33
Leosac::Tools::ScheduleMapping::groups
const std::vector< Auth::GroupLWPtr > & groups() const
Definition: ScheduleMapping.cpp:165
Leosac::Tools::ScheduleLWPtr
odb::lazy_weak_ptr< Schedule > ScheduleLWPtr
Definition: ToolsFwd.hpp:34
Leosac::Tools::ScheduleMappingId
unsigned long ScheduleMappingId
Definition: ToolsFwd.hpp:44
Leosac::Tools::ScheduleMapping::odb_version_
size_t odb_version_
Definition: ScheduleMapping.hpp:164
Leosac::Tools::ScheduleMapping::creds_
std::vector< Cred::CredentialLWPtr > creds_
Definition: ScheduleMapping.hpp:145
Leosac::Cred::CredentialId
unsigned long CredentialId
Definition: CredentialFwd.hpp:35
Leosac::Auth::GroupId
unsigned long GroupId
Definition: AuthFwd.hpp:41
Leosac::Cred::CredentialLPtr
odb::lazy_shared_ptr< Credential > CredentialLPtr
Definition: CredentialFwd.hpp:38
log.hpp
Leosac::Tools::ScheduleMapping::clear_credential
void clear_credential()
Definition: ScheduleMapping.cpp:195
Leosac::Tools::ScheduleMapping::id
ScheduleMappingId id() const
Definition: ScheduleMapping.cpp:35
Leosac::Tools::ScheduleMapping::has_group
bool has_group(Auth::GroupId) const
Check if the group is directly mapping by the ScheduleMapping object.
Definition: ScheduleMapping.cpp:72
Leosac::Auth::UserId
unsigned long UserId
Definition: AuthFwd.hpp:34
Leosac::Tools::ScheduleMapping::clear_groups
void clear_groups()
Definition: ScheduleMapping.cpp:190
ScheduleMapping.hpp
Leosac::Tools::ScheduleMapping::groups_
std::vector< Auth::GroupLWPtr > groups_
Definition: ScheduleMapping.hpp:140
Leosac::Tools::ScheduleMapping::has_cred
bool has_cred(Cred::CredentialId) const
Check if the credential is mapped by this ScheduleMapping.
Definition: ScheduleMapping.cpp:82
Leosac::Tools::ScheduleMapping::has_door
bool has_door(Auth::DoorId) const
Check if the door is mapped by this object.
Definition: ScheduleMapping.cpp:92
Leosac::Auth::DoorLPtr
odb::lazy_shared_ptr< Door > DoorLPtr
Definition: AuthFwd.hpp:111
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
Leosac::Tools::ScheduleMapping::users_
std::vector< Auth::UserLWPtr > users_
Definition: ScheduleMapping.hpp:135
Leosac::Tools::ScheduleMapping::doors_
std::vector< Auth::DoorLWPtr > doors_
Definition: ScheduleMapping.hpp:150
Leosac::Tools::ScheduleMapping::has_user_indirect
bool has_user_indirect(Auth::UserPtr) const
Check wether the user is mapped, either directly or not, by this ScheduleMapping object.
Definition: ScheduleMapping.cpp:102
AssertCast.hpp
Leosac::Tools::ScheduleMapping::add_credential
void add_credential(const Cred::CredentialLPtr &cred)
Add a credential to the mapping.
Definition: ScheduleMapping.cpp:151