Skip to content

Commit 4136c84

Browse files
authored
feat: allow addition of new group member to group via the approval process
Allows user with specific permission to add new member to a group. If the group has NeedsAccessApprove set to true, the membership needs additional approval to accept the membership. If the group has NeedsAccessApprove set to false, the membership will be approved without any additional approval, during addition of member.
1 parent ab60f77 commit 4136c84

File tree

15 files changed

+586
-317
lines changed

15 files changed

+586
-317
lines changed

Access/accessrequest_helper.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ def _create_access(auth_user, access_label, access_type, request_id, access_reas
429429
}
430430

431431
try:
432-
433432
access = _create_access_mapping(
434433
access=access,
435434
user_identity=user_identity,

Access/background_task_manager.py

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
def background_task(func, *args):
2424
if background_task_manager_type == "celery":
2525
if func == "run_access_grant":
26-
run_access_grant.delay(*args)
26+
request_id = args[0]
27+
run_access_grant.delay(request_id)
2728
elif func == "test_grant":
2829
test_grant.delay(*args)
2930
elif func == "run_accept_request":
@@ -32,9 +33,10 @@ def background_task(func, *args):
3233
run_access_revoke.delay(*args)
3334
else:
3435
if func == "run_access_grant":
36+
request_id = args[0]
3537
accessAcceptThread = threading.Thread(
3638
target=run_access_grant,
37-
args=args,
39+
args=(request_id,),
3840
)
3941
accessAcceptThread.start()
4042
elif func == "run_accept_request":
@@ -52,31 +54,47 @@ def background_task(func, *args):
5254
@shared_task(
5355
autoretry_for=(Exception,), retry_kwargs={"max_retries": 3, "countdown": 5}
5456
)
55-
def run_access_grant(requestId, requestObject, accessType, user, approver):
57+
def run_access_grant(request_id):
58+
user_access_mapping = UserAccessMapping.get_access_request(request_id=request_id)
59+
access_type = user_access_mapping.access.access_tag
60+
user = user_access_mapping.user_identity.user
61+
approver = user_access_mapping.approver_1.user.username
5662
message = ""
57-
if not requestObject.user.state == "1":
58-
requestObject.status = "Declined"
59-
requestObject.save()
63+
if not user_access_mapping.user_identity.user.is_active():
64+
user_access_mapping.decline_access(decline_reason="User is not active")
6065
logger.debug(
6166
{
62-
"requestId": requestId,
67+
"requestId": request_id,
6368
"status": "Declined",
6469
"by": approver,
6570
"response": message,
6671
}
6772
)
6873
return False
74+
elif user_access_mapping.user_identity.identity == {}:
75+
user_access_mapping.grant_fail_access(
76+
fail_reason="Failed since identity is blank for user identity"
77+
)
78+
logger.debug(
79+
{
80+
"requestId": request_id,
81+
"status": "GrantFailed",
82+
"by": approver,
83+
"response": message,
84+
}
85+
)
86+
return False
6987

70-
access_module = helpers.get_available_access_module_from_tag(accessType)
88+
access_module = helpers.get_available_access_module_from_tag(access_type)
7189
if not access_module:
7290
return False
7391

7492
try:
7593
response = access_module.approve(
76-
user,
77-
[requestObject.access.access_label],
78-
approver,
79-
requestId,
94+
user_identity=user_access_mapping.user_identity,
95+
labels=[user_access_mapping.access.access_label],
96+
approver=approver,
97+
request=user_access_mapping,
8098
is_group=False,
8199
)
82100
if type(response) is bool:
@@ -90,60 +108,43 @@ def run_access_grant(requestId, requestObject, accessType, user, approver):
90108
)
91109
approve_success = False
92110
message = str(traceback.format_exc())
111+
93112
if approve_success:
94-
requestObject.status = "Approved"
95-
requestObject.save()
113+
user_access_mapping.approve_access()
96114
logger.debug(
97115
{
98-
"requestId": requestId,
116+
"requestId": request_id,
99117
"status": "Approved",
100118
"by": approver,
101119
"response": message,
102120
}
103121
)
104122
else:
105-
requestObject.status = "GrantFailed"
106-
requestObject.save()
123+
user_access_mapping.grant_fail_access(
124+
fail_reason="Error while running approve in module"
125+
)
107126
logger.debug(
108127
{
109-
"requestId": requestId,
128+
"requestId": request_id,
110129
"status": "GrantFailed",
111130
"by": approver,
112131
"response": message,
113132
}
114133
)
115134
try:
116-
destination = [access_module.access_mark_revoke_permission(accessType)]
117-
subject = str("Access Grant Failed - ") + accessType.upper()
118-
body = (
119-
"Request by "
120-
+ user.email
121-
+ " having Request ID = "
122-
+ requestId
123-
+ " is GrantFailed. Please debug and rerun the grant.<BR/>"
124-
)
125-
body = body + "Failure Reason - " + message
126-
body = (
127-
body
128-
+ "<BR/><BR/> <a target='_blank'"
129-
+ "href "
130-
+ "='https://enigma.browserstack.com/resolve/pendingFailure?access_type="
131-
+ accessType
132-
+ "'>View all failed grants</a>"
133-
)
134-
logger.debug(
135-
"Sending Grant Failed email - "
136-
+ str(destination)
137-
+ " - "
138-
+ subject
139-
+ " - "
140-
+ body
135+
destination = access_module.access_mark_revoke_permission(access_type)
136+
notifications.send_mail_for_access_grant_failed(
137+
destination,
138+
access_type.upper(),
139+
user.email,
140+
request_id=request_id,
141+
message=message,
141142
)
142-
general.emailSES(destination, subject, body)
143+
logger.debug("Sending Grant Failed email - " + str(destination))
143144
except Exception:
144145
logger.debug(
145146
"Grant Failed - Error while sending email - "
146-
+ requestId
147+
+ request_id
147148
+ "-"
148149
+ str(str(traceback.format_exc()))
149150
)

0 commit comments

Comments
 (0)