| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Management |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Xml.Linq; |
| | 10 | |
|
| | 11 | | public class AuthorizationRules : List<AuthorizationRule>, IEquatable<AuthorizationRules> |
| | 12 | | { |
| 0 | 13 | | private bool RequiresEncryption => this.Count > 0; |
| | 14 | |
|
| | 15 | | internal XElement Serialize() |
| | 16 | | { |
| 0 | 17 | | var rules = new XElement( |
| 0 | 18 | | XName.Get("AuthorizationRules", ManagementClientConstants.ServiceBusNamespace), |
| 0 | 19 | | this.Select(rule => rule.Serialize())); |
| | 20 | |
|
| 0 | 21 | | return rules; |
| | 22 | | } |
| | 23 | |
|
| | 24 | | internal static AuthorizationRules ParseFromXElement(XElement xElement) |
| | 25 | | { |
| 0 | 26 | | var rules = new AuthorizationRules(); |
| 0 | 27 | | var xRules = xElement.Elements(XName.Get("AuthorizationRule", ManagementClientConstants.ServiceBusNamespace) |
| 0 | 28 | | rules.AddRange(xRules.Select(rule => AuthorizationRule.ParseFromXElement(rule))); |
| 0 | 29 | | return rules; |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public override int GetHashCode() |
| | 33 | | { |
| 0 | 34 | | int hash = 7; |
| | 35 | | unchecked |
| | 36 | | { |
| 0 | 37 | | foreach (var rule in this) |
| | 38 | | { |
| 0 | 39 | | hash = (hash * 7) + rule.GetHashCode(); |
| | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | return hash; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public override bool Equals(object obj) |
| | 47 | | { |
| 0 | 48 | | var other = obj as AuthorizationRules; |
| 0 | 49 | | return this.Equals(other); |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public bool Equals(AuthorizationRules other) |
| | 53 | | { |
| 0 | 54 | | if (ReferenceEquals(other, null) || this.Count != other.Count) |
| | 55 | | { |
| 0 | 56 | | return false; |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | var cnt = new Dictionary<string, AuthorizationRule>(); |
| 0 | 60 | | foreach (AuthorizationRule rule in this) |
| | 61 | | { |
| 0 | 62 | | cnt[rule.KeyName] = rule; |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | foreach (AuthorizationRule otherRule in other) |
| | 66 | | { |
| 0 | 67 | | if (!cnt.TryGetValue(otherRule.KeyName, out var rule) || !rule.Equals(otherRule)) |
| | 68 | | { |
| 0 | 69 | | return false; |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | return true; |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public static bool operator ==(AuthorizationRules o1, AuthorizationRules o2) |
| | 77 | | { |
| 0 | 78 | | if (ReferenceEquals(o1, o2)) |
| | 79 | | { |
| 0 | 80 | | return true; |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | if (ReferenceEquals(o1, null) || ReferenceEquals(o2, null)) |
| | 84 | | { |
| 0 | 85 | | return false; |
| | 86 | | } |
| | 87 | |
|
| 0 | 88 | | return o1.Equals(o2); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public static bool operator !=(AuthorizationRules o1, AuthorizationRules o2) |
| | 92 | | { |
| 0 | 93 | | return !(o1 == o2); |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |