User.cs 8.6 KB

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