From 875aaa002949a8216c7602eb1f99d35d64a7e6e2 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 21 Dec 2016 01:17:36 +0800 Subject: [PATCH] update user model for create application user --- apps/users/models/user.py | 16 ++++++++++++++-- config_example.py | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/users/models/user.py b/apps/users/models/user.py index 470586c76..0de3474fa 100644 --- a/apps/users/models/user.py +++ b/apps/users/models/user.py @@ -7,6 +7,7 @@ from collections import OrderedDict from django.contrib.auth.hashers import make_password from django.contrib.auth.models import AbstractUser from django.core import signing +from django.conf import settings from django.db import models, IntegrityError from django.utils.translation import ugettext_lazy as _ from django.utils import timezone @@ -14,7 +15,7 @@ from django.shortcuts import reverse from rest_framework.authtoken.models import Token from common.utils import signer, date_expired_default -from . import UserGroup +from . import UserGroup, AccessKey __all__ = ['User'] @@ -24,7 +25,7 @@ class User(AbstractUser): ROLE_CHOICES = ( ('Admin', _('Administrator')), ('User', _('User')), - ('APP', _('Application')) + ('App', _('Application')) ) username = models.CharField(max_length=20, unique=True, verbose_name=_('Username')) @@ -45,6 +46,8 @@ class User(AbstractUser): verbose_name=_('Date expired')) created_by = models.CharField(max_length=30, default='', verbose_name=_('Created by')) + + @property def password_raw(self): raise AttributeError('Password raw is not a readable attribute') @@ -173,6 +176,15 @@ class User(AbstractUser): 'date_expired': self.date_expired.strftime('%Y-%m-%d %H:%M:%S') }) + @classmethod + def create_app_user(cls, name, comment): + domain_name = settings.DOMAIN_NAME or 'jumpserver.org' + app = cls.objects.create(username=name, name=name, email='%s@%s' % (name, domain_name), + role='App', enable_otp=False, comment=comment, is_first_login=False, + created_by='System') + AccessKey.object.create(user=app) + return app + @classmethod def validate_reset_token(cls, token): try: diff --git a/config_example.py b/config_example.py index 6c824f5fb..63b8c723b 100644 --- a/config_example.py +++ b/config_example.py @@ -26,6 +26,9 @@ class Config: # HTTP_PROTOCOL://HOST[:PORT] SITE_URL = 'http://localhost' + # Domain name, If set app email will set as it + DOMAIN_NAME = 'jumpserver.org' + # Django security setting, if your disable debug model, you should setting that ALLOWED_HOSTS = ['*']