User.cs 13 KB

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