| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Text; |
| | 8 | |
|
| | 9 | | namespace Azure.Storage.Files.DataLake.Models |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Extension methods for RolePermissions. |
| | 13 | | /// </summary> |
| | 14 | | public static class PathAccessControlExtensions |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Parses octal char to RolePermissions. |
| | 18 | | /// </summary> |
| | 19 | | public static RolePermissions ParseOctalRolePermissions(char c) |
| | 20 | | { |
| 32 | 21 | | RolePermissions rolePermissions = RolePermissions.None; |
| | 22 | |
|
| 32 | 23 | | int value = (int)char.GetNumericValue(c); |
| | 24 | |
|
| 32 | 25 | | if (value < 0 || value > 7) |
| | 26 | | { |
| 4 | 27 | | throw Errors.MustBeBetweenInclusive(nameof(c), 0, 7, value); |
| | 28 | | } |
| | 29 | |
|
| 28 | 30 | | if ((value & 4) > 0) |
| | 31 | | { |
| 20 | 32 | | rolePermissions |= RolePermissions.Read; |
| | 33 | | } |
| | 34 | |
|
| 28 | 35 | | if ((value & 2) > 0) |
| | 36 | | { |
| 20 | 37 | | rolePermissions |= RolePermissions.Write; |
| | 38 | | } |
| | 39 | |
|
| 28 | 40 | | if ((value & 1) > 0) |
| | 41 | | { |
| 20 | 42 | | rolePermissions |= RolePermissions.Execute; |
| | 43 | | } |
| | 44 | |
|
| 28 | 45 | | return rolePermissions; |
| | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Parses symbolic permissions string to RolePermissions. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="s">String to parse.</param> |
| | 52 | | /// <param name="allowStickyBit">If sticky bit is allowed.</param> |
| | 53 | | /// <returns><see cref="RolePermissions"/>.</returns> |
| | 54 | | public static RolePermissions ParseSymbolicRolePermissions(string s, bool allowStickyBit = false) |
| | 55 | | { |
| 1608 | 56 | | RolePermissions rolePermissions = RolePermissions.None; |
| 1608 | 57 | | ArgumentException argumentException = DataLakeErrors.RolePermissionsSymbolicInvalidCharacter(s); |
| | 58 | |
|
| 1608 | 59 | | if (s == null) |
| | 60 | | { |
| 4 | 61 | | throw Errors.ArgumentNull(nameof(s)); |
| | 62 | | } |
| | 63 | |
|
| 1604 | 64 | | if (s.Length != 3) |
| | 65 | | { |
| 8 | 66 | | throw DataLakeErrors.RolePermissionsSymbolicInvalidLength(s); |
| | 67 | | } |
| | 68 | |
|
| 1596 | 69 | | if (s[0] == 'r') |
| | 70 | | { |
| 1076 | 71 | | rolePermissions |= RolePermissions.Read; |
| | 72 | | } |
| 520 | 73 | | else if (s[0] != '-') |
| | 74 | | { |
| 4 | 75 | | throw argumentException; |
| | 76 | | } |
| | 77 | |
|
| 1592 | 78 | | if (s[1] == 'w') |
| | 79 | | { |
| 880 | 80 | | rolePermissions |= RolePermissions.Write; |
| | 81 | | } |
| 712 | 82 | | else if (s[1] != '-') |
| | 83 | | { |
| 4 | 84 | | throw argumentException; |
| | 85 | | } |
| | 86 | |
|
| 1588 | 87 | | if (s[2] == 'x') |
| | 88 | | { |
| 772 | 89 | | rolePermissions |= RolePermissions.Execute; |
| | 90 | | } |
| 816 | 91 | | else if (allowStickyBit) |
| | 92 | | { |
| 198 | 93 | | if (s[2] == 't') |
| | 94 | | { |
| 12 | 95 | | rolePermissions |= RolePermissions.Execute; |
| | 96 | | } |
| 186 | 97 | | else if (s[2] != 'T' && s[2] != '-') |
| | 98 | | { |
| 2 | 99 | | throw argumentException; |
| | 100 | | } |
| | 101 | | } |
| 618 | 102 | | else if (s[2] != '-') |
| | 103 | | { |
| 2 | 104 | | throw argumentException; |
| | 105 | | } |
| | 106 | |
|
| 1584 | 107 | | return rolePermissions; |
| | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Returns the octal string representation of this RolePermissions. |
| | 112 | | /// </summary> |
| | 113 | | /// <returns>String.</returns> |
| | 114 | | public static string ToOctalRolePermissions(this RolePermissions rolePermissions) |
| | 115 | | { |
| 28 | 116 | | int result = 0; |
| | 117 | |
|
| 28 | 118 | | if (rolePermissions.HasFlag(RolePermissions.Read)) |
| | 119 | | { |
| 20 | 120 | | result |= 4; |
| | 121 | | } |
| | 122 | |
|
| 28 | 123 | | if (rolePermissions.HasFlag(RolePermissions.Write)) |
| | 124 | | { |
| 20 | 125 | | result |= 2; |
| | 126 | | } |
| | 127 | |
|
| 28 | 128 | | if (rolePermissions.HasFlag(RolePermissions.Execute)) |
| | 129 | | { |
| 20 | 130 | | result |= 1; |
| | 131 | | } |
| | 132 | |
|
| 28 | 133 | | return result.ToString(CultureInfo.InvariantCulture); |
| | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Returns the octal string respentation of this RolePermissions. |
| | 138 | | /// </summary> |
| | 139 | | /// <returns>String.</returns> |
| | 140 | | public static string ToSymbolicRolePermissions(this RolePermissions rolePermissions) |
| | 141 | | { |
| 748 | 142 | | StringBuilder stringBuilder = new StringBuilder(); |
| | 143 | |
|
| 748 | 144 | | stringBuilder.Append(rolePermissions.HasFlag(RolePermissions.Read) ? "r" : "-"); |
| 748 | 145 | | stringBuilder.Append(rolePermissions.HasFlag(RolePermissions.Write) ? "w" : "-"); |
| 748 | 146 | | stringBuilder.Append(rolePermissions.HasFlag(RolePermissions.Execute) ? "x" : "-"); |
| | 147 | |
|
| 748 | 148 | | return stringBuilder.ToString(); |
| | 149 | | } |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// Converts the Access Control List to a <see cref="string"/>. |
| | 153 | | /// </summary> |
| | 154 | | /// <param name="accessControlList">The Access Control List to serialize</param> |
| | 155 | | /// <returns>string.</returns> |
| | 156 | | public static string ToAccessControlListString(IList<PathAccessControlItem> accessControlList) |
| | 157 | | { |
| 102 | 158 | | if (accessControlList == null) |
| | 159 | | { |
| 2 | 160 | | return null; |
| | 161 | | } |
| | 162 | |
|
| 100 | 163 | | IList<string> serializedAcl = new List<string>(); |
| 980 | 164 | | foreach (PathAccessControlItem ac in accessControlList) |
| | 165 | | { |
| 390 | 166 | | serializedAcl.Add(ac.ToString()); |
| | 167 | | } |
| 100 | 168 | | return string.Join(",", serializedAcl); |
| | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary> |
| | 172 | | /// Deseralizes an access control list string into a list of PathAccessControlEntries. |
| | 173 | | /// </summary> |
| | 174 | | /// <param name="s">The string to parse.</param> |
| | 175 | | /// <returns>A List of <see cref="PathAccessControlItem"/>.</returns> |
| | 176 | | public static IList<PathAccessControlItem> ParseAccessControlList(string s) |
| | 177 | | { |
| 226 | 178 | | if (s == null) |
| | 179 | | { |
| 2 | 180 | | return null; |
| | 181 | | } |
| | 182 | |
|
| 224 | 183 | | string[] strings = s.Split(','); |
| 224 | 184 | | List<PathAccessControlItem> accessControlList = new List<PathAccessControlItem>(); |
| 1948 | 185 | | foreach (string entry in strings) |
| | 186 | | { |
| 750 | 187 | | accessControlList.Add(PathAccessControlItem.Parse(entry)); |
| | 188 | | } |
| 224 | 189 | | return accessControlList; |
| | 190 | | } |
| | 191 | | } |
| | 192 | | } |