User.cs 7.8 KB

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