utils.py 710 B

123456789101112131415161718192021
  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 apps.host.models import Group
  5. def get_host_perms(user):
  6. ids = sub_ids = set(user.group_perms)
  7. while sub_ids:
  8. sub_ids = [x.id for x in Group.objects.filter(parent_id__in=sub_ids)]
  9. ids.update(sub_ids)
  10. return set(x.host_id for x in Group.hosts.through.objects.filter(group_id__in=ids))
  11. def has_host_perm(user, target):
  12. if user.is_supper:
  13. return True
  14. host_ids = get_host_perms(user)
  15. if isinstance(target, (list, set, tuple)):
  16. return set(target).issubset(host_ids)
  17. return int(target) in host_ids