User.cs 12 KB

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