UserController.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using MediaBrowser.Model.Users;
  5. using MediaBrowser.Common.Json;
  6. namespace MediaBrowser.Controller
  7. {
  8. /// <summary>
  9. /// Manages users within the system
  10. /// </summary>
  11. public class UserController
  12. {
  13. /// <summary>
  14. /// Gets or sets the path to folder that contains data for all the users
  15. /// </summary>
  16. public string UsersPath { get; set; }
  17. public UserController(string usersPath)
  18. {
  19. UsersPath = usersPath;
  20. }
  21. /// <summary>
  22. /// Gets all users within the system
  23. /// </summary>
  24. public IEnumerable<User> GetAllUsers()
  25. {
  26. if (!Directory.Exists(UsersPath))
  27. {
  28. Directory.CreateDirectory(UsersPath);
  29. }
  30. List<User> list = new List<User>();
  31. foreach (string folder in Directory.GetDirectories(UsersPath, "*", SearchOption.TopDirectoryOnly))
  32. {
  33. User item = GetFromDirectory(folder);
  34. if (item != null)
  35. {
  36. list.Add(item);
  37. }
  38. }
  39. return list;
  40. }
  41. /// <summary>
  42. /// Gets a User from it's directory
  43. /// </summary>
  44. private User GetFromDirectory(string path)
  45. {
  46. string file = Path.Combine(path, "user.js");
  47. return JsonSerializer.DeserializeFromFile<User>(file);
  48. }
  49. /// <summary>
  50. /// Creates a User with a given name
  51. /// </summary>
  52. public User CreateUser(string name)
  53. {
  54. var now = DateTime.Now;
  55. User user = new User()
  56. {
  57. Name = name,
  58. Id = Guid.NewGuid(),
  59. DateCreated = now,
  60. DateModified = now
  61. };
  62. user.Path = Path.Combine(UsersPath, user.Id.ToString());
  63. Directory.CreateDirectory(user.Path);
  64. JsonSerializer.SerializeToFile(user, Path.Combine(user.Path, "user.js"));
  65. return user;
  66. }
  67. }
  68. }