DomainadminModel.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from modules.Mailcow import Mailcow
  2. from models.BaseModel import BaseModel
  3. class DomainadminModel(BaseModel):
  4. parser_command = "domainadmin"
  5. required_args = {
  6. "add": [["username", "domains", "password"]],
  7. "delete": [["username"]],
  8. "get": [["username"]],
  9. "edit": [["username"]]
  10. }
  11. def __init__(
  12. self,
  13. username=None,
  14. domains=None,
  15. password=None,
  16. active=None,
  17. **kwargs
  18. ):
  19. self.mailcow = Mailcow()
  20. self.username = username
  21. self.domains = domains
  22. self.password = password
  23. self.password2 = password
  24. self.active = active
  25. @classmethod
  26. def from_dict(cls, data):
  27. return cls(
  28. username=data.get("username"),
  29. domains=data.get("domains"),
  30. password=data.get("password"),
  31. active=data.get("active", None),
  32. )
  33. def getAdd(self):
  34. """
  35. Get the domain admin details as a dictionary for adding, sets default values.
  36. :return: Dictionary containing domain admin details.
  37. """
  38. domainadmin = {
  39. "username": self.username,
  40. "domains": self.domains,
  41. "password": self.password,
  42. "password2": self.password2,
  43. "active": self.active if self.active is not None else "1"
  44. }
  45. return {key: value for key, value in domainadmin.items() if value is not None}
  46. def getEdit(self):
  47. """
  48. Get the domain admin details as a dictionary for editing, sets no default values.
  49. :return: Dictionary containing domain admin details.
  50. """
  51. domainadmin = {
  52. "username": self.username,
  53. "domains": self.domains,
  54. "password": self.password,
  55. "password2": self.password2,
  56. "active": self.active
  57. }
  58. return {key: value for key, value in domainadmin.items() if value is not None}
  59. def get(self):
  60. """
  61. Get the domain admin details from the mailcow API.
  62. :return: Response from the mailcow API.
  63. """
  64. return self.mailcow.getDomainadmin(self.username)
  65. def delete(self):
  66. """
  67. Delete the domain admin from the mailcow API.
  68. :return: Response from the mailcow API.
  69. """
  70. return self.mailcow.deleteDomainadmin(self.username)
  71. def add(self):
  72. """
  73. Add the domain admin to the mailcow API.
  74. :return: Response from the mailcow API.
  75. """
  76. return self.mailcow.addDomainadmin(self.getAdd())
  77. def edit(self):
  78. """
  79. Edit the domain admin in the mailcow API.
  80. :return: Response from the mailcow API.
  81. """
  82. return self.mailcow.editDomainadmin(self.username, self.getEdit())
  83. @classmethod
  84. def add_parser(cls, subparsers):
  85. parser = subparsers.add_parser(
  86. cls.parser_command,
  87. help="Manage domain admins (add, delete, get, edit)"
  88. )
  89. parser.add_argument("object", choices=list(cls.required_args.keys()), help="Action to perform: add, delete, get, edit")
  90. parser.add_argument("--username", help="Username for the domain admin")
  91. parser.add_argument("--domains", help="Comma-separated list of domains")
  92. parser.add_argument("--password", help="Password for the domain admin")
  93. parser.add_argument("--active", choices=["1", "0"], help="Activate (1) or deactivate (0) the domain admin")