Django: custom users

I have following users in my Django applications:
1. Normal user (UUID, email, name, address, password)
2. Remote application (UUID, name, generated random secret)
3. (other type of remote application)

The authentication for the Normal user would be with webpage email + password The authentication for the Remote application would be with UUID + random secret with JSON to ask for the temporary token

I do not know how to handle this in Django. I wanted to create AuthBaseUser from AbstractBaseUser like:

class AuthBaseUser(AbstractBaseUser, PermissionsMixin):
    pk = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False,)
    name = models.CharField(_('name'), max_length=128, blank=False)
    typ = models.CharField(max_length=16, choices=USER_TYPES, default='normaluser',)
    date_joined = models.DateTimeField(_('date joined'), auto_now_add=True, default=timezone.now)
    last_login = models.DateTimeField(_('last login'), auto_now_add=True)
    is_active = models.BooleanField(_('active'), default=True)

Then I wanted to create a RemoteAppUser and NormalUser with 1:1 mapping like this:

class NormalUser(AuthBaseUser):
    user = models.OneToOneField(AuthBaseUser, on_delete=models.CASCADE)
    email = models.EmailField(_('email address'), unique=True)
    is_superuser = models.BooleanField(_('superuser'), default=True)
    #password = #not decided yet what to add here; for remote app we will have 256b of SHA256's random generated value

    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = AuthBaseUser.REQUIRED_FIELDS.append(['email', 'password', ])

    objects = NormalUserManager()

    def __init__(self, *args, **kwargs):
        super(AuthBaseUser, self).__init__(*args, **kwargs)

    def __str__(self):
        return self.get_username()

    def get_full_name(self):
        return self.get_username()

    def get_short_name(self):
        return self.get_username()

Of course, I have to add the default auth user:

AUTH_USER_MODEL = 'myauth.AuthBaseUser'

Is there any recommended way to handle such different users? Note that for example I would like store the RemoteApp's secret as SHA256, but I do not need to run the permutations several times with seed etc.


may be just create something like this:

class User(AbstractBaseUser, PermissionsMixin):
    name = models.CharField(_('name'), max_length=128, blank=False)
    is_normal = models.BooleanField(default=False)
    is_other = models.BooleanField(default=False)

and one class for two types of the authentications, and just check is_normal user or is_other user

UPDATE:

try like this:

class EnterpriseEntry(models.Model):
    user = models.ForeignKey('User', null=False, blank=False, verbose_name=u'Пользователь',
                         related_name='enterprise_entries')
    position = models.CharField(
    max_length=100, default="", blank=True, verbose_name=u'Позиция в компании')

    ADMIN_ENTERPRISE = 0
    USER = 1
    ENTREPRENEUR = 2
    UNKNOWN = 3
    MODERATOR = 4

    PROFILE_TYPES = (
        (ADMIN_ENTERPRISE, u'Руководитель юридического лица'),
        (USER, u'Пользователь приглашенный в предприятие'),
        (ENTREPRENEUR, u'Индивидуальный предприниматель'),
        (MODERATOR, u'Модератор компании'),
        (UNKNOWN, u'Не известно'),
    )
链接地址: http://www.djcxy.com/p/33810.html

上一篇: 一个函数是否只有一个返回语句?

下一篇: Django:自定义用户