Change LDAP group membership based on certification

This commit is contained in:
Tanner Collin 2020-09-15 19:17:54 +00:00
parent 44d50735bf
commit dcdfbfa953
3 changed files with 79 additions and 1 deletions

View File

@ -195,6 +195,21 @@ class AdminMemberSerializer(MemberSerializer):
'is_staff', 'is_staff',
] ]
def update(self, instance, validated_data):
if 'rabbit_cert_date' in validated_data:
if validated_data['rabbit_cert_date']:
utils_ldap.add_to_group(instance, 'Laser Users')
else:
utils_ldap.remove_from_group(instance, 'Laser Users')
if 'trotec_cert_date' in validated_data:
if validated_data['trotec_cert_date']:
utils_ldap.add_to_group(instance, 'Trotec Users')
else:
utils_ldap.remove_from_group(instance, 'Trotec Users')
return super().update(instance, validated_data)
# member viewing member list or search result # member viewing member list or search result
class SearchSerializer(serializers.Serializer): class SearchSerializer(serializers.Serializer):

View File

@ -4,6 +4,7 @@ logger = logging.getLogger(__name__)
import requests import requests
from apiserver import secrets from apiserver import secrets
from apiserver.api import utils
def is_configured(): def is_configured():
return bool(secrets.LDAP_API_URL and secrets.LDAP_API_KEY) return bool(secrets.LDAP_API_URL and secrets.LDAP_API_KEY)
@ -39,3 +40,37 @@ def set_password(data):
password=data['password1'], password=data['password1'],
) )
return ldap_api('set-password', ldap_data) return ldap_api('set-password', ldap_data)
def add_to_group(member, group):
try:
ldap_data = dict(group=group)
if member.user:
ldap_data['username'] = member.user.username
else:
ldap_data['email'] = member.old_email
if ldap_api('add-to-group', ldap_data) != 200: raise
except BaseException as e:
logger.error('LDAP Group - {} - {}'.format(e.__class__.__name__, str(e)))
m = '{} {} ({})'.format(member.first_name, member.last_name, member.id)
msg = 'Problem adding {} to group {}!'.format(m, group)
utils.alert_tanner(msg)
logger.info(msg)
def remove_from_group(member, group):
try:
ldap_data = dict(group=group)
if member.user:
ldap_data['username'] = member.user.username
else:
ldap_data['email'] = member.old_email
if ldap_api('remove-from-group', ldap_data) != 200: raise
except BaseException as e:
logger.error('LDAP Group - {} - {}'.format(e.__class__.__name__, str(e)))
m = '{} {} ({})'.format(member.first_name, member.last_name, member.id)
msg = 'Problem adding {} to group {}!'.format(m, group)
utils.alert_tanner(msg)
logger.info(msg)

View File

@ -20,7 +20,7 @@ import datetime, time
import requests import requests
from . import models, serializers, utils, utils_paypal, utils_stats from . import models, serializers, utils, utils_paypal, utils_stats, utils_ldap
from .permissions import ( from .permissions import (
is_admin_director, is_admin_director,
AllowMetadata, AllowMetadata,
@ -234,6 +234,20 @@ class TrainingViewSet(Base, Retrieve, Create, Update):
member.mill_cert_date = utils.today_alberta_tz() if status == 'Attended' else None member.mill_cert_date = utils.today_alberta_tz() if status == 'Attended' else None
elif session.course.id == 259: elif session.course.id == 259:
member.cnc_cert_date = utils.today_alberta_tz() if status == 'Attended' else None member.cnc_cert_date = utils.today_alberta_tz() if status == 'Attended' else None
elif session.course.id == 247:
member.rabbit_cert_date = utils.today_alberta_tz() if status == 'Attended' else None
if status == 'Attended':
utils_ldap.add_to_group(member, 'Laser Users')
else:
utils_ldap.remove_from_group(member, 'Laser Users')
elif session.course.id == 321:
member.trotec_cert_date = utils.today_alberta_tz() if status == 'Attended' else None
if status == 'Attended':
utils_ldap.add_to_group(member, 'Trotec Users')
else:
utils_ldap.remove_from_group(member, 'Trotec Users')
member.save() member.save()
serializer.save(user=user, member_id=member.id, attendance_status=status) serializer.save(user=user, member_id=member.id, attendance_status=status)
@ -273,6 +287,20 @@ class TrainingViewSet(Base, Retrieve, Create, Update):
member.mill_cert_date = utils.today_alberta_tz() if status == 'Attended' else None member.mill_cert_date = utils.today_alberta_tz() if status == 'Attended' else None
elif session.course.id == 259: elif session.course.id == 259:
member.cnc_cert_date = utils.today_alberta_tz() if status == 'Attended' else None member.cnc_cert_date = utils.today_alberta_tz() if status == 'Attended' else None
elif session.course.id == 247:
member.rabbit_cert_date = utils.today_alberta_tz() if status == 'Attended' else None
if status == 'Attended':
utils_ldap.add_to_group(member, 'Laser Users')
else:
utils_ldap.remove_from_group(member, 'Laser Users')
elif session.course.id == 321:
member.trotec_cert_date = utils.today_alberta_tz() if status == 'Attended' else None
if status == 'Attended':
utils_ldap.add_to_group(member, 'Trotec Users')
else:
utils_ldap.remove_from_group(member, 'Trotec Users')
member.save() member.save()