User.cs 9.4 KB

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