1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-03-20 17:44:51 +00:00

Added change password permission

Replaces SHOW_PROFILE_EDIT_PASSWORD option
Can be set on per user or per group basis
This commit is contained in:
Real-Gecko 2020-05-27 18:46:37 +06:00 committed by catborise
parent fec59b1dd7
commit 334d4bff18
8 changed files with 337 additions and 176 deletions

View file

@ -0,0 +1,24 @@
# Generated by Django 2.2.12 on 2020-05-27 12:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0002_addAdmin'),
]
operations = [
migrations.CreateModel(
name='PermissionSet',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
options={
'permissions': (('change_password', 'Can change password'),),
'managed': False,
'default_permissions': (),
},
),
]

View file

@ -0,0 +1,25 @@
from django.db import migrations
def apply_change_password(apps, schema_editor):
from django.conf import settings
from django.contrib.auth.models import User, Permission
if hasattr(settings, 'SHOW_PROFILE_EDIT_PASSWORD'):
if settings.SHOW_PROFILE_EDIT_PASSWORD:
permission = Permission.objects.get(codename='change_password')
users = User.objects.all()
user: User
for user in users:
user.user_permissions.add(permission)
class Migration(migrations.Migration):
dependencies = [
('accounts', '0003_permissionset'),
]
operations = [
migrations.RunPython(apply_change_password),
]

View file

@ -0,0 +1,24 @@
# Generated by Django 2.2.12 on 2020-06-02 10:04
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0004_apply_change_password'),
]
operations = [
migrations.AlterField(
model_name='userattributes',
name='max_cpus',
field=models.IntegerField(default=2, help_text='-1 for unlimited. Any integer value', validators=[django.core.validators.MinValueValidator(-1)]),
),
migrations.AlterField(
model_name='userattributes',
name='max_instances',
field=models.IntegerField(default=2, help_text='-1 for unlimited. Any integer value', validators=[django.core.validators.MinValueValidator(-1)]),
),
]

View file

@ -1,9 +1,11 @@
from django.db.models import Model, BooleanField, IntegerField, CharField
from django.db.models import ForeignKey, OneToOneField
from django.db.models import CASCADE, DO_NOTHING
from django.conf import settings
from django.contrib.auth.models import User
from instances.models import Instance
from django.core.validators import MinValueValidator
from django.db.models import (CASCADE, DO_NOTHING, BooleanField, CharField,
ForeignKey, IntegerField, Model, OneToOneField)
from django.utils.translation import ugettext_lazy as _
from instances.models import Instance
class UserInstance(Model):
@ -29,10 +31,26 @@ class UserSSHKey(Model):
class UserAttributes(Model):
user = OneToOneField(User, on_delete=CASCADE)
can_clone_instances = BooleanField(default=True)
max_instances = IntegerField(default=1, help_text="-1 for unlimited. Any integer value", validators=[MinValueValidator(-1), ])
max_cpus = IntegerField(default=1, help_text="-1 for unlimited. Any integer value", validators=[MinValueValidator(-1)])
max_memory = IntegerField(default=2048, help_text="-1 for unlimited. Any integer value", validators=[MinValueValidator(-1)])
max_disk_size = IntegerField(default=20, help_text="-1 for unlimited. Any integer value", validators=[MinValueValidator(-1)])
max_instances = IntegerField(default=2,
help_text="-1 for unlimited. Any integer value",
validators=[
MinValueValidator(-1),
])
max_cpus = IntegerField(
default=2,
help_text="-1 for unlimited. Any integer value",
validators=[MinValueValidator(-1)],
)
max_memory = IntegerField(
default=2048,
help_text="-1 for unlimited. Any integer value",
validators=[MinValueValidator(-1)],
)
max_disk_size = IntegerField(
default=20,
help_text="-1 for unlimited. Any integer value",
validators=[MinValueValidator(-1)],
)
@staticmethod
def create_missing_userattributes(user):
@ -50,7 +68,7 @@ class UserAttributes(Model):
instance = Instance.objects.get(name=instance_name)
user_instance = UserInstance(user=user, instance=instance)
user_instance.save()
@staticmethod
def configure_user(user):
UserAttributes.create_missing_userattributes(user)
@ -58,3 +76,16 @@ class UserAttributes(Model):
def __unicode__(self):
return self.user.username
class PermissionSet(Model):
"""
Dummy model for holding set of permissions we need to be automatically added by Django
"""
class Meta:
default_permissions = ()
permissions = (
('change_password', _('Can change password')),
)
managed = False

