User.cs 9.4 KB

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