2
0

User.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 the root folder.
  97. /// </summary>
  98. /// <value>The root folder.</value>
  99. [IgnoreDataMember]
  100. public Folder RootFolder
  101. {
  102. get
  103. {
  104. return LibraryManager.GetUserRootFolder();
  105. }
  106. }
  107. /// <summary>
  108. /// Gets or sets the last login date.
  109. /// </summary>
  110. /// <value>The last login date.</value>
  111. public DateTime? LastLoginDate { get; set; }
  112. /// <summary>
  113. /// Gets or sets the last activity date.
  114. /// </summary>
  115. /// <value>The last activity date.</value>
  116. public DateTime? LastActivityDate { get; set; }
  117. private volatile UserConfiguration _config;
  118. private readonly object _configSyncLock = new object();
  119. [IgnoreDataMember]
  120. public UserConfiguration Configuration
  121. {
  122. get
  123. {
  124. if (_config == null)
  125. {
  126. lock (_configSyncLock)
  127. {
  128. if (_config == null)
  129. {
  130. _config = UserManager.GetUserConfiguration(this);
  131. }
  132. }
  133. }
  134. return _config;
  135. }
  136. set { _config = value; }
  137. }
  138. private volatile UserPolicy _policy;
  139. private readonly object _policySyncLock = new object();
  140. [IgnoreDataMember]
  141. public UserPolicy Policy
  142. {
  143. get
  144. {
  145. if (_policy == null)
  146. {
  147. lock (_policySyncLock)
  148. {
  149. if (_policy == null)
  150. {
  151. _policy = UserManager.GetUserPolicy(this);
  152. }
  153. }
  154. }
  155. return _policy;
  156. }
  157. set { _policy = value; }
  158. }
  159. /// <summary>
  160. /// Renames the user.
  161. /// </summary>
  162. /// <param name="newName">The new name.</param>
  163. /// <returns>Task.</returns>
  164. /// <exception cref="System.ArgumentNullException"></exception>
  165. public Task Rename(string newName)
  166. {
  167. if (string.IsNullOrEmpty(newName))
  168. {
  169. throw new ArgumentNullException("newName");
  170. }
  171. // If only the casing is changing, leave the file system alone
  172. if (!UsesIdForConfigurationPath && !string.Equals(newName, Name, StringComparison.OrdinalIgnoreCase))
  173. {
  174. UsesIdForConfigurationPath = true;
  175. // Move configuration
  176. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  177. var oldConfigurationDirectory = ConfigurationDirectoryPath;
  178. // Exceptions will be thrown if these paths already exist
  179. if (FileSystem.DirectoryExists(newConfigDirectory))
  180. {
  181. FileSystem.DeleteDirectory(newConfigDirectory, true);
  182. }
  183. if (FileSystem.DirectoryExists(oldConfigurationDirectory))
  184. {
  185. FileSystem.MoveDirectory(oldConfigurationDirectory, newConfigDirectory);
  186. }
  187. else
  188. {
  189. FileSystem.CreateDirectory(newConfigDirectory);
  190. }
  191. }
  192. Name = newName;
  193. return RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(Logger, FileSystem))
  194. {
  195. ReplaceAllMetadata = true,
  196. ImageRefreshMode = MetadataRefreshMode.FullRefresh,
  197. MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
  198. ForceSave = true
  199. }, CancellationToken.None);
  200. }
  201. public override void UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  202. {
  203. UserManager.UpdateUser(this);
  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. public override double GetDefaultPrimaryImageAspectRatio()
  218. {
  219. return 1;
  220. }
  221. /// <summary>
  222. /// Gets the configuration directory path.
  223. /// </summary>
  224. /// <param name="username">The username.</param>
  225. /// <returns>System.String.</returns>
  226. private string GetConfigurationDirectoryPath(string username)
  227. {
  228. var parentPath = ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath;
  229. // Legacy
  230. if (!UsesIdForConfigurationPath)
  231. {
  232. if (string.IsNullOrEmpty(username))
  233. {
  234. throw new ArgumentNullException("username");
  235. }
  236. var safeFolderName = FileSystem.GetValidFilename(username);
  237. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, safeFolderName);
  238. }
  239. return System.IO.Path.Combine(parentPath, Id.ToString("N"));
  240. }
  241. public bool IsParentalScheduleAllowed()
  242. {
  243. return IsParentalScheduleAllowed(DateTime.UtcNow);
  244. }
  245. public bool IsParentalScheduleAllowed(DateTime date)
  246. {
  247. var schedules = Policy.AccessSchedules;
  248. if (schedules.Length == 0)
  249. {
  250. return true;
  251. }
  252. foreach (var i in schedules)
  253. {
  254. if (IsParentalScheduleAllowed(i, date))
  255. {
  256. return true;
  257. }
  258. }
  259. return false;
  260. }
  261. private bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
  262. {
  263. if (date.Kind != DateTimeKind.Utc)
  264. {
  265. throw new ArgumentException("Utc date expected");
  266. }
  267. var localTime = date.ToLocalTime();
  268. return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek) &&
  269. IsWithinTime(schedule, localTime);
  270. }
  271. private bool IsWithinTime(AccessSchedule schedule, DateTime localTime)
  272. {
  273. var hour = localTime.TimeOfDay.TotalHours;
  274. return hour >= schedule.StartHour && hour <= schedule.EndHour;
  275. }
  276. public bool IsFolderGrouped(Guid id)
  277. {
  278. foreach (var i in Configuration.GroupedFolders)
  279. {
  280. if (new Guid(i) == id)
  281. {
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. [IgnoreDataMember]
  288. public override bool SupportsPeople
  289. {
  290. get
  291. {
  292. return false;
  293. }
  294. }
  295. public long InternalId { get; set;}
  296. }
  297. }