mirror of
https://github.com/retspen/webvirtcloud
synced 2024-12-24 15:15:22 +00:00
Added 'instances:clone_instances' permission
Replaces 'can_clone_instances' user attribute
This commit is contained in:
parent
a62daad87b
commit
3d0493537f
7 changed files with 127 additions and 30 deletions
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 2.2.12 on 2020-05-28 04:24
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('accounts', '0004_apply_change_password'),
|
||||||
|
('instances', '0003_migrate_can_clone_instances'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='userattributes',
|
||||||
|
name='can_clone_instances',
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,52 +1,50 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.core.validators import MinValueValidator
|
from django.core.validators import MinValueValidator
|
||||||
from django.db.models import (CASCADE, DO_NOTHING, BooleanField, CharField,
|
from django.db import models
|
||||||
ForeignKey, IntegerField, Model, OneToOneField)
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from instances.models import Instance
|
from instances.models import Instance
|
||||||
|
|
||||||
|
|
||||||
class UserInstance(Model):
|
class UserInstance(models.Model):
|
||||||
user = ForeignKey(User, on_delete=CASCADE)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
instance = ForeignKey(Instance, on_delete=CASCADE)
|
instance = models.ForeignKey(Instance, on_delete=models.CASCADE)
|
||||||
is_change = BooleanField(default=False)
|
is_change = models.BooleanField(default=False)
|
||||||
is_delete = BooleanField(default=False)
|
is_delete = models.BooleanField(default=False)
|
||||||
is_vnc = BooleanField(default=False)
|
is_vnc = models.BooleanField(default=False)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.instance.name
|
return self.instance.name
|
||||||
|
|
||||||
|
|
||||||
class UserSSHKey(Model):
|
class UserSSHKey(models.Model):
|
||||||
user = ForeignKey(User, on_delete=DO_NOTHING)
|
user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
|
||||||
keyname = CharField(max_length=25)
|
keyname = models.CharField(max_length=25)
|
||||||
keypublic = CharField(max_length=500)
|
keypublic = models.CharField(max_length=500)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.keyname
|
return self.keyname
|
||||||
|
|
||||||
|
|
||||||
class UserAttributes(Model):
|
class UserAttributes(models.Model):
|
||||||
user = OneToOneField(User, on_delete=CASCADE)
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||||
can_clone_instances = BooleanField(default=True)
|
max_instances = models.IntegerField(default=1,
|
||||||
max_instances = IntegerField(default=1,
|
help_text="-1 for unlimited. Any integer value",
|
||||||
help_text="-1 for unlimited. Any integer value",
|
validators=[
|
||||||
validators=[
|
MinValueValidator(-1),
|
||||||
MinValueValidator(-1),
|
])
|
||||||
])
|
max_cpus = models.IntegerField(
|
||||||
max_cpus = IntegerField(
|
|
||||||
default=1,
|
default=1,
|
||||||
help_text="-1 for unlimited. Any integer value",
|
help_text="-1 for unlimited. Any integer value",
|
||||||
validators=[MinValueValidator(-1)],
|
validators=[MinValueValidator(-1)],
|
||||||
)
|
)
|
||||||
max_memory = IntegerField(
|
max_memory = models.IntegerField(
|
||||||
default=2048,
|
default=2048,
|
||||||
help_text="-1 for unlimited. Any integer value",
|
help_text="-1 for unlimited. Any integer value",
|
||||||
validators=[MinValueValidator(-1)],
|
validators=[MinValueValidator(-1)],
|
||||||
)
|
)
|
||||||
max_disk_size = IntegerField(
|
max_disk_size = models.IntegerField(
|
||||||
default=20,
|
default=20,
|
||||||
help_text="-1 for unlimited. Any integer value",
|
help_text="-1 for unlimited. Any integer value",
|
||||||
validators=[MinValueValidator(-1)],
|
validators=[MinValueValidator(-1)],
|
||||||
|
@ -78,14 +76,12 @@ class UserAttributes(Model):
|
||||||
return self.user.username
|
return self.user.username
|
||||||
|
|
||||||
|
|
||||||
class PermissionSet(Model):
|
class PermissionSet(models.Model):
|
||||||
"""
|
"""
|
||||||
Dummy model for holding set of permissions we need to be automatically added by Django
|
Dummy model for holding set of permissions we need to be automatically added by Django
|
||||||
"""
|
"""
|
||||||
class Meta:
|
class Meta:
|
||||||
default_permissions = ()
|
default_permissions = ()
|
||||||
permissions = (
|
permissions = (('change_password', _('Can change password')), )
|
||||||
('change_password', _('Can change password')),
|
|
||||||
)
|
|
||||||
|
|
||||||
managed = False
|
managed = False
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
{% load static %}
|
{% load static %}
|
||||||
|
{% load common_tags %}
|
||||||
{% load font_awesome %}
|
{% load font_awesome %}
|
||||||
{% block title %}{% trans "Users" %}{% endblock %}
|
{% block title %}{% trans "Users" %}{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
@ -33,12 +34,13 @@
|
||||||
<th>{% trans "Status" %}</th>
|
<th>{% trans "Status" %}</th>
|
||||||
<th>{% trans "Staff" %}</th>
|
<th>{% trans "Staff" %}</th>
|
||||||
<th>{% trans "Superuser" %}</th>
|
<th>{% trans "Superuser" %}</th>
|
||||||
<th>{% trans "Clone" %}</th>
|
<th>{% trans "Can Clone" %}</th>
|
||||||
<th>{% trans "" %}</th>
|
<th>{% trans "" %}</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody class="searchable">
|
<tbody class="searchable">
|
||||||
{% for user in users %}
|
{% for user in users %}
|
||||||
|
{% has_perm user 'instances.clone_instances' as can_clone %}
|
||||||
<tr class="{% if not user.is_active %}danger{% endif %}">
|
<tr class="{% if not user.is_active %}danger{% endif %}">
|
||||||
<td>
|
<td>
|
||||||
{{ user.username }}
|
{{ user.username }}
|
||||||
|
@ -52,7 +54,7 @@
|
||||||
</td>
|
</td>
|
||||||
<td>{% if user.is_staff %}{% icon 'check' %}{% endif %}</td>
|
<td>{% if user.is_staff %}{% icon 'check' %}{% endif %}</td>
|
||||||
<td>{% if user.is_superuser %}{% icon 'check' %}</span>{% endif %}</td>
|
<td>{% if user.is_superuser %}{% icon 'check' %}</span>{% endif %}</td>
|
||||||
<td>{% if user.userattributes.can_clone_instances %}{% icon 'check' %}{% endif %}</td>
|
<td>{% if can_clone %}{% icon 'check' %}{% endif %}</td>
|
||||||
<td>
|
<td>
|
||||||
<div class="pull-right btn-group">
|
<div class="pull-right btn-group">
|
||||||
<a class="btn btn-success" title="{%trans "View Profile" %}" href="{% url 'account' user.id %}">{% icon 'eye' %}</a>
|
<a class="btn btn-success" title="{%trans "View Profile" %}" href="{% url 'account' user.id %}">{% icon 'eye' %}</a>
|
||||||
|
|
24
instances/migrations/0002_permissionset.py
Normal file
24
instances/migrations/0002_permissionset.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# Generated by Django 2.2.12 on 2020-05-27 07:01
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('instances', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='PermissionSet',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'permissions': (('clone_instances', 'Can clone instances'),),
|
||||||
|
'managed': False,
|
||||||
|
'default_permissions': (),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
35
instances/migrations/0003_migrate_can_clone_instances.py
Normal file
35
instances/migrations/0003_migrate_can_clone_instances.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_can_clone_instances(apps, schema_editor):
|
||||||
|
from django.contrib.auth.models import User, Permission
|
||||||
|
user: User
|
||||||
|
users = User.objects.all()
|
||||||
|
|
||||||
|
permission = Permission.objects.get(codename='clone_instances')
|
||||||
|
|
||||||
|
for user in users:
|
||||||
|
if user.userattributes.can_clone_instances:
|
||||||
|
user.user_permissions.add(permission)
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_can_clone_instances(apps, schema_editor):
|
||||||
|
from django.contrib.auth.models import User, Permission
|
||||||
|
user: User
|
||||||
|
users = User.objects.all()
|
||||||
|
|
||||||
|
permission = Permission.objects.get(codename='clone_instances')
|
||||||
|
|
||||||
|
for user in users:
|
||||||
|
user.user_permissions.remove(permission)
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('instances', '0002_permissionset'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(migrate_can_clone_instances, reverse_can_clone_instances),
|
||||||
|
]
|
|
@ -1,4 +1,7 @@
|
||||||
from django.db.models import Model, ForeignKey, CharField, BooleanField, DateField, CASCADE
|
from django.db.models import (CASCADE, BooleanField, CharField, DateField,
|
||||||
|
ForeignKey, Model)
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from computes.models import Compute
|
from computes.models import Compute
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,3 +14,15 @@ class Instance(Model):
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
class PermissionSet(Model):
|
||||||
|
"""
|
||||||
|
Dummy model for holding set of permissions we need to be automatically added by Django
|
||||||
|
"""
|
||||||
|
class Meta:
|
||||||
|
default_permissions = ()
|
||||||
|
permissions = (
|
||||||
|
('clone_instances', _('Can clone instances')),
|
||||||
|
)
|
||||||
|
|
||||||
|
managed = False
|
||||||
|
|
|
@ -24,3 +24,10 @@ def class_active(request, pattern):
|
||||||
# Not sure why 'class="active"' returns class=""active""
|
# Not sure why 'class="active"' returns class=""active""
|
||||||
return 'class=active'
|
return 'class=active'
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
@register.simple_tag
|
||||||
|
def has_perm(user, permission_codename):
|
||||||
|
if user.has_perm(permission_codename):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
Loading…
Reference in a new issue