Leosac  0.8.0
Open Source Access Control
User.hpp
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 #pragma once
21 
22 #include "LeosacFwd.hpp"
23 #include "core/auth/AuthFwd.hpp"
28 #include "tools/db/database.hpp"
29 #include "tools/scrypt/Scrypt.hpp"
30 #include <boost/optional.hpp>
31 #include <memory>
32 
33 namespace Leosac
34 {
35 namespace Auth
36 {
37 
41 #pragma db object optimistic
42 class User : public std::enable_shared_from_this<User>
43 {
44  public:
51  User(const std::string &username);
52  User();
53 
58  User(const UserId &id);
59 
60  virtual ~User() = default;
61 
65  const std::string &username() const noexcept;
66 
67  UserId id() const noexcept;
68 
73  void password(const std::string &pw);
74 
81  bool verify_password(const std::string &pw) const;
82 
88  std::string password() const;
89 
99  void username(const std::string &username);
100 
101  IAccessProfilePtr profile() const noexcept;
102 
103  void profile(IAccessProfilePtr user_profile);
104 
105  const std::string &firstname() const;
106  const std::string &lastname() const;
107  const std::string &email() const;
108  const ValidityInfo &validity() const;
109 
110  void firstname(const std::string &);
111  void lastname(const std::string &);
112  void email(const std::string &);
113  void validity(const ValidityInfo &c);
114 
118  bool is_valid() const;
119 
128 
132  UserRank rank() const;
133 
137  void rank(UserRank r);
138 
139  size_t odb_version() const;
140 
141  void add_credential(const Cred::ICredentialPtr &cred);
142 
143  std::vector<Cred::CredentialLWPtr> lazy_credentials() const;
144 
145  std::vector<Tools::ScheduleMappingLWPtr> lazy_schedules_mapping() const;
146 
151  void schedule_mapping_added(const Tools::ScheduleMappingPtr &sched_mapping);
152 
153  protected:
154 #pragma db id auto
156 
160 #pragma db unique
161 #pragma db not_null
162 #pragma db type("VARCHAR(128)")
163  std::string username_;
164 
165 #pragma db type("TEXT")
166  boost::optional<ScryptResult> password_;
167 
168  std::string firstname_;
169  std::string lastname_;
170  std::string email_;
171 
172 #pragma db value_not_null inverse(user_)
174 
175 #pragma db not_null
177 
178 #pragma db value_not_null inverse(owner_)
179  std::vector<Cred::CredentialLWPtr> credentials_;
180 
184 #pragma db value_not_null inverse(users_)
185  std::vector<Tools::ScheduleMappingLWPtr> schedules_mapping_;
186 
191 #pragma db transient
193 
194 #pragma db version
195  const size_t version_;
196 
197  private:
198  friend class odb::access;
199  friend class ::Leosac::TestAccess;
200 };
201 }
202 }
203 
204 #ifdef ODB_COMPILER
205 #include "core/auth/Group.hpp"
208 #include "tools/ScheduleMapping.hpp"
209 #endif
Leosac::Auth::User::lastname
const std::string & lastname() const
Definition: User.cpp:88
Leosac::Auth::User::is_valid
bool is_valid() const
Check the validity status (enabled / disabled) of the user.
Definition: User.cpp:123
database.hpp
Leosac::Auth::User::profile
IAccessProfilePtr profile() const noexcept
Definition: User.cpp:73
Leosac::Auth::UserRank
UserRank
Definition: AuthFwd.hpp:56
Leosac::Auth::User::~User
virtual ~User()=default
Leosac::Auth::User::version_
const size_t version_
Definition: User.hpp:195
Leosac::Auth::User::profile_
IAccessProfilePtr profile_
Definition: User.hpp:192
Leosac::Auth::User::lastname_
std::string lastname_
Definition: User.hpp:169
AuthFwd.hpp
LeosacFwd.hpp
Leosac::Auth::User::rank
UserRank rank() const
Get the global rank of the user.
Definition: User.cpp:161
Leosac::Auth::IAccessProfilePtr
std::shared_ptr< IAccessProfile > IAccessProfilePtr
Definition: AuthFwd.hpp:88
Leosac::Auth::User::odb_version
size_t odb_version() const
Definition: User.cpp:171
ValidityInfo.hpp
Leosac::Tools::ScheduleMappingPtr
std::shared_ptr< ScheduleMapping > ScheduleMappingPtr
Definition: ToolsFwd.hpp:41
UserGroupMembership.hpp
Credential.hpp
Leosac::Auth::User::rank_
UserRank rank_
Definition: User.hpp:176
Leosac::Auth::User::password_
boost::optional< ScryptResult > password_
Definition: User.hpp:166
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Auth::User::id_
UserId id_
Definition: User.hpp:155
Leosac::Auth::User
Represent a user.
Definition: User.hpp:42
Leosac::Auth::User::username
const std::string & username() const noexcept
Get the username of this user.
Definition: User.cpp:55
Group.hpp
Leosac::Auth::User::lazy_schedules_mapping
std::vector< Tools::ScheduleMappingLWPtr > lazy_schedules_mapping() const
Definition: User.cpp:181
Leosac::Auth::User::id
UserId id() const noexcept
Definition: User.cpp:128
Leosac::Auth::User::email_
std::string email_
Definition: User.hpp:170
Leosac::Auth::User::schedule_mapping_added
void schedule_mapping_added(const Tools::ScheduleMappingPtr &sched_mapping)
The user has been mapped by a schedule.
Definition: User.cpp:186
Leosac::Auth::User::password
std::string password() const
Returns the password hash + salt (as stored in the database).
Definition: User.cpp:154
Leosac::Auth::User::group_memberships
const UserGroupMembershipSet & group_memberships() const
Retrieve the UserGroupMembership that this user is involved with.
Definition: User.cpp:133
Leosac::Auth::User::firstname_
std::string firstname_
Definition: User.hpp:168
Leosac::Auth::User::User
User()
Definition: User.cpp:35
Leosac::Auth::User::verify_password
bool verify_password(const std::string &pw) const
Verify that the password pw is equal to the user's password.
Definition: User.cpp:144
Leosac::Auth::User::access
friend class odb::access
Definition: User.hpp:198
IAccessProfile.hpp
Leosac::Cred::ICredentialPtr
std::shared_ptr< ICredential > ICredentialPtr
Definition: CredentialFwd.hpp:32
Leosac::Auth::UserGroupMembershipSet
std::set< UserGroupMembershipPtr, UserGroupMembershipComparator > UserGroupMembershipSet
Definition: UserGroupMembership.hpp:96
Leosac::Auth::User::email
const std::string & email() const
Definition: User.cpp:93
Leosac::Auth::User::validity
const ValidityInfo & validity() const
Definition: User.cpp:113
Leosac::Auth::User::credentials_
std::vector< Cred::CredentialLWPtr > credentials_
Definition: User.hpp:179
Leosac::Auth::UserId
unsigned long UserId
Definition: AuthFwd.hpp:34
Leosac::Auth::User::schedules_mapping_
std::vector< Tools::ScheduleMappingLWPtr > schedules_mapping_
ScheduleMapping object to which we are mapped directly (as user).
Definition: User.hpp:185
ScheduleMapping.hpp
CredentialFwd.hpp
Leosac::Auth::User::username_
std::string username_
This is an (unique) identifier for the user.
Definition: User.hpp:163
Scrypt.hpp
Leosac::Auth::User::lazy_credentials
std::vector< Cred::CredentialLWPtr > lazy_credentials() const
Definition: User.cpp:176
Leosac::Auth::User::validity_
ValidityInfo validity_
A user can have the same validity than credentials.
Definition: User.hpp:190
Leosac::Auth::ValidityInfo
A simple class that stores (and can be queried for) the validity of some objects.
Definition: ValidityInfo.hpp:42
Leosac::Auth::User::firstname
const std::string & firstname() const
Definition: User.cpp:83
Leosac::Auth::User::add_credential
void add_credential(const Cred::ICredentialPtr &cred)
Definition: User.cpp:192
Leosac::Auth::User::membership_
UserGroupMembershipSet membership_
Definition: User.hpp:173