1111from gooddata_pipelines .provisioning .entities .users .models .users import (
1212 UserFullLoad ,
1313 UserIncrementalLoad ,
14+ UserProfile ,
1415)
1516from gooddata_pipelines .provisioning .provisioning import Provisioning
1617from gooddata_pipelines .provisioning .utils .context_objects import UserContext
@@ -30,13 +31,28 @@ class UserProvisioner(Provisioning[UserFullLoad, UserIncrementalLoad]):
3031 source_group_incremental : list [UserIncrementalLoad ]
3132 source_group_full : list [UserFullLoad ]
3233
34+ current_user_id : str
35+
3336 FULL_LOAD_TYPE : type [UserFullLoad ] = UserFullLoad
3437 INCREMENTAL_LOAD_TYPE : type [UserIncrementalLoad ] = UserIncrementalLoad
3538
3639 def __init__ (self , host : str , token : str ) -> None :
3740 super ().__init__ (host , token )
3841 self .upstream_user_cache : dict [UserId , UserModel ] = {}
3942
43+ def _get_current_user_id (self ) -> str :
44+ """Gets the current user ID."""
45+
46+ profile_response = self ._api .get_profile ()
47+
48+ if not profile_response .ok :
49+ raise Exception ("Failed to get current user profile" )
50+
51+ profile_json = profile_response .json ()
52+ profile = UserProfile .model_validate (profile_json )
53+
54+ return profile .user_id
55+
4056 def _try_get_user (
4157 self , user : UserModel , model : type [UserModel ]
4258 ) -> UserModel | None :
@@ -99,6 +115,14 @@ def _create_or_update_user(
99115 for its existence and create it if needed.
100116
101117 """
118+
119+ if user .user_id == self .current_user_id :
120+ self .logger .warning (
121+ f"Skipping creation/update of current user: { user .user_id } . "
122+ + "Current user should not be modified." ,
123+ )
124+ return
125+
102126 user_context = UserContext (
103127 user_id = user .user_id ,
104128 user_groups = user .user_groups ,
@@ -118,6 +142,13 @@ def _create_or_update_user(
118142
119143 def _delete_user (self , user_id : str ) -> None :
120144 """Deletes user from the project."""
145+ if user_id == self .current_user_id :
146+ self .logger .warning (
147+ f"Skipping deletion of current user: { user_id } ."
148+ + " Current user should not be deleted." ,
149+ )
150+ return
151+
121152 try :
122153 self ._api ._sdk .catalog_user .get_user (user_id )
123154 except NotFoundException :
@@ -135,6 +166,9 @@ def _manage_user(self, user: UserIncrementalLoad) -> None:
135166
136167 def _provision_incremental_load (self ) -> None :
137168 """Runs the incremental provisioning logic."""
169+ # Set the current user ID
170+ self .current_user_id = self ._get_current_user_id ()
171+
138172 for user in self .source_group_incremental :
139173 # Attempt to process each user. On failure, log the error and continue
140174 try :
@@ -146,6 +180,10 @@ def _provision_incremental_load(self) -> None:
146180
147181 def _provision_full_load (self ) -> None :
148182 """Runs the full load provisioning logic."""
183+
184+ # Set the current user ID
185+ self .current_user_id = self ._get_current_user_id ()
186+
149187 # Get all upstream users
150188 catalog_upstream_users : list [CatalogUser ] = self ._api .list_users ()
151189
0 commit comments