Leosac  0.8.0
Open Source Access Control
Group.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 "core/auth/Group_odb.h"
21 #include "core/auth/UserGroupMembership_odb.h"
22 #include "tools/log.hpp"
24 
25 using namespace Leosac;
26 using namespace Leosac::Auth;
27 
29  : id_(0)
30  , version_(0)
31 {
32 }
33 
34 Group::Group(const std::string &group_name)
35  : id_(0)
36  , name_(group_name)
37  , version_(0)
38 {
39 }
40 
41 const std::string &Group::name() const
42 {
43  return name_;
44 }
45 
46 const std::vector<UserPtr> &Group::members() const
47 {
48  for (const auto &membership : membership_)
49  {
50  auto ptr = membership->user().get_eager().lock();
51  if (ptr &&
52  std::find(loaded_members_.begin(), loaded_members_.end(), ptr) ==
53  loaded_members_.end())
54  {
55  loaded_members_.push_back(ptr);
56  }
57  }
58  return loaded_members_;
59 }
60 
62  GroupRank rank /*= GroupRank::MEMBER*/)
63 {
64  // Create a new UserGroupMembership describing the relationship.
65  auto ugm = std::make_shared<UserGroupMembership>();
66  ugm->rank(rank);
67  ugm->user(m);
68  ugm->group(shared_from_this());
69  membership_.insert(ugm);
70  return ugm;
71 }
72 
74 {
75  return profile_;
76 }
77 
79 {
80  profile_ = p;
81 }
82 
84 {
85  return id_;
86 }
87 
88 void Group::id(const GroupId &new_id)
89 {
90  id_ = new_id;
91 }
92 
93 std::vector<UserLPtr> Group::lazy_members() const
94 {
95  std::vector<UserLPtr> members;
96  for (const auto &membership : membership_)
97  {
98  ASSERT_LOG(membership->group().object_id() == id_,
99  "Membership doesn't point to self.");
100  ASSERT_LOG(membership->user().lock(), "Why is this null?");
101  if (membership->user().lock())
102  members.push_back(membership->user().lock());
103  }
104  return members;
105 }
106 
107 void Group::name(const std::string &name)
108 {
110  name_ = name;
111 }
112 
113 void Group::odb_callback(odb::callback_event e, odb::database &db) const
114 {
115  if (e == odb::callback_event::post_update ||
116  e == odb::callback_event::post_persist)
117  {
118  for (auto &membership : membership_)
119  {
120  if (membership->id() == 0)
121  db.persist(membership);
122  else
123  db.update(membership);
124  }
125  }
126 }
127 
129 {
130  return membership_;
131 }
132 
133 const std::string &Group::description() const
134 {
135  return description_;
136 }
137 
138 void Group::description(const std::string &desc)
139 {
140  description_ = desc;
141 }
142 
143 bool Group::member_has(UserId user_id, GroupRank *rank_out) const
144 {
145  for (const auto &membership : membership_)
146  {
147  if (membership->user().object_id() == user_id)
148  {
149  if (rank_out)
150  *rank_out = membership->rank();
151  return true;
152  }
153  }
154  return false;
155 }
156 
157 std::vector<Tools::ScheduleMappingLWPtr> Group::lazy_schedules_mapping() const
158 {
159  return schedules_mapping_;
160 }
161 
163 {
164  schedules_mapping_.push_back(sched_mapping);
165 }
166 
168 {
169  validate_name(grp.name());
170 }
171 
172 void GroupValidator::validate_name(const std::string &name)
173 {
174  if (name.size() < 3 || name.size() > 50)
175  {
176  throw ModelException("data/attributes/name", "Length must be >=3 and <=50.");
177  }
178  for (const auto &c : name)
179  {
180  if (!isalnum(c) && (c != '_' && c != '-' && c != '.'))
181  {
182  throw ModelException(
183  "data/attributes/name",
184  BUILD_STR("Usage of unauthorized character: " << c));
185  }
186  }
187 }
Leosac::Auth::Group::user_memberships
const UserGroupMembershipSet & user_memberships() const
Retrieve the UserGroupMembership that this group is involved with.
Definition: Group.cpp:128
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
Leosac::Auth::Group::name_
std::string name_
Definition: Group.hpp:154
BUILD_STR
#define BUILD_STR(param)
Internal macro.
Definition: log.hpp:63
Leosac::Auth::Group::profile
IAccessProfilePtr profile()
Definition: Group.cpp:73
Leosac::Auth::Group::id
GroupId id() const
Retrieve the unique identifier of the group.
Definition: Group.cpp:83
Leosac::Auth::Group::Group
Group()
Definition: Group.cpp:28
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Auth::Group::membership_
UserGroupMembershipSet membership_
Definition: Group.hpp:138
Leosac::Auth::Group::schedules_mapping_
std::vector< Tools::ScheduleMappingLWPtr > schedules_mapping_
Definition: Group.hpp:141
Leosac::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
Leosac::Auth::Group::description_
std::string description_
A (potentially long) description of the group.
Definition: Group.hpp:161
Leosac::Auth::IAccessProfilePtr
std::shared_ptr< IAccessProfile > IAccessProfilePtr
Definition: AuthFwd.hpp:88
Leosac::Tools::ScheduleMappingPtr
std::shared_ptr< ScheduleMapping > ScheduleMappingPtr
Definition: ToolsFwd.hpp:41
Leosac::Auth::Group::lazy_schedules_mapping
std::vector< Tools::ScheduleMappingLWPtr > lazy_schedules_mapping() const
Returns the vector of lazy_weak_ptr to schedule mapping.
Definition: Group.cpp:157
Leosac::Auth::Group::odb_callback
void odb_callback(odb::callback_event e, odb::database &) const
Definition: Group.cpp:113
Leosac::Auth::Group::id_
GroupId id_
The group identifier.
Definition: Group.hpp:135
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Auth::GroupValidator::validate_name
static void validate_name(const std::string &name)
Definition: Group.cpp:172
Leosac::Auth::Group::member_add
UserGroupMembershipPtr member_add(UserPtr m, GroupRank rank=GroupRank::MEMBER)
Definition: Group.cpp:61
Leosac::Auth::Group
A authentication group regroup users that share permissions.
Definition: Group.hpp:57
Leosac::Auth::Group::member_has
bool member_has(Auth::UserId user_id, GroupRank *rank_out=nullptr) const
Check if user_id is a member of this group.
Definition: Group.cpp:143
ModelException.hpp
Leosac::Auth::Group::name
const std::string & name() const
Definition: Group.cpp:41
Leosac::Auth::UserGroupMembershipPtr
std::shared_ptr< UserGroupMembership > UserGroupMembershipPtr
Definition: AuthFwd.hpp:81
Leosac::Auth::Group::profile_
IAccessProfilePtr profile_
Definition: Group.hpp:164
ModelException
An exception class for general API error.
Definition: ModelException.hpp:33
Leosac::Auth::Group::loaded_members_
std::vector< UserPtr > loaded_members_
This returns a vector of loaded User object.
Definition: Group.hpp:149
Leosac::Auth::UserGroupMembershipSet
std::set< UserGroupMembershipPtr, UserGroupMembershipComparator > UserGroupMembershipSet
Definition: UserGroupMembership.hpp:96
Leosac::Auth::GroupId
unsigned long GroupId
Definition: AuthFwd.hpp:41
log.hpp
Leosac::Auth::Group::lazy_members
std::vector< UserLPtr > lazy_members() const
Retrieve lazy pointers to members.
Definition: Group.cpp:93
Leosac::Auth::UserId
unsigned long UserId
Definition: AuthFwd.hpp:34
Leosac::Auth::GroupRank
GroupRank
The rank of an User inside a Group.
Definition: AuthFwd.hpp:49
Leosac::Auth::Group::schedule_mapping_added
void schedule_mapping_added(const Tools::ScheduleMappingPtr &sched_mapping)
The group has been mapped by a schedule.
Definition: Group.cpp:162
Leosac::Auth::Group::members
const std::vector< UserPtr > & members() const
Definition: Group.cpp:46
Leosac::Auth::GroupValidator::validate
static void validate(const Group &grp)
Validate the group's attributes.
Definition: Group.cpp:167
Leosac::Auth::Group::description
const std::string & description() const
Definition: Group.cpp:133