View file

@ -3,116 +3,120 @@
{% load tags_fingerprint %}
{% block title %}{% trans "Profile" %}{% endblock %}
{% block content %}
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h2 class="page-header">{% trans "Profile" %}</h2>
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h2 class="page-header">{% trans "Profile" %}</h2>
</div>
</div>
<!-- /.row -->
{% include 'errors_block.html' %}
<div class="row">
<div class="col-lg-12">
<h3 class="page-header">{% trans "Edit Profile" %}</h3>
<form method="post" action="" role="form" aria-label="Edit user info form">{% csrf_token %}
<div class="form-group">
<label class="col-sm-2 col-form-label">{% trans "Login" %}</label>
<div class="col-sm-4">
<input type="text" class="form-control" value="{{ request.user.username }}" disabled>
</div>
</div>
<!-- /.row -->
{% include 'errors_block.html' %}
<div class="row">
<div class="col-lg-12">
<h3 class="page-header">{% trans "Edit Profile" %}</h3>
<form method="post" action="" role="form" aria-label="Edit user info form">{% csrf_token %}
<div class="form-group">
<label class="col-sm-2 col-form-label">{% trans "Login" %}</label>
<div class="col-sm-4">
<input type="text" class="form-control" value="{{ request.user.username }}" disabled>
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Username" %}</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="username" value="{{ request.user.first_name }}" pattern="[0-9a-zA-Z]+">
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Email" %}</label>
<div class="col-sm-4">
<input type="email" class="form-control" name="email" value="{{ request.user.email }}">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">{% trans "Change" %}</button>
</div>
</div>
</form>
{% if show_profile_edit_password == 'True' %}
<h3 class="page-header">{% trans "Edit Password" %}</h3>
<form method="post" action="" role="form" aria-label="Edit user password form">{% csrf_token %}
<div class="form-group">
<label class="col-sm-2 col-form-label">{% trans "Old" %}</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="oldpasswd" value="">
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "New" %}</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="passwd1" value="">
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Retry" %}</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="passwd2" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">{% trans "Change" %}</button>
</div>
</div>
</form>
{% endif %}
<h3 class="page-header">{% trans "SSH Keys" %}</h3>
{% if publickeys %}
<div class="col-lg-12">
<div class="table-responsive">
<table class="table table-hover">
<tbody class="text-center">
{% for key in publickeys %}
<tr>
<td>{{ key.keyname }} ({% ssh_to_fingerprint key.keypublic %})</td>
<td>
<form action="" method="post" role="form" aria-label="Delete user public key form">{% csrf_token %}
<input type="hidden" name="keyid" value="{{ key.id }}"/>
<button type="submit" class="btn btn-sm btn-secondary" name="keydelete" title="{% trans "Delete" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
<span class="fa fa-trash"></span>
</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}
<form method="post" action="" role="form" aria-label="Add key to user form">{% csrf_token %}
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Key name" %}</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="keyname" placeholder="{% trans "Enter Name" %}">
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Public key" %}</label>
<div class="col-sm-8">
<textarea name="keypublic" class="form-control" rows="6" placeholder="{% trans "Enter Public Key" %}"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">{% trans "Add" %}</button>
</div>
</div>
</form>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Username" %}</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="username" value="{{ request.user.first_name }}"
pattern="[0-9a-zA-Z]+">
</div>
</div>
{% endblock %}
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Email" %}</label>
<div class="col-sm-4">
<input type="email" class="form-control" name="email" value="{{ request.user.email }}">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">{% trans "Change" %}</button>
</div>
</div>
</form>
{% if perms.accounts.change_password %}
<h3 class="page-header">{% trans "Edit Password" %}</h3>
<form method="post" action="" role="form" aria-label="Edit user password form">{% csrf_token %}
<div class="form-group">
<label class="col-sm-2 col-form-label">{% trans "Old" %}</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="oldpasswd" value="">
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "New" %}</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="passwd1" value="">
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Retry" %}</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="passwd2" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">{% trans "Change" %}</button>
</div>
</div>
</form>
{% endif %}
<h3 class="page-header">{% trans "SSH Keys" %}</h3>
{% if publickeys %}
<div class="col-lg-12">
<div class="table-responsive">
<table class="table table-hover">
<tbody class="text-center">
{% for key in publickeys %}
<tr>
<td>{{ key.keyname }} ({% ssh_to_fingerprint key.keypublic %})</td>
<td>
<form action="" method="post" role="form" aria-label="Delete user public key form">
{% csrf_token %}
<input type="hidden" name="keyid" value="{{ key.id }}" />
<button type="submit" class="btn btn-sm btn-secondary" name="keydelete"
title="{% trans "Delete" %}"
onclick="return confirm('{% trans "Are you sure?" %}')">
<span class="fa fa-trash"></span>
</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}
<form method="post" action="" role="form" aria-label="Add key to user form">{% csrf_token %}
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Key name" %}</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="keyname" placeholder="{% trans "Enter Name" %}">
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Public key" %}</label>
<div class="col-sm-8">
<textarea name="keypublic" class="form-control" rows="6"
placeholder="{% trans "Enter Public Key" %}"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">{% trans "Add" %}</button>
</div>
</div>
</form>
</div>
</div>
{% endblock %}

