From 13b66dd32874c502e370853d63beb5a435bdf676 Mon Sep 17 00:00:00 2001 From: Avijit Saha Date: Wed, 20 Sep 2017 12:44:22 -0400 Subject: [PATCH] Addition of passwords manager feature on UI --- Web_Server/static/app_js/pwd_mngr.js | 98 ++++++++++ .../common/templates/common/side_nav.html | 1 + Web_Server/webapps/discovery/forms.py | 2 +- .../webapps/discovery/templates/__init__.py | 0 .../discovery/templates/discovery/__init__.py | 0 .../templates/discovery/manual_discovery.html | 2 +- .../templates/discovery/password_manager.html | 181 ++++++++++++++++++ Web_Server/webapps/discovery/urls.py | 4 +- Web_Server/webapps/discovery/views.py | 49 +++++ 9 files changed, 334 insertions(+), 3 deletions(-) create mode 100644 Web_Server/static/app_js/pwd_mngr.js delete mode 100644 Web_Server/webapps/discovery/templates/__init__.py delete mode 100644 Web_Server/webapps/discovery/templates/discovery/__init__.py create mode 100755 Web_Server/webapps/discovery/templates/discovery/password_manager.html diff --git a/Web_Server/static/app_js/pwd_mngr.js b/Web_Server/static/app_js/pwd_mngr.js new file mode 100644 index 0000000..5955dd4 --- /dev/null +++ b/Web_Server/static/app_js/pwd_mngr.js @@ -0,0 +1,98 @@ + + +$( document ).ready(function() { + $.csrftoken(); + $("#id_form-0-remove").attr('disabled','disabled'); + + $("#new_row").click(function(evt) { + evt.preventDefault(); + addForm(this, 'form'); + }); + + $(".delete_row").click(function(evt) { + evt.preventDefault(); + deleteForm(this, 'form'); + }); + + function updateElementIndex(el, prefix, ndx) { + var id_regex = new RegExp('(' + prefix + '-\\d+)'); + var replacement = prefix + '-' + ndx; + if ($(el).attr("for")) + $(el).attr("for", $(el).attr("for").replace(id_regex, replacement)); + if (el.id) + el.id = el.id.replace(id_regex, replacement); + if (el.name) + el.name = el.name.replace(id_regex, replacement); + } + + function addForm(btn, prefix) { + var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val()); + var row = $('.dynamic-form:first').clone(true).get(0); + $(row).removeAttr('id').insertAfter($('.dynamic-form:last')).children('.hidden').removeClass('hidden'); + //$(row).children().not(':last').children().each(function() { + $(row).children().children().each(function() { + updateElementIndex(this, prefix, formCount); + // updateElementIndex(this, 'remove', formCount); + $("#id_" + prefix + "-" + formCount + "-remove").removeAttr('disabled'); + $(this).val(''); + }); + $(row).find('.delete-row').click(function() { + deleteForm(this, prefix); + }); + $('#id_' + prefix + '-TOTAL_FORMS').val(formCount + 1); + $("#id_form-0-remove").attr('disabled','disabled'); + //return false; + } + + function deleteForm(btn, prefix) { + $(btn).parents('.dynamic-form').remove(); + var forms = $('.dynamic-form'); + $('#id_' + prefix + '-TOTAL_FORMS').val(forms.length); + for (var i=0, formCount=forms.length; i Devices {% get_value_with_default device_count 'PND' 'all' 'all' 0 %} +
  • Device Passwords Manager
  • diff --git a/Web_Server/webapps/discovery/forms.py b/Web_Server/webapps/discovery/forms.py index 60ece1d..2495b74 100644 --- a/Web_Server/webapps/discovery/forms.py +++ b/Web_Server/webapps/discovery/forms.py @@ -49,7 +49,7 @@ from django.forms import ModelForm from webapps.discovery.models import PasswordsManager from django import forms from _utils.encrypt import encrypt_value as _ -from webapps.discovery.models import SupportedDevices +from webapps.deviceinfos.models import SupportedDevices class PasswordManagerForm(ModelForm): diff --git a/Web_Server/webapps/discovery/templates/__init__.py b/Web_Server/webapps/discovery/templates/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/Web_Server/webapps/discovery/templates/discovery/__init__.py b/Web_Server/webapps/discovery/templates/discovery/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/Web_Server/webapps/discovery/templates/discovery/manual_discovery.html b/Web_Server/webapps/discovery/templates/discovery/manual_discovery.html index 433855f..e848509 100644 --- a/Web_Server/webapps/discovery/templates/discovery/manual_discovery.html +++ b/Web_Server/webapps/discovery/templates/discovery/manual_discovery.html @@ -78,6 +78,7 @@ + @@ -85,7 +86,6 @@ - + + + + +{% include 'common/top_nav.html' %} +
    +{% include 'common/side_nav.html' %} +
    +
    + {% csrf_token %} +
    +
    +
    +

    BEMOSS Password Manager

    +
    +
    +
    +
    +
    +
    +

    Add/Edit Cloud Device Authentication Information

    +
    +
    +
    + + + + + + + + + + + {{ formset.management_form }} + + {% for form in formset %} + {{ form.id }} + + + + + + {% endfor %} + + + + + + + + +
    Device ModelUsernamePassword
    {{ form.device_model|addcss:'form-control' }}{{ form.username }}{{ form.password }}
    + +
    +
    + + +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Remove Cloud Device Authentication Information

    +
    +
    +
    + + + + + + + + + + + + {% for data in pwd_data %} + + + + + + + + {% endfor %} + + + + + + + + + + +
    Device ModelUsernamePasswordLast ModifiedDelete Password Entry
    {{ data.device_model }}{{ data.username }}*****{{ data.last_modified }}
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    + + + + + + + + + + + + + + +{% include 'common/theme_changer.html' %} + + + diff --git a/Web_Server/webapps/discovery/urls.py b/Web_Server/webapps/discovery/urls.py index 0a3d86c..ec8af8a 100644 --- a/Web_Server/webapps/discovery/urls.py +++ b/Web_Server/webapps/discovery/urls.py @@ -5,5 +5,7 @@ from . import views urlpatterns = [ url(r'^mdiscover', views.discover_devices, name='discovery-manual-discover'), url(r'^new', views.discover_new_devices), - url(r'^authenticate_device', views.authenticate_device, name='discovery-authenticate-device') + url(r'^authenticate_device', views.authenticate_device, name='discovery-authenticate-device'), + url(r'^manage_dinfo', views.password_manager, name='discovery-passwords-manager'), + url(r'^delete_auth_info', views.remove_authentication_information, name='delete-auth-entry') ] \ No newline at end of file diff --git a/Web_Server/webapps/discovery/views.py b/Web_Server/webapps/discovery/views.py index 23f04ce..cf6fd4f 100644 --- a/Web_Server/webapps/discovery/views.py +++ b/Web_Server/webapps/discovery/views.py @@ -112,3 +112,52 @@ def authenticate_device(request): if request.is_ajax(): return HttpResponse(json.dumps("success"), 'application/json') + +@login_required(login_url='/login') +def password_manager(request): + context = RequestContext(request) + PasswordManagerFormSet = modelformset_factory(PasswordsManager, PasswordManagerForm) + if request.method == 'POST': + formset = PasswordManagerFormSet(request.POST) + for form in formset: + if form.is_valid(): + if form.cleaned_data: + form.cleaned_data['password'] = encrypt_value(form.cleaned_data['password']).encode('utf8') + password_data = form.save(commit=False) + password_data.last_modified = datetime.now() + password_data.save() + password_manager_data = [ob.data_passwords_manager() for ob in PasswordsManager.objects.all()] + formset = PasswordManagerFormSet(queryset=PasswordsManager.objects.all()) + else: + password_manager_data = [ob.data_passwords_manager() for ob in PasswordsManager.objects.all()] + formset = PasswordManagerFormSet(queryset=PasswordsManager.objects.all()) + # Allows initial pre-existing data to be rendered into the form. + # When saving, previously saved data need to be treated as old data and ignored. + # formset = PasswordManagerFormSet(initial=[ob.data_passwords_manager() for ob in + # PasswordsManager.objects.all()]) + if request.user.groups.filter(name__iexact = 'admin').exists(): + device_list_side_nav = get_device_list_and_count(request) + return_data = dict() + return_data.update(device_list_side_nav) + return_data.update({'formset': formset, 'pwd_data': password_manager_data}) + return render(request, 'discovery/password_manager.html', return_data) + else: + return HttpResponseRedirect('/home/') + +@login_required(login_url='/login/') +def remove_authentication_information(request): + if request.POST: + _data = request.body + _data = json.loads(_data) + auth_id = _data['id'] + print auth_id + try: + PasswordsManager.objects.filter(id=int(auth_id)).delete() + json_text = {"status": "success", + "id": auth_id} + except IntegrityError: + json_text = {"status": "failure", + "id": auth_id} + + if request.is_ajax(): + return HttpResponse(json.dumps(json_text)) \ No newline at end of file -- GitLab