Quellcode durchsuchen

set which group had perm to access each instance #10

Roberto Berto vor 5 Jahren
Ursprung
Commit
363df7c0eb

+ 20 - 0
vycenter/config/migrations/0007_instance_group.py

@@ -0,0 +1,20 @@
+# Generated by Django 3.0.5 on 2020-05-08 06:16
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('auth', '0011_update_proxy_permissions'),
+        ('config', '0006_auto_20200427_2202'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='instance',
+            name='group',
+            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='auth.Group'),
+        ),
+    ]

+ 3 - 0
vycenter/config/models.py

@@ -1,4 +1,5 @@
 from django.db import models
+from django.contrib.auth.models import Group
 
 
 class Instance(models.Model):
@@ -8,6 +9,8 @@ class Instance(models.Model):
     key = models.CharField(max_length=100)
     https = models.BooleanField()
     main = models.BooleanField(default=False)
+    group = models.ForeignKey(Group, null=True, on_delete=models.SET_NULL)
+
     
 
 

+ 19 - 0
vycenter/config/templates/config/instances.html

@@ -3,6 +3,12 @@
 {% block header_title %}List instances{% endblock %}
 {% block section_title %}List instances{% endblock %}
 
+{% block debug %}
+    {% for instance in instances %}
+    {{ instance }} - {{ instance.group.name }}
+    {% endfor %}
+{% endblock %}
+
 {% block content %}
 
 {% if instances %}
@@ -16,6 +22,7 @@
         <th>https</th>
         <th>test connection</th>
         <th>default</th>
+        <th>group access</th>
         <th>remove</th>
     </tr>
 
@@ -28,6 +35,18 @@
         <td>{{ instance.https }}</td>
         <td><a href="{% url 'config:instance-conntry' instance.hostname %}">test</a></td>
         <td>{% if instance.main == True %}default{% else %}<a href="{% url 'config:instance-change' instance.hostname %}">set default</a>{% endif %}</td>
+        <td><form action="{% url 'config:instance-changegroup' instance.hostname %}" method="post">
+            {% csrf_token %}
+
+            <select name="group" onchange="this.form.submit()">
+                {% if groups %}
+                    {% for group in groups %}
+                        <option value="{{ group }}" {% if group == instance.group %}selected{% endif %}>{{ group }}</option>
+                    {% endfor %}
+                {% endif %}
+            </select>
+        </form>
+        </td>
         <td>{% if instance.main == True %}-{% else %}<a href="{% url 'config:instance-remove' instance.hostname %}">remove</a>{% endif %}</td>
     </tr>
 

+ 1 - 0
vycenter/config/urls.py

@@ -15,6 +15,7 @@ urlpatterns = [
     path('instance-add', views.instance_add, name='instance-add'),
     path('instance-conntry/<str:hostname>', views.instance_conntry, name='instance-conntry'),
     path('instance-remove/<str:hostname>', views.instance_remove, name='instance-remove'),
+    path('instance-changegroup/<str:hostname>', views.instance_changegroup, name='instance-changegroup'),
     path('instances', views.instances, name='instances'),
 
 ]

+ 34 - 1
vycenter/config/views.py

@@ -31,6 +31,10 @@ def index(request):
         
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
+    for instance in all_instances:
+        if group == None:
+            all_instance[instance]['group'] = "admin"
+            
     hostname_default = vyos.get_hostname_prefered(request)
 
 
@@ -166,11 +170,14 @@ def instances(request):
         else:
             return redirect('config:instance-add')
 
+    groups = Group.objects.all()
+
+
     template = loader.get_template('config/instances.html')
     context = { 
         'instances': all_instances,
         'hostname_default': hostname_default,
-
+        'groups' : groups,
     }   
     return HttpResponse(template.render(context, request))
 
@@ -374,5 +381,31 @@ def instance_remove(request, hostname):
     return redirect('config:instances')
 
 
+def instance_changegroup(request, hostname):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
+        
+    all_instances = vyos.instance_getall()
+
+    group_name = request.POST.get('group')
+    print(group_name, hostname)
+
+    if group_name == "__admin__":
+        instance = Instance.objects.get(hostname=hostname)
+        instance.group = None
+        instance.save()
+    else:
+        group = Group.objects.get(name=group_name)
+        instance = Instance.objects.get(hostname=hostname)
+        instance.group = group
+        instance.save()
+
+
+    return redirect('config:instances')
+
+
+
+
+
 
 

+ 1 - 1
vycenter/interface/templates/interface/index.html

@@ -1,6 +1,6 @@
 {% extends "base.html" %}
 
-{% block header_title %}interfaces List{% endblock %}
+{% block header_title %}Interfaces List{% endblock %}
 {% block section_title %}Interfaces List{% endblock %}
 
 {% block debug %}