User.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.IO;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Configuration;
  5. using MediaBrowser.Model.Serialization;
  6. using System;
  7. using System.IO;
  8. using System.Runtime.Serialization;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  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. /// The _root folder path
  22. /// </summary>
  23. private string _rootFolderPath;
  24. /// <summary>
  25. /// Gets the root folder path.
  26. /// </summary>
  27. /// <value>The root folder path.</value>
  28. [IgnoreDataMember]
  29. public string RootFolderPath
  30. {
  31. get
  32. {
  33. if (_rootFolderPath == null)
  34. {
  35. if (Configuration.UseCustomLibrary)
  36. {
  37. _rootFolderPath = GetRootFolderPath(Name);
  38. if (!Directory.Exists(_rootFolderPath))
  39. {
  40. Directory.CreateDirectory(_rootFolderPath);
  41. }
  42. }
  43. else
  44. {
  45. _rootFolderPath = ConfigurationManager.ApplicationPaths.DefaultUserViewsPath;
  46. }
  47. }
  48. return _rootFolderPath;
  49. }
  50. }
  51. /// <summary>
  52. /// Gets the root folder path based on a given username
  53. /// </summary>
  54. /// <param name="username">The username.</param>
  55. /// <returns>System.String.</returns>
  56. private string GetRootFolderPath(string username)
  57. {
  58. var safeFolderName = FileSystem.GetValidFilename(username);
  59. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.RootFolderPath, safeFolderName);
  60. }
  61. /// <summary>
  62. /// Gets or sets the password.
  63. /// </summary>
  64. /// <value>The password.</value>
  65. public string Password { get; set; }
  66. /// <summary>
  67. /// Gets or sets the path.
  68. /// </summary>
  69. /// <value>The path.</value>
  70. public override string Path
  71. {
  72. get
  73. {
  74. // Return this so that metadata providers will look in here
  75. return ConfigurationDirectoryPath;
  76. }
  77. set
  78. {
  79. base.Path = value;
  80. }
  81. }
  82. /// <summary>
  83. /// Ensure this has a value
  84. /// </summary>
  85. /// <value>The display type of the media.</value>
  86. public override string DisplayMediaType
  87. {
  88. get
  89. {
  90. return base.DisplayMediaType ?? GetType().Name;
  91. }
  92. set
  93. {
  94. base.DisplayMediaType = value;
  95. }
  96. }
  97. /// <summary>
  98. /// The _root folder
  99. /// </summary>
  100. private UserRootFolder _rootFolder;
  101. /// <summary>
  102. /// The _user root folder initialized
  103. /// </summary>
  104. private bool _userRootFolderInitialized;
  105. /// <summary>
  106. /// The _user root folder sync lock
  107. /// </summary>
  108. private object _userRootFolderSyncLock = new object();
  109. /// <summary>
  110. /// Gets the root folder.
  111. /// </summary>
  112. /// <value>The root folder.</value>
  113. [IgnoreDataMember]
  114. public UserRootFolder RootFolder
  115. {
  116. get
  117. {
  118. LazyInitializer.EnsureInitialized(ref _rootFolder, ref _userRootFolderInitialized, ref _userRootFolderSyncLock, () => LibraryManager.GetUserRootFolder(RootFolderPath));
  119. return _rootFolder;
  120. }
  121. private set
  122. {
  123. _rootFolder = value;
  124. if (_rootFolder == null)
  125. {
  126. _userRootFolderInitialized = false;
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Gets or sets the last login date.
  132. /// </summary>
  133. /// <value>The last login date.</value>
  134. public DateTime? LastLoginDate { get; set; }
  135. /// <summary>
  136. /// Gets or sets the last activity date.
  137. /// </summary>
  138. /// <value>The last activity date.</value>
  139. public DateTime? LastActivityDate { get; set; }
  140. /// <summary>
  141. /// The _configuration
  142. /// </summary>
  143. private UserConfiguration _configuration;
  144. /// <summary>
  145. /// The _configuration initialized
  146. /// </summary>
  147. private bool _configurationInitialized;
  148. /// <summary>
  149. /// The _configuration sync lock
  150. /// </summary>
  151. private object _configurationSyncLock = new object();
  152. /// <summary>
  153. /// Gets the user's configuration
  154. /// </summary>
  155. /// <value>The configuration.</value>
  156. [IgnoreDataMember]
  157. public UserConfiguration Configuration
  158. {
  159. get
  160. {
  161. // Lazy load
  162. LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationInitialized, ref _configurationSyncLock, () => (UserConfiguration)ConfigurationHelper.GetXmlConfiguration(typeof(UserConfiguration), ConfigurationFilePath, XmlSerializer));
  163. return _configuration;
  164. }
  165. private set
  166. {
  167. _configuration = value;
  168. if (value == null)
  169. {
  170. _configurationInitialized = false;
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// Gets the last date modified of the configuration
  176. /// </summary>
  177. /// <value>The configuration date last modified.</value>
  178. [IgnoreDataMember]
  179. public DateTime ConfigurationDateLastModified
  180. {
  181. get
  182. {
  183. // Ensure it's been lazy loaded
  184. var config = Configuration;
  185. return File.GetLastWriteTimeUtc(ConfigurationFilePath);
  186. }
  187. }
  188. /// <summary>
  189. /// Reloads the root media folder
  190. /// </summary>
  191. /// <param name="cancellationToken">The cancellation token.</param>
  192. /// <param name="progress">The progress.</param>
  193. /// <returns>Task.</returns>
  194. public async Task ValidateMediaLibrary(IProgress<double> progress, CancellationToken cancellationToken)
  195. {
  196. Logger.Info("Validating media library for {0}", Name);
  197. await RootFolder.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  198. cancellationToken.ThrowIfCancellationRequested();
  199. await RootFolder.ValidateChildren(progress, cancellationToken).ConfigureAwait(false);
  200. }
  201. /// <summary>
  202. /// Renames the user.
  203. /// </summary>
  204. /// <param name="newName">The new name.</param>
  205. /// <returns>Task.</returns>
  206. /// <exception cref="System.ArgumentNullException"></exception>
  207. public Task Rename(string newName)
  208. {
  209. if (string.IsNullOrEmpty(newName))
  210. {
  211. throw new ArgumentNullException();
  212. }
  213. // If only the casing is changing, leave the file system alone
  214. if (!newName.Equals(Name, StringComparison.OrdinalIgnoreCase))
  215. {
  216. // Move configuration
  217. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  218. // Exceptions will be thrown if these paths already exist
  219. if (Directory.Exists(newConfigDirectory))
  220. {
  221. Directory.Delete(newConfigDirectory, true);
  222. }
  223. Directory.Move(ConfigurationDirectoryPath, newConfigDirectory);
  224. var customLibraryPath = GetRootFolderPath(Name);
  225. // Move the root folder path if using a custom library
  226. if (Directory.Exists(customLibraryPath))
  227. {
  228. var newRootFolderPath = GetRootFolderPath(newName);
  229. if (Directory.Exists(newRootFolderPath))
  230. {
  231. Directory.Delete(newRootFolderPath, true);
  232. }
  233. Directory.Move(customLibraryPath, newRootFolderPath);
  234. }
  235. }
  236. Name = newName;
  237. // Force these to be lazy loaded again
  238. _configurationDirectoryPath = null;
  239. _rootFolderPath = null;
  240. RootFolder = null;
  241. // Kick off a task to validate the media library
  242. Task.Run(() => ValidateMediaLibrary(new Progress<double>(), CancellationToken.None));
  243. return RefreshMetadata(CancellationToken.None, forceSave: true, forceRefresh: true);
  244. }
  245. /// <summary>
  246. /// The _configuration directory path
  247. /// </summary>
  248. private string _configurationDirectoryPath;
  249. /// <summary>
  250. /// Gets the path to the user's configuration directory
  251. /// </summary>
  252. /// <value>The configuration directory path.</value>
  253. private string ConfigurationDirectoryPath
  254. {
  255. get
  256. {
  257. if (_configurationDirectoryPath == null)
  258. {
  259. _configurationDirectoryPath = GetConfigurationDirectoryPath(Name);
  260. if (!Directory.Exists(_configurationDirectoryPath))
  261. {
  262. Directory.CreateDirectory(_configurationDirectoryPath);
  263. }
  264. }
  265. return _configurationDirectoryPath;
  266. }
  267. }
  268. /// <summary>
  269. /// Gets the configuration directory path.
  270. /// </summary>
  271. /// <param name="username">The username.</param>
  272. /// <returns>System.String.</returns>
  273. private string GetConfigurationDirectoryPath(string username)
  274. {
  275. var safeFolderName = FileSystem.GetValidFilename(username);
  276. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, safeFolderName);
  277. }
  278. /// <summary>
  279. /// Gets the path to the user's configuration file
  280. /// </summary>
  281. /// <value>The configuration file path.</value>
  282. public string ConfigurationFilePath
  283. {
  284. get
  285. {
  286. return System.IO.Path.Combine(ConfigurationDirectoryPath, "config.xml");
  287. }
  288. }
  289. /// <summary>
  290. /// Saves the current configuration to the file system
  291. /// </summary>
  292. public void SaveConfiguration(IXmlSerializer serializer)
  293. {
  294. serializer.SerializeToFile(Configuration, ConfigurationFilePath);
  295. }
  296. /// <summary>
  297. /// Refresh metadata on us by execution our provider chain
  298. /// The item will be persisted if a change is made by a provider, or if it's new or changed.
  299. /// </summary>
  300. /// <param name="cancellationToken">The cancellation token.</param>
  301. /// <param name="forceSave">if set to <c>true</c> [is new item].</param>
  302. /// <param name="forceRefresh">if set to <c>true</c> [force].</param>
  303. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  304. /// <param name="resetResolveArgs">if set to <c>true</c> [reset resolve args].</param>
  305. /// <returns>true if a provider reports we changed</returns>
  306. public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
  307. {
  308. if (resetResolveArgs)
  309. {
  310. ResolveArgs = null;
  311. }
  312. var changed = await ProviderManager.ExecuteMetadataProviders(this, cancellationToken, forceRefresh, allowSlowProviders).ConfigureAwait(false);
  313. if (changed || forceSave)
  314. {
  315. cancellationToken.ThrowIfCancellationRequested();
  316. await UserManager.UpdateUser(this).ConfigureAwait(false);
  317. }
  318. return changed;
  319. }
  320. /// <summary>
  321. /// Updates the configuration.
  322. /// </summary>
  323. /// <param name="config">The config.</param>
  324. /// <param name="serializer">The serializer.</param>
  325. /// <exception cref="System.ArgumentNullException">config</exception>
  326. public void UpdateConfiguration(UserConfiguration config, IXmlSerializer serializer)
  327. {
  328. if (config == null)
  329. {
  330. throw new ArgumentNullException("config");
  331. }
  332. var customLibraryChanged = config.UseCustomLibrary != Configuration.UseCustomLibrary;
  333. Configuration = config;
  334. SaveConfiguration(serializer);
  335. // Force these to be lazy loaded again
  336. if (customLibraryChanged)
  337. {
  338. _rootFolderPath = null;
  339. RootFolder = null;
  340. }
  341. }
  342. }
  343. }