2
0

User.cs 11 KB

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