From 753384aa3b35378d2f4522b723d754d19bc06203 Mon Sep 17 00:00:00 2001
From: Kálmán Viktor <kviktor@cloud.bme.hu>
Date: Thu, 5 Jun 2014 16:02:43 +0200
Subject: [PATCH] network: vlan acl

---
 circle/network/static/js/network.js             |  4 ++++
 circle/network/static/network/network.css       |  8 ++++++++
 circle/network/templates/network/base.html      |  2 +-
 circle/network/templates/network/vlan-edit.html | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 circle/network/urls.py                          |  5 ++++-
 circle/network/views.py                         | 17 +++++++++++++++++
 6 files changed, 99 insertions(+), 6 deletions(-)
 create mode 100644 circle/network/static/network/network.css

diff --git a/circle/network/static/js/network.js b/circle/network/static/js/network.js
index 6189c34..e59d0e8 100644
--- a/circle/network/static/js/network.js
+++ b/circle/network/static/js/network.js
@@ -27,3 +27,7 @@ function getURLParameter(name) {
         (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
     );
 }
+
+$(function() {
+  $("[title]").tooltip();
+});
diff --git a/circle/network/static/network/network.css b/circle/network/static/network/network.css
new file mode 100644
index 0000000..c13fdd8
--- /dev/null
+++ b/circle/network/static/network/network.css
@@ -0,0 +1,8 @@
+.table-with-form-fields tbody tr td {
+  line-height: 34px;
+}
+
+#vlan-access-table th:last-child, #vlan-access-table td:last-child {
+  text-align: center;
+}
+
diff --git a/circle/network/templates/network/base.html b/circle/network/templates/network/base.html
index bc27643..0430382 100644
--- a/circle/network/templates/network/base.html
+++ b/circle/network/templates/network/base.html
@@ -12,6 +12,7 @@
     <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
     <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css">
     <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet" />
