Browse Source

add new group and set users to groups

Roberto Berto 5 years ago
parent
commit
5c9044a894

+ 1 - 1
README.md

@@ -23,7 +23,7 @@ It will work with a single VyoS server or to multiple VyOS servers, so datacente
 * logout - done
 * lost password - todo
 * create users - admin only todo
-* associate users to groups - admin only todo
+* associate users to groups - admin only done
 * associate groups to vyos servers - admin only todo
 * allow commum users to add vyos servers?
 * start page to create initial superadmin user on new installations - done

+ 25 - 0
vycenter/config/templates/config/group_add.html

@@ -0,0 +1,25 @@
+{% extends "base.html" %}
+
+{% block header_title %}Add new group{% endblock %}
+{% block section_title %}Add new group{% endblock %}
+
+{% block content %}
+
+{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
+
+<form action="{% url 'config:group-add' %}" method="post">
+{% csrf_token %}
+
+<p>
+<label for="alias">Name</label><br>
+<input type="text" name="name" id="name" value="{{ name }}" size="90">
+</p>
+
+
+<input type="submit" value="Add group">
+</form>
+
+
+
+{% endblock %}
+

+ 1 - 8
vycenter/config/templates/config/groups_list.html

@@ -19,14 +19,7 @@
 
     <table border="1" width="100%">
     <tr>
-        <th>alias</th>
-        <th>hostname</th>
-        <th>port</th>
-        <th>key</th>
-        <th>https</th>
-        <th>test connection</th>
-        <th>default</th>
-        <th>remove</th>
+        <th>name</th>
     </tr>
 
     {% for group in groups %}

+ 28 - 41
vycenter/config/templates/config/users_list.html

@@ -2,73 +2,60 @@
 
 {% block debug %}
 {{ users }}
+{{ groups }}
+{{ user_groups }}
 {% endblock %}
 
 
-{% block header_title %}Manage users{% endblock %}
-{% block section_title %}Manage users{% endblock %}
+{% block header_title %}List users{% endblock %}
+{% block section_title %}List users{% endblock %}
 
 {% block content %}
 
-{% if instances %}
 
-    <table border="1" width="100%">
-    <tr>
-        <th>alias</th>
-        <th>hostname</th>
-        <th>port</th>
-        <th>key</th>
-        <th>https</th>
-        <th>test connection</th>
-        <th>default</th>
-        <th>remove</th>
-    </tr>
 
-    {% for instance in instances %}
-    <tr>
-        <td>{{ instance.alias }}</td>
-        <td>{{ instance.hostname }}</td>
-        <td>{{ instance.port }}</td>
-        <td>show</td>
-        <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-default' instance.hostname %}">set default</a>{% endif %}</td>
-        <td>{% if instance.main == True %}-{% else %}<a href="{% url 'config:instance-remove' instance.hostname %}">remove</a>{% endif %}</td>
-    </tr>
 
-    {% endfor %}
 
-    </table>   
-{% else %}
-    <p>No instances.</p>
-{% endif %}
 
+{% if users %}
 
+    {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
 
+    <form action="{% url 'config:users-list' %}" method="post">
+    {% csrf_token %}
 
-{% if users %}
 
+    
     <table border="1" width="100%">
     <tr>
-        <th>alias</th>
-        <th>hostname</th>
-        <th>port</th>
-        <th>key</th>
-        <th>https</th>
-        <th>test connection</th>
-        <th>default</th>
-        <th>remove</th>
+        <th>name</th>
+        <th>group</th>
     </tr>
 
     {% for user in users %}
     <tr>
         <td>{{ user }}</td>
-    
+        <td><select id="group-{{ user }}" name="group-{{ user }}"><option value="">None</option>
+            {% if groups != False %}
+                {% for fkey in groups %}    
+                    <option value="{{ fkey }}" {% if user_groups|get_item:user.username == fkey %}selected="selected"{% endif %}>{{ fkey }}</option>
+                {% endfor %}
+            {% endif %}
+        </select></td>
     </tr>
 
     {% endfor %}
 
-    </table>   
+    </table>  
+    
+
+    <div class="separe-form" class="text-right">
+        <input type="submit" value="Set Groups">
+    </div>
+    
+    </form>
+    
+    
 {% else %}
     <p>No users.</p>
 {% endif %}

+ 1 - 0
vycenter/config/urls.py

@@ -9,6 +9,7 @@ urlpatterns = [
     path('', views.index, name='index'),
     path('users-list', views.users_list, name='users-list'),
     path('groups-list', views.groups_list, name='groups-list'),
+    path('group-add', views.group_add, name='group-add'),
     path('instance-add', views.instance_add, name='instance-add'),
     path('instance-conntry/<str:hostname>', views.instance_conntry, name='instance-conntry'),
     path('instance-default/<str:hostname>', views.instance_default, name='instance-default'),

+ 102 - 2
vycenter/config/views.py

@@ -4,7 +4,7 @@ from django.template import loader
 from django.shortcuts import redirect
 from django.conf import settings
 from django.urls import reverse
-
+from django.contrib.auth.models import Group
 
 import pprint
 import vyos
@@ -14,6 +14,26 @@ from .models import Instance
 from django.contrib.auth.models import User
 from django.contrib.auth.models import Group
 
+from django.template.defaultfilters import register
+
+@register.filter(name='dict_key')
+def dict_key(d, k):
+    '''Returns the given key from a dictionary.'''
+    return d[k]
+
+@register.filter('get_value_from_dict')
+def get_value_from_dict(dict_data, key):
+    """
+    usage example {{ your_dict|get_value_from_dict:your_key }}
+    """
+    if key:
+        return dict_data.get(key)
+
+@register.filter
+def get_item(dictionary, key):
+    return dictionary.get(key)
+
+
 
 def index(request):
     if not request.user.is_authenticated:
@@ -41,14 +61,62 @@ def users_list(request):
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
     users = User.objects.all()
+    groups = Group.objects.all()
+
+    group_show = []
+    for group in groups:
+        if group.name != "admin":
+            group_show.append(group.name)
 
 
+    has_group_add = False
+    for el in request.POST:
+
+        if el.startswith('group-') and request.POST[el]:
+            pos = el.split("-", 1)
+            
+            el_username = pos[1]
+            el_groupname = request.POST[el]
+            
+            # test also if username is member of admin or superuser, than this one should not being no group
+            if el_groupname not in ['admin']:
+                try:
+                    el_userid = User.objects.get(username=el_username) 
+                except User.DoesNotExist:
+                    return redirect('config:users_list')
+
+                try:
+                    # remove any group user is inside, we support just only group per user
+                    if el_userid.groups.exists():
+                        for g in el_userid.groups.all():
+                            el_userid.groups.remove(g)
+
+                    el_groupadd = Group.objects.get(name=el_groupname) 
+                    el_groupadd.user_set.add(el_userid)
+                    has_group_add = has_group_add  + 1
+                except Group.DoesNotExist:
+                    return redirect('config:users_list')
+
+    if has_group_add > 0:
+        return redirect('config:users-list')
+
+
+    user_groups = {}
+    for user in users:
+        user_groups_list = user.groups.all()
+        if len(user_groups_list) > 0:
+            user_groups[str(user)] = str(user_groups_list[0])
+        else:
+            user_groups[str(user)] = None
+
     template = loader.get_template('config/users_list.html')
     context = { 
         #'interfaces': interfaces,
         'instances': all_instances,
         'hostname_default': hostname_default,
-        'users' : users
+        'users' : users,
+        'groups': group_show,
+        'user_groups': user_groups
     }   
     return HttpResponse(template.render(context, request))
 
@@ -133,6 +201,38 @@ def instance_add(request):
     }   
     return HttpResponse(template.render(context, request))
 
+
+
+def group_add(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
+        
+    #interfaces = vyos.get_interfaces()
+    all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
+
+    error_message = None
+
+    if len(request.POST) > 0 and 'name' in request.POST:
+        try:
+            group_get = Group.objects.get(name=request.POST['name'])       
+            error_message = 'Group already exists'
+        except Group.DoesNotExist:
+            group_create = Group(name=request.POST['name'])
+            group_create.save()
+            return redirect('config:groups-list')
+    else:
+        instance_id = 0
+
+    template = loader.get_template('config/group_add.html')
+    context = { 
+        'hostname_default': hostname_default,
+        'instance_id': instance_id,
+        'instances': all_instances,
+        'error_message' : error_message
+    }   
+    return HttpResponse(template.render(context, request))    
+
 def instance_conntry(request, hostname):
     if not request.user.is_authenticated:
         return redirect('%s?next=%s' % (reverse('registration-login'), request.path))

+ 3 - 0
vycenter/vycenter/settings.py

@@ -22,6 +22,9 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 # SECURITY WARNING: keep the secret key used in production secret!
 SECRET_KEY = '*wv2=o(o5$i2qim7yxras_7jf%n!*1rrzehv3o2f-ebsr@ba%4'
 
+SESSION_EXPIRE_AT_BROWSER_CLOSE = True
+SESSION_COOKIE_AGE = 60 * 60 # 60 minutes
+
 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = True
 

+ 5 - 3
vycenter/vycenter/templates/base.html

@@ -102,9 +102,11 @@
     <ul>
     <li><a href="{% url 'config:instance-add' %}">Add new instance</a></li>
     <li><a href="{% url 'config:instances' %}">List instances</a></li>
-    <li><a href="{% url 'config:users-list' %}">Manage users</a></li>
-    <li><a href="{% url 'config:groups-list' %}">Manage groups</a></li>
-    </ul>
+    <li><a href="{% url 'config:users-list' %}">List users</a></li>
+    <li><a href="{% url 'config:groups-list' %}">List groups</a></li>
+    <li><a href="{% url 'config:group-add' %}">Add new group</a></li>
+
+  </ul>
 
 
     </div>