User.cs 9.3 KB

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