Skip to content

Commit 76b6f2a

Browse files
feat: Read and display existing/configured user identity
Once an identity is configured users will be able to view it on the update user page
1 parent f7820b6 commit 76b6f2a

File tree

6 files changed

+100
-29
lines changed

6 files changed

+100
-29
lines changed

Access/group_helper.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
"msg": "The requested URL is of POST method but was called with other.",
5757
}
5858

59+
class GroupAccessExistsException(Exception):
60+
def __init__(self):
61+
self.message = "Group Access Exists"
62+
super().__init__(self.message)
63+
5964

6065
class GroupAccessExistsException(Exception):
6166
def __init__(self):

Access/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ def get_active_identity(self, access_tag):
200200
return self.module_identity.filter(
201201
access_tag=access_tag, status="Active"
202202
).first()
203+
204+
def get_all_active_identity(self):
205+
return self.module_identity.filter(status = "Active")
203206

204207
@staticmethod
205208
def get_user_by_email(email):
@@ -325,7 +328,7 @@ def revoke_membership(self):
325328
@staticmethod
326329
def get_membership(membership_id):
327330
return MembershipV2.objects.get(membership_id=membership_id)
328-
331+
329332
def __str__(self):
330333
return self.group.name + "-" + self.user.email + "-" + self.status
331334

@@ -768,6 +771,8 @@ def getAccessRequestDetails(self, access_module):
768771
access_request_data["grantOwner"] = ",".join(access_module.grant_owner())
769772

770773
return access_request_data
774+
775+
771776

772777

773778
class AccessV2(models.Model):

Access/notifications.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
GROUP_ACCESS_ADDED_SUBJECT = "Group: {group_name} new access added"
2424

25-
2625
def send_new_group_create_notification(auth_user, date_time, new_group, member_list):
2726
subject = NEW_GROUP_EMAIL_SUBJECT + auth_user.email + " -- " + date_time
2827
body = helpers.generateStringFromTemplate(

Access/userlist_helper.py

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from . import helpers as helper
55
from django.db import transaction
6+
import json
67

78
logger = logging.getLogger(__name__)
89

@@ -22,35 +23,69 @@
2223
"msg": "Identity could not be updated for {modulename}. Please connect with admin",
2324
}
2425

