User.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /// <summary>
  55. /// Returns the folder containing the item.
  56. /// If the item is a folder, it returns the folder itself
  57. /// </summary>
  58. /// <value>The containing folder path.</value>
  59. [IgnoreDataMember]
  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. [IgnoreDataMember]
  72. public override bool IsOwnedItem
  73. {
  74. get
  75. {
  76. return false;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets the root folder.
  81. /// </summary>
  82. /// <value>The root folder.</value>
  83. [IgnoreDataMember]
  84. public Folder RootFolder
  85. {
  86. get
  87. {
  88. return LibraryManager.GetUserRootFolder();
  89. }
  90. }
  91. /// <summary>
  92. /// Gets or sets the last login date.
  93. /// </summary>
  94. /// <value>The last login date.</value>
  95. public DateTime? LastLoginDate { get; set; }
  96. /// <summary>
  97. /// Gets or sets the last activity date.
  98. /// </summary>
  99. /// <value>The last activity date.</value>
  100. public DateTime? LastActivityDate { get; set; }
  101. private UserConfiguration _config;
  102. private readonly object _configSyncLock = new object();
  103. [IgnoreDataMember]
  104. public UserConfiguration Configuration
  105. {
  106. get
  107. {
  108. if (_config == null)
  109. {
  110. lock (_configSyncLock)
  111. {
  112. if (_config == null)
  113. {
  114. _config = UserManager.GetUserConfiguration(this);
  115. }
  116. }
  117. }
  118. return _config;
  119. }
  120. set { _config = value; }
  121. }
  122. private UserPolicy _policy;
  123. private readonly object _policySyncLock = new object();
  124. [IgnoreDataMember]
  125. public UserPolicy Policy
  126. {
  127. get
  128. {
  129. if (_policy == null)
  130. {
  131. lock (_policySyncLock)
  132. {
  133. if (_policy == null)
  134. {
  135. _policy = UserManager.GetUserPolicy(this);
  136. }
  137. }
  138. }
  139. return _policy;
  140. }
  141. set { _policy = value; }
  142. }
  143. /// <summary>
  144. /// Renames the user.
  145. /// </summary>
  146. /// <param name="newName">The new name.</param>
  147. /// <returns>Task.</returns>
  148. /// <exception cref="System.ArgumentNullException"></exception>
  149. public Task Rename(string newName)
  150. {
  151. if (string.IsNullOrEmpty(newName))
  152. {
  153. throw new ArgumentNullException("newName");
  154. }
  155. // If only the casing is changing, leave the file system alone
  156. if (!UsesIdForConfigurationPath && !string.Equals(newName, Name, StringComparison.OrdinalIgnoreCase))
  157. {
  158. UsesIdForConfigurationPath = true;
  159. // Move configuration
  160. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  161. var oldConfigurationDirectory = ConfigurationDirectoryPath;
  162. // Exceptions will be thrown if these paths already exist
  163. if (Directory.Exists(newConfigDirectory))
  164. {
  165. FileSystem.DeleteDirectory(newConfigDirectory, true);
  166. }
  167. if (Directory.Exists(oldConfigurationDirectory))
  168. {
  169. Directory.Move(oldConfigurationDirectory, newConfigDirectory);
  170. }
  171. else
  172. {
  173. Directory.CreateDirectory(newConfigDirectory);
  174. }
  175. }
  176. Name = newName;
  177. return RefreshMetadata(new MetadataRefreshOptions(new DirectoryService())
  178. {
  179. ReplaceAllMetadata = true,
  180. ImageRefreshMode = ImageRefreshMode.FullRefresh,
  181. MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
  182. ForceSave = true
  183. }, CancellationToken.None);
  184. }
  185. public override Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  186. {
  187. return UserManager.UpdateUser(this);
  188. }
  189. /// <summary>
  190. /// Gets the path to the user's configuration directory
  191. /// </summary>
  192. /// <value>The configuration directory path.</value>
  193. [IgnoreDataMember]
  194. public string ConfigurationDirectoryPath
  195. {
  196. get
  197. {
  198. return GetConfigurationDirectoryPath(Name);
  199. }
  200. }
  201. /// <summary>
  202. /// Gets the configuration directory path.
  203. /// </summary>
  204. /// <param name="username">The username.</param>
  205. /// <returns>System.String.</returns>
  206. private string GetConfigurationDirectoryPath(string username)
  207. {
  208. var parentPath = ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath;
  209. // Legacy
  210. if (!UsesIdForConfigurationPath)
  211. {
  212. if (string.IsNullOrEmpty(username))
  213. {
  214. throw new ArgumentNullException("username");
  215. }
  216. var safeFolderName = FileSystem.GetValidFilename(username);
  217. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, safeFolderName);
  218. }
  219. return System.IO.Path.Combine(parentPath, Id.ToString("N"));
  220. }
  221. public bool IsParentalScheduleAllowed()
  222. {
  223. return IsParentalScheduleAllowed(DateTime.UtcNow);
  224. }
  225. public bool IsParentalScheduleAllowed(DateTime date)
  226. {
  227. var schedules = Policy.AccessSchedules;
  228. if (schedules.Length == 0)
  229. {
  230. return true;
  231. }
  232. return schedules.Any(i => IsParentalScheduleAllowed(i, date));
  233. }
  234. private bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
  235. {
  236. if (date.Kind != DateTimeKind.Utc)
  237. {
  238. throw new ArgumentException("Utc date expected");
  239. }
  240. var localTime = date.ToLocalTime();
  241. return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek) &&
  242. IsWithinTime(schedule, localTime);
  243. }
  244. private bool IsWithinTime(AccessSchedule schedule, DateTime localTime)
  245. {
  246. var hour = localTime.TimeOfDay.TotalHours;
  247. return hour >= schedule.StartHour && hour <= schedule.EndHour;
  248. }
  249. public bool IsFolderGrouped(Guid id)
  250. {
  251. var config = Configuration;
  252. if (config.ExcludeFoldersFromGrouping != null)
  253. {
  254. return !config.ExcludeFoldersFromGrouping.Select(i => new Guid(i)).Contains(id);
  255. }
  256. return config.GroupedFolders.Select(i => new Guid(i)).Contains(id);
  257. }
  258. [IgnoreDataMember]
  259. public override bool SupportsPeople
  260. {
  261. get
  262. {
  263. return false;
  264. }
  265. }
  266. }
  267. }