User.cs 9.5 KB

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