repos
/ timestrap-django master

timestrap-django

mirror archived upstream

Time tracking you can host anywhere, built on Django. Full export support in multiple formats and easily extensible.

djangodockerhandcodedpythonself-hostedtime-trackingtimetracker

1.1 KB · 32 lines · Python Raw History
 1from django.contrib import messages
 2from django.contrib.auth.backends import ModelBackend
 3from django.core.exceptions import PermissionDenied
 4
 5from conf.models import Site
 6from conf.utils import current_site_id
 7
 8
 9class SitePermissionBackend(ModelBackend):
10    """
11    Checks for permission to the current site for the logging in user.
12    """
13
14    def authenticate(self, request, username=None, password=None, **kwargs):
15        user = super().authenticate(request, username, password, **kwargs)
16
17        if user and user.is_active and hasattr(user, "sitepermission"):
18            site = Site.objects.get(id=current_site_id())
19            if site in user.sitepermission.sites.all():
20                return user
21            else:
22                messages.error(
23                    request,
24                    "This account does not have permission to log "
25                    "in to {}.".format(request.site),
26                    "no_sitepermission",
27                )
28                raise PermissionDenied
29
30    def get_user(self, user_id):
31        return super().get_user(user_id)