LoggedAttribute.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Session;
  4. using MediaBrowser.Model.Logging;
  5. using ServiceStack.Web;
  6. using System;
  7. namespace MediaBrowser.Controller.Net
  8. {
  9. public class LoggedAttribute : Attribute, IHasRequestFilter
  10. {
  11. public ILogger Logger { get; set; }
  12. public IUserManager UserManager { get; set; }
  13. public ISessionManager SessionManager { get; set; }
  14. public IAuthorizationContext AuthorizationContext { 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 serviceRequest = new ServiceStackServiceRequest(request);
  24. //This code is executed before the service
  25. var auth = AuthorizationContext.GetAuthorizationInfo(serviceRequest);
  26. if (auth != null)
  27. {
  28. User user = null;
  29. if (!string.IsNullOrWhiteSpace(auth.UserId))
  30. {
  31. var userId = auth.UserId;
  32. user = UserManager.GetUserById(userId);
  33. }
  34. string deviceId = auth.DeviceId;
  35. string device = auth.Device;
  36. string client = auth.Client;
  37. string version = auth.Version;
  38. if (!string.IsNullOrEmpty(client) && !string.IsNullOrEmpty(deviceId) && !string.IsNullOrEmpty(device) && !string.IsNullOrEmpty(version))
  39. {
  40. var remoteEndPoint = request.RemoteIp;
  41. SessionManager.LogSessionActivity(client, version, deviceId, device, remoteEndPoint, user);
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// A new shallow copy of this filter is used on every request.
  47. /// </summary>
  48. /// <returns>IHasRequestFilter.</returns>
  49. public IHasRequestFilter Copy()
  50. {
  51. return this;
  52. }
  53. /// <summary>
  54. /// Order in which Request Filters are executed.
  55. /// &lt;0 Executed before global request filters
  56. /// &gt;0 Executed after global request filters
  57. /// </summary>
  58. /// <value>The priority.</value>
  59. public int Priority
  60. {
  61. get { return 0; }
  62. }
  63. }
  64. }