@@ -26,14 +26,21 @@ public partial class Policy
26
26
/// <summary>
27
27
/// Adds the specified member to the specified role. If the role does
28
28
/// not already exist, it is created.
29
+ /// This method will fail with an <see cref="InvalidOperationException"/>
30
+ /// if it is called on a Policy with a <see cref="Version"/> greater than 1,
31
+ /// or if any of the bindings contain conditions,
32
+ /// as that indicates a more complicated policy than this method is prepared
33
+ /// to handle. Changes to such policies must be made manually.
29
34
/// </summary>
30
35
/// <param name="role">The role to add the member to. Must not be null or empty.</param>
31
36
/// <param name="member">The member to add to the role. Must not be null or empty.</param>
32
37
/// <returns><c>true</c> if the policy was changed; <c>false</c> if the member already existed in the role.</returns>
38
+ /// <exception cref="InvalidOperationException">The <see cref="Version"/> is greater than 1.</exception>
33
39
public bool AddRoleMember ( string role , string member )
34
40
{
35
41
GaxPreconditions . CheckNotNullOrEmpty ( role , nameof ( role ) ) ;
36
42
GaxPreconditions . CheckNotNullOrEmpty ( member , nameof ( member ) ) ;
43
+ ValidatePolicyVersion ( ) ;
37
44
var binding = FindRole ( role ) ;
38
45
if ( binding == null )
39
46
{
@@ -51,15 +58,22 @@ public bool AddRoleMember(string role, string member)
51
58
/// <summary>
52
59
/// Removes the specified member to the specified role, if they belong to it. If the role becomes empty after
53
60
/// removing the member, it is removed from the policy.
61
+ /// This method will fail with an <see cref="InvalidOperationException"/>
62
+ /// if it is called on a Policy with a <see cref="Version"/> greater than 1,
63
+ /// or if any of the bindings contain conditions,
64
+ /// as that indicates a more complicated policy than this method is prepared
65
+ /// to handle. Changes to such policies must be made manually.
54
66
/// </summary>
55
67
/// <param name="role">The role to remove the member from. Must not be null or empty.</param>
56
68
/// <param name="member">The member to remove from the role. Must not be null or empty.</param>
57
69
/// <returns><c>true</c> if the policy was changed; <c>false</c> if the member didn't exist in the role
58
70
/// or the role didn't exist.</returns>
71
+ /// <exception cref="InvalidOperationException">The <see cref="Version"/> is greater than 1.</exception>
59
72
public bool RemoveRoleMember ( string role , string member )
60
73
{
61
74
GaxPreconditions . CheckNotNullOrEmpty ( role , nameof ( role ) ) ;
62
75
GaxPreconditions . CheckNotNullOrEmpty ( member , nameof ( member ) ) ;
76
+ ValidatePolicyVersion ( ) ;
63
77
var binding = FindRole ( role ) ;
64
78
if ( binding == null )
65
79
{
@@ -77,5 +91,9 @@ public bool RemoveRoleMember(string role, string member)
77
91
}
78
92
79
93
private Binding FindRole ( string role ) => Bindings . FirstOrDefault ( binding => binding . Role == role ) ;
94
+
95
+ private void ValidatePolicyVersion ( ) =>
96
+ GaxPreconditions . CheckState ( Version <= 1 && Bindings . All ( b => b . Condition == null ) ,
97
+ "Helper methods cannot be invoked on policies with version {0} or with conditional bindings. For more information, see https://cloud.google.com/iam/docs/policies." , Version ) ;
80
98
}
81
99
}
0 commit comments