diff --git a/apps/assets/hands.py b/apps/assets/hands.py index e87060ecd..3dab35ec7 100644 --- a/apps/assets/hands.py +++ b/apps/assets/hands.py @@ -11,4 +11,4 @@ """ - +from users.utils import AdminUserRequiredMixin diff --git a/apps/assets/migrations/0001_initial.py b/apps/assets/migrations/0001_initial.py index e2ae4c8a1..94a2908dd 100644 --- a/apps/assets/migrations/0001_initial.py +++ b/apps/assets/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.10 on 2016-09-05 12:50 +# Generated by Django 1.10 on 2016-09-07 15:11 from __future__ import unicode_literals from django.db import migrations, models @@ -24,12 +24,12 @@ class Migration(migrations.Migration): ('private_key', models.CharField(blank=True, max_length=4096, null=True, verbose_name='SSH private key')), ('is_default', models.BooleanField(default=True, verbose_name='As default')), ('auto_update', models.BooleanField(default=True, verbose_name='Auto update pass/key')), - ('date_added', models.DateTimeField(auto_now=True, null=True)), + ('date_created', models.DateTimeField(auto_now=True, null=True)), ('create_by', models.CharField(blank=True, max_length=32, null=True, verbose_name='Created by')), ('comment', models.TextField(blank=True, verbose_name='Comment')), ], options={ - 'db_table': 'adminuser', + 'db_table': 'admin_user', }, ), migrations.CreateModel( @@ -55,9 +55,9 @@ class Migration(migrations.Migration): ('sn', models.CharField(blank=True, max_length=128, null=True, unique=True, verbose_name='Serial number')), ('created_by', models.CharField(blank=True, max_length=32, null=True, verbose_name='Created by')), ('is_active', models.BooleanField(default=True, verbose_name='Is active')), - ('date_added', models.DateTimeField(auto_now=True, null=True, verbose_name='Date added')), + ('date_created', models.DateTimeField(auto_now=True, null=True, verbose_name='Date added')), ('comment', models.CharField(blank=True, max_length=128, null=True, verbose_name='Comment')), - ('admin_user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='assets.AdminUser', verbose_name='Admin user')), + ('admin_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='assets.AdminUser', verbose_name='Admin user')), ], options={ 'db_table': 'asset', @@ -70,23 +70,24 @@ class Migration(migrations.Migration): ('key', models.CharField(blank=True, max_length=64, null=True, verbose_name='KEY')), ('value', models.CharField(blank=True, max_length=64, null=True, verbose_name='VALUE')), ('created_by', models.CharField(blank=True, max_length=32, verbose_name='Created by')), - ('date_added', models.DateTimeField(auto_now=True, null=True)), + ('date_created', models.DateTimeField(auto_now=True, null=True)), ('comment', models.TextField(blank=True, verbose_name='Comment')), ], options={ - 'db_table': 'assetextend', + 'db_table': 'asset_extend', }, ), migrations.CreateModel( name='AssetGroup', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(blank=True, max_length=64, null=True, unique=True, verbose_name='Name')), - ('created_by', models.CharField(blank=True, max_length=32, null=True, verbose_name='Created by')), + ('name', models.CharField(max_length=64, unique=True, verbose_name='Name')), + ('created_by', models.CharField(blank=True, max_length=32, verbose_name='Created by')), + ('date_created', models.DateTimeField(auto_now=True, null=True, verbose_name='Date added')), ('comment', models.TextField(blank=True, verbose_name='Comment')), ], options={ - 'db_table': 'assetgroup', + 'db_table': 'asset_group', }, ), migrations.CreateModel( @@ -99,7 +100,7 @@ class Migration(migrations.Migration): ('phone', models.CharField(blank=True, max_length=32, verbose_name='Phone')), ('address', models.CharField(blank=True, max_length=128, verbose_name='Address')), ('network', models.TextField(blank=True, verbose_name='Network')), - ('date_added', models.DateField(auto_now=True, null=True, verbose_name='Date added')), + ('date_created', models.DateTimeField(auto_now=True, null=True, verbose_name='Date added')), ('operator', models.CharField(blank=True, max_length=32, verbose_name='Operator')), ('created_by', models.CharField(blank=True, max_length=32, verbose_name='Created by')), ('comment', models.TextField(blank=True, verbose_name='Comment')), @@ -115,7 +116,7 @@ class Migration(migrations.Migration): ('key', models.CharField(blank=True, max_length=64, null=True, verbose_name='KEY')), ('value', models.CharField(blank=True, max_length=64, null=True, verbose_name='VALUE')), ('created_by', models.CharField(blank=True, max_length=32, verbose_name='Created by')), - ('date_added', models.DateTimeField(auto_now=True, null=True)), + ('date_created', models.DateTimeField(auto_now=True, null=True)), ('comment', models.CharField(blank=True, max_length=128, verbose_name='Comment')), ('asset', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='assets.Asset', verbose_name='Asset')), ], @@ -124,7 +125,7 @@ class Migration(migrations.Migration): }, ), migrations.CreateModel( - name='SysUser', + name='SystemUser', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=128, unique=True, verbose_name='Name')), @@ -140,14 +141,19 @@ class Migration(migrations.Migration): ('shell', models.CharField(blank=True, max_length=64, verbose_name='Shell')), ('home', models.CharField(blank=True, max_length=64, verbose_name='Home')), ('uid', models.IntegerField(blank=True, verbose_name='Uid')), - ('date_added', models.DateTimeField(auto_now=True, null=True)), + ('date_created', models.DateTimeField(auto_now=True, null=True)), ('create_by', models.CharField(blank=True, max_length=32, verbose_name='Created by')), ('comment', models.CharField(blank=True, max_length=128, verbose_name='Comment')), ], options={ - 'db_table': 'sysuser', + 'db_table': 'system_user', }, ), + migrations.AddField( + model_name='assetgroup', + name='system_users', + field=models.ManyToManyField(blank=True, related_name='asset_groups', to='assets.SystemUser'), + ), migrations.AddField( model_name='asset', name='env', @@ -156,12 +162,12 @@ class Migration(migrations.Migration): migrations.AddField( model_name='asset', name='groups', - field=models.ManyToManyField(blank=True, null=True, to='assets.AssetGroup', verbose_name='Asset groups'), + field=models.ManyToManyField(related_name='assets', to='assets.AssetGroup', verbose_name='Asset groups'), ), migrations.AddField( model_name='asset', name='idc', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='assets.IDC', verbose_name='IDC'), + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='assets', to='assets.IDC', verbose_name='IDC'), ), migrations.AddField( model_name='asset', @@ -170,8 +176,8 @@ class Migration(migrations.Migration): ), migrations.AddField( model_name='asset', - name='sys_user', - field=models.ManyToManyField(blank=True, null=True, to='assets.SysUser', verbose_name='System User'), + name='system_user', + field=models.ManyToManyField(blank=True, to='assets.SystemUser', verbose_name='System User'), ), migrations.AddField( model_name='asset', diff --git a/apps/assets/models.py b/apps/assets/models.py index 642966e92..f968f05b0 100644 --- a/apps/assets/models.py +++ b/apps/assets/models.py @@ -5,6 +5,8 @@ from django.db import models import logging from django.utils.translation import ugettext_lazy as _ +from common.utils import encrypt, decrypt + logger = logging.getLogger(__name__) @@ -68,20 +70,64 @@ class AssetExtend(models.Model): class AdminUser(models.Model): name = models.CharField(max_length=128, unique=True, null=True, blank=True, verbose_name=_('Name')) username = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('Username')) - password = models.CharField(max_length=256, null=True, blank=True, verbose_name=_('Password')) - private_key = models.CharField(max_length=4096, null=True, blank=True, verbose_name=_('SSH private key')) - is_default = models.BooleanField(default=True, verbose_name=_('As default')) - auto_update = models.BooleanField(default=True, verbose_name=_('Auto update pass/key')) - date_created = models.DateTimeField(auto_now=True, null=True, blank=True) - create_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by')) + _password = models.CharField(max_length=256, null=True, blank=True, verbose_name=_('Password')) + _private_key = models.CharField(max_length=4096, null=True, blank=True, verbose_name=_('SSH private key')) + _public_key = models.CharField(max_length=4096, null=True, blank=True, verbose_name=_('SSH public key')) + as_default = models.BooleanField(default=True, verbose_name=_('As default')) comment = models.TextField(blank=True, verbose_name=_('Comment')) + date_created = models.DateTimeField(auto_now=True, null=True, blank=True) + created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by')) def __unicode__(self): return self.name + @property + def password(self): + return decrypt(self._password) + + @password.setter + def password(self, password_raw): + self._password = encrypt(password_raw) + + @property + def private_key(self): + return decrypt(self._private_key) + + @private_key.setter + def private_key(self, private_key_raw): + self._private_key = encrypt(private_key_raw) + + @property + def public_key(self): + return decrypt(self._public_key) + + @public_key.setter + def public_key(self, public_key_raw): + self._public_key = encrypt(public_key_raw) + class Meta: db_table = 'admin_user' + @classmethod + def generate_fake(cls, count=100): + from random import seed, choice + import forgery_py + from django.db import IntegrityError + + seed() + for i in range(count): + obj = cls(name=forgery_py.name.full_name(), + username=forgery_py.internet.user_name(), + password=forgery_py.lorem_ipsum.word(), + comment=forgery_py.lorem_ipsum.sentence(), + created_by='Fake') + try: + obj.save() + logger.debug('Generate fake asset group: %s' % obj.name) + except IntegrityError: + print('Error continue') + continue + class SystemUser(models.Model): PROTOCOL_CHOICES = ( @@ -102,7 +148,7 @@ class SystemUser(models.Model): home = models.CharField(max_length=64, blank=True, verbose_name=_('Home')) uid = models.IntegerField(blank=True, verbose_name=_('Uid')) date_created = models.DateTimeField(auto_now=True, null=True) - create_by = models.CharField(max_length=32, blank=True, verbose_name=_('Created by')) + created_by = models.CharField(max_length=32, blank=True, verbose_name=_('Created by')) comment = models.CharField(max_length=128, blank=True, verbose_name=_('Comment')) def __unicode__(self): @@ -223,3 +269,7 @@ class Label(models.Model): class Meta: db_table = 'label' + +def generate_fake(): + for cls in (Asset, AssetGroup, IDC): + cls.generate_fake() diff --git a/apps/assets/templates/assets/admin_user_list.html b/apps/assets/templates/assets/admin_user_list.html new file mode 100644 index 000000000..d7aae75aa --- /dev/null +++ b/apps/assets/templates/assets/admin_user_list.html @@ -0,0 +1,41 @@ +{% extends '_list_base.html' %} +{% load i18n %} +{% load common_tags %} +{% block content_left_head %} + {% trans "Create admin user" %} +{% endblock %} + +{% block table_head %} +