View file

@ -13,6 +13,7 @@ from instances.models import Instance
import sass
import os
def profile(request):
"""
:param request:
@ -22,7 +23,6 @@ def profile(request):
error_messages = []
# user = User.objects.get(id=request.user.id)
publickeys = UserSSHKey.objects.filter(user_id=request.user.id)
show_profile_edit_password = AppSettings.objects.get(key="SHOW_PROFILE_EDIT_PASSWORD").value
if request.method == 'POST':
if 'username' in request.POST:
@ -60,7 +60,8 @@ def profile(request):
msg = _("Invalid characters in public key")
error_messages.append(msg)
if not error_messages:
addkeypublic = UserSSHKey(user_id=request.user.id, keyname=keyname, keypublic=keypublic)
addkeypublic = UserSSHKey(
user_id=request.user.id, keyname=keyname, keypublic=keypublic)
addkeypublic.save()
return HttpResponseRedirect(request.get_full_path())
if 'keydelete' in request.POST:
@ -70,6 +71,7 @@ def profile(request):
return HttpResponseRedirect(request.get_full_path())
return render(request, 'profile.html', locals())
@superuser_only
def account(request, user_id):
"""
@ -103,17 +105,20 @@ def account(request, user_id):
return HttpResponseRedirect(request.get_full_path())
if 'add' in request.POST:
inst_id = request.POST.get('inst_id', '')
if AppSettings.objects.get(key="ALLOW_INSTANCE_MULTIPLE_OWNER").value == 'True':
check_inst = UserInstance.objects.filter(instance_id=int(inst_id), user_id=int(user_id))
check_inst = UserInstance.objects.filter(
instance_id=int(inst_id), user_id=int(user_id))
else:
check_inst = UserInstance.objects.filter(instance_id=int(inst_id))
check_inst = UserInstance.objects.filter(
instance_id=int(inst_id))
if check_inst:
msg = _("Instance already added")
error_messages.append(msg)
else:
add_user_inst = UserInstance(instance_id=int(inst_id), user_id=int(user_id))
add_user_inst = UserInstance(
instance_id=int(inst_id), user_id=int(user_id))
add_user_inst.save()
return HttpResponseRedirect(request.get_full_path())