LoggedAttribute.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. //This code is executed before the service
  24. var auth = AuthorizationContext.GetAuthorizationInfo(request);
  25. if (auth != null)
  26. {
  27. User user = null;
  28. if (!string.IsNullOrWhiteSpace(auth.UserId))
  29. {
  30. var userId = auth.UserId;
  31. user = UserManager.GetUserById(userId);
  32. }
  33. string deviceId = auth.DeviceId;
  34. string device = auth.Device;
  35. string client = auth.Client;
  36. string version = auth.Version;
  37. if (!string.IsNullOrEmpty(client) && !string.IsNullOrEmpty(deviceId) && !string.IsNullOrEmpty(device) && !string.IsNullOrEmpty(version))
  38. {
  39. var remoteEndPoint = request.RemoteIp;
  40. SessionManager.LogSessionActivity(client, version, deviceId, device, remoteEndPoint, user);
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// A new shallow copy of this filter is used on every request.
  46. /// </summary>
  47. /// <returns>IHasRequestFilter.</returns>
  48. public IHasRequestFilter Copy()
  49. {
  50. return this;
  51. }
  52. /// <summary>
  53. /// Order in which Request Filters are executed.
  54. /// &lt;0 Executed before global request filters
  55. /// &gt;0 Executed after global request filters
  56. /// </summary>
  57. /// <value>The priority.</value>
  58. public int Priority
  59. {
  60. get { return 0; }
  61. }
  62. }
  63. }