From: Radek Czajka Date: Fri, 17 Sep 2021 21:35:00 +0000 (+0200) Subject: Add FTP users. X-Git-Url: https://git.mdrn.pl/cas.git/commitdiff_plain/0eafaa3ce7287b5ba7a95f3b6d528dffd9648a1d?ds=inline Add FTP users. --- diff --git a/src/cas/settings.py b/src/cas/settings.py index 346e6f4..e71c5d4 100644 --- a/src/cas/settings.py +++ b/src/cas/settings.py @@ -89,6 +89,7 @@ LOCALE_PATHS = ( INSTALLED_APPS = ( 'accounts.apps.AccountsConfig', 'emails.apps.EmailsConfig', + 'ftp', 'services.apps.ServicesConfig', 'ssh_keys.apps.SshKeysConfig', diff --git a/src/ftp/__init__.py b/src/ftp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/ftp/admin.py b/src/ftp/admin.py new file mode 100644 index 0000000..31eda1d --- /dev/null +++ b/src/ftp/admin.py @@ -0,0 +1,28 @@ +from django.contrib import admin +from django.utils.safestring import mark_safe +from django.utils.translation import gettext as _ +from . import models +from .utils import generate_password + + +@admin.register(models.FtpUser) +class FtpUserAdmin(admin.ModelAdmin): + fields = ['login', 'created_at', 'last_seen_at', 'password', 'password_set_at'] + readonly_fields = ['created_at', 'last_seen_at', 'password_set_at'] + + def save_model(self, request, obj, form, change): + if not obj.password: + pwd = obj.set_password(save=False) + self.message_user( + request, + mark_safe( + _('Password for %(login)s set to: .') % { + "login": obj.login, + "password": pwd, + } + ) + ) + return super().save_model(request, obj, form, change) + + + diff --git a/src/ftp/apps.py b/src/ftp/apps.py new file mode 100644 index 0000000..df1618a --- /dev/null +++ b/src/ftp/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class FtpConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'ftp' diff --git a/src/ftp/migrations/0001_initial.py b/src/ftp/migrations/0001_initial.py new file mode 100644 index 0000000..563acaa --- /dev/null +++ b/src/ftp/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2.6 on 2021-09-14 14:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='FtpUser', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('last_seen_at', models.DateTimeField(null=True)), + ('login', models.CharField(max_length=255, unique=True)), + ('passwd', models.CharField(blank=True, max_length=255)), + ], + ), + ] diff --git a/src/ftp/migrations/0002_auto_20210917_2116.py b/src/ftp/migrations/0002_auto_20210917_2116.py new file mode 100644 index 0000000..2c6e453 --- /dev/null +++ b/src/ftp/migrations/0002_auto_20210917_2116.py @@ -0,0 +1,28 @@ +# Generated by Django 3.2.6 on 2021-09-17 21:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('ftp', '0001_initial'), + ] + + operations = [ + migrations.RenameField( + model_name='ftpuser', + old_name='passwd', + new_name='password', + ), + migrations.AddField( + model_name='ftpuser', + name='password_set_at', + field=models.DateTimeField(editable=False, null=True), + ), + migrations.AlterField( + model_name='ftpuser', + name='last_seen_at', + field=models.DateTimeField(editable=False, null=True), + ), + ] diff --git a/src/ftp/migrations/__init__.py b/src/ftp/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/ftp/models.py b/src/ftp/models.py new file mode 100644 index 0000000..4d6bad3 --- /dev/null +++ b/src/ftp/models.py @@ -0,0 +1,24 @@ +from crypt import crypt +from django.db import models +from django.contrib.messages import add_message +from django.utils.timezone import now +from .utils import generate_password + + +class FtpUser(models.Model): + created_at = models.DateTimeField(auto_now_add=True) + last_seen_at = models.DateTimeField(null=True, editable=False) + login = models.CharField(max_length=255, unique=True) + password = models.CharField(max_length=255, blank=True) + password_set_at = models.DateTimeField(null=True, editable=False) + + def __str__(self): + return self.login + + def set_password(self, save=True): + pwd = generate_password() + self.password = crypt(pwd) + self.password_set_at = now() + if save: + self.save() + return pwd diff --git a/src/ftp/utils.py b/src/ftp/utils.py new file mode 100644 index 0000000..0246b21 --- /dev/null +++ b/src/ftp/utils.py @@ -0,0 +1,6 @@ +import secrets +import string + +def generate_password(length=10): + chars = string.ascii_letters + string.digits + string.punctuation + return ''.join(secrets.choice(chars) for i in range(length))