1818
1919import com .google .api .services .storage .model .BucketAccessControl ;
2020import com .google .api .services .storage .model .ObjectAccessControl ;
21- import com .google .gcloud .storage .Acl .Project .ProjectRole ;
2221
2322import java .io .Serializable ;
2423
25- public abstract class Acl implements Serializable {
24+ public final class Acl implements Serializable {
2625
2726 private static final long serialVersionUID = 6435575339887912222L ;
2827
29- private final Type type ;
28+ private final Entity entity ;
3029 private final Role role ;
3130
3231 public enum Role {
@@ -35,114 +34,137 @@ public enum Role {
3534 WRITER
3635 }
3736
38- public enum Type {
37+ public enum EntityType {
3938 DOMAIN ,
4039 GROUP ,
4140 USER ,
4241 PROJECT
4342 }
4443
45- public static class Domain extends Acl {
44+ public static abstract class Entity implements Serializable {
4645
47- private static final long serialVersionUID = -3033025857280447253L ;
48- private final String domain ;
46+ private static final long serialVersionUID = -2707407252771255840L ;
47+ private final EntityType type ;
48+ private final String value ;
4949
50- Domain ( String domain , Role role ) {
51- super ( Type . DOMAIN , role ) ;
52- this .domain = domain ;
50+ Entity ( EntityType type , String value ) {
51+ this . type = type ;
52+ this .value = value ;
5353 }
5454
55- public String domain () {
56- return domain ;
55+ public EntityType type () {
56+ return type ;
5757 }
5858
59- String entity () {
60- return "domain-" + domain ;
59+ protected String value () {
60+ return value ;
6161 }
6262
63- public static Domain of (String domain , Role role ) {
64- return new Domain (domain , role );
63+ @ Override
64+ public String toString () {
65+ return type .name ().toLowerCase () + "-" + value ();
66+ }
67+
68+ static Entity fromPb (String entity ) {
69+ if (entity .startsWith ("user-" )) {
70+ return new User (entity .substring (5 ));
71+ }
72+ if (entity .equals (User .ALL_USERS )) {
73+ return User .ofAllUsers ();
74+ }
75+ if (entity .equals (User .ALL_AUTHENTICATED_USERS )) {
76+ return User .ofAllAuthenticatedUsers ();
77+ }
78+ if (entity .startsWith ("group-" )) {
79+ return new Group (entity .substring (6 ));
80+ }
81+ if (entity .startsWith ("project-" )) {
82+ int idx = entity .indexOf ('-' , 9 );
83+ String team = entity .substring (9 , idx );
84+ String projectId = entity .substring (idx + 1 );
85+ return new Project (Project .ProjectRole .valueOf (team .toUpperCase ()), projectId );
86+ }
87+ return null ;
6588 }
6689 }
6790
68- public static class Group extends Acl {
91+ public static class Domain extends Entity {
6992
70- private static final long serialVersionUID = -1660987136294408826L ;
71- private final String email ;
93+ private static final long serialVersionUID = -3033025857280447253L ;
94+
95+ public Domain (String domain ) {
96+ super (EntityType .DOMAIN , domain );
7297
73- Group (String email , Role role ) {
74- super (Type .GROUP , role );
75- this .email = email ;
7698 }
7799
78- public String email () {
79- return email ;
100+ public String domain () {
101+ return value () ;
80102 }
103+ }
104+
105+ public static class Group extends Entity {
106+
107+ private static final long serialVersionUID = -1660987136294408826L ;
81108
82- String entity ( ) {
83- return "group-" + email ;
109+ public Group ( String email ) {
110+ super ( EntityType . GROUP , email ) ;
84111 }
85112
86- public static Group of ( String email , Role role ) {
87- return new Group ( email , role );
113+ public String email ( ) {
114+ return value ( );
88115 }
89116 }
90117
91- public static class User extends Acl {
118+ public static class User extends Entity {
92119
93120 private static final long serialVersionUID = 3076518036392737008L ;
94121 private static final String ALL_USERS = "allUsers" ;
95122 private static final String ALL_AUTHENTICATED_USERS = "allAuthenticatedUsers" ;
96123
97- private final String email ;
98-
99- User (String email , Role role ) {
100- super (Type .USER , role );
101- this .email = email ;
124+ public User (String email ) {
125+ super (EntityType .USER , email );
102126 }
103127
104128 public String email () {
105- return email ;
129+ return value () ;
106130 }
107131
108- String entity () {
109- switch (email ) {
132+ @ Override
133+ public String toString () {
134+ switch (value ()) {
110135 case ALL_AUTHENTICATED_USERS :
111136 return ALL_AUTHENTICATED_USERS ;
112137 case ALL_USERS :
113138 return ALL_USERS ;
114139 default :
115- return "user-" + email ;
140+ break ;
116141 }
142+ return super .toString ();
117143 }
118144
119145
120- public static User of ( String email , Role role ) {
121- return new User (email , role );
146+ public static User ofAllUsers ( ) {
147+ return new User (ALL_USERS );
122148 }
123149
124- public static User ofAllUsers (Role role ) {
125- return of (ALL_USERS , role );
126- }
127-
128- public static User ofAllAuthenticatedUsers (Role role ) {
129- return of (ALL_AUTHENTICATED_USERS , role );
150+ public static User ofAllAuthenticatedUsers () {
151+ return new User (ALL_AUTHENTICATED_USERS );
130152 }
131153 }
132154
133- public static class Project extends Acl {
155+ public static class Project extends Entity {
134156
135- private final String projectId ;
136157 private final ProjectRole pRole ;
158+ private final String projectId ;
137159
138160 enum ProjectRole {
139161 OWNERS ,
140162 EDITORS ,
141163 VIEWERS
142164 }
143165
144- Project (ProjectRole pRole , String projectId , Role role ) {
145- super (Type .PROJECT , role );
166+ Project (ProjectRole pRole , String projectId ) {
167+ super (EntityType .PROJECT , pRole . name (). toLowerCase () + "-" + projectId );
146168 this .pRole = pRole ;
147169 this .projectId = projectId ;
148170 }
@@ -154,23 +176,15 @@ public ProjectRole projectRole() {
154176 public String projectId () {
155177 return projectId ;
156178 }
157-
158- String entity () {
159- return "project-" + pRole .name ().toLowerCase () + "-" + projectId ;
160- }
161-
162- public static Project of (ProjectRole pRole , String projectId , Role role ) {
163- return new Project (pRole , projectId , role );
164- }
165179 }
166180
167- Acl (Type type , Role role ) {
168- this .type = type ;
181+ public Acl (Entity entity , Role role ) {
182+ this .entity = entity ;
169183 this .role = role ;
170184 }
171185
172- public Type type () {
173- return type ;
186+ public Entity entity () {
187+ return entity ;
174188 }
175189
176190 public Role role () {
@@ -180,48 +194,24 @@ public Role role() {
180194 BucketAccessControl toBucketPb () {
181195 BucketAccessControl bucketPb = new BucketAccessControl ();
182196 bucketPb .setRole (role ().toString ());
183- bucketPb .setEntity (entity ());
197+ bucketPb .setEntity (entity (). toString () );
184198 return bucketPb ;
185199 }
186200
187201 ObjectAccessControl toObjectPb () {
188202 ObjectAccessControl objectPb = new ObjectAccessControl ();
189203 objectPb .setRole (role ().toString ());
190- objectPb .setEntity (entity ());
204+ objectPb .setEntity (entity (). toString () );
191205 return objectPb ;
192206 }
193207
194- abstract String entity ();
195-
196208 static Acl fromPb (ObjectAccessControl objectAccessControl ) {
197209 Role role = Role .valueOf (objectAccessControl .getRole ());
198- return forEntity ( objectAccessControl .getEntity (), role );
210+ return new Acl ( Entity . fromPb ( objectAccessControl .getEntity () ), role );
199211 }
200212
201213 static Acl fromPb (BucketAccessControl bucketAccessControl ) {
202214 Role role = Role .valueOf (bucketAccessControl .getRole ());
203- return forEntity (bucketAccessControl .getEntity (), role );
204- }
205-
206- private static Acl forEntity (String entity , Role role ) {
207- if (entity .startsWith ("user-" )) {
208- return User .of (entity .substring (5 ), role );
209- }
210- if (entity .equals (User .ALL_USERS )) {
211- return User .ofAllUsers (role );
212- }
213- if (entity .equals (User .ALL_AUTHENTICATED_USERS )) {
214- return User .ofAllAuthenticatedUsers (role );
215- }
216- if (entity .startsWith ("group-" )) {
217- return Group .of (entity .substring (6 ), role );
218- }
219- if (entity .startsWith ("project-" )) {
220- int idx = entity .indexOf ('-' , 9 );
221- String team = entity .substring (9 , idx );
222- String projectId = entity .substring (idx + 1 );
223- return Project .of (ProjectRole .valueOf (team .toUpperCase ()), projectId , role );
224- }
225- return null ;
215+ return new Acl (Entity .fromPb (bucketAccessControl .getEntity ()), role );
226216 }
227217}
0 commit comments