99from Access import helpers
1010from bootprocess import general
1111from BrowserStackAutomation .settings import AUTOMATED_EXEC_IDENTIFIER
12- from Access .models import UserAccessMapping , User
12+ from Access .models import UserAccessMapping , ApprovalType
1313from Access import notifications
1414
1515logger = logging .getLogger (__name__ )
1616
17+
1718with open ("config.json" ) as data_file :
1819 background_task_manager_type = json .load (data_file )["background_task_manager" ][
1920 "type"
@@ -30,7 +31,8 @@ def background_task(func, *args):
3031 elif func == "run_accept_request" :
3132 run_accept_request .delay (* args )
3233 elif func == "run_access_revoke" :
33- run_access_revoke .delay (* args )
34+ request_id = args [0 ]
35+ run_access_revoke .delay (request_id )
3436 else :
3537 if func == "run_access_grant" :
3638 request_id = args [0 ]
@@ -56,7 +58,7 @@ def background_task(func, *args):
5658)
5759def run_access_grant (request_id ):
5860 user_access_mapping = UserAccessMapping .get_access_request (request_id = request_id )
59- access_type = user_access_mapping .access .access_tag
61+ access_tag = user_access_mapping .access .access_tag
6062 user = user_access_mapping .user_identity .user
6163 approver = user_access_mapping .approver_1 .user .username
6264 message = ""
@@ -75,6 +77,10 @@ def run_access_grant(request_id):
7577 user_access_mapping .grant_fail_access (
7678 fail_reason = "Failed since identity is blank for user identity"
7779 )
80+ notifications .send_mail_for_request_granted_failure (
81+ user , approver , access_tag , request_id
82+ )
83+
7884 logger .debug (
7985 {
8086 "requestId" : request_id ,
@@ -85,7 +91,7 @@ def run_access_grant(request_id):
8591 )
8692 return False
8793
88- access_module = helpers .get_available_access_module_from_tag (access_type )
94+ access_module = helpers .get_available_access_module_from_tag (access_tag )
8995 if not access_module :
9096 return False
9197
@@ -132,10 +138,10 @@ def run_access_grant(request_id):
132138 }
133139 )
134140 try :
135- destination = access_module .access_mark_revoke_permission (access_type )
141+ destination = access_module .access_mark_revoke_permission (access_tag )
136142 notifications .send_mail_for_access_grant_failed (
137143 destination ,
138- access_type .upper (),
144+ access_tag .upper (),
139145 user .email ,
140146 request_id = request_id ,
141147 message = message ,
@@ -157,37 +163,36 @@ def run_access_grant(request_id):
157163@shared_task (
158164 autoretry_for = (Exception ,), retry_kwargs = {"max_retries" : 3 , "countdown" : 5 }
159165)
160- def run_access_revoke (data ):
161- data = json .loads (data )
162- access_mapping = UserAccessMapping .get_access_request (data ["request_id" ])
166+ def run_access_revoke (request_id ):
167+ access_mapping = UserAccessMapping .get_access_request (request_id = request_id )
163168 if not access_mapping :
164169 # TODO: Have to add the email targets for failure
165170 targets = []
166171 message = "Request not found"
167172 notifications .send_revoke_failure_mail (
168- targets , data [ " request_id" ], data [ "revoker_email" ] , 0 , message
173+ targets , request_id , access_mapping . revoker . email , 0 , message
169174 )
170- return { "status" : False }
175+ return False
171176 elif access_mapping .status == "Revoked" :
172- return { "status" : True }
177+ return True
173178 access = access_mapping .access
174179 user_identity = access_mapping .user_identity
175180
176- revoker = User . get_user_by_email ( data [ "revoker_email" ])
181+ revoker = access_mapping . revoker
177182 if not revoker :
178183 # TODO: Have to add the email targets for failure
179184 targets = []
180185 message = "Revoker not found"
181186 notifications .send_revoke_failure_mail (
182187 targets ,
183- data [ " request_id" ] ,
184- data [ "revoker_email" ] ,
188+ request_id ,
189+ access_mapping . revoker . email ,
185190 0 ,
186191 message ,
187192 access .access_tag ,
188193 )
189194 user_identity .mark_revoke_failed_for_approved_access_mapping (access )
190- return { "status" : False }
195+ return False
191196
192197 access_modules = helpers .get_available_access_modules ()
193198
@@ -227,7 +232,7 @@ def run_access_revoke(data):
227232 user_identity .mark_revoke_failed_for_approved_access_mapping (access )
228233 raise Exception ("Failed to revoke the access due to: " + str (message ))
229234
230- return { "status" : True }
235+ return True
231236
232237
233238@task_success .connect (sender = run_access_grant )
@@ -278,6 +283,7 @@ def run_accept_request(data):
278283 result = background_task ("run_access_grant" , request_id )
279284 if result :
280285 return {"status" : True }
286+
281287 notifications .send_mail_for_request_granted_failure (
282288 user , approver , access_type , request_id
283289 )
@@ -291,3 +297,27 @@ def run_accept_request(data):
291297 )
292298
293299 return {"status" : False }
300+
301+ def accept_request (user_access_mapping ):
302+ try :
303+ result = run_access_grant .delay (user_access_mapping .request_id )
304+ except Exception :
305+ user_access_mapping .grant_fail_access (fail_reason = "Task could not be queued" )
306+
307+ if result :
308+ return True
309+ return False
310+
311+
312+ def revoke_request (user_access_mapping , revoker = None ):
313+ result = None
314+ # change the status to revoke processing
315+ user_access_mapping .revoking (revoker )
316+ try :
317+ result = run_access_revoke .delay (user_access_mapping .request_id )
318+ except Exception :
319+ user_access_mapping .RevokeFailed (fail_reason = "Task could not be queued" )
320+
321+ if result :
322+ return True
323+ return False
0 commit comments