AuthenticatedAttribute.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using ServiceStack.Web;
  2. using System;
  3. using System.Linq;
  4. namespace MediaBrowser.Controller.Net
  5. {
  6. public class AuthenticatedAttribute : Attribute, IHasRequestFilter
  7. {
  8. public IAuthService AuthService { get; set; }
  9. /// <summary>
  10. /// Gets or sets a value indicating whether or not to allow local unauthenticated access.
  11. /// </summary>
  12. /// <value><c>true</c> if [allow local]; otherwise, <c>false</c>.</value>
  13. public bool AllowLocal { get; set; }
  14. public string Roles { get; set; }
  15. /// <summary>
  16. /// The request filter is executed before the service.
  17. /// </summary>
  18. /// <param name="request">The http request wrapper</param>
  19. /// <param name="response">The http response wrapper</param>
  20. /// <param name="requestDto">The request DTO</param>
  21. public void RequestFilter(IRequest request, IResponse response, object requestDto)
  22. {
  23. var roles = (Roles ?? string.Empty).Split(',')
  24. .Where(i => !string.IsNullOrWhiteSpace(i))
  25. .ToArray();
  26. AuthService.Authenticate(request, response, requestDto, AllowLocal, roles);
  27. }
  28. /// <summary>
  29. /// A new shallow copy of this filter is used on every request.
  30. /// </summary>
  31. /// <returns>IHasRequestFilter.</returns>
  32. public IHasRequestFilter Copy()
  33. {
  34. return 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. }
  47. }