Use LdapManager in password reset too

This commit is contained in:
PCoder 2019-02-24 18:42:27 +01:00
parent 16b02cfe3f
commit acff3fc592

View file

@ -175,22 +175,46 @@ class ResetPassword(View):
service = 'send a password reset request' service = 'send a password reset request'
user = request.POST.get('user') user = request.POST.get('user')
# First, check if the user exists # First, check if the user exists
if not check_user_exists(user): ldap_manager = LdapManager()
return render(request, 'error.html', { 'urlname': urlname, 'service': service, 'error': 'The user does not exist.' } ) user_exists, entries = ldap_manager.check_user_exists(
uid=user,
attributes=['uid', 'givenName', 'sn', 'mail']
)
if user_exists and request.user.username == user:
# user exists, so try to get email # user exists, so try to get email
with get_pool().next() as rpc: # with get_pool().next() as rpc:
(state, tmp1, tmp2, email) = rpc.getuserdata.get_data(user) # (state, tmp1, tmp2, email) = rpc.getuserdata.get_data(user)
# Either error with the datalookup or no email provided # Either error with the datalookup or no email provided
if state == "error" or email == 'No email given' or not email: email = entries[0].mail.value
return render(request, 'error.html', { 'urlname': urlname, 'service': service, 'error': 'Unable to retrieve email address for user.' } ) if email is None:
return render(
request, 'error.html',
{'urlname': urlname, 'service': service,
'error': 'Unable to retrieve email address for user.'}
)
base_url = "{0}://{1}".format(self.request.scheme,
self.request.get_host())
# Try to send the email out # Try to send the email out
emailsend = self.email(user, email) emailsend = self.email(user, email, base_url)
# Email got sent out # Email got sent out
if emailsend == True: if emailsend == True:
return render(request, 'send_resetrequest.html', { 'user': user } ) return render(
request, 'send_resetrequest.html', {'user': user}
)
# Error while trying to send email # Error while trying to send email
else: else:
return render(request, 'error.html', { 'urlname': urlname, 'service': service, 'error': emailsend } ) return render(
request, 'error.html',
{'urlname': urlname, 'service': service,
'error': emailsend}
)
else:
return render(
request, 'error.html',
{ 'urlname': urlname, 'service': service,
'error': 'The user does not exist.' }
)
# Sends an email to the user with the 24h active link for a password reset # Sends an email to the user with the 24h active link for a password reset
def email(self, user, email, base_url): def email(self, user, email, base_url):