zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
acl.hpp
1 #pragma once
2 
3 #include <zk/config.hpp>
4 
5 #include <initializer_list>
6 #include <iosfwd>
7 #include <string>
8 #include <utility>
9 #include <vector>
10 
11 #include "forwards.hpp"
12 
13 namespace zk
14 {
15 
18 
21 enum class permission : unsigned int
22 {
23  none = 0b00000,
24  read = 0b00001,
25  write = 0b00010,
26  create = 0b00100,
27  erase = 0b01000,
28  admin = 0b10000,
29  all = 0b11111,
30 };
31 
34 {
35  return permission(static_cast<unsigned int>(a) | static_cast<unsigned int>(b));
36 }
37 
40 {
41  return permission(static_cast<unsigned int>(a) & static_cast<unsigned int>(b));
42 }
43 
47 {
48  return permission(~static_cast<unsigned int>(a)) & permission::all;
49 }
50 
54 constexpr bool allows(permission self, permission perform)
55 {
56  return (self & perform) == perform;
57 }
58 
59 std::ostream& operator<<(std::ostream&, const permission&);
60 
61 std::string to_string(const permission&);
62 
68 class acl_rule final
69 {
70 public:
72  acl_rule(std::string scheme, std::string id, permission permissions);
73 
74  ~acl_rule() noexcept;
75 
91  const std::string& scheme() const
92  {
93  return _scheme;
94  }
95 
98  const std::string& id() const
99  {
100  return _id;
101  }
102 
104  const permission& permissions() const
105  {
106  return _permissions;
107  }
108 
109 private:
110  std::string _scheme;
111  std::string _id;
112  permission _permissions;
113 };
114 
116 std::size_t hash(const acl_rule& rule);
117 
118 [[gnu::pure]] bool operator==(const acl_rule& lhs, const acl_rule& rhs);
119 [[gnu::pure]] bool operator!=(const acl_rule& lhs, const acl_rule& rhs);
120 [[gnu::pure]] bool operator< (const acl_rule& lhs, const acl_rule& rhs);
121 [[gnu::pure]] bool operator<=(const acl_rule& lhs, const acl_rule& rhs);
122 [[gnu::pure]] bool operator> (const acl_rule& lhs, const acl_rule& rhs);
123 [[gnu::pure]] bool operator>=(const acl_rule& lhs, const acl_rule& rhs);
124 
125 std::ostream& operator<<(std::ostream&, const acl_rule&);
126 
127 std::string to_string(const acl_rule&);
128 
139 class acl final
140 {
141 public:
142  using iterator = std::vector<acl_rule>::iterator;
143  using const_iterator = std::vector<acl_rule>::const_iterator;
144  using size_type = std::size_t;
145 
146 public:
148  acl() = default;
149 
151  acl(std::vector<acl_rule> rules) noexcept;
152 
154  acl(std::initializer_list<acl_rule> rules) :
155  acl(std::vector<acl_rule>(rules))
156  { }
157 
158  ~acl() noexcept;
159 
161  size_type size() const { return _impl.size(); }
162 
165  const acl_rule& operator[](size_type idx) const { return _impl[idx]; }
166  acl_rule& operator[](size_type idx) { return _impl[idx]; }
168 
173  const acl_rule& at(size_type idx) const { return _impl.at(idx); }
174  acl_rule& at(size_type idx) { return _impl.at(idx); }
176 
179  iterator begin() { return _impl.begin(); }
180  const_iterator begin() const { return _impl.begin(); }
181  const_iterator cbegin() const { return _impl.begin(); }
183 
186  iterator end() { return _impl.end(); }
187  const_iterator end() const { return _impl.end(); }
188  const_iterator cend() const { return _impl.end(); }
190 
192  void reserve(size_type capacity) { _impl.reserve(capacity); }
193 
197  template <typename... TArgs>
198  void emplace_back(TArgs&&... args)
199  {
200  _impl.emplace_back(std::forward<TArgs>(args)...);
201  }
202 
205  void push_back(acl_rule&& x) { emplace_back(std::move(x)); }
206  void push_back(const acl_rule& x) { emplace_back(x); }
208 
209 private:
210  std::vector<acl_rule> _impl;
211 };
212 
213 [[gnu::pure]] bool operator==(const acl& lhs, const acl& rhs);
214 [[gnu::pure]] bool operator!=(const acl& lhs, const acl& rhs);
215 
216 std::ostream& operator<<(std::ostream&, const acl&);
217 
218 std::string to_string(const acl& self);
219 
221 class acls
222 {
223 public:
225  static const acl& creator_all();
226 
229  static const acl& open_unsafe();
230 
232  static const acl& read_unsafe();
233 };
234 
236 
237 }
238 
239 namespace std
240 {
241 
242 template <>
243 class hash<zk::acl_rule>
244 {
245 public:
246  using argument_type = zk::acl_rule;
247  using result_type = std::size_t;
248 
249  result_type operator()(const argument_type& x) const
250  {
251  return zk::hash(x);
252  }
253 };
254 
255 }
acl()=default
Create an empty ACL. Keep in mind that an empty ACL is an illegal ACL.
void push_back(acl_rule &&x)
Definition: acl.hpp:205
const acl_rule & operator[](size_type idx) const
Definition: acl.hpp:165
You can do anything.
static const acl & read_unsafe()
This ACL gives the world the ability to read.
Definition: acl.cpp:167
const acl_rule & at(size_type idx) const
Definition: acl.hpp:173
You can alter permissions on this node.
void emplace_back(TArgs &&...args)
Construct a rule emplace on the end of the list using args.
Definition: acl.hpp:198
An individual rule in an acl.
Definition: acl.hpp:68
const permission & permissions() const
The permissions associated with this ACL.
Definition: acl.hpp:104
constexpr bool allows(permission self, permission perform)
Check that self allows it perform all operations.
Definition: acl.hpp:54
iterator begin()
Definition: acl.hpp:179
static const acl & creator_all()
This ACL gives the creators authentication id's all permissions.
Definition: acl.cpp:155
No permissions are set (server could have been configured without ACL support).
Commonly-used ACLs.
Definition: acl.hpp:221
static const acl & open_unsafe()
This is a completely open ACL.
Definition: acl.cpp:161
void reserve(size_type capacity)
Increase the reserved memory block so it can store at least capacity rules without reallocating...
Definition: acl.hpp:192
You can create a child node.
You can access the data of a node and can list its children.
size_type size() const
The number of rules in this ACL.
Definition: acl.hpp:161
const std::string & id() const
The ID of the user under the scheme.
Definition: acl.hpp:98
const std::string & scheme() const
The authentication scheme this list is used for.
Definition: acl.hpp:91
permission
Describes the ability of a user to perform a certain action.
Definition: acl.hpp:21
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:139
acl(std::initializer_list< acl_rule > rules)
Create an instance with the provided rules.
Definition: acl.hpp:154
You can erase a child node (but not necessarily this one).
constexpr permission operator|(permission a, permission b)
Set union operation of permission.
Definition: acl.hpp:33
constexpr permission operator&(permission a, permission b)
Set intersection operation of permission.
Definition: acl.hpp:39
You can set the data of a node.
constexpr permission operator~(permission a)
Set inverse operation of permission.
Definition: acl.hpp:46
acl_rule(std::string scheme, std::string id, permission permissions)
Create an ACL under the given scheme and id with the given permissions.
Definition: acl.cpp:44
std::size_t hash(const acl_rule &self)
Compute a hash for the given rule.
Definition: acl.cpp:53
iterator end()
Definition: acl.hpp:186