User.cs 9.3 KB

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