예제 #1
0
 public Task <bool> UpdateResourcePermissions(UpdateResourcePermissionsParameter updateResourcePermissionsParameter)
 {
     return(_updateResourcePermissionsAction.Execute(updateResourcePermissionsParameter));
 }
예제 #2
0
        public async Task <bool> Execute(UpdateResourcePermissionsParameter updateResourcePermissionsParameter)
        {
            if (updateResourcePermissionsParameter == null)
            {
                throw new ArgumentNullException(nameof(updateResourcePermissionsParameter));
            }

            if (string.IsNullOrWhiteSpace(updateResourcePermissionsParameter.ResourceId))
            {
                throw new BaseUmaException(Errors.ErrorCodes.InvalidRequestCode, Errors.ErrorDescriptions.TheResourceIdMustBeSpecified);
            }

            var resource = await _resourceSetRepository.Get(updateResourcePermissionsParameter.ResourceId).ConfigureAwait(false);

            if (resource == null)
            {
                throw new UmaResourceNotFoundException();
            }

            if (updateResourcePermissionsParameter.Subject != resource.Owner)
            {
                throw new UmaNotAuthorizedException();
            }

            var policiesToBeUpdated = resource.AuthPolicies.ToList();
            var policiesToBeRemoved = new List <string>();
            var length = policiesToBeUpdated.Count();

            for (int i = length - 1; i >= 0; i--)
            {
                var policy          = policiesToBeUpdated.ElementAt(i);
                var policyParameter = updateResourcePermissionsParameter.PolicyIds.FirstOrDefault(p => p == policy.Id);
                if (policyParameter == null)
                {
                    policiesToBeUpdated.Remove(policy);
                    policiesToBeRemoved.Add(policy.Id);
                }
            }

            using (var transaction = new CommittableTransaction(new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            }))
            {
                try
                {
                    var operations = new List <Task <bool> >();
                    foreach (var policyId in policiesToBeRemoved)
                    {
                        operations.Add(_policyRepository.Delete(policyId));
                    }

                    await Task.WhenAll(operations).ConfigureAwait(false);

                    transaction.Commit();
                    return(true);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }