Leosac  0.8.0
Open Source Access Control
ValidityInfo.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 <cassert>
22 #include <stdexcept>
23 #include <tools/log.hpp>
24 
25 using namespace Leosac::Auth;
26 
28  : start_(std::chrono::system_clock::time_point::min())
29  , end_(std::chrono::system_clock::time_point::max())
30  , enabled_(true)
31 {
32 }
33 
35 {
36  return is_enabled() && is_in_range();
37 }
38 
40 {
41  return enabled_;
42 }
43 
45 {
46  TimePoint now = std::chrono::system_clock::now();
47 
48  return now >= start_ && now <= end_;
49 }
50 
51 void ValidityInfo::set_start_date(const std::string &s)
52 {
53  if (s.empty())
54  {
55  start_ = std::chrono::system_clock::time_point::min();
56  return;
57  }
58 
59  std::tm tm;
60  bzero(&tm, sizeof(tm));
61  if (strptime(s.c_str(), "%d/%m/%Y %H:%M", &tm))
62  {
63  start_ = std::chrono::system_clock::from_time_t(std::mktime(&tm));
64  }
65  else
66  {
67  assert(0);
68  throw std::runtime_error("invalid date.");
69  }
70 }
71 
72 void ValidityInfo::set_end_date(const std::string &s)
73 {
74  if (s.empty())
75  {
76  end_ = std::chrono::system_clock::time_point::max();
77  return;
78  }
79 
80  std::tm tm;
81  bzero(&tm, sizeof(tm));
82  if (strptime(s.c_str(), "%d/%m/%Y %H:%M", &tm))
83  {
84  end_ = std::chrono::system_clock::from_time_t(std::mktime(&tm));
85  }
86  else
87  {
88  assert(0);
89  throw std::runtime_error("invalid date.");
90  }
91 }
92 
94 {
95  enabled_ = v;
96 }
97 
99 {
100  return start_;
101 }
102 
104 {
105  return end_;
106 }
107 
109 {
110  start_ = tp;
111 }
112 
114 {
115  end_ = tp;
116 }
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
Leosac::Auth::ValidityInfo::set_start_date
void set_start_date(const std::string &s)
Set the start validity date.
Definition: ValidityInfo.cpp:51
Leosac::Auth::ValidityInfo::enabled_
bool enabled_
Definition: ValidityInfo.hpp:104
Leosac::Auth::ValidityInfo::is_enabled
bool is_enabled() const
Is the credential enabled ?
Definition: ValidityInfo.cpp:39
ValidityInfo.hpp
Leosac::Auth::ValidityInfo::ValidityInfo
ValidityInfo()
Default status is: enabled and no time-based limitation.
Definition: ValidityInfo.cpp:27
Leosac::Auth::ValidityInfo::set_enabled
void set_enabled(bool v)
Definition: ValidityInfo.cpp:93
Leosac::Auth::ValidityInfo::end
const TimePoint & end() const
Definition: ValidityInfo.cpp:103
Leosac::Auth::ValidityInfo::TimePoint
std::chrono::system_clock::time_point TimePoint
Definition: ValidityInfo.hpp:45
Leosac::Auth::ValidityInfo::is_valid
bool is_valid() const
Check that the current date is between validity start and end and make sure its enabled too.
Definition: ValidityInfo.cpp:34
Leosac::Auth::ValidityInfo::end_
TimePoint end_
Definition: ValidityInfo.hpp:102
Leosac::Auth::ValidityInfo::start
const TimePoint & start() const
Definition: ValidityInfo.cpp:98
Leosac::Auth::ValidityInfo::start_
TimePoint start_
Definition: ValidityInfo.hpp:100
log.hpp
Leosac::Auth::ValidityInfo::set_end_date
void set_end_date(const std::string &s)
Set the end validity date.
Definition: ValidityInfo.cpp:72
Leosac::Auth::ValidityInfo::is_in_range
bool is_in_range() const
Are we currently is the validity range of the credential ?
Definition: ValidityInfo.cpp:44