User.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Configuration;
  5. using MediaBrowser.Model.Connect;
  6. using MediaBrowser.Model.Serialization;
  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. /// <summary>
  100. /// The _configuration
  101. /// </summary>
  102. private UserConfiguration _configuration;
  103. /// <summary>
  104. /// The _configuration initialized
  105. /// </summary>
  106. private bool _configurationInitialized;
  107. /// <summary>
  108. /// The _configuration sync lock
  109. /// </summary>
  110. private object _configurationSyncLock = new object();
  111. /// <summary>
  112. /// Gets the user's configuration
  113. /// </summary>
  114. /// <value>The configuration.</value>
  115. [IgnoreDataMember]
  116. public UserConfiguration Configuration
  117. {
  118. get
  119. {
  120. // Lazy load
  121. LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationInitialized, ref _configurationSyncLock, () => (UserConfiguration)ConfigurationHelper.GetXmlConfiguration(typeof(UserConfiguration), ConfigurationFilePath, XmlSerializer));
  122. return _configuration;
  123. }
  124. private set
  125. {
  126. _configuration = value;
  127. _configurationInitialized = value != null;
  128. }
  129. }
  130. /// <summary>
  131. /// Renames the user.
  132. /// </summary>
  133. /// <param name="newName">The new name.</param>
  134. /// <returns>Task.</returns>
  135. /// <exception cref="System.ArgumentNullException"></exception>
  136. public Task Rename(string newName)
  137. {
  138. if (string.IsNullOrEmpty(newName))
  139. {
  140. throw new ArgumentNullException("newName");
  141. }
  142. // If only the casing is changing, leave the file system alone
  143. if (!UsesIdForConfigurationPath && !newName.Equals(Name, StringComparison.OrdinalIgnoreCase))
  144. {
  145. UsesIdForConfigurationPath = true;
  146. // Move configuration
  147. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  148. var oldConfigurationDirectory = ConfigurationDirectoryPath;
  149. // Exceptions will be thrown if these paths already exist
  150. if (Directory.Exists(newConfigDirectory))
  151. {
  152. Directory.Delete(newConfigDirectory, true);
  153. }
  154. if (Directory.Exists(oldConfigurationDirectory))
  155. {
  156. Directory.Move(oldConfigurationDirectory, newConfigDirectory);
  157. }
  158. else
  159. {
  160. Directory.CreateDirectory(newConfigDirectory);
  161. }
  162. }
  163. Name = newName;
  164. return RefreshMetadata(new MetadataRefreshOptions
  165. {
  166. ReplaceAllMetadata = true,
  167. ImageRefreshMode = ImageRefreshMode.FullRefresh,
  168. MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
  169. ForceSave = true
  170. }, CancellationToken.None);
  171. }
  172. public override Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  173. {
  174. return UserManager.UpdateUser(this);
  175. }
  176. /// <summary>
  177. /// Gets the path to the user's configuration directory
  178. /// </summary>
  179. /// <value>The configuration directory path.</value>
  180. [IgnoreDataMember]
  181. private string ConfigurationDirectoryPath
  182. {
  183. get
  184. {
  185. return GetConfigurationDirectoryPath(Name);
  186. }
  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. if (string.IsNullOrEmpty(username))
  196. {
  197. throw new ArgumentNullException("username");
  198. }
  199. var parentPath = ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath;
  200. // Legacy
  201. if (!UsesIdForConfigurationPath)
  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. /// <summary>
  209. /// Gets the path to the user's configuration file
  210. /// </summary>
  211. /// <value>The configuration file path.</value>
  212. [IgnoreDataMember]
  213. public string ConfigurationFilePath
  214. {
  215. get
  216. {
  217. return System.IO.Path.Combine(ConfigurationDirectoryPath, "config.xml");
  218. }
  219. }
  220. /// <summary>
  221. /// Updates the configuration.
  222. /// </summary>
  223. /// <param name="config">The config.</param>
  224. /// <exception cref="System.ArgumentNullException">config</exception>
  225. public void UpdateConfiguration(UserConfiguration config)
  226. {
  227. if (config == null)
  228. {
  229. throw new ArgumentNullException("config");
  230. }
  231. Configuration = config;
  232. UserManager.UpdateConfiguration(this, Configuration);
  233. }
  234. public bool IsParentalScheduleAllowed()
  235. {
  236. return IsParentalScheduleAllowed(DateTime.UtcNow);
  237. }
  238. public bool IsParentalScheduleAllowed(DateTime date)
  239. {
  240. var schedules = Configuration.AccessSchedules;
  241. if (schedules.Length == 0)
  242. {
  243. return true;
  244. }
  245. return schedules.Any(i => IsParentalScheduleAllowed(i, date));
  246. }
  247. private bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
  248. {
  249. if (date.Kind != DateTimeKind.Utc)
  250. {
  251. throw new ArgumentException("Utc date expected");
  252. }
  253. var localTime = date.ToLocalTime();
  254. return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek) &&
  255. IsWithinTime(schedule, localTime);
  256. }
  257. private bool IsWithinTime(AccessSchedule schedule, DateTime localTime)
  258. {
  259. var hour = localTime.TimeOfDay.TotalHours;
  260. return hour >= schedule.StartHour && hour <= schedule.EndHour;
  261. }
  262. }
  263. }