User.cs 12 KB

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