User.cs 9.7 KB

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