User.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 ConnectUserName { get; set; }
  31. public string ConnectUserId { get; set; }
  32. public UserLinkType? ConnectLinkType { get; set; }
  33. public string ConnectAccessKey { get; set; }
  34. /// <summary>
  35. /// Gets or sets the path.
  36. /// </summary>
  37. /// <value>The path.</value>
  38. [IgnoreDataMember]
  39. public override string Path
  40. {
  41. get
  42. {
  43. // Return this so that metadata providers will look in here
  44. return ConfigurationDirectoryPath;
  45. }
  46. set
  47. {
  48. base.Path = value;
  49. }
  50. }
  51. private string _name;
  52. /// <summary>
  53. /// Gets or sets the name.
  54. /// </summary>
  55. /// <value>The name.</value>
  56. public override string Name
  57. {
  58. get
  59. {
  60. return _name;
  61. }
  62. set
  63. {
  64. _name = value;
  65. // lazy load this again
  66. SortName = null;
  67. }
  68. }
  69. /// <summary>
  70. /// Returns the folder containing the item.
  71. /// If the item is a folder, it returns the folder itself
  72. /// </summary>
  73. /// <value>The containing folder path.</value>
  74. [IgnoreDataMember]
  75. public override string ContainingFolderPath
  76. {
  77. get
  78. {
  79. return Path;
  80. }
  81. }
  82. /// <summary>
  83. /// Gets a value indicating whether this instance is owned item.
  84. /// </summary>
  85. /// <value><c>true</c> if this instance is owned item; otherwise, <c>false</c>.</value>
  86. [IgnoreDataMember]
  87. public override bool IsOwnedItem
  88. {
  89. get
  90. {
  91. return false;
  92. }
  93. }
  94. /// <summary>
  95. /// Gets the root folder.
  96. /// </summary>
  97. /// <value>The root folder.</value>
  98. [IgnoreDataMember]
  99. public Folder RootFolder
  100. {
  101. get
  102. {
  103. return LibraryManager.GetUserRootFolder();
  104. }
  105. }
  106. /// <summary>
  107. /// Gets or sets the last login date.
  108. /// </summary>
  109. /// <value>The last login date.</value>
  110. public DateTime? LastLoginDate { get; set; }
  111. /// <summary>
  112. /// Gets or sets the last activity date.
  113. /// </summary>
  114. /// <value>The last activity date.</value>
  115. public DateTime? LastActivityDate { get; set; }
  116. private volatile UserConfiguration _config;
  117. private readonly object _configSyncLock = new object();
  118. [IgnoreDataMember]
  119. public UserConfiguration Configuration
  120. {
  121. get
  122. {
  123. if (_config == null)
  124. {
  125. lock (_configSyncLock)
  126. {
  127. if (_config == null)
  128. {
  129. _config = UserManager.GetUserConfiguration(this);
  130. }
  131. }
  132. }
  133. return _config;
  134. }
  135. set { _config = value; }
  136. }
  137. private volatile UserPolicy _policy;
  138. private readonly object _policySyncLock = new object();
  139. [IgnoreDataMember]
  140. public UserPolicy Policy
  141. {
  142. get
  143. {
  144. if (_policy == null)
  145. {
  146. lock (_policySyncLock)
  147. {
  148. if (_policy == null)
  149. {
  150. _policy = UserManager.GetUserPolicy(this);
  151. }
  152. }
  153. }
  154. return _policy;
  155. }
  156. set { _policy = value; }
  157. }
  158. /// <summary>
  159. /// Renames the user.
  160. /// </summary>
  161. /// <param name="newName">The new name.</param>
  162. /// <returns>Task.</returns>
  163. /// <exception cref="System.ArgumentNullException"></exception>
  164. public Task Rename(string newName)
  165. {
  166. if (string.IsNullOrEmpty(newName))
  167. {
  168. throw new ArgumentNullException("newName");
  169. }
  170. // If only the casing is changing, leave the file system alone
  171. if (!UsesIdForConfigurationPath && !string.Equals(newName, Name, StringComparison.OrdinalIgnoreCase))
  172. {
  173. UsesIdForConfigurationPath = true;
  174. // Move configuration
  175. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  176. var oldConfigurationDirectory = ConfigurationDirectoryPath;
  177. // Exceptions will be thrown if these paths already exist
  178. if (FileSystem.DirectoryExists(newConfigDirectory))
  179. {
  180. FileSystem.DeleteDirectory(newConfigDirectory, true);
  181. }
  182. if (FileSystem.DirectoryExists(oldConfigurationDirectory))
  183. {
  184. FileSystem.MoveDirectory(oldConfigurationDirectory, newConfigDirectory);
  185. }
  186. else
  187. {
  188. FileSystem.CreateDirectory(newConfigDirectory);
  189. }
  190. }
  191. Name = newName;
  192. return RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(Logger, FileSystem))
  193. {
  194. ReplaceAllMetadata = true,
  195. ImageRefreshMode = ImageRefreshMode.FullRefresh,
  196. MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
  197. ForceSave = true
  198. }, CancellationToken.None);
  199. }
  200. public override Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  201. {
  202. UserManager.UpdateUser(this);
  203. return Task.FromResult(true);
  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. foreach (var i in schedules)
  249. {
  250. if (IsParentalScheduleAllowed(i, date))
  251. {
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. private bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
  258. {
  259. if (date.Kind != DateTimeKind.Utc)
  260. {
  261. throw new ArgumentException("Utc date expected");
  262. }
  263. var localTime = date.ToLocalTime();
  264. return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek) &&
  265. IsWithinTime(schedule, localTime);
  266. }
  267. private bool IsWithinTime(AccessSchedule schedule, DateTime localTime)
  268. {
  269. var hour = localTime.TimeOfDay.TotalHours;
  270. return hour >= schedule.StartHour && hour <= schedule.EndHour;
  271. }
  272. public bool IsFolderGrouped(Guid id)
  273. {
  274. foreach (var i in Configuration.GroupedFolders)
  275. {
  276. if (new Guid(i) == id)
  277. {
  278. return true;
  279. }
  280. }
  281. return false;
  282. }
  283. [IgnoreDataMember]
  284. public override bool SupportsPeople
  285. {
  286. get
  287. {
  288. return false;
  289. }
  290. }
  291. }
  292. }