mirror of
https://gitlab.com/sfz.aalen/infra/fabaccess.git
synced 2025-03-12 15:01:47 +01:00
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
|
from config import Config
|
||
|
from keycloak import KeycloakAdmin
|
||
|
|
||
|
|
||
|
class KeycloakHandler:
|
||
|
Config.keycloak_password
|
||
|
|
||
|
@staticmethod
|
||
|
def login():
|
||
|
KeycloakHandler.admin = KeycloakAdmin(
|
||
|
server_url=Config.keycloak_url,
|
||
|
username=Config.keycloak_username,
|
||
|
password=Config.keycloak_password,
|
||
|
realm_name=Config.keycloak_realm,
|
||
|
verify=True
|
||
|
)
|
||
|
|
||
|
@staticmethod
|
||
|
def get_user_by_card_id(card_id):
|
||
|
users = KeycloakHandler.admin.get_users(
|
||
|
{ 'attributes': { 'FabCard': card_id }}
|
||
|
)
|
||
|
print(f'Found {len(users)} users with card_id: {card_id}')
|
||
|
|
||
|
match len(users):
|
||
|
case 0:
|
||
|
return None
|
||
|
case 1:
|
||
|
print(f'FabCard matches with user {users[0]["username"]}')
|
||
|
return users[0]
|
||
|
case other:
|
||
|
print(f'Error! too many users with card_id: {card_id}')
|
||
|
return None
|
||
|
|
||
|
@staticmethod
|
||
|
def user_is_privileged(username):
|
||
|
groups = KeycloakHandler.admin.get_user_groups(user_id=username)
|
||
|
groups = [group['name'] for group in groups]
|
||
|
|
||
|
if 'Mentoren' in groups:
|
||
|
print('Overrided becouse of "Mentor" group')
|
||
|
return True
|
||
|
else:
|
||
|
return False
|