User.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. /// From now on all user paths will be Id-based.
  22. /// This is for backwards compatibility.
  23. /// </summary>
  24. public bool UsesIdForConfigurationPath { get; set; }
  25. /// <summary>
  26. /// Gets or sets the password.
  27. /// </summary>
  28. /// <value>The password.</value>
  29. public string Password { get; set; }
  30. public string LocalPassword { get; set; }
  31. public string ConnectUserName { get; set; }
  32. public string ConnectUserId { get; set; }
  33. public string ConnectAccessKey { get; set; }
  34. /// <summary>
  35. /// Gets or sets the path.
  36. /// </summary>
  37. /// <value>The path.</value>
  38. [IgnoreDataMember]
  39. public override string Path
  40. {
  41. get
  42. {
  43. // Return this so that metadata providers will look in here
  44. return ConfigurationDirectoryPath;
  45. }
  46. set
  47. {
  48. base.Path = value;
  49. }
  50. }
  51. /// <summary>
  52. /// Returns the folder containing the item.
  53. /// If the item is a folder, it returns the folder itself
  54. /// </summary>
  55. /// <value>The containing folder path.</value>
  56. public override string ContainingFolderPath
  57. {
  58. get
  59. {
  60. return Path;
  61. }
  62. }
  63. /// <summary>
  64. /// Gets a value indicating whether this instance is owned item.
  65. /// </summary>
  66. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  67. public override bool IsOwnedItem
  68. {
  69. get
  70. {
  71. return false;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets the root folder.
  76. /// </summary>
  77. /// <value>The root folder.</value>
  78. [IgnoreDataMember]
  79. public Folder RootFolder
  80. {
  81. get
  82. {
  83. return LibraryManager.GetUserRootFolder();
  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. _configurationInitialized = value != null;
  125. }
  126. }
  127. /// <summary>
  128. /// Renames the user.
  129. /// </summary>
  130. /// <param name="newName">The new name.</param>
  131. /// <returns>Task.</returns>
  132. /// <exception cref="System.ArgumentNullException"></exception>
  133. public Task Rename(string newName)
  134. {
  135. if (string.IsNullOrEmpty(newName))
  136. {
  137. throw new ArgumentNullException("newName");
  138. }
  139. // If only the casing is changing, leave the file system alone
  140. if (!UsesIdForConfigurationPath && !newName.Equals(Name, StringComparison.OrdinalIgnoreCase))
  141. {
  142. UsesIdForConfigurationPath = true;
  143. // Move configuration
  144. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  145. var oldConfigurationDirectory = ConfigurationDirectoryPath;
  146. // Exceptions will be thrown if these paths already exist
  147. if (Directory.Exists(newConfigDirectory))
  148. {
  149. Directory.Delete(newConfigDirectory, true);
  150. }
  151. if (Directory.Exists(oldConfigurationDirectory))
  152. {
  153. Directory.Move(oldConfigurationDirectory, newConfigDirectory);
  154. }
  155. else
  156. {
  157. Directory.CreateDirectory(newConfigDirectory);
  158. }
  159. }
  160. Name = newName;
  161. return RefreshMetadata(new MetadataRefreshOptions
  162. {
  163. ReplaceAllMetadata = true,
  164. ImageRefreshMode = ImageRefreshMode.FullRefresh,
  165. MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
  166. ForceSave = true
  167. }, CancellationToken.None);
  168. }
  169. public override Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  170. {
  171. return UserManager.UpdateUser(this);
  172. }
  173. /// <summary>
  174. /// Gets the path to the user's configuration directory
  175. /// </summary>
  176. /// <value>The configuration directory path.</value>
  177. [IgnoreDataMember]
  178. private string ConfigurationDirectoryPath
  179. {
  180. get
  181. {
  182. return GetConfigurationDirectoryPath(Name);
  183. }
  184. }
  185. /// <summary>
  186. /// Gets the configuration directory path.
  187. /// </summary>
  188. /// <param name="username">The username.</param>
  189. /// <returns>System.String.</returns>
  190. private string GetConfigurationDirectoryPath(string username)
  191. {
  192. if (string.IsNullOrEmpty(username))
  193. {
  194. throw new ArgumentNullException("username");
  195. }
  196. var parentPath = ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath;
  197. // Legacy
  198. if (!UsesIdForConfigurationPath)
  199. {
  200. var safeFolderName = FileSystem.GetValidFilename(username);
  201. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, safeFolderName);
  202. }
  203. return System.IO.Path.Combine(parentPath, Id.ToString("N"));
  204. }
  205. /// <summary>
  206. /// Gets the path to the user's configuration file
  207. /// </summary>
  208. /// <value>The configuration file path.</value>
  209. [IgnoreDataMember]
  210. public string ConfigurationFilePath
  211. {
  212. get
  213. {
  214. return System.IO.Path.Combine(ConfigurationDirectoryPath, "config.xml");
  215. }
  216. }
  217. /// <summary>
  218. /// Updates the configuration.
  219. /// </summary>
  220. /// <param name="config">The config.</param>
  221. /// <exception cref="System.ArgumentNullException">config</exception>
  222. public void UpdateConfiguration(UserConfiguration config)
  223. {
  224. if (config == null)
  225. {
  226. throw new ArgumentNullException("config");
  227. }
  228. Configuration = config;
  229. UserManager.UpdateConfiguration(this, Configuration);
  230. }
  231. }
  232. }