User.cs 12 KB

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