| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text; |
| | 6 | |
|
| | 7 | | namespace Azure.Storage.Files.DataLake.Models |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Represents an access control in a file access control list. |
| | 11 | | /// </summary> |
| | 12 | | public class PathAccessControlItem |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Indicates whether this is the default entry for the ACL. |
| | 16 | | /// </summary> |
| 450 | 17 | | public bool DefaultScope { get; set; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Specifies which role this entry targets. |
| | 21 | | /// </summary> |
| 1196 | 22 | | public AccessControlType AccessControlType { get; set; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Specifies the entity for which this entry applies. |
| | 26 | | /// Must be omitted for types mask or other. It must also be omitted when the user or group is the owner. |
| | 27 | | /// </summary> |
| 446 | 28 | | public string EntityId { get; set; } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Specifies the permissions granted to this entry. |
| | 32 | | /// </summary> |
| 1196 | 33 | | public RolePermissions Permissions { get; set; } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Empty constructor. |
| | 37 | | /// </summary> |
| 1524 | 38 | | public PathAccessControlItem() { } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Constructor. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="accessControlType">Specifies which role this entry targets.</param> |
| | 44 | | /// <param name="permissions">Specifies the permissions granted to this entry.</param> |
| | 45 | | /// <param name="defaultScope">Indicates whether this is the default entry for the ACL.</param> |
| | 46 | | /// <param name="entityId">Optional entity ID to which this entry applies.</param> |
| 22 | 47 | | public PathAccessControlItem( |
| 22 | 48 | | AccessControlType accessControlType, |
| 22 | 49 | | RolePermissions permissions, |
| 22 | 50 | | bool defaultScope = false, |
| 22 | 51 | | string entityId = default) |
| | 52 | | { |
| 22 | 53 | | if (entityId != null |
| 22 | 54 | | && !(accessControlType == AccessControlType.User || accessControlType == AccessControlType.Group)) |
| | 55 | | { |
| 2 | 56 | | throw DataLakeErrors.EntityIdAndInvalidAccessControlType(accessControlType.ToString()); |
| | 57 | | } |
| | 58 | |
|
| 20 | 59 | | DefaultScope = defaultScope; |
| 20 | 60 | | AccessControlType = accessControlType; |
| 20 | 61 | | EntityId = entityId; |
| 20 | 62 | | Permissions = permissions; |
| 20 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Override of ToString(). |
| | 67 | | /// </summary> |
| | 68 | | /// <returns></returns> |
| | 69 | | public override string ToString() |
| | 70 | | { |
| 396 | 71 | | StringBuilder stringBuilder = new StringBuilder(); |
| | 72 | |
|
| 396 | 73 | | if (DefaultScope) |
| | 74 | | { |
| 8 | 75 | | stringBuilder.Append("default:"); |
| | 76 | | } |
| 396 | 77 | | stringBuilder.Append(AccessControlType.ToString().ToLowerInvariant()); |
| 396 | 78 | | stringBuilder.Append(":"); |
| 396 | 79 | | stringBuilder.Append(EntityId ?? ""); |
| 396 | 80 | | stringBuilder.Append(":"); |
| 396 | 81 | | stringBuilder.Append(Permissions.ToSymbolicRolePermissions()); |
| | 82 | |
|
| 396 | 83 | | return stringBuilder.ToString(); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Parses the provided string into a <see cref="PathAccessControlItem"/> |
| | 88 | | /// </summary> |
| | 89 | | /// <param name="s">The string representation of the access control list.</param> |
| | 90 | | /// <returns>A <see cref="PathAccessControlItem"/>.</returns> |
| | 91 | | public static PathAccessControlItem Parse(string s) |
| | 92 | | { |
| 764 | 93 | | if (s == null) |
| | 94 | | { |
| 2 | 95 | | return null; |
| | 96 | | } |
| | 97 | |
|
| 762 | 98 | | PathAccessControlItem entry = new PathAccessControlItem(); |
| 762 | 99 | | string[] parts = s.Split(':'); |
| 762 | 100 | | int indexOffset = 0; |
| | 101 | |
|
| 762 | 102 | | if (parts.Length < 3 || parts.Length > 4) |
| | 103 | | { |
| 4 | 104 | | throw DataLakeErrors.PathAccessControlItemStringInvalidLength(s); |
| | 105 | | } |
| | 106 | |
|
| 758 | 107 | | if (parts.Length == 4) |
| | 108 | | { |
| 12 | 109 | | if (!parts[0].Equals("default", StringComparison.OrdinalIgnoreCase)) |
| | 110 | | { |
| 2 | 111 | | throw DataLakeErrors.PathAccessControlItemStringInvalidPrefix(s); |
| | 112 | | } |
| 10 | 113 | | entry.DefaultScope = true; |
| 10 | 114 | | indexOffset = 1; |
| | 115 | | } |
| 756 | 116 | | entry.AccessControlType = ParseAccesControlType(parts[indexOffset]); |
| | 117 | |
|
| 756 | 118 | | if (!string.IsNullOrEmpty(parts[1 + indexOffset])) |
| | 119 | | { |
| 6 | 120 | | entry.EntityId = parts[1 + indexOffset]; |
| | 121 | | } |
| | 122 | |
|
| 756 | 123 | | entry.Permissions = PathAccessControlExtensions.ParseSymbolicRolePermissions(parts[2 + indexOffset], false); |
| 756 | 124 | | return entry; |
| | 125 | | } |
| | 126 | |
|
| | 127 | | internal static AccessControlType ParseAccesControlType(string s) |
| | 128 | | { |
| 756 | 129 | | if ("user".Equals(s, StringComparison.OrdinalIgnoreCase)) |
| | 130 | | { |
| 226 | 131 | | return AccessControlType.User; |
| | 132 | | } |
| 530 | 133 | | else if ("group".Equals(s, StringComparison.OrdinalIgnoreCase)) |
| | 134 | | { |
| 220 | 135 | | return AccessControlType.Group; |
| | 136 | | } |
| 310 | 137 | | else if ("mask".Equals(s, StringComparison.OrdinalIgnoreCase)) |
| | 138 | | { |
| 90 | 139 | | return AccessControlType.Mask; |
| | 140 | | } |
| 220 | 141 | | else if ("other".Equals(s, StringComparison.OrdinalIgnoreCase)) |
| | 142 | | { |
| 220 | 143 | | return AccessControlType.Other; |
| | 144 | | } |
| | 145 | | else |
| | 146 | | { |
| 0 | 147 | | throw Errors.InvalidArgument(nameof(s)); |
| | 148 | | } |
| | 149 | | } |
| | 150 | | } |
| | 151 | | } |