| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using Azure.Core; |
| | | 7 | | |
| | | 8 | | namespace Azure.Security.KeyVault.Administration |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// A scope of the role assignment. |
| | | 12 | | /// </summary> |
| | | 13 | | public readonly struct RoleAssignmentScope : IEquatable<RoleAssignmentScope> |
| | | 14 | | { |
| | | 15 | | internal const string GlobalValue = "/"; |
| | | 16 | | internal const string KeysValue = "/keys"; |
| | | 17 | | |
| | | 18 | | private readonly string _value; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="RoleAssignmentScope"/> structure. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="value">The string value of the instance.</param> |
| | | 24 | | public RoleAssignmentScope(string value) |
| | | 25 | | { |
| | 12 | 26 | | _value = value ?? throw new ArgumentNullException(nameof(value)); |
| | 8 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="RoleAssignmentScope"/> structure. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="resourceId">The Resource Id for the given Resource.</param> |
| | | 33 | | public RoleAssignmentScope(Uri resourceId) |
| | | 34 | | { |
| | 18 | 35 | | Argument.AssertNotNull(resourceId, nameof(resourceId)); |
| | | 36 | | |
| | | 37 | | // Remove the version segment from a Key Id, if present. |
| | 14 | 38 | | string[] segments = resourceId.Segments; |
| | | 39 | | |
| | 14 | 40 | | if (resourceId.AbsolutePath.StartsWith("/keys/", StringComparison.Ordinal) && segments.Length == 4) |
| | | 41 | | { |
| | 6 | 42 | | _value = resourceId.AbsolutePath.Remove(resourceId.AbsolutePath.Length - segments[3].Length - 1); |
| | | 43 | | } |
| | | 44 | | else |
| | | 45 | | { |
| | 8 | 46 | | _value = resourceId.AbsolutePath; |
| | | 47 | | } |
| | 8 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Role assignments apply to everything on the resource. |
| | | 52 | | /// </summary> |
| | 82 | 53 | | public static RoleAssignmentScope Global { get; } = new RoleAssignmentScope(GlobalValue); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Role assignments apply to all Keys. |
| | | 57 | | /// </summary> |
| | 6 | 58 | | public static RoleAssignmentScope Keys { get; } = new RoleAssignmentScope(KeysValue); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Determines if two <see cref="RoleAssignmentScope"/> values are the same. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="left">The first <see cref="RoleAssignmentScope"/> to compare.</param> |
| | | 64 | | /// <param name="right">The second <see cref="RoleAssignmentScope"/> to compare.</param> |
| | | 65 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</retur |
| | 0 | 66 | | public static bool operator ==(RoleAssignmentScope left, RoleAssignmentScope right) => left.Equals(right); |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Determines if two <see cref="RoleAssignmentScope"/> values are different. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="left">The first <see cref="RoleAssignmentScope"/> to compare.</param> |
| | | 72 | | /// <param name="right">The second <see cref="RoleAssignmentScope"/> to compare.</param> |
| | | 73 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</retu |
| | 0 | 74 | | public static bool operator !=(RoleAssignmentScope left, RoleAssignmentScope right) => !left.Equals(right); |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Converts a string to a <see cref="RoleAssignmentScope"/>. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="value">The string value to convert.</param> |
| | 0 | 80 | | public static implicit operator RoleAssignmentScope(string value) => new RoleAssignmentScope(value); |
| | | 81 | | |
| | | 82 | | /// <inheritdoc/> |
| | | 83 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 84 | | public override bool Equals(object obj) => obj is RoleAssignmentScope other && Equals(other); |
| | | 85 | | |
| | | 86 | | /// <inheritdoc/> |
| | 0 | 87 | | public bool Equals(RoleAssignmentScope other) => string.Equals(_value, other._value, StringComparison.Ordinal); |
| | | 88 | | |
| | | 89 | | /// <inheritdoc/> |
| | | 90 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 91 | | public override int GetHashCode() => _value?.GetHashCode() ?? 0; |
| | | 92 | | |
| | | 93 | | /// <inheritdoc/> |
| | 94 | 94 | | public override string ToString() => _value; |
| | | 95 | | } |
| | | 96 | | } |