models.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. from django.db import models
  5. from libs import ModelMixin, human_datetime
  6. from django.contrib.auth.hashers import make_password, check_password
  7. import json
  8. class User(models.Model, ModelMixin):
  9. username = models.CharField(max_length=100)
  10. nickname = models.CharField(max_length=100)
  11. password_hash = models.CharField(max_length=100) # hashed password
  12. type = models.CharField(max_length=20, default='default')
  13. is_supper = models.BooleanField(default=False)
  14. is_active = models.BooleanField(default=True)
  15. access_token = models.CharField(max_length=32)
  16. token_expired = models.IntegerField(null=True)
  17. last_login = models.CharField(max_length=20)
  18. last_ip = models.CharField(max_length=50)
  19. wx_token = models.CharField(max_length=50, null=True)
  20. roles = models.ManyToManyField('Role', db_table='user_role_rel')
  21. created_at = models.CharField(max_length=20, default=human_datetime)
  22. created_by = models.ForeignKey('User', models.PROTECT, related_name='+', null=True)
  23. deleted_at = models.CharField(max_length=20, null=True)
  24. deleted_by = models.ForeignKey('User', models.PROTECT, related_name='+', null=True)
  25. @staticmethod
  26. def make_password(plain_password: str) -> str:
  27. return make_password(plain_password, hasher='pbkdf2_sha256')
  28. def verify_password(self, plain_password: str) -> bool:
  29. return check_password(plain_password, self.password_hash)
  30. @property
  31. def page_perms(self):
  32. data = set()
  33. for item in self.roles.all():
  34. if item.page_perms:
  35. perms = json.loads(item.page_perms)
  36. for m, v in perms.items():
  37. for p, d in v.items():
  38. data.update(f'{m}.{p}.{x}' for x in d)
  39. return list(data)
  40. @property
  41. def deploy_perms(self):
  42. data = {'apps': set(), 'envs': set()}
  43. for item in self.roles.all():
  44. if item.deploy_perms:
  45. perms = json.loads(item.deploy_perms)
  46. data['apps'].update(perms.get('apps', []))
  47. data['envs'].update(perms.get('envs', []))
  48. return data
  49. @property
  50. def group_perms(self):
  51. data = set()
  52. for item in self.roles.all():
  53. if item.group_perms:
  54. data.update(json.loads(item.group_perms))
  55. return list(data)
  56. def has_perms(self, codes):
  57. # return self.is_supper or self.role in codes
  58. return self.is_supper
  59. def __repr__(self):
  60. return '<User %r>' % self.username
  61. class Meta:
  62. db_table = 'users'
  63. ordering = ('-id',)
  64. class Role(models.Model, ModelMixin):
  65. name = models.CharField(max_length=50)
  66. desc = models.CharField(max_length=255, null=True)
  67. page_perms = models.TextField(null=True)
  68. deploy_perms = models.TextField(null=True)
  69. group_perms = models.TextField(null=True)
  70. created_at = models.CharField(max_length=20, default=human_datetime)
  71. created_by = models.ForeignKey(User, on_delete=models.PROTECT, related_name='+')
  72. def to_dict(self, *args, **kwargs):
  73. tmp = super().to_dict(*args, **kwargs)
  74. tmp['page_perms'] = json.loads(self.page_perms) if self.page_perms else {}
  75. tmp['deploy_perms'] = json.loads(self.deploy_perms) if self.deploy_perms else {}
  76. tmp['group_perms'] = json.loads(self.group_perms) if self.group_perms else []
  77. tmp['used'] = self.user_set.count()
  78. return tmp
  79. def add_deploy_perm(self, target, value):
  80. perms = {'apps': [], 'envs': []}
  81. if self.deploy_perms:
  82. perms.update(json.loads(self.deploy_perms))
  83. perms[target].append(value)
  84. self.deploy_perms = json.dumps(perms)
  85. self.save()
  86. def __repr__(self):
  87. return '<Role name=%r>' % self.name
  88. class Meta:
  89. db_table = 'roles'
  90. ordering = ('-id',)
  91. class History(models.Model, ModelMixin):
  92. user = models.ForeignKey(User, on_delete=models.CASCADE)
  93. ip = models.CharField(max_length=50)
  94. created_at = models.CharField(max_length=20, default=human_datetime)
  95. class Meta:
  96. db_table = 'login_histories'
  97. ordering = ('-id',)