zookeeper-cpp
ZooKeeper Client for C++
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 
23 enum class permission : unsigned int
24 {
25  none = 0b00000,
26  read = 0b00001,
27  write = 0b00010,
28  create = 0b00100,
29  erase = 0b01000,
30  admin = 0b10000,
31  all = 0b11111,
32 };
33 
34 constexpr permission operator|(permission a, permission b)
35 {
36  return permission(static_cast<unsigned int>(a) | static_cast<unsigned int>(b));
37 }
38 
39 constexpr permission operator&(permission a, permission b)
40 {
41  return permission(static_cast<unsigned int>(a) & static_cast<unsigned int>(b));
42 }
43 
44 constexpr permission operator~(permission a)
45 {
46  return permission(~static_cast<unsigned int>(a));
47 }
48 
53 constexpr bool allows(permission self, permission perform)
54 {
55  return (self & perform) == perform;
56 }
57 
58 std::ostream& operator<<(std::ostream&, const permission&);
59 
60 std::string to_string(const permission&);
61 
67 class acl_rule final
68 {
69 public:
71  acl_rule(std::string scheme, std::string id, permission permissions);
72 
73  ~acl_rule() noexcept;
74 
78  const std::string& scheme() const
79  {
80  return _scheme;
81  }
82 
86  const std::string& id() const
87  {
88  return _id;
89  }
90 
92  const permission& permissions() const
93  {
94  return _permissions;
95  }
96 
97 private:
98  std::string _scheme;
99  std::string _id;
100  permission _permissions;
101 };
102 
103 std::size_t hash(const acl_rule&);
104 
105 [[gnu::pure]] bool operator==(const acl_rule& lhs, const acl_rule& rhs);
106 [[gnu::pure]] bool operator!=(const acl_rule& lhs, const acl_rule& rhs);
107 [[gnu::pure]] bool operator< (const acl_rule& lhs, const acl_rule& rhs);
108 [[gnu::pure]] bool operator<=(const acl_rule& lhs, const acl_rule& rhs);
109 [[gnu::pure]] bool operator> (const acl_rule& lhs, const acl_rule& rhs);
110 [[gnu::pure]] bool operator>=(const acl_rule& lhs, const acl_rule& rhs);
111 
112 std::ostream& operator<<(std::ostream&, const acl_rule&);
113 
114 std::string to_string(const acl_rule&);
115 
124 class acl final
125 {
126 public:
127  using iterator = std::vector<acl_rule>::iterator;
128  using const_iterator = std::vector<acl_rule>::const_iterator;
129  using size_type = std::size_t;
130 
131 public:
132  acl() = default;
133 
134  acl(std::vector<acl_rule> rules) noexcept;
135 
136  acl(std::initializer_list<acl_rule> rules) :
137  acl(std::vector<acl_rule>(rules))
138  { }
139 
140  ~acl() noexcept;
141 
142  size_type size() const { return _impl.size(); }
143 
144  const acl_rule& operator[](size_type idx) const { return _impl[idx]; }
145 
146  iterator begin() { return _impl.begin(); }
147  const_iterator begin() const { return _impl.begin(); }
148  const_iterator cbegin() const { return _impl.begin(); }
149 
150  iterator end() { return _impl.end(); }
151  const_iterator end() const { return _impl.end(); }
152  const_iterator cend() const { return _impl.end(); }
153 
154  void reserve(size_type sz) { _impl.reserve(sz); }
155 
156  template <typename... TArgs>
157  void emplace_back(TArgs&&... args)
158  {
159  _impl.emplace_back(std::forward<TArgs>(args)...);
160  }
161 
162 private:
163  std::vector<acl_rule> _impl;
164 };
165 
166 [[gnu::pure]] bool operator==(const acl& lhs, const acl& rhs);
167 [[gnu::pure]] bool operator!=(const acl& lhs, const acl& rhs);
168 
169 std::ostream& operator<<(std::ostream&, const acl&);
170 
171 std::string to_string(const acl& self);
172 
174 class acls
175 {
176 public:
178  static const acl& creator_all();
179 
181  static const acl& open_unsafe();
182 
184  static const acl& read_unsafe();
185 };
186 
189 }
190 
191 namespace std
192 {
193 
194 template <>
195 class hash<zk::acl_rule>
196 {
197 public:
198  using argument_type = zk::acl_rule;
199  using result_type = std::size_t;
200 
201  result_type operator()(const argument_type& x) const
202  {
203  return zk::hash(x);
204  }
205 };
206 
207 }
Definition: acl.cpp:8
You can do anything.
STL namespace.
You can alter permissions on this node.
An individual rule in an acl.
Definition: acl.hpp:67
const permission & permissions() const
The permissions associated with this ACL.
Definition: acl.hpp:92
constexpr bool allows(permission self, permission perform)
Check that self allows it perform all operations.
Definition: acl.hpp:53
No permissions are set (server could have been configured without ACL support).
Convenience operations around commonly-used ACLs.
Definition: acl.hpp:174
You can create a child node.
You can access the data of a node and can list its children.
const std::string & id() const
The ID of the user under the scheme.
Definition: acl.hpp:86
const std::string & scheme() const
The authentication scheme this list is used for.
Definition: acl.hpp:78
permission
Describes the ability of a user to perform a certain action.
Definition: acl.hpp:23
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:124
You can erase a child node (but not necessarily this one).
You can set the data of a node.