mirror of
https://github.com/retspen/webvirtcloud
synced 2024-10-31 19:44:16 +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.contrib.auth.models import User
|
||||
from django.core.validators import MinValueValidator
|
||||
from django.db.models import (CASCADE, DO_NOTHING, BooleanField, CharField,
|
||||
ForeignKey, IntegerField, Model, OneToOneField)
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from instances.models import Instance
|
||||
|
||||
|
||||
class UserInstance(Model):
|
||||
user = ForeignKey(User, on_delete=CASCADE)
|
||||
instance = ForeignKey(Instance, on_delete=CASCADE)
|
||||
is_change = BooleanField(default=False)
|
||||
is_delete = BooleanField(default=False)
|
||||
is_vnc = BooleanField(default=False)
|
||||
class UserInstance(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
instance = models.ForeignKey(Instance, on_delete=models.CASCADE)
|
||||
is_change = models.BooleanField(default=False)
|
||||
is_delete = models.BooleanField(default=False)
|
||||
is_vnc = models.BooleanField(default=False)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.instance.name
|
||||
|
||||
|
||||
class UserSSHKey(Model):
|
||||
user = ForeignKey(User, on_delete=DO_NOTHING)
|
||||
keyname = CharField(max_length=25)
|
||||
keypublic = CharField(max_length=500)
|
||||
class UserSSHKey(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
|
||||
keyname = models.CharField(max_length=25)
|
||||
keypublic = models.CharField(max_length=500)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.keyname
|
||||
|
||||
|
||||
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(
|
||||
class UserAttributes(models.Model):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
max_instances = models.IntegerField(default=1,
|
||||
help_text="-1 for unlimited. Any integer value",
|
||||
validators=[
|
||||
MinValueValidator(-1),
|
||||
])
|
||||
max_cpus = models.IntegerField(
|
||||
default=1,
|
||||
help_text="-1 for unlimited. Any integer value",
|
||||
validators=[MinValueValidator(-1)],
|
||||
)
|
||||
max_memory = IntegerField(
|
||||
max_memory = models.IntegerField(
|
||||
default=2048,
|
||||
help_text="-1 for unlimited. Any integer value",
|
||||
validators=[MinValueValidator(-1)],
|
||||
)
|
||||
max_disk_size = IntegerField(
|
||||
max_disk_size = models.IntegerField(
|
||||
default=20,
|
||||
help_text="-1 for unlimited. Any integer value",
|
||||
validators=[MinValueValidator(-1)],
|
||||
|
@ -78,14 +76,12 @@ class UserAttributes(Model):
|
|||
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
|
||||
"""
|
||||
class Meta:
|
||||
default_permissions = ()
|
||||
permissions = (
|
||||
('change_password', _('Can change password')),
|
||||
)
|
||||
permissions = (('change_password', _('Can change password')), )
|
||||
|
||||
managed = False
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{% extends "base.html" %}
|
||||
{% load i18n %}
|
||||
{% load static %}
|
||||
{% load common_tags %}
|
||||
{% load font_awesome %}
|
||||
{% block title %}{% trans "Users" %}{% endblock %}
|
||||
{% block content %}
|
||||
|
@ -33,12 +34,13 @@
|
|||
<th>{% trans "Status" %}</th>
|
||||
<th>{% trans "Staff" %}</th>
|
||||
<th>{% trans "Superuser" %}</th>
|
||||
<th>{% trans "Clone" %}</th>
|
||||
<th>{% trans "Can Clone" %}</th>
|
||||
<th>{% trans "" %}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="searchable">
|
||||
{% for user in users %}
|
||||
{% has_perm user 'instances.clone_instances' as can_clone %}
|
||||
<tr class="{% if not user.is_active %}danger{% endif %}">
|
||||
<td>
|
||||
{{ user.username }}
|
||||
|
@ -52,7 +54,7 @@
|
|||
</td>
|
||||
<td>{% if user.is_staff %}{% icon 'check' %}{% 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>
|
||||
<div class="pull-right btn-group">
|
||||
<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
|
||||
|
||||
|
||||
|
@ -11,3 +14,15 @@ class Instance(Model):
|
|||
|
||||
def __unicode__(self):
|
||||
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""
|
||||
return 'class=active'
|
||||
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