AuthenticatedAttribute.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using ServiceStack.Web;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace MediaBrowser.Controller.Net
  6. {
  7. public class AuthenticatedAttribute : Attribute, IHasRequestFilter, IAuthenticationAttributes
  8. {
  9. public 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 ServiceStackServiceRequest(request);
  34. AuthService.Authenticate(serviceRequest, this);
  35. }
  36. /// <summary>
  37. /// A new shallow copy of this filter is used on every request.
  38. /// </summary>
  39. /// <returns>IHasRequestFilter.</returns>
  40. public IHasRequestFilter Copy()
  41. {
  42. return this;
  43. }
  44. /// <summary>
  45. /// Order in which Request Filters are executed.
  46. /// &lt;0 Executed before global request filters
  47. /// &gt;0 Executed after global request filters
  48. /// </summary>
  49. /// <value>The priority.</value>
  50. public int Priority
  51. {
  52. get { return 0; }
  53. }
  54. public IEnumerable<string> GetRoles()
  55. {
  56. return (Roles ?? string.Empty).Split(',')
  57. .Where(i => !string.IsNullOrWhiteSpace(i));
  58. }
  59. }
  60. public interface IAuthenticationAttributes
  61. {
  62. bool EscapeParentalControl { get; }
  63. bool AllowBeforeStartupWizard { get; }
  64. IEnumerable<string> GetRoles();
  65. }
  66. }