1+ import json
12from Access import helpers
2- from Access .models import User
3+ from Access .background_task_manager import background_task
4+ from Access .models import MembershipV2 , User
35import logging
46from . import helpers as helper
57from django .db import transaction
@@ -41,25 +43,25 @@ def get_identity_templates(auth_user):
4143 context ["configured_identity_template" ] = []
4244 context ["unconfigured_identity_template" ] = []
4345 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)
46+ for user_identity in user_identities :
47+ is_identity_configured = _is_valid_identity_json (identity = user_identity .identity )
48+ if is_identity_configured :
49+ module = all_modules [user_identity .access_tag ]
50+ context ["configured_identity_template" ].append (
51+ {
52+ "accessUserTemplatePath" : module .get_identity_template (),
53+ "identity" : user_identity .identity
54+ }
55+ )
56+ all_modules .pop (user_identity .access_tag )
5557
56- # for mod in all_modules.values():
57- # context["unconfigured_identity_template"].append(
58- # {
59- # "accessUserTemplatePath": mod.get_identity_template(),
60- # }
61- # )
62- # context["aws_username"] = "some name"
58+ for mod in all_modules .values ():
59+ context ["unconfigured_identity_template" ].append (
60+ {
61+ "accessUserTemplatePath" : mod .get_identity_template (),
62+ }
63+ )
64+ context ["aws_username" ] = "some name"
6365 return context
6466
6567def _is_valid_identity_json (identity ):
@@ -166,7 +168,7 @@ def getallUserList(request):
166168 "last_name" : each_user .user .last_name ,
167169 "email" : each_user .email ,
168170 "username" : each_user .user .username ,
169- "git_username" : each_user .gitusername ,
171+ # "git_username": each_user.gitusername,
170172 "is_active" : each_user .user .is_active ,
171173 "offbaord_date" : each_user .offbaord_date ,
172174 "state" : each_user .current_state (),
@@ -185,3 +187,40 @@ def getallUserList(request):
185187 json_response = {}
186188 json_response ["error" ] = {"error_msg" : str (e ), "msg" : ERROR_MESSAGE }
187189 return json_response
190+
191+
192+ def offboard_user (request ):
193+ if not (request .user .user .has_permission ("VIEW_USER_LIST" ) and request .user .user .has_permission ("ALLOW_USER_OFFBOARD" )):
194+ raise Exception ("Requested User is unauthorised to offboard user." )
195+ try :
196+ offboard_user_email = request .POST .get ("offboard_email" )
197+ if not offboard_user_email :
198+ raise Exception ("Invalid request, attribute not found" )
199+
200+ user = User .objects .filter (email = offboard_user_email ).first ()
201+ if not user :
202+ raise Exception ("User not found" )
203+
204+ except Exception as e :
205+ logger .debug ("Error in request, not found or Invalid request type" )
206+ logger .exception (str (e ))
207+ return {"error" : ERROR_MESSAGE }
208+
209+ user .offboard (request .user .user )
210+
211+ module_identities = user .get_all_active_identity ()
212+
213+ for module_identity in module_identities :
214+ module_identity .decline_all_non_approved_access_mappings ()
215+ access_mappings = module_identity .get_all_granted_access_mappings ()
216+
217+ for access_mapping in access_mappings :
218+ module_identity .offboarding_approved_access_mapping (access_mapping .access )
219+ background_task ("run_access_revoke" , json .dumps ({"request_id" : access_mapping .request_id , "revoker_email" : request .user .user .email }))
220+
221+ module_identity .deactivate ()
222+
223+ user .revoke_all_memberships ()
224+
225+ return {"message" : "Successfully initiated Offboard user" }
226+
0 commit comments