/// <summary> /// Checks if the policies are satisfied for the given action. /// </summary> /// <param name="context">The policies checking context.</param> /// <returns>The policies checking result.</returns> public async Task <IPolicyResult <TAction> > CheckPoliciesAsync(TPolicyContext context) { if (this.logger == null) { // Getting the logger lazilly at the first call. this.logger = this.serviceProvider.GetService <ILogger <AuthorizedActionChecker <TPolicyContext, TAction> > >(); } this.logger.LogDebug($"Checking the policies of the {typeof(TAction).FullName} action."); if (this.specificActionCheckers == null) { // Getting the specific actions checkers lazilly at the first call. this.specificActionCheckers = new List <IAuthorizedSpecificActionChecker <TPolicyContext, TAction> >(this.serviceProvider.GetServices <IAuthorizedSpecificActionChecker <TPolicyContext, TAction> >()); } // Returning the first specific action that checks all its policies. foreach (IAuthorizedSpecificActionChecker <TPolicyContext, TAction> specificActionChecker in this.specificActionCheckers) { IPolicyResult <TAction> result = await specificActionChecker.CheckPoliciesAsync(context).ConfigureAwait(false); if (result.Allowed) { return(result); } } return(NotAllowedResult <TAction> .Default); }
public async Task <IActionResult> ListerEnquetes() { IPolicyResult <IListerEnquetes> result = await this.listerEnquetesChecker.CheckPoliciesAsync(new VoidPolicyContext()).ConfigureAwait(false); if (result.Allowed) { ICollection <Enquete> enquetes = await result.Action.ExecuteAsync().ConfigureAwait(false); return(this.View(enquetes)); } return(this.View("Unauthorized")); }
public async Task <IActionResult> CloturerEnquete() { CloturerEnquetePolicyContext context = new CloturerEnquetePolicyContext { Utilisateur = Utilisateur.CreateAdmin(), Enquete = Enquete.Create() }; IPolicyResult <ICloturerEnquete> result = await this.cloturerEnqueteChecker.CheckPoliciesAsync(context).ConfigureAwait(false); if (result.Allowed) { result.Action.Execute(context.Enquete, context.Utilisateur); return(this.View(context.Enquete)); } return(this.View("Unauthorized")); }