users_collection.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from .user import user
  2. from .user import user_builder
  3. from .user import user_factory
  4. from .exception import in_collection_exception
  5. class users_collection:
  6. """
  7. This is responsible for managing users in the system. It could login new
  8. users, or load them by ApiKey.
  9. """
  10. def __init__(self):
  11. """
  12. This create new collection.
  13. """
  14. self.__by_apikey = dict()
  15. self.__by_nick = dict()
  16. @property
  17. def all(self) -> list:
  18. """
  19. This return all users in the collection, as list, like in the
  20. config file.
  21. """
  22. return list(self.__by_apikey.values())
  23. def add(self, target: user) -> None:
  24. """
  25. This add new user to the collection. When ApiKey or login already
  26. exists in the collection, then error had been raised.
  27. Parameters:
  28. target (user): New user to add
  29. """
  30. if target.apikey in self.__by_apikey:
  31. error = "User with ApiKey \"" + target.apikey + "\" "
  32. error = error + "already in collection."
  33. raise in_collection_exception(error)
  34. if target.nick in self.__by_nick:
  35. error = "User with nick \"" + target.nick + "\" "
  36. error = error + "already in collection."
  37. raise in_collection_exception(error)
  38. self.__by_apikey[target.apikey] = target
  39. self.__by_nick[target.nick] = target
  40. def login(self, nick: str, password: str) -> user | None:
  41. """
  42. This try to login user by nick and password. When nick or password
  43. had been wrong, then None had been returned. When nick and password
  44. are correct, then return that user.
  45. Parameters:
  46. nick (str): Nick of the user
  47. password (str): Password of the user
  48. Returns:
  49. (user): User with that nick and password, if exists
  50. (None): When user with that nick and password not exists
  51. """
  52. if not nick in self.__by_nick:
  53. return None
  54. target = self.__by_nick[nick]
  55. if target.password != password:
  56. return None
  57. return target
  58. def remove(self, target: user) -> None:
  59. """
  60. This function remove target user from collection.
  61. Parameters:
  62. target (user): Target user to remove from collection
  63. """
  64. exists = target.nick in self.__by_nick
  65. exists = target.apikey in self.__by_apikey
  66. if not exists:
  67. raise in_collection_exception("Can not found required user.")
  68. by_nick = self.__by_nick.copy()
  69. by_apikey = self.__by_apikey.copy()
  70. by_nick.pop(target.nick)
  71. by_apikey.pop(target.apikey)
  72. self.__by_nick = by_nick
  73. self.__by_apikey = by_apikey
  74. def get(self, apikey: str) -> user | None:
  75. """
  76. This try to load user by ApiKey. When user with that ApiKey exists
  77. then return it. When not, return None.
  78. Parameters:
  79. apikey (str): ApiKey of the user to load
  80. Returns:
  81. (user): User with that ApiKey
  82. (None): When user with that ApiKey not exists
  83. """
  84. if not apikey in self.__by_apikey:
  85. return None
  86. return self.__by_apikey[apikey]