From 8da2313d9791c27c6e67511bb0e392aec73ec7c4 Mon Sep 17 00:00:00 2001 From: lombardofr Date: Sun, 2 Jun 2019 17:18:48 +0200 Subject: [PATCH] fix on users and projects Change-Id: Ibb4258095eca92685e76187b66fca003f834dc6e Signed-off-by: lombardofr --- build-debpkg.sh | 2 +- lib/osm/osmclient/clientv2.py | 92 +++++++- .../project/osm/osm_project_left_sidebar.html | 17 +- .../template/project/projectlist.html | 45 +++- rolehandler/__init__.py | 0 rolehandler/apps.py | 8 + rolehandler/migrations/__init__.py | 0 rolehandler/models.py | 6 + rolehandler/templates/modal/role_create.html | 38 +++ rolehandler/templates/modal/role_edit.html | 38 +++ rolehandler/templates/role_list.html | 222 ++++++++++++++++++ rolehandler/urls.py | 26 ++ rolehandler/views.py | 96 ++++++++ sf_t3d/settings.py | 1 + sf_t3d/urls.py | 1 + static/src/rolehandler/role_list.js | 58 +++++ userhandler/templates/modal/user_edit.html | 4 +- userhandler/urls.py | 4 +- userhandler/views.py | 14 ++ 19 files changed, 658 insertions(+), 14 deletions(-) create mode 100644 rolehandler/__init__.py create mode 100644 rolehandler/apps.py create mode 100644 rolehandler/migrations/__init__.py create mode 100644 rolehandler/models.py create mode 100644 rolehandler/templates/modal/role_create.html create mode 100644 rolehandler/templates/modal/role_edit.html create mode 100644 rolehandler/templates/role_list.html create mode 100644 rolehandler/urls.py create mode 100644 rolehandler/views.py create mode 100644 static/src/rolehandler/role_list.js diff --git a/build-debpkg.sh b/build-debpkg.sh index 19565e1..607bfe5 100755 --- a/build-debpkg.sh +++ b/build-debpkg.sh @@ -15,7 +15,7 @@ # under the License. -PKG_DIRECTORIES="authosm descriptorhandler instancehandler lib projecthandler sdnctrlhandler sf_t3d static template userhandler vimhandler packagehandler netslicehandler wimhandler" +PKG_DIRECTORIES="authosm descriptorhandler instancehandler lib projecthandler sdnctrlhandler sf_t3d static template userhandler vimhandler packagehandler netslicehandler wimhandler rolehandler" PKG_FILES="bower.json django.ini LICENSE manage.py nginx-app.conf README.md requirements.txt supervisor-app.conf .bowerrc entrypoint.sh" MDG_NAME=lightui DEB_INSTALL=debian/osm-${MDG_NAME}.install diff --git a/lib/osm/osmclient/clientv2.py b/lib/osm/osmclient/clientv2.py index 52e20b7..384eeaa 100644 --- a/lib/osm/osmclient/clientv2.py +++ b/lib/osm/osmclient/clientv2.py @@ -73,6 +73,97 @@ class Client(object): return result + def role_list(self, token): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/admin/v1/roles".format(self._base_path) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code in (200, 201, 202, 204): + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + + return result + + def role_create(self, token, role_data): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/admin/v1/roles".format(self._base_path) + + try: + r = requests.post(_url, json=role_data, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code in (200, 201, 202, 204): + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def role_update(self, token, role_id, role_data): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/admin/v1/roles/{1}".format(self._base_path, role_id) + + try: + r = requests.patch(_url, json=role_data, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code in (200, 201, 202, 204): + result['error'] = False + else: + result['data'] = Util.json_loads_byteified(r.text) + return result + + def role_delete(self, token, id, force=None): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + query_path = '' + if force: + query_path = '?FORCE=true' + _url = "{0}/admin/v1/roles/{1}{2}".format(self._base_path, id, query_path) + try: + r = requests.delete(_url, params=None, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code in (200, 201, 202, 204): + result['error'] = False + else: + result['data'] = Util.json_loads_byteified(r.text) + return result + + def role_get(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/admin/v1/roles/{1}".format(self._base_path, id) + try: + r = requests.delete(_url, params=None, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code in (200, 201, 202, 204): + result['error'] = False + else: + result['data'] = Util.json_loads_byteified(r.text) + return result + def user_list(self, token): result = {'error': True, 'data': ''} headers = {"Content-Type": "application/json", "accept": "application/json", @@ -230,7 +321,6 @@ class Client(object): return result if r.status_code in (200, 201, 202, 204): result['error'] = False - result['data'] = Util.json_loads_byteified(r.text) return result def project_delete(self, token, id): diff --git a/projecthandler/template/project/osm/osm_project_left_sidebar.html b/projecthandler/template/project/osm/osm_project_left_sidebar.html index 7067ca7..7259834 100644 --- a/projecthandler/template/project/osm/osm_project_left_sidebar.html +++ b/projecthandler/template/project/osm/osm_project_left_sidebar.html @@ -107,18 +107,27 @@ {% if user.is_admin %}
  • ADMIN
  • + {% url "projects:projects_list" as proj_list_url %} +
  • + + Projects + +
  • {% url "users:list" as user_list_url %}
  • Users
  • - {% url "projects:projects_list" as proj_list_url %} -
  • - - Projects + {%comment%} + {% url "roles:list" as role_list_url %} +
  • + + Roles
  • + {%endcomment%} + {% endif %} diff --git a/projecthandler/template/project/projectlist.html b/projecthandler/template/project/projectlist.html index 28888fc..62282ad 100644 --- a/projecthandler/template/project/projectlist.html +++ b/projecthandler/template/project/projectlist.html @@ -67,8 +67,9 @@ + + + + + + +{% endblock %} + +{% block footer %} + {% include "footer.html" %} +{% endblock %} \ No newline at end of file diff --git a/rolehandler/urls.py b/rolehandler/urls.py new file mode 100644 index 0000000..c37325d --- /dev/null +++ b/rolehandler/urls.py @@ -0,0 +1,26 @@ +# +# Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from django.conf.urls import url +from rolehandler import views + +urlpatterns = [ + url(r'^list$', views.role_list, name='list'), + url(r'^create$', views.create, name='create'), + url(r'^(?P[0-9a-zA-Z]+)/delete$', views.delete, name='delete'), + url(r'^(?P[0-9a-zA-Z]+)', views.update, name='update') + +] diff --git a/rolehandler/views.py b/rolehandler/views.py new file mode 100644 index 0000000..f6a0799 --- /dev/null +++ b/rolehandler/views.py @@ -0,0 +1,96 @@ +# +# Copyright 2019 EveryUP Srl +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from django.shortcuts import render, redirect +from sf_t3d.decorators import login_required +from django.http import HttpResponse +import json +import logging +import authosm.utils as osmutils +from lib.osm.osmclient.clientv2 import Client + +logging.basicConfig(level=logging.DEBUG) +log = logging.getLogger(__name__) + + +@login_required +def role_list(request): + user = osmutils.get_user(request) + client = Client() + result = client.role_list(user.get_token()) + result = { + 'roles': result['data'] if result and result['error'] is False else [] + } + return __response_handler(request, result, 'role_list.html') + + +@login_required +def create(request): + user = osmutils.get_user(request) + client = Client() + role_data ={ + 'name' + } + result = client.role_create(user.get_token(), role_data) + if result['error']: + return __response_handler(request, result['data'], url=None, + status=result['data']['status'] if 'status' in result['data'] else 500) + else: + return __response_handler(request, {}, url=None, status=200) + + +@login_required +def delete(request, role_id=None): + user = osmutils.get_user(request) + try: + client = Client() + result = client.role_delete(user.get_token(), role_id) + except Exception as e: + log.exception(e) + result = {'error': True, 'data': str(e)} + if result['error']: + return __response_handler(request, result['data'], url=None, + status=result['data']['status'] if 'status' in result['data'] else 500) + else: + return __response_handler(request, {}, url=None, status=200) + +@login_required +def update(request, role_id=None): + user = osmutils.get_user(request) + try: + client = Client() + payload = {} + if request.POST.get('name') and request.POST.get('name') is not '': + payload["name"] = request.POST.get('name') + update_res = client.role_update(user.get_token(), role_id, payload) + except Exception as e: + log.exception(e) + update_res = {'error': True, 'data': str(e)} + if update_res['error']: + return __response_handler(request, update_res['data'], url=None, + status=update_res['data']['status'] if 'status' in update_res['data'] else 500) + else: + return __response_handler(request, {}, url=None, status=200) + + +def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs): + raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') + if 'application/json' in raw_content_types or url is None: + return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs) + elif to_redirect: + return redirect(url, *args, **kwargs) + else: + return render(request, url, data_res) diff --git a/sf_t3d/settings.py b/sf_t3d/settings.py index 768152a..644cba9 100644 --- a/sf_t3d/settings.py +++ b/sf_t3d/settings.py @@ -61,6 +61,7 @@ INSTALLED_APPS = [ 'instancehandler', 'sdnctrlhandler', 'userhandler', + 'rolehandler', 'netslicehandler' ] diff --git a/sf_t3d/urls.py b/sf_t3d/urls.py index d13bd0f..9c821b1 100644 --- a/sf_t3d/urls.py +++ b/sf_t3d/urls.py @@ -31,6 +31,7 @@ urlpatterns = [ url(r'^instances/', include('instancehandler.urls', namespace='instances'), name='instances_base'), url(r'^netslices/', include('netslicehandler.urls', namespace='netslices'), name='netslices_base'), url(r'^admin/users/', include('userhandler.urls', namespace='users'), name='users_base'), + url(r'^admin/roles/', include('rolehandler.urls', namespace='roles'), name='roles_base'), url(r'^forbidden', views.forbidden, name='forbidden'), ] diff --git a/static/src/rolehandler/role_list.js b/static/src/rolehandler/role_list.js new file mode 100644 index 0000000..fc19f52 --- /dev/null +++ b/static/src/rolehandler/role_list.js @@ -0,0 +1,58 @@ +/* + Copyright 2019 EveryUP srl + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +function openModalCreateRole(args) { + + + + $('#modal_new_role').modal('show'); +} + +function openModalEditRole(args) { + var url = '/admin/roles/' + args.role_id; + + $("#formEditRole").attr("action", url); + + + $('#modal_edit_role').modal('show'); +} + +function deleteRole(role_id, name) { + var delete_url = '/admin/roles/' + role_id + '/delete'; + bootbox.confirm("Are you sure want to delete " + name + "?", function (confirm) { + if (confirm) { + var dialog = bootbox.dialog({ + message: '
    Loading...
    ', + closeButton: false + }); + $.ajax({ + url: delete_url, + dataType: "json", + contentType: "application/json;charset=utf-8", + success: function (result) { + //$('#modal_show_vim_body').empty(); + dialog.modal('hide'); + table.ajax.reload(); + }, + error: function (result) { + dialog.modal('hide'); + bootbox.alert("An error occurred."); + } + }); + } + }) + +} \ No newline at end of file diff --git a/userhandler/templates/modal/user_edit.html b/userhandler/templates/modal/user_edit.html index 21dbe8a..49eda46 100644 --- a/userhandler/templates/modal/user_edit.html +++ b/userhandler/templates/modal/user_edit.html @@ -27,14 +27,14 @@
    - +
    - +