User.cs 9.2 KB

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