25-
26-
ERROR_INVALID_ACCESS_MODULE = {
27-
"title": "Invalid Access Module",
28-
"msg": "Invalid Access Module - {modulename}",
26+
IDENTITY_UNCHANGED_ERROR_MESSAGE = {
27+
"title": "Identity value not changed",
28+
"msg": "Identity could not be updated for {modulename}. The new identity is same as the old identity",
2929
}
3030

3131

32-
def get_identity_templates():
32+
class IdentityNotChangedException(Exception):
33+
def __init__(self):
34+
self.message = "Identity Unchanged"
35+
super().__init__(self.message)
36+
37+
38+
def get_identity_templates(auth_user):
39+
user_identities = auth_user.user.get_all_active_identity()
3340
context = {}
34-
context["identity_template"] = []
35-
for mod in helper.get_available_access_modules().values():
36-
context["identity_template"].append(
37-
{"accessUserTemplatePath": mod.get_identity_template()}
41+
context["configured_identity_template"] = []
42+
context["unconfigured_identity_template"] = []
43+
all_modules = helper.get_available_access_modules()
44+
for user_identity in user_identities:
45+
is_identity_configured = _is_valid_identity_json(identity=user_identity.identity)
46+
if is_identity_configured:
47+
module = all_modules[user_identity.access_tag]
48+
context["configured_identity_template"].append(
49+
{
50+
"accessUserTemplatePath": module.get_identity_template(),
51+
"identity" : user_identity.identity
52+
}
53+
)
54+
all_modules.pop(user_identity.access_tag)
55+
56+
for mod in all_modules.values():
57+
context["unconfigured_identity_template"].append(
58+
{
59+
"accessUserTemplatePath": mod.get_identity_template(),
60+
}
3861
)
62+
# context["aws_username"] = "some name"
3963
return context
4064

65+
def _is_valid_identity_json(identity):
66+
try:
67+
identity_json = json.loads(json.dumps(identity))
68+
identity_dict = dict(identity_json)
69+
if len(identity_dict)>0:
70+
return True
71+
return False
72+
except Exception:
73+
return False
4174

4275
def create_identity(user_identity_form, auth_user):
4376
user = auth_user.user
4477
mod_name = user_identity_form.get("modname")
4578
selected_access_module = helper.get_available_access_modules()[mod_name]
46-
79+
context = {}
4780
if selected_access_module:
48-
new_module_identity = selected_access_module.verify_identity(
81+
new_module_identity_json = selected_access_module.verify_identity(
4982
user_identity_form, user.email
5083
)
5184
existing_user_identity = user.get_active_identity(
5285
access_tag=selected_access_module.tag()
5386
)
87+
if new_module_identity_json == existing_user_identity.identity:
88+
raise IdentityNotChangedException()
5489
existing_user_access_mapping = None
5590

5691
# get useraccess if an identity already exists
@@ -65,7 +100,7 @@ def create_identity(user_identity_form, auth_user):
65100
access_tag=selected_access_module.tag(),
66101
existing_user_identity=existing_user_identity,
67102
existing_user_access_mapping=existing_user_access_mapping,
68-
new_module_identity=new_module_identity,
103+
new_module_identity=new_module_identity_json,
69104
)
70105
if True:
71106
for access_mapping in existing_user_access_mapping:
@@ -79,7 +114,7 @@ def create_identity(user_identity_form, auth_user):
79114
# mod.approve(membership)
80115
raise Exception("Not Implemented")
81116

82-
context = {}
117+
83118
context["status"] = {
84119
"title": NEW_IDENTITY_CREATE_SUCCESS_MESSAGE["title"],
85120
"msg": NEW_IDENTITY_CREATE_SUCCESS_MESSAGE["msg"].format(

Access/views.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818
create_request,
1919
)
2020
from Access.models import User
21-
from Access.userlist_helper import (
22-
getallUserList,
23-
get_identity_templates,
24-
create_identity,
25-
NEW_IDENTITY_CREATE_ERROR_MESSAGE,
26-
)
21+
from Access.userlist_helper import getallUserList, get_identity_templates, create_identity, NEW_IDENTITY_CREATE_ERROR_MESSAGE, IDENTITY_UNCHANGED_ERROR_MESSAGE, IdentityNotChangedException
2722
from Access.views_helper import render_error_message
2823
from BrowserStackAutomation.settings import PERMISSION_CONSTANTS
2924

@@ -94,8 +89,8 @@ def pending_revoke(request):
9489

9590
@login_required
9691
def updateUserInfo(request):
97-
context = get_identity_templates()
98-
return render(request, "updateUser.html", context)
92+
context = get_identity_templates(request.user)
93+
return render(request,'updateUser.html',context)
9994

10095

10196
@api_view(["POST"])
@@ -108,6 +103,14 @@ def saveIdentity(request):
108103
user_identity_form=request.POST, auth_user=request.user
109104
)
110105
return JsonResponse(json.dumps(context), safe=False, status=200)
106+
except IdentityNotChangedException:
107+
context = {}
108+
context["error"] = {
109+
"title": IDENTITY_UNCHANGED_ERROR_MESSAGE["title"],
110+
"msg": IDENTITY_UNCHANGED_ERROR_MESSAGE["msg"].format(modulename = modname),
111+
}
112+
return JsonResponse(json.dumps(context), safe=False, status=400)
113+
111114
except Exception:
112115
context = {}
113116
context["error"] = {

templates/updateUser.html

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,55 @@ <h2 class="h5 no-margin-bottom">Update User Info: {{request.user}}</h2>
99
</div>
1010

1111
<div class="container">
12-
<b>Update Identity</b>
13-
{% for each_access in identity_template %}
12+
{% for module in configured_identity_template %}
1413
<section class="content">
1514
<!-- Horizontal Form -->
1615
<div class="box box-danger">
17-
<form class="form-horizontal" action="create_post" method="post" id="usrform{{ forloop.counter }}" name="usrform">
16+
<form class="form-horizontal" action="create_post" method="post" id="config_usrform{{ forloop.counter }}" name="usrform">
1817
{% csrf_token %}
1918
<div class="page-header mx-3">
2019
<div style="color: red;" id="error-msg">
2120
</div>
2221
<div style="color: green;" id="success-msg">
2322
</div>
24-
{% with each_access.accessUserTemplatePath as includePath %}
23+
{% with module.accessUserTemplatePath as includePath %}
2524
{% include includePath %}
2625
{% endwith %}
2726
<div class="form-group">
28-
<button id="update_identity" onclick = "form_post('usrform{{ forloop.counter }}', 'update')" type="submit" value="update" name="state" class="btn btn-primary" style="border: none;color: white;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;">Update</button>
29-
<button id="git_bulk_approve" onclick = "form_post('usrform{{ forloop.counter }}', 'regrant')" type="submit" value="git_bulk_approve" class="btn btn-primary" style="border: none;color: white;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;">Regrant</button>
27+
<button id="update_identity" onclick = "form_post('config_usrform{{ forloop.counter }}', 'update')" type="submit" value="update" name="state" class="btn btn-primary" style="border: none;color: white;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;">Update</button>
3028
</div>
3129
</div>
3230
</form>
3331
</div>
3432
</section>
3533
{% endfor %}
3634
</div>
35+
<div class="container">
36+
<b>Unconfigured Identity</b>
37+
{% for module in unconfigured_identity_template %}
38+
<section class="content">
39+
<!-- Horizontal Form -->
40+
<div class="box box-danger">
41+
<form class="form-horizontal" action="create_post" method="post" id="unconfig_usrform{{ forloop.counter }}" name="usrform">
42+
{% csrf_token %}
43+
<div class="page-header mx-3">
44+
<div style="color: red;" id="error-msg">
45+
</div>
46+
<div style="color: green;" id="success-msg">
47+
</div>
48+
{% with module.accessUserTemplatePath as includePath %}
49+
{% include includePath %}
50+
{% endwith %}
51+
<div class="form-group">
52+
<button id="update_identity" onclick = "form_post('unconfig_usrform{{ forloop.counter }}', 'update')" type="submit" value="update" name="state" class="btn btn-primary" style="border: none;color: white;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;">Update</button>
53+
</div>
54+
</div>
55+
</form>
56+
</div>
57+
</section>
58+
{% endfor %}
59+
</div>
60+
3761
<script id="js">
3862
function form_post(formId, action) {
3963
if (action == 'update'){

0 commit comments

Comments
 (0)