@@ -98,22 +98,27 @@ public static void addBinding(
9898 // Gets the project's policy.
9999 Policy policy = getPolicy (crmService , projectId );
100100
101- // If binding already exists, adds member to binding.
102- List < Binding > bindings = policy . getBindings () ;
103- for (Binding b : bindings ) {
101+ // Finds binding in policy, if it exists
102+ Binding binding = null ;
103+ for (Binding b : policy . getBindings () ) {
104104 if (b .getRole ().equals (role )) {
105- b . getMembers (). add ( member );
105+ binding = b ;
106106 break ;
107107 }
108108 }
109109
110- // If binding does not exist, adds binding to policy.
111- Binding binding = new Binding ();
112- binding .setRole (role );
113- binding .setMembers (Collections .singletonList (member ));
114- policy .getBindings ().add (binding );
110+ if (binding != null ) {
111+ // If binding already exists, adds member to binding.
112+ binding .getMembers ().add (member );
113+ } else {
114+ // If binding does not exist, adds binding to policy.
115+ binding = new Binding ();
116+ binding .setRole (role );
117+ binding .setMembers (Collections .singletonList (member ));
118+ policy .getBindings ().add (binding );
119+ }
115120
116- // Set the updated policy
121+ // Sets the updated policy
117122 setPolicy (crmService , projectId , policy );
118123 }
119124
@@ -123,9 +128,8 @@ public static void removeMember(
123128 Policy policy = getPolicy (crmService , projectId );
124129
125130 // Removes the member from the role.
126- List <Binding > bindings = policy .getBindings ();
127131 Binding binding = null ;
128- for (Binding b : bindings ) {
132+ for (Binding b : policy . getBindings () ) {
129133 if (b .getRole ().equals (role )) {
130134 binding = b ;
131135 break ;
0 commit comments