User.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. private UserPolicy _policy;
  132. private readonly object _policySyncLock = new object();
  133. [IgnoreDataMember]
  134. public UserPolicy Policy
  135. {
  136. get
  137. {
  138. if (_policy == null)
  139. {
  140. lock (_policySyncLock)
  141. {
  142. if (_policy == null)
  143. {
  144. _policy = UserManager.GetUserPolicy(this);
  145. }
  146. }
  147. }
  148. return _policy;
  149. }
  150. set { _policy = value; }
  151. }
  152. /// <summary>
  153. /// Renames the user.
  154. /// </summary>
  155. /// <param name="newName">The new name.</param>
  156. /// <returns>Task.</returns>
  157. /// <exception cref="System.ArgumentNullException"></exception>
  158. public Task Rename(string newName)
  159. {
  160. if (string.IsNullOrEmpty(newName))
  161. {
  162. throw new ArgumentNullException("newName");
  163. }
  164. // If only the casing is changing, leave the file system alone
  165. if (!UsesIdForConfigurationPath && !newName.Equals(Name, StringComparison.OrdinalIgnoreCase))
  166. {
  167. UsesIdForConfigurationPath = true;
  168. // Move configuration
  169. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  170. var oldConfigurationDirectory = ConfigurationDirectoryPath;
  171. // Exceptions will be thrown if these paths already exist
  172. if (Directory.Exists(newConfigDirectory))
  173. {
  174. Directory.Delete(newConfigDirectory, true);
  175. }
  176. if (Directory.Exists(oldConfigurationDirectory))
  177. {
  178. Directory.Move(oldConfigurationDirectory, newConfigDirectory);
  179. }
  180. else
  181. {
  182. Directory.CreateDirectory(newConfigDirectory);
  183. }
  184. }
  185. Name = newName;
  186. return RefreshMetadata(new MetadataRefreshOptions(new DirectoryService())
  187. {
  188. ReplaceAllMetadata = true,
  189. ImageRefreshMode = ImageRefreshMode.FullRefresh,
  190. MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
  191. ForceSave = true
  192. }, CancellationToken.None);
  193. }
  194. public override Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  195. {
  196. return UserManager.UpdateUser(this);
  197. }
  198. /// <summary>
  199. /// Gets the path to the user's configuration directory
  200. /// </summary>
  201. /// <value>The configuration directory path.</value>
  202. [IgnoreDataMember]
  203. public string ConfigurationDirectoryPath
  204. {
  205. get
  206. {
  207. return GetConfigurationDirectoryPath(Name);
  208. }
  209. }
  210. /// <summary>
  211. /// Gets the configuration directory path.
  212. /// </summary>
  213. /// <param name="username">The username.</param>
  214. /// <returns>System.String.</returns>
  215. private string GetConfigurationDirectoryPath(string username)
  216. {
  217. if (string.IsNullOrEmpty(username))
  218. {
  219. throw new ArgumentNullException("username");
  220. }
  221. var parentPath = ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath;
  222. // Legacy
  223. if (!UsesIdForConfigurationPath)
  224. {
  225. var safeFolderName = FileSystem.GetValidFilename(username);
  226. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, safeFolderName);
  227. }
  228. return System.IO.Path.Combine(parentPath, Id.ToString("N"));
  229. }
  230. /// <summary>
  231. /// Gets the path to the user's configuration file
  232. /// </summary>
  233. /// <value>The configuration file path.</value>
  234. [IgnoreDataMember]
  235. public string ConfigurationFilePath
  236. {
  237. get
  238. {
  239. return System.IO.Path.Combine(ConfigurationDirectoryPath, "config.xml");
  240. }
  241. }
  242. /// <summary>
  243. /// Updates the configuration.
  244. /// </summary>
  245. /// <param name="config">The config.</param>
  246. /// <exception cref="System.ArgumentNullException">config</exception>
  247. public void UpdateConfiguration(UserConfiguration config)
  248. {
  249. if (config == null)
  250. {
  251. throw new ArgumentNullException("config");
  252. }
  253. Configuration = config;
  254. UserManager.UpdateConfiguration(this, Configuration);
  255. }
  256. public bool IsParentalScheduleAllowed()
  257. {
  258. return IsParentalScheduleAllowed(DateTime.UtcNow);
  259. }
  260. public bool IsParentalScheduleAllowed(DateTime date)
  261. {
  262. var schedules = Configuration.AccessSchedules;
  263. if (schedules.Length == 0)
  264. {
  265. return true;
  266. }
  267. return schedules.Any(i => IsParentalScheduleAllowed(i, date));
  268. }
  269. private bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
  270. {
  271. if (date.Kind != DateTimeKind.Utc)
  272. {
  273. throw new ArgumentException("Utc date expected");
  274. }
  275. var localTime = date.ToLocalTime();
  276. return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek) &&
  277. IsWithinTime(schedule, localTime);
  278. }
  279. private bool IsWithinTime(AccessSchedule schedule, DateTime localTime)
  280. {
  281. var hour = localTime.TimeOfDay.TotalHours;
  282. return hour >= schedule.StartHour && hour <= schedule.EndHour;
  283. }
  284. }
  285. }