2014-05-08 16:59:35 +00:00
|
|
|
from django import forms
|
2014-09-26 15:05:20 +00:00
|
|
|
from django.contrib.auth.forms import ReadOnlyPasswordHashField
|
2015-06-04 14:15:17 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.utils.html import format_html
|
|
|
|
from django.utils.safestring import mark_safe
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-12-21 11:34:07 +00:00
|
|
|
from orchestra.core import validators
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-14 13:50:19 +00:00
|
|
|
from .models import DatabaseUser, Database
|
2023-12-15 08:58:44 +00:00
|
|
|
from .settings import DATABASES_SERVERS
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DatabaseUserCreationForm(forms.ModelForm):
|
|
|
|
password1 = forms.CharField(label=_("Password"), required=False,
|
2015-09-30 13:22:17 +00:00
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'off'}),
|
2015-12-21 11:34:07 +00:00
|
|
|
validators=[validators.validate_password])
|
2014-05-08 16:59:35 +00:00
|
|
|
password2 = forms.CharField(label=_("Password confirmation"), required=False,
|
|
|
|
widget=forms.PasswordInput,
|
|
|
|
help_text=_("Enter the same password as above, for verification."))
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
model = DatabaseUser
|
|
|
|
fields = ('username', 'account', 'type')
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2023-12-15 08:58:44 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DatabaseUserCreationForm, self).__init__(*args, **kwargs)
|
|
|
|
qsServer = self.fields['target_server'].queryset.filter(name__in=DATABASES_SERVERS)
|
|
|
|
self.fields['target_server'].queryset = qsServer
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def clean_password2(self):
|
|
|
|
password1 = self.cleaned_data.get("password1")
|
|
|
|
password2 = self.cleaned_data.get("password2")
|
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
msg = _("The two password fields didn't match.")
|
2015-06-04 14:15:17 +00:00
|
|
|
raise ValidationError(msg)
|
2014-05-08 16:59:35 +00:00
|
|
|
return password2
|
|
|
|
|
|
|
|
|
2023-10-24 16:59:02 +00:00
|
|
|
class DatabaseForm(forms.ModelForm):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Database
|
|
|
|
fields = ('name', 'users', 'type', 'account', 'target_server')
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DatabaseForm, self).__init__(*args, **kwargs)
|
|
|
|
# muestra solo los usuarios del mismo server
|
|
|
|
account_id = self.instance.account_id
|
|
|
|
database_server_id = self.instance.target_server_id
|
|
|
|
if account_id:
|
|
|
|
self.fields['users'].queryset = DatabaseUser.objects.filter(account=account_id, target_server=database_server_id)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
# verifica que los usuarios petenecen al servidor de la bbdd
|
|
|
|
database_server_id = self.instance.target_server_id
|
|
|
|
users = self.cleaned_data.get('users')
|
|
|
|
if users and database_server_id:
|
|
|
|
for user in users:
|
|
|
|
if user.target_server_id != database_server_id:
|
|
|
|
self.add_error("users", _(f"{user.username} does not belong to the database server"))
|
|
|
|
|
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class DatabaseCreationForm(DatabaseUserCreationForm):
|
2023-10-24 16:59:02 +00:00
|
|
|
username = forms.CharField(label=_("Username"), max_length=32,
|
2015-12-21 11:34:07 +00:00
|
|
|
required=False, validators=[validators.validate_name],
|
2023-10-24 16:59:02 +00:00
|
|
|
help_text=_("Required. 32 characters or fewer. Letters, digits and "
|
2014-05-08 16:59:35 +00:00
|
|
|
"@/./+/-/_ only."),
|
|
|
|
error_messages={
|
2023-10-24 16:59:02 +00:00
|
|
|
'invalid': _("This value may contain 32 characters or fewer, only letters, numbers and "
|
2014-05-08 16:59:35 +00:00
|
|
|
"@/./+/-/_ characters.")})
|
|
|
|
user = forms.ModelChoiceField(required=False, queryset=DatabaseUser.objects)
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
model = Database
|
|
|
|
fields = ('username', 'account', 'type')
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DatabaseCreationForm, self).__init__(*args, **kwargs)
|
2015-06-04 14:15:17 +00:00
|
|
|
account_id = self.initial.get('account', self.initial_account)
|
2023-12-15 08:58:44 +00:00
|
|
|
|
|
|
|
qsServer = self.fields['target_server'].queryset.filter(name__in=DATABASES_SERVERS)
|
|
|
|
self.fields['target_server'].queryset = qsServer
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
if account_id:
|
2015-06-17 10:34:14 +00:00
|
|
|
qs = self.fields['user'].queryset.filter(account=account_id).order_by('username')
|
2023-10-24 16:59:02 +00:00
|
|
|
choices = [ (u.pk, "%s (%s) (%s)" % (u, u.get_type_display(), str(u.target_server.name) )) for u in qs ]
|
2014-05-08 16:59:35 +00:00
|
|
|
self.fields['user'].queryset = qs
|
|
|
|
self.fields['user'].choices = [(None, '--------'),] + choices
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2015-06-04 14:15:17 +00:00
|
|
|
def clean_username(self):
|
|
|
|
username = self.cleaned_data.get('username')
|
2023-10-24 16:59:02 +00:00
|
|
|
server = self.cleaned_data.get('target_server')
|
|
|
|
if DatabaseUser.objects.filter(username=username, target_server=server).exists():
|
2015-06-04 14:15:17 +00:00
|
|
|
raise ValidationError("Provided username already exists.")
|
2015-09-30 13:22:17 +00:00
|
|
|
return username
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def clean_password2(self):
|
|
|
|
username = self.cleaned_data.get('username')
|
|
|
|
password1 = self.cleaned_data.get('password1')
|
|
|
|
password2 = self.cleaned_data.get('password2')
|
|
|
|
if username and not (password1 and password2):
|
2015-06-04 14:15:17 +00:00
|
|
|
raise ValidationError(_("Missing password"))
|
2014-05-08 16:59:35 +00:00
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
msg = _("The two password fields didn't match.")
|
2015-06-04 14:15:17 +00:00
|
|
|
raise ValidationError(msg)
|
2014-05-08 16:59:35 +00:00
|
|
|
return password2
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def clean_user(self):
|
|
|
|
user = self.cleaned_data.get('user')
|
|
|
|
if user and user.type != self.cleaned_data.get('type'):
|
|
|
|
msg = _("Database type and user type doesn't match")
|
2015-06-04 14:15:17 +00:00
|
|
|
raise ValidationError(msg)
|
2023-10-24 16:59:02 +00:00
|
|
|
if user and user.target_server != self.cleaned_data.get('target_server'):
|
|
|
|
msg = _("Database server and user server doesn't match")
|
|
|
|
raise ValidationError(msg)
|
2014-05-08 16:59:35 +00:00
|
|
|
return user
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super(DatabaseCreationForm, self).clean()
|
|
|
|
if 'user' in cleaned_data and 'username' in cleaned_data:
|
2015-09-30 13:22:17 +00:00
|
|
|
msg = _("Use existing user or create a new one? you have provided both.")
|
2014-05-08 16:59:35 +00:00
|
|
|
if cleaned_data['user'] and self.cleaned_data['username']:
|
2015-06-04 14:15:17 +00:00
|
|
|
raise ValidationError(msg)
|
2014-05-08 16:59:35 +00:00
|
|
|
elif not (cleaned_data['username'] or cleaned_data['user']):
|
2015-06-04 14:15:17 +00:00
|
|
|
raise ValidationError(msg)
|
2014-05-08 16:59:35 +00:00
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
|
|
|
|
class ReadOnlySQLPasswordHashField(ReadOnlyPasswordHashField):
|
|
|
|
class ReadOnlyPasswordHashWidget(forms.Widget):
|
2021-05-13 12:42:05 +00:00
|
|
|
def render(self, name, value, attrs, renderer=None):
|
2014-05-08 16:59:35 +00:00
|
|
|
original = ReadOnlyPasswordHashField.widget().render(name, value, attrs)
|
|
|
|
if 'Invalid' not in original:
|
|
|
|
return original
|
|
|
|
encoded = value
|
|
|
|
if not encoded:
|
|
|
|
summary = mark_safe("<strong>%s</strong>" % _("No password set."))
|
|
|
|
else:
|
|
|
|
size = len(value)
|
2015-04-03 13:03:08 +00:00
|
|
|
summary = value[:int(size/2)] + '*'*int(size-size/2)
|
2014-05-08 16:59:35 +00:00
|
|
|
summary = "<strong>hash</strong>: %s" % summary
|
|
|
|
if value.startswith('*'):
|
|
|
|
summary = "<strong>algorithm</strong>: sha1_bin_hex %s" % summary
|
|
|
|
return format_html("<div>%s</div>" % summary)
|
|
|
|
widget = ReadOnlyPasswordHashWidget
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseUserChangeForm(forms.ModelForm):
|
|
|
|
password = ReadOnlySQLPasswordHashField(label=_("Password"),
|
|
|
|
help_text=_("Raw passwords are not stored, so there is no way to see "
|
|
|
|
"this user's password, but you can change the password "
|
2016-05-11 12:56:10 +00:00
|
|
|
"using <a href='../password/'>this form</a>. "
|
|
|
|
"<a onclick='return showAddAnotherPopup(this);' href='../hash/'>Show hash</a>."))
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
model = DatabaseUser
|
2014-07-08 15:19:15 +00:00
|
|
|
fields = ('username', 'password', 'type', 'account')
|
2021-05-13 12:42:05 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def clean_password(self):
|
|
|
|
return self.initial["password"]
|