Skip to content

Commit

Permalink
Add logging to keycloak behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanning9 committed Sep 18, 2023
1 parent 65df832 commit 321361f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jasmin_services/models/behaviours/keycloak.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Behaviours to allow integration with KeyCloak."""

import logging

import django.conf
import keycloak

from .base import Behaviour

logger = logging.getLogger(__name__)


class KeycloakAttributeBehaviour(Behaviour):
"""Behaviour to add keycloak attributes to a user."""
Expand All @@ -25,12 +29,14 @@ def __init__(self, *args, **kwargs):

def apply(self, user, role):
"""Add the user to the specified keycloak groups."""
logger.info("Applying keycloak group %s to user %s.", role.service.name, user.username)
kc_group = self.keycloak.get_group_by_path(f"/{role.service.name}")
kc_user_id = self.keycloak.get_user_id(user.username)
self.keycloak.group_user_add(kc_user_id, kc_group.get("id"))

def unapply(self, user, role):
"""Remove the user from the specified keycloak groups."""
logger.info("Removing keycloak group %s from user %s.", role.service.name, user.username)
kc_group = self.keycloak.get_group_by_path(f"/{role.service.name}")
kc_user_id = self.keycloak.get_user_id(user.username)
self.keycloak.group_user_remove(kc_user_id, kc_group.get("id"))

0 comments on commit 321361f

Please sign in to comment.