User.cs 11 KB

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