AuthenticatedAttribute.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Model.Services;
  5. namespace MediaBrowser.Controller.Net
  6. {
  7. public class AuthenticatedAttribute : Attribute, IHasRequestFilter, IAuthenticationAttributes
  8. {
  9. public static IAuthService AuthService { get; set; }
  10. /// <summary>
  11. /// Gets or sets the roles.
  12. /// </summary>
  13. /// <value>The roles.</value>
  14. public string Roles { get; set; }
  15. /// <summary>
  16. /// Gets or sets a value indicating whether [escape parental control].
  17. /// </summary>
  18. /// <value><c>true</c> if [escape parental control]; otherwise, <c>false</c>.</value>
  19. public bool EscapeParentalControl { get; set; }
  20. /// <summary>
  21. /// Gets or sets a value indicating whether [allow before startup wizard].
  22. /// </summary>
  23. /// <value><c>true</c> if [allow before startup wizard]; otherwise, <c>false</c>.</value>
  24. public bool AllowBeforeStartupWizard { get; set; }
  25. /// <summary>
  26. /// The request filter is executed before the service.
  27. /// </summary>
  28. /// <param name="request">The http request wrapper</param>
  29. /// <param name="response">The http response wrapper</param>
  30. /// <param name="requestDto">The request DTO</param>
  31. public void RequestFilter(IRequest request, IResponse response, object requestDto)
  32. {
  33. var serviceRequest = new ServiceRequest(request);
  34. AuthService.Authenticate(serviceRequest, this);
  35. }
  36. /// <summary>
  37. /// Order in which Request Filters are executed.
  38. /// &lt;0 Executed before global request filters
  39. /// &gt;0 Executed after global request filters
  40. /// </summary>
  41. /// <value>The priority.</value>
  42. public int Priority
  43. {
  44. get { return 0; }
  45. }
  46. public IEnumerable<string> GetRoles()
  47. {
  48. return (Roles ?? string.Empty).Split(',')
  49. .Where(i => !string.IsNullOrWhiteSpace(i));
  50. }
  51. }
  52. public interface IAuthenticationAttributes
  53. {
  54. bool EscapeParentalControl { get; }
  55. bool AllowBeforeStartupWizard { get; }
  56. IEnumerable<string> GetRoles();
  57. }
  58. }