User.cs 9.7 KB

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