User.cs 7.4 KB

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