Add change_user_details

This commit is contained in:
PCoder 2019-02-24 17:25:13 +01:00
parent 3bc2e0a7e5
commit a909a9b5c5
2 changed files with 40 additions and 10 deletions

View file

@ -142,6 +142,33 @@ class LdapManager:
conn.unbind() conn.unbind()
return return_val return return_val
def change_user_details(self, uid, details):
"""
Updates the user details as per given values in kwargs of the user
identified by user_dn.
Assumes that all attributes passed in kwargs are valid.
:param uid: str The uid that identifies the user
:param details: dict A dictionary containing the new values
:return: True if user details were updated successfully False otherwise
"""
conn = self.get_admin_conn()
details_dict = {k: (ldap3.MODIFY_REPLACE, [v.encode("utf-8")]) for k, v in details.items()}
try:
return_val = conn.modify(
("uid={uid}," + settings.LDAP_CUSTOMER_DN).format(uid=uid),
details_dict
)
msg = "success"
except Exception as ex:
msg = str(ex)
logger.error("Exception: " + msg)
return_val = False
finally:
conn.unbind()
return return_val, msg
def check_user_exists(self, uid, is_customer=True, search_filter="", def check_user_exists(self, uid, is_customer=True, search_filter="",
attributes=None): attributes=None):
""" """

View file

@ -106,7 +106,7 @@ class ChangeData(View):
user_exists, entries = ldap_manager.check_user_exists( user_exists, entries = ldap_manager.check_user_exists(
uid=user.username, uid=user.username,
is_customer=True, is_customer=True,
attributes=['uid', 'givenName', 'sn', 'email'] attributes=['uid', 'givenName', 'sn', 'mail']
) )
if user_exists: if user_exists:
@ -118,8 +118,8 @@ class ChangeData(View):
if entries[0].givenName.value is not None else '', if entries[0].givenName.value is not None else '',
'lastname': entries[0].sn 'lastname': entries[0].sn
if entries[0].sn.value is not None else '', if entries[0].sn.value is not None else '',
'email': entries[0].email 'email': entries[0].mail
if entries[0].email.value is not None else ''} if entries[0].mail.value is not None else ''}
) )
else: else:
return render(request, 'error.html', return render(request, 'error.html',
@ -136,7 +136,6 @@ class ChangeData(View):
if not request.user.is_authenticated: if not request.user.is_authenticated:
return render(request, 'mustbeloggedin.html') return render(request, 'mustbeloggedin.html')
user = str(request.user)
firstname = request.POST.get('firstname') firstname = request.POST.get('firstname')
lastname = request.POST.get('lastname') lastname = request.POST.get('lastname')
email = request.POST.get('email') email = request.POST.get('email')
@ -152,15 +151,19 @@ class ChangeData(View):
validate_email(email) validate_email(email)
except ValidationError: except ValidationError:
return render(request, 'error.html', { 'urlname': urlname, 'service': service, 'error': 'The supplied email address is invalid.' } ) return render(request, 'error.html', { 'urlname': urlname, 'service': service, 'error': 'The supplied email address is invalid.' } )
# Trying to change the data
with get_pool().next() as rpc: ldap_manager = LdapManager()
result = rpc.changeuserdata.change_data(user, firstname, lastname, email) result, msg = ldap_manager.change_user_details(
uid=request.user.username,
details={"givenName": firstname, "sn": lastname, "mail": email}
)
# Data change worked # Data change worked
if result == True: if result:
return render(request, 'changeddata.html', { 'user': user, 'firstname': firstname, 'lastname': lastname, 'email': email } ) return render(request, 'changeddata.html', { 'user': request.user.username, 'firstname': firstname, 'lastname': lastname, 'email': email } )
# Data change did not work, display error # Data change did not work, display error
else: else:
return render(request, 'error.html', { 'urlname': urlname, 'service': service, 'error': result } ) return render(request, 'error.html', { 'urlname': urlname, 'service': service, 'error': msg } )
class ResetPassword(View): class ResetPassword(View):