+    <link href="{% static "network/network.css" %}" rel="stylesheet">
     <style type="text/css">
     body {
       padding-top:40px;
@@ -35,7 +36,6 @@
     <!--[if lt IE 9]>
       <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
     <![endif]-->
-    <!--<link href="{% static "css/network.css" %}" rel="stylesheet">-->
     {% block extra_css %}{% endblock %}
   </head>
   <body>
diff --git a/circle/network/templates/network/vlan-edit.html b/circle/network/templates/network/vlan-edit.html
index 95c2b59..95a5ae8 100644
--- a/circle/network/templates/network/vlan-edit.html
+++ b/circle/network/templates/network/vlan-edit.html
@@ -12,11 +12,72 @@
 </div>
 
 <div class="row">
-    <div class="col-sm-8">
-        {% crispy form %}
+  <div class="col-sm-6">
+    {% crispy form %}
+  </div>
+  <div class="col-sm-6">
+    <div class="page-header">
+      <h3>{% trans "Host list" %}</h3>
     </div>
-    <div class="col-sm-4">
-        {% render_table host_list %}
+    {% render_table host_list %}
+    <div class="page-header">
+      <h3>{% trans "Manage access" %}</h3>
     </div>
+    <form action="{% url "network.vlan-acl" vid=vlan_vid %}" method="post">{% csrf_token %}
+      <table class="table table-striped table-with-form-fields" id="vlan-access-table">
+        <thead>
+          <tr>
+            <th></th>
+            <th>{% trans "Who" %}</th>
+            <th>{% trans "What" %}</th>
+            <th><i class="icon-remove"></i></th>
+        </tr></thead>
+        <tbody>
+            {% for i in acl.users %}
+            <tr>
+              <td><i class="icon-user"></i></td><td>{{i.user}}</td>
+              <td>
+                <select class="form-control" name="perm-u-{{i.user.id}}">
+                  {% for id, name in acl.levels %}
+                  <option{%if id = i.level%} selected="selected"{%endif%} value="{{id}}">{{name}}</option>
+                  {% endfor %}
+                </select>
+              </td>
+              <td>
+                <input type="checkbox" name="remove-u-{{i.user.id}}" title="{% trans "Remove" %}"/>
+              </td>
+            </tr>
+            {% endfor %}
+            {% for i in acl.groups %}
+            <tr>
+              <td><i class="icon-group"></i></td><td>{{i.group}}</td>
+              <td>
+                <select class="form-control" name="perm-g-{{i.group.id}}">
+                  {% for id, name in acl.levels %}
+                  <option{%if id = i.level%} selected="selected"{%endif%} value="{{id}}">{{name}}</option>
+                  {% endfor %}
+                </select>
+              </td>
+              <td>
+                <input type="checkbox" name="remove-g-{{i.group.id}}" title="{% trans "Remove" %}"/>
+              </td>
+            </tr>
+            {% endfor %}
+            <tr><td><i class="icon-plus"></i></td>
+                <td><input type="text" class="form-control" name="perm-new-name"
+                    placeholder="{% trans "Name of group or user" %}"></td>
+                <td><select class="form-control" name="perm-new">
+                        {% for id, name in acl.levels %}
+                        <option value="{{id}}">{{name}}</option>
+                        {% endfor %}
+                </select></td><td></td>
+            </tr>
+      </tbody>
+      </table>
+      <div class="form-actions">
+        <button type="submit" class="btn btn-success">{% trans "Save" %}</button>
+      </div>
+    </form>
+  </div>
 </div>
 {% endblock %}
diff --git a/circle/network/urls.py b/circle/network/urls.py
index e402a4a..fb4c328 100644
--- a/circle/network/urls.py
+++ b/circle/network/urls.py
@@ -30,7 +30,8 @@ from .views import (IndexView,
                     VlanGroupList, VlanGroupDetail, VlanGroupDelete,
                     VlanGroupCreate,
                     remove_host_group, add_host_group,
-                    remove_switch_port_device, add_switch_port_device)
+                    remove_switch_port_device, add_switch_port_device,
+                    VlanAclUpdateView)
 
 urlpatterns = patterns(
     '',
@@ -83,6 +84,8 @@ urlpatterns = patterns(
     url('^vlans/$', VlanList.as_view(), name='network.vlan_list'),
     url('^vlans/create$', VlanCreate.as_view(), name='network.vlan_create'),
     url('^vlans/(?P<vid>\d+)/$', VlanDetail.as_view(), name='network.vlan'),
+    url('^vlans/(?P<vid>\d+)/acl/$', VlanAclUpdateView.as_view(),
+        name='network.vlan-acl'),
     url('^vlans/delete/(?P<vid>\d+)/$', VlanDelete.as_view(),
         name="network.vlan_delete"),
     url('^vlangroups/$', VlanGroupList.as_view(),
diff --git a/circle/network/views.py b/circle/network/views.py
index a0566c9..e880753 100644
--- a/circle/network/views.py
+++ b/circle/network/views.py
@@ -41,6 +41,7 @@ from braces.views import LoginRequiredMixin, SuperuserRequiredMixin
 from operator import itemgetter
 from itertools import chain
 import json
+from dashboard.views import AclUpdateView
 
 
 class SuccessMessageMixin(FormMixin):
@@ -628,6 +629,21 @@ class VlanList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView):
     table_pagination = False
 
 
+def get_vlan_acl_data(obj):
+    levels = obj.ACL_LEVELS
+    users = obj.get_users_with_level()
+    users = [{'user': u, 'level': l} for u, l in users]
+    groups = obj.get_groups_with_level()
+    groups = [{'group': g, 'level': l} for g, l in groups]
+    return {'users': users, 'groups': groups, 'levels': levels}
+
+
+class VlanAclUpdateView(AclUpdateView):
+    model = Vlan
+    slug_field = "vid"
+    slug_url_kwarg = "vid"
+
+
 class VlanDetail(LoginRequiredMixin, SuperuserRequiredMixin,
                  SuccessMessageMixin, UpdateView):
     model = Vlan
@@ -646,6 +662,7 @@ class VlanDetail(LoginRequiredMixin, SuperuserRequiredMixin,
 
         context['host_list'] = SmallHostTable(q)
         context['vlan_vid'] = self.kwargs.get('vid')
+        context['acl'] = get_vlan_acl_data(self.get_object())
         return context
 
     success_url = reverse_lazy('network.vlan_list')
--
libgit2 0.26.0