Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
django-iam / models.py
Size: Mime:
from typing import Callable, Any

from building_blocks.models import Archivable
from django.conf import settings
from django.db import models

from .mixins import RolePredicateMixin


class UserProfileModel(
    RolePredicateMixin,
    Archivable,
    models.Model
):
    """
    Abstract model for profile models to inherit from. Provides a one-to-one user field that points to the owner of a
    profile.
    Override the user field, if you'd like to set its `related_name` or set it to optional (e.g. for bot profiles)
    """

    class Meta:
        abstract = True

    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

    deactivate = Archivable.archive

    def __str__(self):
        return f"{self.user} as {self._meta.verbose_name}"

    @classmethod
    def check_user(cls, check_func) -> Callable[[Any], bool]:
        """
        Return a function that first, checks if a user has the role denoted by this profile class, and then runs
        `check_func` on the user's profile for this role to determine extra permissions.
        :param check_func: Function that receives a profile instance and checks if it passes a condition or not.
        :return: Function that accepts a user instance as an argument, and checks if they have the role and some extra
            conditions.
        """

        def check_user_func(user) -> bool:
            profile = user.get_or_set_role(cls)
            if not profile:
                return False
            return check_func(profile)

        return check_user_func


__all__ = [
    'UserProfileModel',
]