User.cs 9.2 KB

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