Skip to content

Commit 6d7007c

Browse files
authored
Notify requester and approvers via Slack on access request creation and completion (#258)
1 parent d78beac commit 6d7007c

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

examples/plugins/notifications_slack/notifications.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,29 @@ def send_message() -> Dict[str, Any]:
168168
logger.error(f"Failed to send Slack DM to {user.email} after multiple attempts")
169169

170170

171-
def send_slack_channel_message(message: str) -> None:
171+
def send_slack_channel_message(user: OktaUser, message: str) -> None:
172172
"""Send a message to a Slack channel with retry logic.
173173
174174
Args:
175175
message (str): The message content.
176+
user (OktaUser): The user to relate the message to.
176177
"""
177178
if alerts_channel:
179+
user_id = get_user_id_by_email(user.email)
178180

179-
def send_message() -> Dict[str, Any]:
180-
response = client.chat_postMessage(
181-
channel=alerts_channel, text=message, as_user=True, unfurl_links=True, unfurl_media=True
182-
)
183-
logger.info(f"Slack channel message sent: {response['ts']}")
184-
return response
181+
if user_id:
182+
channel_message = f"{user.email} - {message}"
185183

186-
result = retry_operation(send_message)
187-
if not result:
188-
logger.error(f"Failed to send message to channel {alerts_channel} after multiple attempts")
184+
def send_message() -> Dict[str, Any]:
185+
response = client.chat_postMessage(
186+
channel=alerts_channel, text=channel_message, as_user=True, unfurl_links=True, unfurl_media=True
187+
)
188+
logger.info(f"Slack channel message sent: {response['ts']}")
189+
return response
190+
191+
result = retry_operation(send_message)
192+
if not result:
193+
logger.error(f"Failed to send message to channel {alerts_channel} after multiple attempts")
189194

190195

191196
@notification_hook_impl
@@ -214,8 +219,13 @@ def access_request_created(
214219
send_slack_dm(approver, approver_message)
215220
logger.info(f"Approver message: {approver_message}")
216221

222+
# Send the message to the requester only if they're not already an approver
223+
if requester.id not in [approver.id for approver in approvers]:
224+
send_slack_dm(requester, approver_message)
225+
logger.info("Requester received creation notification")
226+
217227
# Post to the alerts channel
218-
send_slack_channel_message(approver_message)
228+
send_slack_channel_message(requester, approver_message)
219229

220230

221231
@notification_hook_impl
@@ -224,7 +234,6 @@ def access_request_completed(
224234
group: OktaGroup,
225235
requester: OktaUser,
226236
approvers: List[OktaUser],
227-
notify_requester: bool,
228237
) -> None:
229238
"""Notify the requester that their access request has been processed.
230239
@@ -233,7 +242,6 @@ def access_request_completed(
233242
group (OktaGroup): The group for which access is requested.
234243
requester (OktaUser): The user requesting access.
235244
approvers (List[OktaUser]): The list of approvers.
236-
notify_requester (bool): Whether to notify the requester.
237245
"""
238246
access_request_url = get_base_url() + f"/requests/{access_request.id}"
239247
emoji = ":white_check_mark:" if access_request.status.lower() == "approved" else ":x:"
@@ -244,12 +252,17 @@ def access_request_completed(
244252
)
245253

246254
# Send the message to the requester
247-
if notify_requester:
248-
send_slack_dm(requester, requester_message)
255+
send_slack_dm(requester, requester_message)
249256
logger.info(f"Requester message: {requester_message}")
250257

258+
# Send the message to all approvers (except the requester)
259+
for approver in approvers:
260+
if approver.id != requester.id: # Skip if approver is the requester
261+
send_slack_dm(approver, requester_message)
262+
logger.info("Approvers received completion notification")
263+
251264
# Post to the alerts channel
252-
send_slack_channel_message(requester_message)
265+
send_slack_channel_message(requester, requester_message)
253266

254267

255268
@notification_hook_impl
@@ -275,7 +288,7 @@ def access_expiring_user(groups: List[OktaGroup], user: OktaUser, expiration_dat
275288
logger.info(f"User message: {message}")
276289

277290
# Post to the alerts channel
278-
send_slack_channel_message(message)
291+
send_slack_channel_message(user, message)
279292

280293

281294
@notification_hook_impl
@@ -312,7 +325,7 @@ def access_expiring_owner(
312325
logger.info(f"Owner message: {message}")
313326

314327
# Post to the alerts channel
315-
send_slack_channel_message(message)
328+
send_slack_channel_message(owner, message)
316329

317330
if roles is not None and len(roles) > 0:
318331
expiring_access_url = get_base_url() + "/expiring-roles?owner_id=@me"
@@ -331,4 +344,4 @@ def access_expiring_owner(
331344
logger.info(f"Owner message: {message}")
332345

333346
# Post to the alerts channel
334-
send_slack_channel_message(message)
347+
send_slack_channel_message(owner, message)

0 commit comments

Comments
 (0)