DomainadminModel.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. password2=data.get("password"),
  32. active=data.get("active", None),
  33. )
  34. def getAdd(self):
  35. """
  36. Get the domain admin details as a dictionary for adding, sets default values.
  37. :return: Dictionary containing domain admin details.
  38. """
  39. domainadmin = {
  40. "username": self.username,
  41. "domains": self.domains,
  42. "password": self.password,
  43. "password2": self.password2,
  44. "active": self.active if self.active is not None else "1"
  45. }
  46. return {key: value for key, value in domainadmin.items() if value is not None}
  47. def getEdit(self):
  48. """
  49. Get the domain admin details as a dictionary for editing, sets no default values.
  50. :return: Dictionary containing domain admin details.
  51. """
  52. domainadmin = {
  53. "username": self.username,
  54. "domains": self.domains,
  55. "password": self.password,
  56. "password2": self.password2,
  57. "active": self.active
  58. }
  59. return {key: value for key, value in domainadmin.items() if value is not None}
  60. def get(self):
  61. """
  62. Get the domain admin details from the mailcow API.
  63. :return: Response from the mailcow API.
  64. """
  65. return self.mailcow.getDomainadmin(self.username)
  66. def delete(self):
  67. """
  68. Delete the domain admin from the mailcow API.
  69. :return: Response from the mailcow API.
  70. """
  71. return self.mailcow.deleteDomainadmin(self.username)
  72. def add(self):
  73. """
  74. Add the domain admin to the mailcow API.
  75. :return: Response from the mailcow API.
  76. """
  77. return self.mailcow.addDomainadmin(self.getAdd())
  78. def edit(self):
  79. """
  80. Edit the domain admin in the mailcow API.
  81. :return: Response from the mailcow API.
  82. """
  83. return self.mailcow.editDomainadmin(self.username, self.getEdit())
  84. @classmethod
  85. def add_parser(cls, subparsers):
  86. parser = subparsers.add_parser(
  87. cls.parser_command,
  88. help="Manage domain admins (add, delete, get, edit)"
  89. )
  90. parser.add_argument("object", choices=list(cls.required_args.keys()), help="Action to perform: add, delete, get, edit")
  91. parser.add_argument("--username", help="Username for the domain admin")
  92. parser.add_argument("--domains", help="Comma-separated list of domains")
  93. parser.add_argument("--password", help="Password for the domain admin")
  94. parser.add_argument("--active", choices=["1", "0"], help="Activate (1) or deactivate (0) the domain admin")