Google Cloud Storage C++ Client  1.42.0
A C++ Client Library for Google Cloud Storage
object_access_control.h
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OBJECT_ACCESS_CONTROL_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OBJECT_ACCESS_CONTROL_H
17 
18 #include "google/cloud/storage/internal/access_control_common.h"
19 #include "google/cloud/storage/internal/common_metadata.h"
20 #include "google/cloud/storage/internal/patch_builder.h"
21 #include "google/cloud/storage/version.h"
22 #include "google/cloud/status_or.h"
23 #include <string>
24 
25 namespace google {
26 namespace cloud {
27 namespace storage {
29 namespace internal {
30 struct ObjectAccessControlParser;
31 struct GrpcObjectAccessControlParser;
32 } // namespace internal
33 
34 /**
35  * Wraps the objectAccessControl resource in Google Cloud Storage.
36  *
37  * objectAccessControl describes the access to a bucket for a single entity,
38  * where the entity might be a user, group, or other role.
39  *
40  * @see
41  * https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls for
42  * an authoritative source of field definitions.
43  */
44 class ObjectAccessControl : private internal::AccessControlCommon {
45  public:
46  ObjectAccessControl() = default;
47 
48  using AccessControlCommon::ROLE_OWNER;
49  using AccessControlCommon::ROLE_READER;
50  using AccessControlCommon::TEAM_EDITORS;
51  using AccessControlCommon::TEAM_OWNERS;
52  using AccessControlCommon::TEAM_VIEWERS;
53 
54  using AccessControlCommon::bucket;
55  using AccessControlCommon::domain;
56  using AccessControlCommon::email;
57 
58  using AccessControlCommon::entity;
59  ObjectAccessControl& set_entity(std::string v) {
60  AccessControlCommon::set_entity(std::move(v));
61  return *this;
62  }
63 
64  using AccessControlCommon::entity_id;
65  using AccessControlCommon::etag;
66  using AccessControlCommon::has_project_team;
67  using AccessControlCommon::id;
68  using AccessControlCommon::kind;
69  using AccessControlCommon::project_team;
70  using AccessControlCommon::project_team_as_optional;
71 
72  using AccessControlCommon::role;
73  ObjectAccessControl& set_role(std::string v) {
74  AccessControlCommon::set_role(std::move(v));
75  return *this;
76  }
77 
78  using AccessControlCommon::self_link;
79 
80  std::int64_t generation() const { return generation_; }
81  std::string const& object() const { return object_; }
82 
83  friend bool operator==(ObjectAccessControl const& lhs,
84  ObjectAccessControl const& rhs);
85  friend bool operator!=(ObjectAccessControl const& lhs,
86  ObjectAccessControl const& rhs) {
87  return !(lhs == rhs);
88  }
89 
90  private:
91  friend struct internal::ObjectAccessControlParser;
92  friend struct internal::GrpcObjectAccessControlParser;
93 
94  std::int64_t generation_ = 0;
95  std::string object_;
96 };
97 
98 std::ostream& operator<<(std::ostream& os, ObjectAccessControl const& rhs);
99 
100 /**
101  * Prepares a patch for an ObjectAccessControl resource.
102  *
103  * The ObjectAccessControl resource only has two modifiable fields: entity
104  * and role. This class allows application developers to setup a PATCH message,
105  * note that some of the possible PATCH messages may result in errors from the
106  * server, for example: while it is possible to express "change the value of the
107  * entity field" with a PATCH request, the server rejects such changes.
108  *
109  * @see
110  * https://cloud.google.com/storage/docs/json_api/v1/how-tos/performance#patch
111  * for general information on PATCH requests for the Google Cloud Storage
112  * JSON API.
113  */
115  public:
117 
118  std::string BuildPatch() const { return impl_.ToString(); }
119 
121  impl_.SetStringField("entity", v);
122  return *this;
123  }
124 
126  impl_.RemoveField("entity");
127  return *this;
128  }
129 
130  ObjectAccessControlPatchBuilder& set_role(std::string const& v) {
131  impl_.SetStringField("role", v);
132  return *this;
133  }
134 
136  impl_.RemoveField("role");
137  return *this;
138  }
139 
140  private:
141  friend struct internal::GrpcObjectAccessControlParser;
142 
143  internal::PatchBuilder impl_;
144 };
145 
147 } // namespace storage
148 } // namespace cloud
149 } // namespace google
150 
151 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OBJECT_ACCESS_CONTROL_H