Exemplo n.º 1
0
        /// <summary>
        /// Gets the first plugin satisfying the required operation claims for the current package source.
        /// </summary>
        /// <param name="requiredClaim">The required operation claim.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>A task that represents the asynchronous operation.
        /// The task result (<see cref="Task{TResult}.Result" />) returns a <see cref="GetPluginResult" />.</returns>
        /// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
        /// is cancelled.</exception>
        public async Task <GetPluginResult> GetPluginAsync(
            OperationClaim requiredClaim,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var messages = new List <string>();

            foreach (var result in _pluginCreationResults)
            {
                if (!string.IsNullOrEmpty(result.Message))
                {
                    throw new PluginException(result.Message);
                }

                if (result.Claims.Contains(requiredClaim))
                {
                    var key = $"{MessageMethod.SetCredentials}.{_packageSource.SourceUri}";

                    await result.PluginMulticlientUtilities.DoOncePerPluginLifetimeAsync(
                        key,
                        () => SetPackageSourceCredentialsAsync(result.Plugin, cancellationToken),
                        cancellationToken);

                    return(new GetPluginResult(result.Plugin, result.PluginMulticlientUtilities));
                }
            }

            return(null);
        }
 public IResult AddUserOperationClaim(AppUser userResult, OperationClaim operationClaim)
 {
     _userOperationClaimDal.Add(new UserOperationClaim {
         User = userResult, OperationClaim = operationClaim
     });
     return(new SuccessResult());
 }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a plugin from the discovered plugin.
        /// We firstly check the cache for the operation claims for the given request key.
        /// If there is a valid cache entry, and it does contain the requested operation claim, then we start the plugin, and if need be update the cache value itself.
        /// If there is a valid cache entry, and it does NOT contain the requested operation claim, then we return a null.
        /// If there is no valid cache entry or an invalid one, we start the plugin as normally, return an active plugin even if the requested claim is not available, and write a cache entry.
        /// </summary>
        /// <param name="result">plugin discovery result</param>
        /// <param name="requestedOperationClaim">The requested operation claim</param>
        /// <param name="requestKey">plugin request key</param>
        /// <param name="packageSourceRepository">package source repository</param>
        /// <param name="serviceIndex">service index</param>
        /// <param name="cancellationToken">cancellation token</param>
        /// <returns>A plugin creation result, null if the requested plugin cannot handle the given operation claim</returns>
        private async Task <Tuple <bool, PluginCreationResult> > TryCreatePluginAsync(
            PluginDiscoveryResult result,
            OperationClaim requestedOperationClaim,
            PluginRequestKey requestKey,
            string packageSourceRepository,
            JObject serviceIndex,
            CancellationToken cancellationToken)
        {
            PluginCreationResult pluginCreationResult = null;
            var cacheEntry = new PluginCacheEntry(_pluginsCacheDirectory.Value, result.PluginFile.Path, requestKey.PackageSourceRepository);

            return(await ConcurrencyUtilities.ExecuteWithFileLockedAsync(
                       cacheEntry.CacheFileName,
                       action : async lockedToken =>
            {
                if (cacheEntry.OperationClaims == null || cacheEntry.OperationClaims.Contains(requestedOperationClaim))
                {
                    if (result.PluginFile.State.Value == PluginFileState.Valid)
                    {
                        var plugin = await _pluginFactory.GetOrCreateAsync(
                            result.PluginFile.Path,
                            PluginConstants.PluginArguments,
                            new RequestHandlers(),
                            _connectionOptions,
                            cancellationToken);

                        var utilities = await PerformOneTimePluginInitializationAsync(plugin, cancellationToken);

                        // We still make the GetOperationClaims call even if we have the operation claims cached. This is a way to self-update the cache.
                        var operationClaims = await _pluginOperationClaims.GetOrAdd(
                            requestKey,
                            key => new Lazy <Task <IReadOnlyList <OperationClaim> > >(() =>
                                                                                      GetPluginOperationClaimsAsync(
                                                                                          plugin,
                                                                                          packageSourceRepository,
                                                                                          serviceIndex,
                                                                                          cancellationToken))).Value;

                        if (!EqualityUtility.SequenceEqualWithNullCheck(operationClaims, cacheEntry.OperationClaims))
                        {
                            cacheEntry.OperationClaims = operationClaims;
                            await cacheEntry.UpdateCacheFileAsync();
                        }

                        pluginCreationResult = new PluginCreationResult(
                            plugin,
                            utilities.Value,
                            operationClaims);
                    }
                    else
                    {
                        pluginCreationResult = new PluginCreationResult(result.Message);
                    }
                }
                return new Tuple <bool, PluginCreationResult>(pluginCreationResult != null, pluginCreationResult);
            },
                       token : cancellationToken
                       ));
        }
        public IActionResult Update(OperationClaim operation)
        {
            var result = _operationService.Update(operation);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
        public IActionResult Delete(OperationClaim operationClaim)
        {
            var result = _operationClaimService.Delete(operationClaim);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
Exemplo n.º 6
0
        public ActionResult Add(OperationClaim operationClaim)
        {
            var result = _operationClaimService.Add(operationClaim);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
Exemplo n.º 7
0
        public IResult Add(OperationClaim operationClaim)
        {
            var result = _operationClaimDal.GetAll(o => o.Name == operationClaim.Name);

            if (result.Count != 0)
            {
                return(new ErrorResult(Messages.OperationClaimAlreadyExist));
            }
            _operationClaimDal.Add(operationClaim);
            return(new SuccessResult(Messages.AddedOperationClaim));
        }
Exemplo n.º 8
0
            public async Task <IResult> Handle(UpdateOperationClaimCommand request, CancellationToken cancellationToken)
            {
                var claimToUpdate = new OperationClaim
                {
                    Id   = request.Id,
                    Name = request.ClaimName
                };
                await _operationClaimDal.UpdateAsync(claimToUpdate);

                return(new SuccessResult(Messages.OperationClaimUpdated));
            }
        //[ValidationAspect(typeof(OperationClaimValidator))]
        public IResult Add(OperationClaim entity)
        {
            var result = BusinessRules.Run(CheckOperationClaimCount());

            if (result != null)
            {
                return(result);
            }
            _userDal.Add(entity);
            return(new SuccessResult(Messages.Added));
        }
        public IActionResult GetAll(OperationClaim operationClaim)
        {
            var result = _operationClaimService.GetAll();

            if (result.Success == true)
            {
                return(Ok(result));
            }

            return(BadRequest(result));
        }
        public IResult Update(OperationClaim operationClaim)
        {
            IResult result = BusinessRules.Run(CheckIfOperationClaimAlreadyExist(operationClaim));

            if (result != null)
            {
                return(result);
            }
            _operationClaimDal.Update(operationClaim);
            return(new SuccessResult(Messages.OperationClaimUpdated));
        }
        public IResult Delete(OperationClaim operationClaim)
        {
            IResult result = BusinessRules.Run();

            if (result != null)
            {
                return(result);
            }

            _operationClaimDal.Delete(operationClaim);
            return(new SuccessResult());
        }
Exemplo n.º 13
0
        public void InvalidParameters_ThrowValidationException()
        {
            OperationClaim operationClaim = new OperationClaim
            {
                Id   = 1,
                Name = "O"
            };

            var result = _validator.TestValidate(operationClaim);

            result.ShouldHaveValidationErrorFor("Name");
        }
Exemplo n.º 14
0
        public void Update_UpdateOperationClaim_ReturnTrueResult()
        {
            IOperationClaimService service        = new OperationClaimManager(_mockOperationClaimDal.Object);
            OperationClaim         operationClaim = new OperationClaim
            {
                Id   = 1,
                Name = It.IsAny <string>()
            };

            var result = service.Update(operationClaim);

            Assert.IsTrue(result.Success);
        }
        private IResult CheckIfOperationClaimAlreadyExist(OperationClaim operationClaim)
        {
            var result = _operationClaimDal.Get(o => o.OperationClaimName == operationClaim.OperationClaimName);

            if (result != null)
            {
                if (result.OperationClaimID != operationClaim.OperationClaimID)
                {
                    return(new ErrorResult(Messages.OperationClaimAlreadyExist));
                }
            }
            return(new SuccessResult());
        }
        public IResult Update(OperationClaim operationClaim)
        {
            IResult result = BusinessRule.Run
                             (
                CheckIfClaimExist(operationClaim.Id)
                             );

            if (result != null)
            {
                return(result);
            }
            _operationClaimDal.Update(operationClaim);
            return(new SuccessResult(OperationClaimMessages.Updated));
        }
        public IResult Update(OperationClaim operationClaim)
        {
            IResult result = BusinessRules.Run(
                CheckIfOperationClaimNameIsExists(operationClaim.Name)
                );

            if (result != null)
            {
                return(result);
            }

            _operationClaimDal.Update(operationClaim);
            return(new SuccessResult());
        }
        public IResult Add(OperationClaim operationClaim)
        {
            IResult result = BusinessRule.Run
                             (
                CheckIfClaimAlreadyExist(operationClaim.Name)
                             );

            if (result != null)
            {
                return(result);
            }

            _operationClaimDal.Add(operationClaim);
            return(new SuccessResult());
        }
            public async Task <IResult> Handle(CreateGroupClaimCommand request, CancellationToken cancellationToken)
            {
                if (IsClaimExists(request.ClaimName))
                {
                    return(new ErrorResult(Messages.OperationClaimExists));
                }

                var operationClaim = new OperationClaim
                {
                    Name = request.ClaimName
                };
                await _operationClaimDal.AddAsync(operationClaim);

                return(new SuccessResult(Messages.OperationClaimAdded));
            }
            public async Task <IResult> Handle(CreateOperationClaimCommand request, CancellationToken cancellationToken)
            {
                if (IsClaimExists(request.ClaimName))
                {
                    return(new ErrorResult(Messages.OperationClaimExists));
                }

                var operationClaim = new OperationClaim
                {
                    Name = request.ClaimName
                };

                _operationClaimRepository.Add(operationClaim);
                await _operationClaimRepository.SaveChangesAsync();

                return(new SuccessResult(Messages.Added));
            }
Exemplo n.º 21
0
        public async Task <IResult> Register(User user)
        {
            await _userDal.AddAsync(user);

            var defaultOperationClaim = await _operationClaimDal.GetAsync(o => o.Name == "user");

            if (defaultOperationClaim is null)
            {
                defaultOperationClaim = new OperationClaim
                {
                    Name = "user"
                };
                await _operationClaimDal.AddAsync(defaultOperationClaim);
            }

            await _userOperationClaimDal.AddAsync(new UserOperationClaim
            {
                UserId           = user.Id,
                OperationClaimId = defaultOperationClaim.Id
            });

            return(new SuccessResult(Messages.UserCreated));
        }
Exemplo n.º 22
0
 public IResult Update(OperationClaim operationClaim)
 {
     _operationClaimDal.Update(operationClaim);
     return(new SuccessResult(Messages.Successful));
 }
 //[ValidationAspect(typeof(OperationClaimValidator))]
 public IResult Update(OperationClaim entity)
 {
     _userDal.Update(entity);
     return(new SuccessResult(Messages.ItemUpdated));
 }
Exemplo n.º 24
0
        /// <summary>
        /// Creates a plugin from the given pluginDiscoveryResult.
        /// This plugin's operations will be source agnostic ones (Authentication)
        /// </summary>
        /// <param name="pluginDiscoveryResult">plugin discovery result</param>
        /// <param name="requestedOperationClaim">The requested operation claim</param>
        /// <param name="cancellationToken">cancellation token</param>
        /// <returns>A plugin creation result, null if the requested plugin cannot handle the given operation claim</returns>
        public Task <Tuple <bool, PluginCreationResult> > TryGetSourceAgnosticPluginAsync(PluginDiscoveryResult pluginDiscoveryResult, OperationClaim requestedOperationClaim, CancellationToken cancellationToken)
        {
            if (pluginDiscoveryResult == null)
            {
                throw new ArgumentNullException(nameof(pluginDiscoveryResult));
            }

            return(TryCreatePluginAsync(
                       pluginDiscoveryResult,
                       requestedOperationClaim,
                       new PluginRequestKey(pluginDiscoveryResult.PluginFile.Path, "Source-Agnostic"),
                       packageSourceRepository: null,
                       serviceIndex: null,
                       cancellationToken: cancellationToken));
        }
        public IResult Delete(OperationClaim operationClaim)
        {
            _operationClaimDal.Delete(operationClaim);

            return(new SuccessResult(Messages.OperationClaimDeleted));
        }
        public IResult Add(OperationClaim operationClaim)
        {
            _operationClaimDal.Add(operationClaim);

            return(new SuccessResult(Messages.OperationClaimAdded));
        }
Exemplo n.º 27
0
        /// <summary>
        /// Creates a plugin from the discovered plugin.
        /// We firstly check the cache for the operation claims for the given request key.
        /// If there is a valid cache entry, and it does contain the requested operation claim, then we start the plugin, and if need be update the cache value itself.
        /// If there is a valid cache entry, and it does NOT contain the requested operation claim, then we return a null.
        /// If there is no valid cache entry or an invalid one, we start the plugin as normally, return an active plugin even if the requested claim is not available, and write a cache entry.
        /// </summary>
        /// <param name="result">plugin discovery result</param>
        /// <param name="requestedOperationClaim">The requested operation claim</param>
        /// <param name="requestKey">plugin request key</param>
        /// <param name="packageSourceRepository">package source repository</param>
        /// <param name="serviceIndex">service index</param>
        /// <param name="cancellationToken">cancellation token</param>
        /// <returns>A plugin creation result, null if the requested plugin cannot handle the given operation claim</returns>
        private async Task <Tuple <bool, PluginCreationResult> > TryCreatePluginAsync(
            PluginDiscoveryResult result,
            OperationClaim requestedOperationClaim,
            PluginRequestKey requestKey,
            string packageSourceRepository,
            JObject serviceIndex,
            CancellationToken cancellationToken)
        {
            // This is a non cancellable task.
            // We should only honor cancellation requests we can recover from.
            // Once we have reached this part of the code, we do the plugin initialization
            // handshake, operation claims, and shut down set up.
            // If either one of these tasks fails then the plugin itself is not usable for the rest of the process.
            // We could consider handling each of this operations more cleverly,
            // but simplicity and readability is prioritized
            cancellationToken = CancellationToken.None;
            PluginCreationResult pluginCreationResult = null;
            var cacheEntry = new PluginCacheEntry(_pluginsCacheDirectoryPath.Value, result.PluginFile.Path, requestKey.PackageSourceRepository);

            ConcurrencyUtilities.ExecuteWithFileLocked(cacheEntry.CacheFileName, cacheEntry.LoadFromFile);

            if (cacheEntry.OperationClaims == null || cacheEntry.OperationClaims.Contains(requestedOperationClaim))
            {
                try
                {
                    if (result.PluginFile.State.Value == PluginFileState.Valid)
                    {
                        var plugin = await _pluginFactory.GetOrCreateAsync(
                            result.PluginFile.Path,
                            PluginConstants.PluginArguments,
                            new RequestHandlers(),
                            _connectionOptions,
                            cancellationToken);

                        var utilities = await PerformOneTimePluginInitializationAsync(plugin, cancellationToken);

                        // We still make the GetOperationClaims call even if we have the operation claims cached. This is a way to self-update the cache.
                        var operationClaims = await _pluginOperationClaims.GetOrAdd(
                            requestKey,
                            key => new Lazy <Task <IReadOnlyList <OperationClaim> > >(() =>
                                                                                      GetPluginOperationClaimsAsync(
                                                                                          plugin,
                                                                                          packageSourceRepository,
                                                                                          serviceIndex,
                                                                                          cancellationToken))).Value;

                        if (!EqualityUtility.SequenceEqualWithNullCheck(operationClaims, cacheEntry.OperationClaims))
                        {
                            cacheEntry.OperationClaims = operationClaims;

                            await utilities.Value.DoOncePerPluginLifetimeAsync(
                                nameof(PluginCacheEntry),
                                () => ConcurrencyUtilities.ExecuteWithFileLockedAsync(
                                    cacheEntry.CacheFileName,
                                    action: async lockedToken =>
                            {
                                await cacheEntry.UpdateCacheFileAsync();

                                return(Task.FromResult <object>(null));
                            },
                                    token: cancellationToken),
                                cancellationToken);
                        }

                        pluginCreationResult = new PluginCreationResult(
                            plugin,
                            utilities.Value,
                            operationClaims);
                    }
                    else
                    {
                        pluginCreationResult = new PluginCreationResult(result.Message);
                    }
                }
                catch (Exception e)
                {
                    pluginCreationResult = new PluginCreationResult(
                        string.Format(CultureInfo.CurrentCulture,
                                      Strings.Plugin_ProblemStartingPlugin,
                                      result.PluginFile.Path,
                                      e.Message),
                        e);
                }
            }

            return(new Tuple <bool, PluginCreationResult>(pluginCreationResult != null, pluginCreationResult));
        }
 public IResult Add(OperationClaim operationClaim)
 {
     _operationClaimDal.Add(operationClaim);
     return(new SuccessResult());
 }
Exemplo n.º 29
0
 public IResult Update(OperationClaim entity)
 {
     _operationClaimDal.Update(entity);
     return(new SuccessResult(Messages.UpdateSuccess));
 }
Exemplo n.º 30
0
 public IResult Add(OperationClaim entity)
 {
     _operationClaimDal.Add(entity);
     return(new SuccessResult(Messages.AddedSuccess));
 }