2
0

AuthenticatedAttribute.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 a value indicating whether or not to allow local unauthenticated access.
  12. /// </summary>
  13. /// <value><c>true</c> if [allow local]; otherwise, <c>false</c>.</value>
  14. public bool AllowLocal { get; set; }
  15. /// <summary>
  16. /// Gets or sets the roles.
  17. /// </summary>
  18. /// <value>The roles.</value>
  19. public string Roles { get; set; }
  20. /// <summary>
  21. /// Gets or sets a value indicating whether [escape parental control].
  22. /// </summary>
  23. /// <value><c>true</c> if [escape parental control]; otherwise, <c>false</c>.</value>
  24. public bool EscapeParentalControl { 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. AuthService.Authenticate(request, response, requestDto, this);
  34. }
  35. /// <summary>
  36. /// A new shallow copy of this filter is used on every request.
  37. /// </summary>
  38. /// <returns>IHasRequestFilter.</returns>
  39. public IHasRequestFilter Copy()
  40. {
  41. return this;
  42. }
  43. /// <summary>
  44. /// Order in which Request Filters are executed.
  45. /// &lt;0 Executed before global request filters
  46. /// &gt;0 Executed after global request filters
  47. /// </summary>
  48. /// <value>The priority.</value>
  49. public int Priority
  50. {
  51. get { return 0; }
  52. }
  53. public IEnumerable<string> GetRoles()
  54. {
  55. return (Roles ?? string.Empty).Split(',')
  56. .Where(i => !string.IsNullOrWhiteSpace(i));
  57. }
  58. }
  59. public interface IAuthenticated
  60. {
  61. bool EscapeParentalControl { get; }
  62. bool AllowLocal { get; }
  63. IEnumerable<string> GetRoles();
  64. }
  65. }