User.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. ReplaceAllMetadata = true,
  194. ImageRefreshMode = ImageRefreshMode.FullRefresh,
  195. MetadataRefreshMode = MetadataRefreshMode.FullRefresh
  196. }, CancellationToken.None);
  197. }
  198. /// <summary>
  199. /// Gets the path to the user's configuration directory
  200. /// </summary>
  201. /// <value>The configuration directory path.</value>
  202. [IgnoreDataMember]
  203. private string ConfigurationDirectoryPath
  204. {
  205. get
  206. {
  207. return GetConfigurationDirectoryPath(Name);
  208. }
  209. }
  210. /// <summary>
  211. /// Gets the configuration directory path.
  212. /// </summary>
  213. /// <param name="username">The username.</param>
  214. /// <returns>System.String.</returns>
  215. private string GetConfigurationDirectoryPath(string username)
  216. {
  217. if (string.IsNullOrEmpty(username))
  218. {
  219. throw new ArgumentNullException("username");
  220. }
  221. var safeFolderName = FileSystem.GetValidFilename(username);
  222. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, safeFolderName);
  223. }
  224. /// <summary>
  225. /// Gets the path to the user's configuration file
  226. /// </summary>
  227. /// <value>The configuration file path.</value>
  228. [IgnoreDataMember]
  229. public string ConfigurationFilePath
  230. {
  231. get
  232. {
  233. return System.IO.Path.Combine(ConfigurationDirectoryPath, "config.xml");
  234. }
  235. }
  236. /// <summary>
  237. /// Saves the current configuration to the file system
  238. /// </summary>
  239. public void SaveConfiguration(IXmlSerializer serializer)
  240. {
  241. var xmlPath = ConfigurationFilePath;
  242. Directory.CreateDirectory(System.IO.Path.GetDirectoryName(xmlPath));
  243. serializer.SerializeToFile(Configuration, xmlPath);
  244. }
  245. /// <summary>
  246. /// Updates the configuration.
  247. /// </summary>
  248. /// <param name="config">The config.</param>
  249. /// <param name="serializer">The serializer.</param>
  250. /// <exception cref="System.ArgumentNullException">config</exception>
  251. public void UpdateConfiguration(UserConfiguration config, IXmlSerializer serializer)
  252. {
  253. if (config == null)
  254. {
  255. throw new ArgumentNullException("config");
  256. }
  257. var customLibraryChanged = config.UseCustomLibrary != Configuration.UseCustomLibrary;
  258. Configuration = config;
  259. SaveConfiguration(serializer);
  260. // Force these to be lazy loaded again
  261. if (customLibraryChanged)
  262. {
  263. RootFolder = null;
  264. }
  265. }
  266. }
  267. }