Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rgw / rgw_basic_types.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 #ifndef CEPH_RGW_BASIC_TYPES_H
4 #define CEPH_RGW_BASIC_TYPES_H
5
6 #include <string>
7
8 #include "include/types.h"
9
10 struct rgw_user {
11   std::string tenant;
12   std::string id;
13
14   rgw_user() {}
15   // cppcheck-suppress noExplicitConstructor
16   rgw_user(const std::string& s) {
17     from_str(s);
18   }
19   rgw_user(const std::string& tenant, const std::string& id)
20     : tenant(tenant),
21       id(id) {
22   }
23   rgw_user(std::string&& tenant, std::string&& id)
24     : tenant(std::move(tenant)),
25       id(std::move(id)) {
26   }
27
28   void encode(bufferlist& bl) const {
29     ENCODE_START(1, 1, bl);
30     ::encode(tenant, bl);
31     ::encode(id, bl);
32     ENCODE_FINISH(bl);
33   }
34   void decode(bufferlist::iterator& bl) {
35     DECODE_START(1, bl);
36     ::decode(tenant, bl);
37     ::decode(id, bl);
38     DECODE_FINISH(bl);
39   }
40
41   void to_str(std::string& str) const {
42     if (!tenant.empty()) {
43       str = tenant + '$' + id;
44     } else {
45       str = id;
46     }
47   }
48
49   void clear() {
50     tenant.clear();
51     id.clear();
52   }
53
54   bool empty() const {
55     return id.empty();
56   }
57
58   string to_str() const {
59     string s;
60     to_str(s);
61     return s;
62   }
63
64   void from_str(const std::string& str) {
65     size_t pos = str.find('$');
66     if (pos != std::string::npos) {
67       tenant = str.substr(0, pos);
68       id = str.substr(pos + 1);
69     } else {
70       tenant.clear();
71       id = str;
72     }
73   }
74
75   rgw_user& operator=(const string& str) {
76     from_str(str);
77     return *this;
78   }
79
80   int compare(const rgw_user& u) const {
81     int r = tenant.compare(u.tenant);
82     if (r != 0)
83       return r;
84
85     return id.compare(u.id);
86   }
87   int compare(const string& str) const {
88     rgw_user u(str);
89     return compare(u);
90   }
91
92   bool operator!=(const rgw_user& rhs) const {
93     return (compare(rhs) != 0);
94   }
95   bool operator==(const rgw_user& rhs) const {
96     return (compare(rhs) == 0);
97   }
98   bool operator<(const rgw_user& rhs) const {
99     if (tenant < rhs.tenant) {
100       return true;
101     } else if (tenant > rhs.tenant) {
102       return false;
103     }
104     return (id < rhs.id);
105   }
106 };
107 WRITE_CLASS_ENCODER(rgw_user)
108
109 // Represents an identity. This is more wide-ranging than a
110 // 'User'. Its purposes is to be matched against by an
111 // IdentityApplier. The internal representation will doubtless change as
112 // more types are added. We may want to expose the type enum and make
113 // the member public so people can switch/case on it.
114
115 namespace rgw {
116 namespace auth {
117 class Principal {
118   enum types { User, Role, Tenant, Wildcard };
119   types t;
120   rgw_user u;
121
122   Principal(types t)
123     : t(t) {}
124
125   Principal(types t, std::string&& n, std::string i)
126     : t(t), u(std::move(n), std::move(i)) {}
127
128 public:
129
130   static Principal wildcard() {
131     return Principal(Wildcard);
132   }
133
134   static Principal user(std::string&& t, std::string&& u) {
135     return Principal(User, std::move(t), std::move(u));
136   }
137
138   static Principal role(std::string&& t, std::string&& u) {
139     return Principal(Role, std::move(t), std::move(u));
140   }
141
142   static Principal tenant(std::string&& t) {
143     return Principal(Tenant, std::move(t), {});
144   }
145
146   bool is_wildcard() const {
147     return t == Wildcard;
148   }
149
150   bool is_user() const {
151     return t == User;
152   }
153
154   bool is_role() const {
155     return t == Role;
156   }
157
158   bool is_tenant() const {
159     return t == Tenant;
160   }
161
162   const std::string& get_tenant() const {
163     return u.tenant;
164   }
165
166   const std::string& get_id() const {
167     return u.id;
168   }
169
170   bool operator ==(const Principal& o) const {
171     return (t == o.t) && (u == o.u);
172   }
173
174   bool operator <(const Principal& o) const {
175     return (t < o.t) || ((t == o.t) && (u < o.u));
176   }
177 };
178
179 std::ostream& operator <<(std::ostream& m, const Principal& p);
180 std::string to_string(const Principal& p);
181 }
182 }
183
184 class JSONObj;
185
186 void decode_json_obj(rgw_user& val, JSONObj *obj);
187 void encode_json(const char *name, const rgw_user& val, Formatter *f);
188
189 inline ostream& operator<<(ostream& out, const rgw_user &u) {
190   string s;
191   u.to_str(s);
192   return out << s;
193 }
194
195
196 #endif