]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_basic_types.h
update sources to v12.1.0
[ceph.git] / ceph / src / rgw / rgw_basic_types.h
CommitLineData
31f18b77
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
7c673cae
FG
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
10struct 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 }
31f18b77
FG
23 rgw_user(std::string&& tenant, std::string&& id)
24 : tenant(std::move(tenant)),
25 id(std::move(id)) {
26 }
7c673cae
FG
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};
107WRITE_CLASS_ENCODER(rgw_user)
108
31f18b77
FG
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
115namespace rgw {
116namespace auth {
117class 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
128public:
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 ceph_assert(t != Wildcard);
164 return u.tenant;
165 }
166
167 const std::string& get_id() const {
168 ceph_assert(t != Wildcard && t != Tenant);
169 return u.id;
170 }
171
172 bool operator ==(const Principal& o) const {
173 return (t == o.t) && (u == o.u);
174 }
175
176 bool operator <(const Principal& o) const {
177 return (t < o.t) || ((t == o.t) && (u < o.u));
178 }
179};
180
181std::ostream& operator <<(std::ostream& m, const Principal& p);
182std::string to_string(const Principal& p);
183}
184}
7c673cae
FG
185
186class JSONObj;
187
188void decode_json_obj(rgw_user& val, JSONObj *obj);
189void encode_json(const char *name, const rgw_user& val, Formatter *f);
190
191inline ostream& operator<<(ostream& out, const rgw_user &u) {
192 string s;
193 u.to_str(s);
194 return out << s;
195}
196
197
198#endif