User.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Configuration;
  5. using MediaBrowser.Model.Connect;
  6. using MediaBrowser.Model.Serialization;
  7. using System;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Controller.Entities
  13. {
  14. /// <summary>
  15. /// Class User
  16. /// </summary>
  17. public class User : BaseItem
  18. {
  19. public static IUserManager UserManager { get; set; }
  20. public static IXmlSerializer XmlSerializer { get; set; }
  21. /// <summary>
  22. /// From now on all user paths will be Id-based.
  23. /// This is for backwards compatibility.
  24. /// </summary>
  25. public bool UsesIdForConfigurationPath { get; set; }
  26. /// <summary>
  27. /// Gets or sets the password.
  28. /// </summary>
  29. /// <value>The password.</value>
  30. public string Password { get; set; }
  31. public string LocalPassword { get; set; }
  32. public string ConnectUserName { get; set; }
  33. public string ConnectUserId { get; set; }
  34. public UserLinkType ConnectLinkType { get; set; }
  35. public string ConnectAccessKey { get; set; }
  36. /// <summary>
  37. /// Gets or sets the path.
  38. /// </summary>
  39. /// <value>The path.</value>
  40. [IgnoreDataMember]
  41. public override string Path
  42. {
  43. get
  44. {
  45. // Return this so that metadata providers will look in here
  46. return ConfigurationDirectoryPath;
  47. }
  48. set
  49. {
  50. base.Path = value;
  51. }
  52. }
  53. /// <summary>
  54. /// Returns the folder containing the item.
  55. /// If the item is a folder, it returns the folder itself
  56. /// </summary>
  57. /// <value>The containing folder path.</value>
  58. public override string ContainingFolderPath
  59. {
  60. get
  61. {
  62. return Path;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets a value indicating whether this instance is owned item.
  67. /// </summary>
  68. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  69. public override bool IsOwnedItem
  70. {
  71. get
  72. {
  73. return false;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets the root folder.
  78. /// </summary>
  79. /// <value>The root folder.</value>
  80. [IgnoreDataMember]
  81. public Folder RootFolder
  82. {
  83. get
  84. {
  85. return LibraryManager.GetUserRootFolder();
  86. }
  87. }
  88. /// <summary>
  89. /// Gets or sets the last login date.
  90. /// </summary>
  91. /// <value>The last login date.</value>
  92. public DateTime? LastLoginDate { get; set; }
  93. /// <summary>
  94. /// Gets or sets the last activity date.
  95. /// </summary>
  96. /// <value>The last activity date.</value>
  97. public DateTime? LastActivityDate { get; set; }
  98. /// <summary>
  99. /// The _configuration
  100. /// </summary>
  101. private UserConfiguration _configuration;
  102. /// <summary>
  103. /// The _configuration initialized
  104. /// </summary>
  105. private bool _configurationInitialized;
  106. /// <summary>
  107. /// The _configuration sync lock
  108. /// </summary>
  109. private object _configurationSyncLock = new object();
  110. /// <summary>
  111. /// Gets the user's configuration
  112. /// </summary>
  113. /// <value>The configuration.</value>
  114. [IgnoreDataMember]
  115. public UserConfiguration Configuration
  116. {
  117. get
  118. {
  119. // Lazy load
  120. LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationInitialized, ref _configurationSyncLock, () => (UserConfiguration)ConfigurationHelper.GetXmlConfiguration(typeof(UserConfiguration), ConfigurationFilePath, XmlSerializer));
  121. return _configuration;
  122. }
  123. private set
  124. {
  125. _configuration = value;
  126. _configurationInitialized = value != null;
  127. }
  128. }
  129. /// <summary>
  130. /// Renames the user.
  131. /// </summary>
  132. /// <param name="newName">The new name.</param>
  133. /// <returns>Task.</returns>
  134. /// <exception cref="System.ArgumentNullException"></exception>
  135. public Task Rename(string newName)
  136. {
  137. if (string.IsNullOrEmpty(newName))
  138. {
  139. throw new ArgumentNullException("newName");
  140. }
  141. // If only the casing is changing, leave the file system alone
  142. if (!UsesIdForConfigurationPath && !newName.Equals(Name, StringComparison.OrdinalIgnoreCase))
  143. {
  144. UsesIdForConfigurationPath = true;
  145. // Move configuration
  146. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  147. var oldConfigurationDirectory = ConfigurationDirectoryPath;
  148. // Exceptions will be thrown if these paths already exist
  149. if (Directory.Exists(newConfigDirectory))
  150. {
  151. Directory.Delete(newConfigDirectory, true);
  152. }
  153. if (Directory.Exists(oldConfigurationDirectory))
  154. {
  155. Directory.Move(oldConfigurationDirectory, newConfigDirectory);
  156. }
  157. else
  158. {
  159. Directory.CreateDirectory(newConfigDirectory);
  160. }
  161. }
  162. Name = newName;
  163. return RefreshMetadata(new MetadataRefreshOptions
  164. {
  165. ReplaceAllMetadata = true,
  166. ImageRefreshMode = ImageRefreshMode.FullRefresh,
  167. MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
  168. ForceSave = true
  169. }, CancellationToken.None);
  170. }
  171. public override Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  172. {
  173. return UserManager.UpdateUser(this);
  174. }
  175. /// <summary>
  176. /// Gets the path to the user's configuration directory
  177. /// </summary>
  178. /// <value>The configuration directory path.</value>
  179. [IgnoreDataMember]
  180. private string ConfigurationDirectoryPath
  181. {
  182. get
  183. {
  184. return GetConfigurationDirectoryPath(Name);
  185. }
  186. }
  187. /// <summary>
  188. /// Gets the configuration directory path.
  189. /// </summary>
  190. /// <param name="username">The username.</param>
  191. /// <returns>System.String.</returns>
  192. private string GetConfigurationDirectoryPath(string username)
  193. {
  194. if (string.IsNullOrEmpty(username))
  195. {
  196. throw new ArgumentNullException("username");
  197. }
  198. var parentPath = ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath;
  199. // Legacy
  200. if (!UsesIdForConfigurationPath)
  201. {
  202. var safeFolderName = FileSystem.GetValidFilename(username);
  203. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, safeFolderName);
  204. }
  205. return System.IO.Path.Combine(parentPath, Id.ToString("N"));
  206. }
  207. /// <summary>
  208. /// Gets the path to the user's configuration file
  209. /// </summary>
  210. /// <value>The configuration file path.</value>
  211. [IgnoreDataMember]
  212. public string ConfigurationFilePath
  213. {
  214. get
  215. {
  216. return System.IO.Path.Combine(ConfigurationDirectoryPath, "config.xml");
  217. }
  218. }
  219. /// <summary>
  220. /// Updates the configuration.
  221. /// </summary>
  222. /// <param name="config">The config.</param>
  223. /// <exception cref="System.ArgumentNullException">config</exception>
  224. public void UpdateConfiguration(UserConfiguration config)
  225. {
  226. if (config == null)
  227. {
  228. throw new ArgumentNullException("config");
  229. }
  230. Configuration = config;
  231. UserManager.UpdateConfiguration(this, Configuration);
  232. }
  233. }
  234. }