User.cs 8.9 KB

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