User.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text.Json.Serialization;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Configuration;
  10. using MediaBrowser.Model.Users;
  11. namespace MediaBrowser.Controller.Entities
  12. {
  13. /// <summary>
  14. /// Class User
  15. /// </summary>
  16. public class User : BaseItem
  17. {
  18. public static IUserManager UserManager { get; set; }
  19. /// <summary>
  20. /// Gets or sets the password.
  21. /// </summary>
  22. /// <value>The password.</value>
  23. public string Password { get; set; }
  24. public string EasyPassword { get; set; }
  25. // Strictly to remove JsonIgnore
  26. public override ItemImageInfo[] ImageInfos
  27. {
  28. get => base.ImageInfos;
  29. set => base.ImageInfos = value;
  30. }
  31. /// <summary>
  32. /// Gets or sets the path.
  33. /// </summary>
  34. /// <value>The path.</value>
  35. [JsonIgnore]
  36. public override string Path
  37. {
  38. get => ConfigurationDirectoryPath;
  39. set => base.Path = value;
  40. }
  41. private string _name;
  42. /// <summary>
  43. /// Gets or sets the name.
  44. /// </summary>
  45. /// <value>The name.</value>
  46. public override string Name
  47. {
  48. get => _name;
  49. set
  50. {
  51. _name = value;
  52. // lazy load this again
  53. SortName = null;
  54. }
  55. }
  56. /// <summary>
  57. /// Returns the folder containing the item.
  58. /// If the item is a folder, it returns the folder itself
  59. /// </summary>
  60. /// <value>The containing folder path.</value>
  61. [JsonIgnore]
  62. public override string ContainingFolderPath => Path;
  63. /// <summary>
  64. /// Gets the root folder.
  65. /// </summary>
  66. /// <value>The root folder.</value>
  67. [JsonIgnore]
  68. public Folder RootFolder => LibraryManager.GetUserRootFolder();
  69. /// <summary>
  70. /// Gets or sets the last login date.
  71. /// </summary>
  72. /// <value>The last login date.</value>
  73. public DateTime? LastLoginDate { get; set; }
  74. /// <summary>
  75. /// Gets or sets the last activity date.
  76. /// </summary>
  77. /// <value>The last activity date.</value>
  78. public DateTime? LastActivityDate { get; set; }
  79. private volatile UserConfiguration _config;
  80. private readonly object _configSyncLock = new object();
  81. [JsonIgnore]
  82. public UserConfiguration Configuration
  83. {
  84. get
  85. {
  86. if (_config == null)
  87. {
  88. lock (_configSyncLock)
  89. {
  90. if (_config == null)
  91. {
  92. _config = UserManager.GetUserConfiguration(this);
  93. }
  94. }
  95. }
  96. return _config;
  97. }
  98. set => _config = value;
  99. }
  100. private volatile UserPolicy _policy;
  101. private readonly object _policySyncLock = new object();
  102. [JsonIgnore]
  103. public UserPolicy Policy
  104. {
  105. get
  106. {
  107. if (_policy == null)
  108. {
  109. lock (_policySyncLock)
  110. {
  111. if (_policy == null)
  112. {
  113. _policy = UserManager.GetUserPolicy(this);
  114. }
  115. }
  116. }
  117. return _policy;
  118. }
  119. set => _policy = value;
  120. }
  121. /// <summary>
  122. /// Renames the user.
  123. /// </summary>
  124. /// <param name="newName">The new name.</param>
  125. /// <returns>Task.</returns>
  126. /// <exception cref="ArgumentNullException"></exception>
  127. public Task Rename(string newName)
  128. {
  129. if (string.IsNullOrWhiteSpace(newName))
  130. {
  131. throw new ArgumentException("Username can't be empty", nameof(newName));
  132. }
  133. Name = newName;
  134. return RefreshMetadata(
  135. new MetadataRefreshOptions(new DirectoryService(FileSystem))
  136. {
  137. ReplaceAllMetadata = true,
  138. ImageRefreshMode = MetadataRefreshMode.FullRefresh,
  139. MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
  140. ForceSave = true
  141. },
  142. CancellationToken.None);
  143. }
  144. public override void UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  145. {
  146. UserManager.UpdateUser(this);
  147. }
  148. /// <summary>
  149. /// Gets the path to the user's configuration directory
  150. /// </summary>
  151. /// <value>The configuration directory path.</value>
  152. [JsonIgnore]
  153. public string ConfigurationDirectoryPath => GetConfigurationDirectoryPath(Name);
  154. public override double GetDefaultPrimaryImageAspectRatio()
  155. {
  156. return 1;
  157. }
  158. /// <summary>
  159. /// Gets the configuration directory path.
  160. /// </summary>
  161. /// <param name="username">The username.</param>
  162. /// <returns>System.String.</returns>
  163. private string GetConfigurationDirectoryPath(string username)
  164. {
  165. var parentPath = ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath;
  166. // TODO: Remove idPath and just use usernamePath for future releases
  167. var usernamePath = System.IO.Path.Combine(parentPath, username);
  168. var idPath = System.IO.Path.Combine(parentPath, Id.ToString("N", CultureInfo.InvariantCulture));
  169. if (!Directory.Exists(usernamePath) && Directory.Exists(idPath))
  170. {
  171. Directory.Move(idPath, usernamePath);
  172. }
  173. return usernamePath;
  174. }
  175. public bool IsParentalScheduleAllowed()
  176. {
  177. return IsParentalScheduleAllowed(DateTime.UtcNow);
  178. }
  179. public bool IsParentalScheduleAllowed(DateTime date)
  180. {
  181. var schedules = Policy.AccessSchedules;
  182. if (schedules.Length == 0)
  183. {
  184. return true;
  185. }
  186. foreach (var i in schedules)
  187. {
  188. if (IsParentalScheduleAllowed(i, date))
  189. {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. private bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
  196. {
  197. if (date.Kind != DateTimeKind.Utc)
  198. {
  199. throw new ArgumentException("Utc date expected");
  200. }
  201. var localTime = date.ToLocalTime();
  202. return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek) &&
  203. IsWithinTime(schedule, localTime);
  204. }
  205. private bool IsWithinTime(AccessSchedule schedule, DateTime localTime)
  206. {
  207. var hour = localTime.TimeOfDay.TotalHours;
  208. return hour >= schedule.StartHour && hour <= schedule.EndHour;
  209. }
  210. public bool IsFolderGrouped(Guid id)
  211. {
  212. foreach (var i in Configuration.GroupedFolders)
  213. {
  214. if (new Guid(i) == id)
  215. {
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. [JsonIgnore]
  222. public override bool SupportsPeople => false;
  223. public long InternalId { get; set; }
  224. }
  225. }