AuthenticatedAttribute.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, IAuthenticated
  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. /// The request filter is executed before the service.
  22. /// </summary>
  23. /// <param name="request">The http request wrapper</param>
  24. /// <param name="response">The http response wrapper</param>
  25. /// <param name="requestDto">The request DTO</param>
  26. public void RequestFilter(IRequest request, IResponse response, object requestDto)
  27. {
  28. AuthService.Authenticate(request, response, requestDto, this);
  29. }
  30. /// <summary>
  31. /// A new shallow copy of this filter is used on every request.
  32. /// </summary>
  33. /// <returns>IHasRequestFilter.</returns>
  34. public IHasRequestFilter Copy()
  35. {
  36. return this;
  37. }
  38. /// <summary>
  39. /// Order in which Request Filters are executed.
  40. /// &lt;0 Executed before global request filters
  41. /// &gt;0 Executed after global request filters
  42. /// </summary>
  43. /// <value>The priority.</value>
  44. public int Priority
  45. {
  46. get { return 0; }
  47. }
  48. public IEnumerable<string> GetRoles()
  49. {
  50. return (Roles ?? string.Empty).Split(',')
  51. .Where(i => !string.IsNullOrWhiteSpace(i));
  52. }
  53. }
  54. public interface IAuthenticated
  55. {
  56. bool EscapeParentalControl { get; }
  57. IEnumerable<string> GetRoles();
  58. }
  59. }