2016-08-13 16:40:21 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
2016-08-08 16:43:11 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-08-14 09:21:04 +00:00
|
|
|
import datetime
|
2016-08-20 17:16:30 +00:00
|
|
|
from django.contrib.auth.hashers import make_password
|
2016-08-18 17:39:08 +00:00
|
|
|
from django.utils import timezone
|
2016-08-08 16:43:11 +00:00
|
|
|
from django.db import models
|
2016-08-14 09:21:04 +00:00
|
|
|
from django.contrib.auth.models import AbstractUser, Permission
|
2016-08-22 16:39:07 +00:00
|
|
|
from django.db import OperationalError
|
2016-08-08 16:43:11 +00:00
|
|
|
|
2016-08-09 17:30:19 +00:00
|
|
|
|
2016-08-24 09:14:21 +00:00
|
|
|
# class Role(models.Model):
|
|
|
|
# name = models.CharField('name', max_length=80, unique=True)
|
|
|
|
# permissions = models.ManyToManyField(
|
|
|
|
# Permission,
|
|
|
|
# verbose_name='permissions',
|
|
|
|
# blank=True,
|
|
|
|
# )
|
|
|
|
# date_added = models.DateTimeField(auto_now_add=True)
|
|
|
|
# created_by = models.CharField(max_length=100)
|
|
|
|
# comment = models.CharField(max_length=80, blank=True)
|
|
|
|
#
|
|
|
|
# def __unicode__(self):
|
|
|
|
# return self.name
|
|
|
|
#
|
|
|
|
# def delete(self, using=None, keep_parents=False):
|
|
|
|
# if self.user_set.all().count() > 0:
|
|
|
|
# raise OperationalError('Role %s has some member, should not be delete.' % self.name)
|
|
|
|
# else:
|
|
|
|
# return super(Role, self).delete(using=using, keep_parents=keep_parents)
|
|
|
|
#
|
|
|
|
# class Meta:
|
|
|
|
# db_table = 'role'
|
|
|
|
#
|
|
|
|
# @classmethod
|
|
|
|
# def initial(cls):
|
|
|
|
# roles = {
|
|
|
|
# 'Administrator': {'permissions': Permission.objects.all(), 'comment': '管理员'},
|
|
|
|
# 'User': {'permissions': [], 'comment': '用户'},
|
|
|
|
# 'Auditor': {'permissions': Permission.objects.filter(content_type__app_label='audits'),
|
|
|
|
# 'comment': '审计员'},
|
|
|
|
# }
|
|
|
|
|
|
|
|
# for role_name, props in roles.items():
|
|
|
|
# if not cls.objects.filter(name=role_name):
|
|
|
|
# role = cls.objects.create(name=role_name, comment=props.get('comment', ''), created_by='System')
|
|
|
|
# if props.get('permissions'):
|
|
|
|
# role.permissions = props.get('permissions')
|
2016-08-14 09:21:04 +00:00
|
|
|
|
2016-08-13 16:40:21 +00:00
|
|
|
|
|
|
|
class UserGroup(models.Model):
|
2016-08-14 09:21:04 +00:00
|
|
|
name = models.CharField(max_length=100, unique=True, verbose_name='组名称')
|
|
|
|
comment = models.TextField(blank=True, verbose_name='描述')
|
2016-08-13 16:40:21 +00:00
|
|
|
date_added = models.DateTimeField(auto_now_add=True)
|
|
|
|
created_by = models.CharField(max_length=100)
|
2016-08-09 17:30:19 +00:00
|
|
|
|
2016-08-13 16:40:21 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = 'usergroup'
|
|
|
|
|
2016-08-15 16:09:48 +00:00
|
|
|
@classmethod
|
2016-08-22 11:53:01 +00:00
|
|
|
def initial(cls):
|
2016-08-22 16:39:07 +00:00
|
|
|
group_or_create = cls.objects.get_or_create(name='Default', comment='Default user group for all user',
|
2016-08-22 11:53:01 +00:00
|
|
|
created_by='System')
|
|
|
|
return group_or_create[0]
|
2016-08-15 16:09:48 +00:00
|
|
|
|
2016-08-18 17:39:08 +00:00
|
|
|
@classmethod
|
|
|
|
def generate_fake(cls, count=100):
|
|
|
|
from random import seed, randint, choice
|
|
|
|
import forgery_py
|
|
|
|
from django.db import IntegrityError
|
|
|
|
|
|
|
|
seed()
|
|
|
|
for i in range(count):
|
|
|
|
group = cls(name=forgery_py.name.full_name(),
|
|
|
|
comment=forgery_py.lorem_ipsum.sentence(),
|
|
|
|
created_by=choice(User.objects.all()).username
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
group.save()
|
|
|
|
except IntegrityError:
|
|
|
|
print('Error continue')
|
|
|
|
continue
|
|
|
|
|
2016-08-13 16:40:21 +00:00
|
|
|
|
2016-08-20 17:16:30 +00:00
|
|
|
def date_expired_default():
|
|
|
|
return timezone.now() + timezone.timedelta(days=365 * 70)
|
|
|
|
|
|
|
|
|
2016-08-13 16:40:21 +00:00
|
|
|
class User(AbstractUser):
|
2016-08-24 09:14:21 +00:00
|
|
|
ROLE_CHOICES = (
|
|
|
|
('Admin', '管理员'),
|
|
|
|
('User', '用户'),
|
|
|
|
)
|
|
|
|
|
2016-08-19 16:42:50 +00:00
|
|
|
username = models.CharField(max_length=20, unique=True, verbose_name='用户名')
|
2016-08-22 11:53:01 +00:00
|
|
|
name = models.CharField(max_length=20, blank=True, verbose_name='姓名')
|
2016-08-21 14:37:55 +00:00
|
|
|
email = models.EmailField(max_length=30, unique=True, verbose_name='邮件')
|
2016-08-24 09:14:21 +00:00
|
|
|
groups = models.ManyToManyField(UserGroup, blank=True, verbose_name='用户组')
|
|
|
|
role = models.CharField(choices=ROLE_CHOICES, default='User', max_length=10, blank=True, verbose_name='角色')
|
2016-08-17 14:17:16 +00:00
|
|
|
avatar = models.ImageField(upload_to="avatar", verbose_name='头像')
|
2016-08-14 09:21:04 +00:00
|
|
|
wechat = models.CharField(max_length=30, blank=True, verbose_name='微信')
|
|
|
|
phone = models.CharField(max_length=20, blank=True, verbose_name='手机号')
|
2016-08-20 17:16:30 +00:00
|
|
|
enable_otp = models.BooleanField(default=False, verbose_name='启用二次验证')
|
|
|
|
secret_key_otp = models.CharField(max_length=16, blank=True)
|
2016-08-14 09:21:04 +00:00
|
|
|
private_key = models.CharField(max_length=5000, blank=True, verbose_name='ssh私钥') # ssh key max length 4096 bit
|
|
|
|
public_key = models.CharField(max_length=1000, blank=True, verbose_name='公钥')
|
2016-08-15 16:09:48 +00:00
|
|
|
comment = models.TextField(max_length=200, blank=True, verbose_name='描述')
|
2016-08-24 09:14:21 +00:00
|
|
|
date_expired = models.DateTimeField(default=date_expired_default, blank=True, null=True, verbose_name='有效期')
|
2016-08-14 09:21:04 +00:00
|
|
|
created_by = models.CharField(max_length=30, default='')
|
2016-08-09 17:30:19 +00:00
|
|
|
|
2016-08-22 11:53:01 +00:00
|
|
|
@property
|
|
|
|
def password_raw(self):
|
|
|
|
raise AttributeError('Password raw is not readable attribute')
|
|
|
|
|
|
|
|
#: Use this attr to set user object password, example
|
|
|
|
#: user = User(username='example', password_raw='password', ...)
|
|
|
|
#: It's equal:
|
|
|
|
#: user = User(username='example', ...)
|
|
|
|
#: user.set_password('password')
|
|
|
|
@password_raw.setter
|
|
|
|
def password_raw(self, raw_password):
|
|
|
|
self.set_password(raw_password)
|
2016-08-18 17:39:08 +00:00
|
|
|
|
2016-08-24 09:14:21 +00:00
|
|
|
@property
|
2016-08-20 17:16:30 +00:00
|
|
|
def is_expired(self):
|
|
|
|
if self.date_expired > timezone.now():
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2016-08-24 09:14:21 +00:00
|
|
|
@property
|
|
|
|
def is_superuser(self):
|
|
|
|
if self.role == 'Admin':
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@is_superuser.setter
|
|
|
|
def is_superuser(self, value):
|
|
|
|
if value is True:
|
|
|
|
self.role = 'Admin'
|
|
|
|
else:
|
|
|
|
self.role = 'User'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_staff(self):
|
|
|
|
if self.is_authenticated and self.is_active and not self.is_expired:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@is_staff.setter
|
|
|
|
def is_staff(self, value):
|
|
|
|
pass
|
|
|
|
|
2016-08-22 11:53:01 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
# If user not set name, it's default equal username
|
|
|
|
if not self.name:
|
|
|
|
self.name = self.username
|
2016-08-24 09:14:21 +00:00
|
|
|
|
2016-08-23 11:36:15 +00:00
|
|
|
super(User, self).save(*args, **kwargs)
|
2016-08-22 11:53:01 +00:00
|
|
|
# Set user default group 'All'
|
2016-08-22 16:39:07 +00:00
|
|
|
# Todo: It's have bug
|
2016-08-22 11:53:01 +00:00
|
|
|
group = UserGroup.initial()
|
2016-08-22 16:39:07 +00:00
|
|
|
if group not in self.groups.all():
|
|
|
|
self.groups.add(group)
|
2016-08-23 11:36:15 +00:00
|
|
|
# super(User, self).save(*args, **kwargs)
|
2016-08-22 11:53:01 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = 'user'
|
|
|
|
|
|
|
|
#: Use this method
|
2016-08-20 17:16:30 +00:00
|
|
|
@classmethod
|
2016-08-22 11:53:01 +00:00
|
|
|
def initial(cls):
|
2016-08-20 17:16:30 +00:00
|
|
|
user = cls(username='admin',
|
|
|
|
email='admin@jumpserver.org',
|
|
|
|
name='Administrator',
|
2016-08-22 11:53:01 +00:00
|
|
|
password_raw='admin',
|
2016-08-24 09:14:21 +00:00
|
|
|
role='Admin',
|
2016-08-20 17:16:30 +00:00
|
|
|
comment='Administrator is the super user of system',
|
|
|
|
created_by='System')
|
|
|
|
user.save()
|
2016-08-22 11:53:01 +00:00
|
|
|
user.groups.add(UserGroup.initial())
|
2016-08-20 17:16:30 +00:00
|
|
|
|
2016-08-18 17:39:08 +00:00
|
|
|
@classmethod
|
|
|
|
def generate_fake(cls, count=100):
|
2016-08-20 17:16:30 +00:00
|
|
|
from random import seed, choice
|
2016-08-18 17:39:08 +00:00
|
|
|
import forgery_py
|
|
|
|
from django.db import IntegrityError
|
|
|
|
|
|
|
|
seed()
|
|
|
|
for i in range(count):
|
|
|
|
user = cls(username=forgery_py.internet.user_name(True),
|
|
|
|
email=forgery_py.internet.email_address(),
|
|
|
|
name=forgery_py.name.full_name(),
|
|
|
|
password=make_password(forgery_py.lorem_ipsum.word()),
|
2016-08-24 09:14:21 +00:00
|
|
|
role=choice(dict(User.ROLE_CHOICES).keys()),
|
2016-08-18 17:39:08 +00:00
|
|
|
wechat=forgery_py.internet.user_name(True),
|
|
|
|
comment=forgery_py.lorem_ipsum.sentence(),
|
|
|
|
created_by=choice(cls.objects.all()).username,
|
2016-08-24 09:14:21 +00:00
|
|
|
)
|
2016-08-18 17:39:08 +00:00
|
|
|
try:
|
|
|
|
user.save()
|
|
|
|
except IntegrityError:
|
2016-08-22 11:53:01 +00:00
|
|
|
print('Duplicate Error, continue ...')
|
2016-08-18 17:39:08 +00:00
|
|
|
continue
|
|
|
|
user.groups.add(choice(UserGroup.objects.all()))
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
2016-08-20 17:16:30 +00:00
|
|
|
def init_all_models():
|
2016-08-24 09:14:21 +00:00
|
|
|
for model in (UserGroup, User):
|
2016-08-22 11:53:01 +00:00
|
|
|
if hasattr(model, 'initial'):
|
|
|
|
model.initial()
|
2016-08-18 17:39:08 +00:00
|
|
|
|
|
|
|
|
2016-08-20 17:16:30 +00:00
|
|
|
def generate_fake():
|
2016-08-24 09:14:21 +00:00
|
|
|
for model in (UserGroup, User):
|
2016-08-20 17:16:30 +00:00
|
|
|
if hasattr(model, 'generate_fake'):
|
|
|
|
model.generate_fake()
|