User.cs 10 KB

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