User.cs 11 KB

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