SimplePermissionDependency.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Threading.Tasks;
  2. namespace Abp.Authorization
  3. {
  4. /// <summary>
  5. /// Most simple implementation of <see cref="IPermissionDependency"/>.
  6. /// It checks one or more permissions if they are granted.
  7. /// </summary>
  8. public class SimplePermissionDependency : IPermissionDependency
  9. {
  10. /// <summary>
  11. /// A list of permissions to be checked if they are granted.
  12. /// </summary>
  13. public string[] Permissions { get; set; }
  14. /// <summary>
  15. /// If this property is set to true, all of the <see cref="Permissions"/> must be granted.
  16. /// If it's false, at least one of the <see cref="Permissions"/> must be granted.
  17. /// Default: false.
  18. /// </summary>
  19. public bool RequiresAll { get; set; }
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="SimplePermissionDependency"/> class.
  22. /// </summary>
  23. /// <param name="permissions">The permissions.</param>
  24. public SimplePermissionDependency(params string[] permissions)
  25. {
  26. Permissions = permissions;
  27. }
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="SimplePermissionDependency"/> class.
  30. /// </summary>
  31. /// <param name="requiresAll">
  32. /// If this is set to true, all of the <see cref="Permissions"/> must be granted.
  33. /// If it's false, at least one of the <see cref="Permissions"/> must be granted.
  34. /// </param>
  35. /// <param name="permissions">The permissions.</param>
  36. public SimplePermissionDependency(bool requiresAll, params string[] permissions)
  37. : this(permissions)
  38. {
  39. RequiresAll = requiresAll;
  40. }
  41. /// <inheritdoc/>
  42. public Task<bool> IsSatisfiedAsync(IPermissionDependencyContext context)
  43. {
  44. return context.User != null
  45. ? context.PermissionChecker.IsGrantedAsync(context.User, RequiresAll, Permissions)
  46. : context.PermissionChecker.IsGrantedAsync(RequiresAll, Permissions);
  47. }
  48. }
  49. }