User.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. if (value == null)
  116. {
  117. _configurationInitialized = false;
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// Renames the user.
  123. /// </summary>
  124. /// <param name="newName">The new name.</param>
  125. /// <returns>Task.</returns>
  126. /// <exception cref="System.ArgumentNullException"></exception>
  127. public Task Rename(string newName)
  128. {
  129. if (string.IsNullOrEmpty(newName))
  130. {
  131. throw new ArgumentNullException();
  132. }
  133. // If only the casing is changing, leave the file system alone
  134. if (!newName.Equals(Name, StringComparison.OrdinalIgnoreCase))
  135. {
  136. // Move configuration
  137. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  138. var oldConfigurationDirectory = ConfigurationDirectoryPath;
  139. // Exceptions will be thrown if these paths already exist
  140. if (Directory.Exists(newConfigDirectory))
  141. {
  142. Directory.Delete(newConfigDirectory, true);
  143. }
  144. if (Directory.Exists(oldConfigurationDirectory))
  145. {
  146. Directory.Move(oldConfigurationDirectory, newConfigDirectory);
  147. }
  148. else
  149. {
  150. Directory.CreateDirectory(newConfigDirectory);
  151. }
  152. }
  153. Name = newName;
  154. return RefreshMetadata(new MetadataRefreshOptions
  155. {
  156. ReplaceAllMetadata = true,
  157. ImageRefreshMode = ImageRefreshMode.FullRefresh,
  158. MetadataRefreshMode = MetadataRefreshMode.FullRefresh
  159. }, CancellationToken.None);
  160. }
  161. public override Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  162. {
  163. return UserManager.UpdateUser(this);
  164. }
  165. /// <summary>
  166. /// Gets the path to the user's configuration directory
  167. /// </summary>
  168. /// <value>The configuration directory path.</value>
  169. [IgnoreDataMember]
  170. private string ConfigurationDirectoryPath
  171. {
  172. get
  173. {
  174. return GetConfigurationDirectoryPath(Name);
  175. }
  176. }
  177. /// <summary>
  178. /// Gets the configuration directory path.
  179. /// </summary>
  180. /// <param name="username">The username.</param>
  181. /// <returns>System.String.</returns>
  182. private string GetConfigurationDirectoryPath(string username)
  183. {
  184. if (string.IsNullOrEmpty(username))
  185. {
  186. throw new ArgumentNullException("username");
  187. }
  188. var safeFolderName = FileSystem.GetValidFilename(username);
  189. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, safeFolderName);
  190. }
  191. /// <summary>
  192. /// Gets the path to the user's configuration file
  193. /// </summary>
  194. /// <value>The configuration file path.</value>
  195. [IgnoreDataMember]
  196. public string ConfigurationFilePath
  197. {
  198. get
  199. {
  200. return System.IO.Path.Combine(ConfigurationDirectoryPath, "config.xml");
  201. }
  202. }
  203. /// <summary>
  204. /// Saves the current configuration to the file system
  205. /// </summary>
  206. public void SaveConfiguration()
  207. {
  208. var xmlPath = ConfigurationFilePath;
  209. Directory.CreateDirectory(System.IO.Path.GetDirectoryName(xmlPath));
  210. XmlSerializer.SerializeToFile(Configuration, xmlPath);
  211. }
  212. /// <summary>
  213. /// Updates the configuration.
  214. /// </summary>
  215. /// <param name="config">The config.</param>
  216. /// <param name="serializer">The serializer.</param>
  217. /// <exception cref="System.ArgumentNullException">config</exception>
  218. public void UpdateConfiguration(UserConfiguration config, IXmlSerializer serializer)
  219. {
  220. if (config == null)
  221. {
  222. throw new ArgumentNullException("config");
  223. }
  224. Configuration = config;
  225. SaveConfiguration();
  226. }
  227. }
  228. }