django-helpdeskmig/helpdesk/user.py

94 lines
3.1 KiB
Python
Raw Permalink Normal View History

2019-10-11 17:12:39 +02:00
from helpdesk import settings as helpdesk_settings
from helpdesk.models import Queue, Ticket
2019-10-11 17:12:39 +02:00
2022-03-18 19:33:31 +01:00
if helpdesk_settings.HELPDESK_KB_ENABLED:
from helpdesk.models import KBCategory, KBItem
2022-07-12 12:34:19 +02:00
2020-01-27 17:37:59 +01:00
def huser_from_request(req):
return HelpdeskUser(req.user)
2019-10-11 17:12:39 +02:00
class HelpdeskUser:
def __init__(self, user):
self.user = user
def get_queues(self):
"""Return the list of Queues the user can access.
:param user: The User (the class should have the has_perm method)
:return: A Python list of Queues
"""
user = self.user
all_queues = Queue.objects.all()
public_ids = [q.pk for q in
Queue.objects.filter(allow_public_submission=True)]
limit_queues_by_user = \
helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_PERMISSION \
and not user.is_superuser
if limit_queues_by_user:
2022-07-12 12:34:19 +02:00
id_list = [q.pk for q in all_queues if user.has_perm(
q.permission_name)]
2019-10-11 17:12:39 +02:00
id_list += public_ids
return all_queues.filter(pk__in=id_list)
else:
return all_queues
def get_allowed_kb_categories(self):
2020-01-27 17:37:59 +01:00
categories = []
2022-03-18 19:33:31 +01:00
if helpdesk_settings.HELPDESK_KB_ENABLED:
for cat in KBCategory.objects.all():
if self.can_access_kbcategory(cat):
categories.append(cat)
2020-01-27 17:37:59 +01:00
return categories
def get_assigned_kb_items(self):
kbitems = []
2022-03-18 19:33:31 +01:00
if helpdesk_settings.HELPDESK_KB_ENABLED:
for item in KBItem.objects.all():
if item.get_team() and item.get_team().is_member(self.user):
kbitems.append(item)
return kbitems
2019-10-11 17:12:39 +02:00
def get_tickets_in_queues(self):
return Ticket.objects.filter(queue__in=self.get_queues())
2020-01-27 17:37:59 +01:00
def has_full_access(self):
return self.user.is_superuser or self.user.is_staff \
or helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE
2020-01-27 17:37:59 +01:00
2019-10-11 17:12:39 +02:00
def can_access_queue(self, queue):
"""Check if a certain user can access a certain queue.
:param user: The User (the class should have the has_perm method)
:param queue: The django-helpdesk Queue instance
:return: True if the user has permission (either by default or explicitly), false otherwise
"""
2020-01-27 17:37:59 +01:00
if self.has_full_access():
2019-10-11 17:12:39 +02:00
return True
else:
return (
helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_PERMISSION
and
self.user.has_perm(queue.permission_name)
)
2019-10-11 17:12:39 +02:00
def can_access_ticket(self, ticket):
"""Check to see if the user has permission to access
a ticket. If not then deny access."""
user = self.user
if self.can_access_queue(ticket.queue):
return True
2020-01-27 17:37:59 +01:00
elif self.has_full_access() or \
(ticket.assigned_to and user.id == ticket.assigned_to.id):
2019-10-11 17:12:39 +02:00
return True
else:
return False
2020-01-27 17:37:59 +01:00
def can_access_kbcategory(self, category):
if category.public:
return True
return self.has_full_access() or (category.queue and self.can_access_queue(category.queue))