|
| 1 | +using Box.V2.Auth; |
| 2 | +using Box.V2.Config; |
| 3 | +using Box.V2.Extensions; |
| 4 | +using Box.V2.Converter; |
| 5 | +using Box.V2.Models; |
| 6 | +using Box.V2.Services; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System; |
| 9 | +using Newtonsoft.Json.Linq; |
| 10 | + |
| 11 | +namespace Box.V2.Managers |
| 12 | +{ |
| 13 | + public class BoxStoragePoliciesManager : BoxResourceManager |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Create a new BoxStoragePolicies object. |
| 17 | + /// </summary> |
| 18 | + public BoxStoragePoliciesManager(IBoxConfig config, IBoxService service, IBoxConverter converter, IAuthRepository auth, string asUser = null, bool? suppressNotifications = null) |
| 19 | + : base(config, service, converter, auth, asUser, suppressNotifications) { } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Get details of a single Box Storage Policy. |
| 23 | + /// </summary> |
| 24 | + /// <param name="policyId">Id of the Box Storage Policy to retrieve.</param> |
| 25 | + /// <returns>If the Id is valid, information about the Box Storage Policy is returned. </returns> |
| 26 | + public async Task<BoxStoragePolicy> GetStoragePolicyAsync(String policyId) |
| 27 | + { |
| 28 | + policyId.ThrowIfNullOrWhiteSpace("policyId"); |
| 29 | + |
| 30 | + BoxRequest request = new BoxRequest(_config.StoragePoliciesUri, policyId) |
| 31 | + .Method(RequestMethod.Get); |
| 32 | + |
| 33 | + IBoxResponse<BoxStoragePolicy> response = await ToResponseAsync<BoxStoragePolicy>(request).ConfigureAwait(false); |
| 34 | + |
| 35 | + return response.ResponseObject; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Get a list of Storage Policies that belong to your Enterprise. |
| 40 | + /// </summary> |
| 41 | + /// <param name="fields">Attribute(s) to include in the response.</param> |
| 42 | + /// <param name="marker">Take from "next_marker" column of a prior call to get the next page.</param> |
| 43 | + /// <param name="limit">Limit result size to this number. Defults to 100, maximum is 1,000.</param> |
| 44 | + /// <param name="autoPaginate">Whether or not to auto-paginate to fetch all items; defaults to false.</param> |
| 45 | + /// <returns>Returns the list of Storage Policies in your Enterprise that match the filer parameters (if passedin).</returns> |
| 46 | + public async Task<BoxCollectionMarkerBased<BoxStoragePolicy>> GetListStoragePoliciesAsync(string fields = null, string marker = null, int limit = 100, bool autoPaginate = false) |
| 47 | + { |
| 48 | + BoxRequest request = new BoxRequest(_config.StoragePoliciesUri) |
| 49 | + .Method(RequestMethod.Get) |
| 50 | + .Param("fields", fields) |
| 51 | + .Param("limit", limit.ToString()) |
| 52 | + .Param("marker", marker); |
| 53 | + |
| 54 | + if (autoPaginate) |
| 55 | + { |
| 56 | + return await AutoPaginateMarker<BoxStoragePolicy>(request, limit); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + IBoxResponse<BoxCollectionMarkerBased<BoxStoragePolicy>> response = await ToResponseAsync<BoxCollectionMarkerBased<BoxStoragePolicy>>(request).ConfigureAwait(false); |
| 61 | + return response.ResponseObject; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Get details of a single assignment. |
| 67 | + /// </summary> |
| 68 | + /// <param name="assignmentId">Id of the assignment.</param> |
| 69 | + /// <returns>If the assignmentId is valid, information about the assignment is returned.</returns> |
| 70 | + public async Task<BoxStoragePolicyAssignment> GetAssignmentAsync(string assignmentId) |
| 71 | + { |
| 72 | + assignmentId.ThrowIfNullOrWhiteSpace("assignmentId"); |
| 73 | + |
| 74 | + BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsUri, assignmentId) |
| 75 | + .Method(RequestMethod.Get); |
| 76 | + |
| 77 | + IBoxResponse<BoxStoragePolicyAssignment> response = await ToResponseAsync<BoxStoragePolicyAssignment>(request).ConfigureAwait(false); |
| 78 | + |
| 79 | + return response.ResponseObject; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Get details of a Storage Policy Assignment for target entity. |
| 84 | + /// </summary> |
| 85 | + /// <param name="userId">User Id of the assignment.</param> |
| 86 | + /// <param name="entityType">Entity type of the storage policy assignment.</param> |
| 87 | + /// <returns></returns> |
| 88 | + public async Task<BoxStoragePolicyAssignment> GetAssignmentForTargetAsync(string entityId, string entityType = "user") |
| 89 | + { |
| 90 | + entityId.ThrowIfNullOrWhiteSpace("entityId"); |
| 91 | + |
| 92 | + BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsForTargetUri) |
| 93 | + .Method(RequestMethod.Get) |
| 94 | + .Param("resolved_for_type", entityType) |
| 95 | + .Param("resolved_for_id", entityId); |
| 96 | + |
| 97 | + IBoxResponse<BoxCollectionMarkerBased<BoxStoragePolicyAssignment>> response = await ToResponseAsync<BoxCollectionMarkerBased<BoxStoragePolicyAssignment>>(request).ConfigureAwait(false); |
| 98 | + return response.ResponseObject.Entries[0]; |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Update the storage policy information for storage policy assignment. |
| 103 | + /// </summary> |
| 104 | + /// <param name="assignmentId">Storage Policy assignment Id to update.</param> |
| 105 | + /// <param name="policyId">"The Id of the Storage Policy to update to."</param> |
| 106 | + /// <returns></returns> The updated Storage Policy object with new assignment. |
| 107 | + public async Task<BoxStoragePolicyAssignment> UpdateStoragePolicyAssignment(string assignmentId, String policyId) |
| 108 | + { |
| 109 | + policyId.ThrowIfNullOrWhiteSpace("policyId"); |
| 110 | + |
| 111 | + dynamic req = new JObject(); |
| 112 | + dynamic storagePolicyObject = new JObject(); |
| 113 | + |
| 114 | + storagePolicyObject.type = "storage_policy"; |
| 115 | + storagePolicyObject.id = policyId; |
| 116 | + req.storage_policy = storagePolicyObject; |
| 117 | + |
| 118 | + string jsonStr = req.ToString(); |
| 119 | + |
| 120 | + BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsUri, assignmentId) |
| 121 | + .Method(RequestMethod.Put) |
| 122 | + .Payload(jsonStr); |
| 123 | + |
| 124 | + IBoxResponse<BoxStoragePolicyAssignment> response = await ToResponseAsync<BoxStoragePolicyAssignment>(request).ConfigureAwait(false); |
| 125 | + return response.ResponseObject; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Create a storage policy assignment to a Box User. |
| 130 | + /// </summary> |
| 131 | + /// <param name="userId">The user Id to create assignment for.</param> |
| 132 | + /// <param name="policyId">The Id of the storage policy to assign the user to.</param> |
| 133 | + /// <returns>The assignment object for the storage policy assignment to user.</returns> |
| 134 | + public async Task<BoxStoragePolicyAssignment> CreateAssignmentAsync(string userId, string policyId) |
| 135 | + { |
| 136 | + userId.ThrowIfNullOrWhiteSpace("userId"); |
| 137 | + policyId.ThrowIfNullOrWhiteSpace("policyId"); |
| 138 | + |
| 139 | + dynamic req = new JObject(); |
| 140 | + dynamic storagePolicyObject = new JObject(); |
| 141 | + dynamic userObject = new JObject(); |
| 142 | + |
| 143 | + storagePolicyObject.type = "storage_policy"; |
| 144 | + storagePolicyObject.id = policyId; |
| 145 | + |
| 146 | + userObject.type = "user"; |
| 147 | + userObject.id = userId; |
| 148 | + |
| 149 | + req.storage_policy = storagePolicyObject; |
| 150 | + req.assigned_to = userObject; |
| 151 | + |
| 152 | + string jsonStr = req.ToString(); |
| 153 | + |
| 154 | + BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsForTargetUri) |
| 155 | + .Method(RequestMethod.Post) |
| 156 | + .Payload(jsonStr); |
| 157 | + |
| 158 | + IBoxResponse<BoxStoragePolicyAssignment> response = await ToResponseAsync<BoxStoragePolicyAssignment>(request).ConfigureAwait(false); |
| 159 | + return response.ResponseObject; |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Sends request to delete an existing assignment. |
| 164 | + /// </summary> |
| 165 | + /// <param name="assignmentId">Id of the storage policy assignment.</param> |
| 166 | + /// <returns>A successful request returns 204 No Content.</returns> |
| 167 | + public async Task<bool> DeleteAssignmentAsync(string assignmentId) |
| 168 | + { |
| 169 | + BoxRequest request = new BoxRequest(_config.StoragePolicyAssignmentsUri, assignmentId) |
| 170 | + .Method(RequestMethod.Delete); |
| 171 | + |
| 172 | + var response = await ToResponseAsync<BoxStoragePolicyAssignment>(request).ConfigureAwait(false); |
| 173 | + |
| 174 | + return response.Status == ResponseStatus.Success; |
| 175 | + } |
| 176 | + |
| 177 | + /// <summary> |
| 178 | + /// Checks if a storage policy assignment exists. If it does not then create an assignment. |
| 179 | + /// </summary> |
| 180 | + /// <param name="userId">The id of the user to assign storage policy to.</param> |
| 181 | + /// <param name="storagePolicyId">The storage policy id to assign to user.</param> |
| 182 | + /// <returns></returns> |
| 183 | + public async Task<BoxStoragePolicyAssignment> AssignAsync(string userId, string storagePolicyId) |
| 184 | + { |
| 185 | + userId.ThrowIfNullOrWhiteSpace("userId"); |
| 186 | + storagePolicyId.ThrowIfNullOrWhiteSpace("storagePolicyId"); |
| 187 | + |
| 188 | + var result = await GetAssignmentForTargetAsync(userId); |
| 189 | + |
| 190 | + if(result.BoxStoragePolicy.Id.Equals(storagePolicyId)) |
| 191 | + { |
| 192 | + return result; |
| 193 | + } |
| 194 | + |
| 195 | + if(result.AssignedTo.Type.Equals("enterprise")) |
| 196 | + { |
| 197 | + return await CreateAssignmentAsync(userId, storagePolicyId); |
| 198 | + } |
| 199 | + |
| 200 | + return await UpdateStoragePolicyAssignment(result.Id, storagePolicyId); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments