Fixed some typos and a strange result

This commit is contained in:
downhill 2018-10-14 18:17:59 +02:00
parent 2b566aeb8e
commit 09f7d42de4
2 changed files with 35 additions and 28 deletions

View file

@ -55,6 +55,8 @@ class Register(View):
# urlname for 'go back' on the errorpage # urlname for 'go back' on the errorpage
urlname = 'register' urlname = 'register'
username = request.POST.get('username') username = request.POST.get('username')
if username == "" or not username:
return render(request, 'error.html', { 'urlname': urlname, 'service': service, 'error': 'Please supply a username.' } )
# Check to see if username is already taken # Check to see if username is already taken
if check_user_exists(username): if check_user_exists(username):
return render(request, 'registererror.html', { 'urlname': urlname, 'service': service, 'error': 'User already exists.' } ) return render(request, 'registererror.html', { 'urlname': urlname, 'service': service, 'error': 'User already exists.' } )

View file

@ -64,17 +64,22 @@ class UserLookUp(object):
server = ldapservers() server = ldapservers()
conn = Connection(server) conn = Connection(server)
conn.bind() conn.bind()
# Strange result. It keeps complaining LDAP_UID not set if I try to directly
# substitute x and y to the if, see comment above the if x or y:
x = conn.search('ou=customers,dc=ungleich,dc=ch', '(%s)' % LDAP_UID)
y = conn.search('ou=users,dc=ungleich,dc=ch', '(%s)' % LDAP_UID)
# Search ou=users and ou=customers # Search ou=users and ou=customers
if conn.search('ou=customers,dc=ungleich,dc=ch', '(%s)' % LDAP_UID) or conn.search('ou=users,dc=ungleich,dc=ch', '(%s)' % LPAD_UID): #if conn.search('ou=customers,dc=ungleich,dc=ch', '(%s)' % LDAP_UID) or conn.search('ou=users,dc=ungleich,dc=ch', '(%s)' % LPAD_UID):
if x or y:
# return conn.entries[0] for first search result since we can assume uid is unique # return conn.entries[0] for first search result since we can assume uid is unique
self.dispatch('ldap', '%s [Info: UserLookUp] Searched for %s and found it: %s' % (datetime.now(), LDAP_UID, str(conn.entries[0])) ) self.dispatch('ldap', '%s [Info: UserLookUp] Searched for %s and found it: %s\n' % (datetime.now(), LDAP_UID, str(conn.entries[0])) )
conn.unbind() conn.unbind()
# return True since the user is already in LDAP # return True since the user is already in LDAP
return True return True
# User not in LDAP, so just close it down, write the log and return False # User not in LDAP, so just close it down, write the log and return False
else: else:
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Info: UserLookUp] Searched for %s and not found it.' % (datetime.now(), LDAP_UID) ) self.dispatch('ldap', '%s [Info: UserLookUp] Searched for %s and not found it.\n' % (datetime.now(), LDAP_UID) )
return False return False
@ -87,9 +92,9 @@ class CreateUser(object):
def create_user(self, user, password, firstname, lastname, email): def create_user(self, user, password, firstname, lastname, email):
# Creates a user with some basic data # Creates a user with some basic data
server = ldapservers() server = ldapservers()
conn = Connection(server, conf['LDAP']['LDAPMANAGER'], conf['LDAP']['LDAPMANAGERPASSWORD']) conn = Connection(server, config['LDAP']['LDAPMANAGER'], config['LDAP']['LDAPMANAGERPASSWORD'])
if not conn.bind(): if not conn.bind():
self.dispatch('ldap', '%s [Error CreateUser] Could not connect to LDAPserver' % datetime.now() ) self.dispatch('ldap', '%s [Error CreateUser] Could not connect to LDAPserver\n' % datetime.now() )
return "Could not connect to LDAP Server." return "Could not connect to LDAP Server."
# set objectClasses for the new user # set objectClasses for the new user
obj_new_user = ObjectDef(['inetOrgPerson', 'posixAccount', 'shadowAccount'], conn) obj_new_user = ObjectDef(['inetOrgPerson', 'posixAccount', 'shadowAccount'], conn)
@ -110,10 +115,10 @@ class CreateUser(object):
w[0].gidNumber = randint(1200,50000) w[0].gidNumber = randint(1200,50000)
if not w.commit(): if not w.commit():
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Error CreateUser] Could not write new user %s to LDAP DB' % (datetime.now(), dn) ) self.dispatch('ldap', '%s [Error CreateUser] Could not write new user %s to LDAP DB\n' % (datetime.now(), dn) )
return "Couldn't write data to the LDAP Server." return "Couldn't write data to the LDAP Server."
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Info CreateUser] %s created.' % (datetime.now(), dn) ) self.dispatch('ldap', '%s [Info CreateUser] %s created.\n' % (datetime.now(), dn) )
return True return True
# Returns some basic data from an user # Returns some basic data from an user
@ -128,12 +133,12 @@ class GetUserData(object):
server = ldapservers() server = ldapservers()
conn = Connection(server) conn = Connection(server)
if not conn.bind(): if not conn.bind():
self.dispatch('ldap', '%s [Error GetUserData] Could not connect to LDAP server.' % datetime.now() ) self.dispatch('ldap', '%s [Error GetUserData] Could not connect to LDAP server.\n' % datetime.now() )
return ("error", "Could not connect to LDAP server.", "", "") return ("error", "Could not connect to LDAP server.", "", "")
rdn = user_or_customer(LDAP_UID) rdn = user_or_customer(LDAP_UID)
if rdn == False: if rdn == False:
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Info GetUserData] Could not find user %s' % (datetime.now(), LDAP_UID) ) self.dispatch('ldap', '%s [Info GetUserData] Could not find user %s\n' % (datetime.now(), LDAP_UID) )
return ("error", "Could not find the user.", "", "") return ("error", "Could not find the user.", "", "")
obj = ObjectDef(['inetOrgPerson', 'posixAccount', 'shadowAccount'], conn) obj = ObjectDef(['inetOrgPerson', 'posixAccount', 'shadowAccount'], conn)
# The Reader gets the data for the user # The Reader gets the data for the user
@ -144,7 +149,7 @@ class GetUserData(object):
x = r[0].sn x = r[0].sn
except: except:
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Error GetUserData] Could not open Reader for %s' % (datetime.now(), rdn) ) self.dispatch('ldap', '%s [Error GetUserData] Could not open Reader for %s\n' % (datetime.now(), rdn) )
return ("error", "Could not read data for user.", "", "") return ("error", "Could not read data for user.", "", "")
# Putting the results into strings and then clean it up a bit if some attribute is not set in LDAP # Putting the results into strings and then clean it up a bit if some attribute is not set in LDAP
(firstname, lastname, email) = (str(r[0].givenName), str(r[0].sn), str(r[0].mail)) (firstname, lastname, email) = (str(r[0].givenName), str(r[0].sn), str(r[0].mail))
@ -155,7 +160,7 @@ class GetUserData(object):
if email == '[]': if email == '[]':
email = 'No email given' email = 'No email given'
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Info GetUserData] Got data for %s Firstname: %s Lastname: %s Email: %s' % (datetime.now(), rdn, firstname, lastname, email) ) self.dispatch('ldap', '%s [Info GetUserData] Got data for %s Firstname: %s Lastname: %s Email: %s\n' % (datetime.now(), rdn, firstname, lastname, email) )
return ("OK", firstname, lastname, email) return ("OK", firstname, lastname, email)
@ -170,15 +175,15 @@ class ChangeUserData(object):
LDAP_UID = 'uid=%s' % user LDAP_UID = 'uid=%s' % user
server = ldapservers() server = ldapservers()
# Establish connection with a user who can change the data # Establish connection with a user who can change the data
conn = Connection(server, conf['LDAP']['LDAPMANAGER'], conf['LDAP']['LDAPMANAGERPASSWORD']) conn = Connection(server, config['LDAP']['LDAPMANAGER'], config['LDAP']['LDAPMANAGERPASSWORD'])
if not conn.bind(): if not conn.bind():
self.dispatch('ldap', '%s [Error ChangeUserData] Could not connect to LDAP server.' % datetime.now() ) self.dispatch('ldap', '%s [Error ChangeUserData] Could not connect to LDAP server.\n' % datetime.now() )
return "Could not connect to LDAP server." return "Could not connect to LDAP server."
# get the DN of the user # get the DN of the user
rdn = user_or_customer(LDAP_UID) rdn = user_or_customer(LDAP_UID)
if rdn == False: if rdn == False:
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Info ChangeUserData] User with %s not found.' % (datetime.now(), LDAP_UID) ) self.dispatch('ldap', '%s [Info ChangeUserData] User with %s not found.\n' % (datetime.now(), LDAP_UID) )
return "Could not find user." return "Could not find user."
# Set up a reader for the user # Set up a reader for the user
obj = ObjectDef(['inetOrgPerson', 'posixAccount', 'shadowAccount'], conn) obj = ObjectDef(['inetOrgPerson', 'posixAccount', 'shadowAccount'], conn)
@ -189,7 +194,7 @@ class ChangeUserData(object):
x = r[0].sn x = r[0].sn
except: except:
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Error ChangeUserData] Could not open Reader for %s' % (datetime.now(), rdn) ) self.dispatch('ldap', '%s [Error ChangeUserData] Could not open Reader for %s\n' % (datetime.now(), rdn) )
return "Could not open the data of user." return "Could not open the data of user."
# Opens a Writer instance prefilled with the old data # Opens a Writer instance prefilled with the old data
# We could check if something has changed, but since the form takes the old data as standard values, let's # We could check if something has changed, but since the form takes the old data as standard values, let's
@ -202,10 +207,10 @@ class ChangeUserData(object):
# check if the data is written # check if the data is written
if not w.commit(): if not w.commit():
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Error ChangeUserData] Could not write changes for %s' % (datetime.now(), rdn) ) self.dispatch('ldap', '%s [Error ChangeUserData] Could not write changes for %s\n' % (datetime.now(), rdn) )
return "Could not write changes for user." return "Could not write changes for user."
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Info ChangeUserData] Changed data for %s Firstname: %s Lastname: %s Email: %s' % (datetime.now(), rdn, firstname, lastname, email) ) self.dispatch('ldap', '%s [Info ChangeUserData] Changed data for %s Firstname: %s Lastname: %s Email: %s\n' % (datetime.now(), rdn, firstname, lastname, email) )
return True return True
@ -231,15 +236,15 @@ class ChangePassword(object):
def change_password(self, user, newpassword): def change_password(self, user, newpassword):
LDAP_UID = 'uid=%s' LDAP_UID = 'uid=%s'
server = ldapservers() server = ldapservers()
conn = Connection(server, conf['LDAP']['LDAPMANAGER'], conf['LDAP']['LDAPMANAGERPASSWORD']) conn = Connection(server, config['LDAP']['LDAPMANAGER'], config['LDAP']['LDAPMANAGERPASSWORD'])
if not conn.bind(): if not conn.bind():
self.dispatch('ldap', '%s [Error ChangePassword] Could not connect to LDAP server.' % datetime.now() ) self.dispatch('ldap', '%s [Error ChangePassword] Could not connect to LDAP server.\n' % datetime.now() )
return "Could not connect to LDAP server." return "Could not connect to LDAP server."
# check if uid=user is in either ou=customers or ou=users # check if uid=user is in either ou=customers or ou=users
rdn = user_or_customer(LDAP_UID) rdn = user_or_customer(LDAP_UID)
if rdn == False: if rdn == False:
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Error ChangePassword] Could not find user %s' % (datetime.now(), LDAP_UID) ) self.dispatch('ldap', '%s [Error ChangePassword] Could not find user %s\n' % (datetime.now(), LDAP_UID) )
return "Could not find the user." return "Could not find the user."
# Set up a Reader for the DN # Set up a Reader for the DN
obj = ObjectDef(['inetOrgPerson', 'posixAccount', 'shadowAccount'], conn) obj = ObjectDef(['inetOrgPerson', 'posixAccount', 'shadowAccount'], conn)
@ -250,7 +255,7 @@ class ChangePassword(object):
x = r[0].sn x = r[0].sn
except: except:
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Error ChangePassword] Could not open Reader for %s' % (datetime.now(), rdn) ) self.dispatch('ldap', '%s [Error ChangePassword] Could not open Reader for %s\n' % (datetime.now(), rdn) )
return "Could not open the data for the user." return "Could not open the data for the user."
# Set up the writer and overwrite the attribute with the new password # Set up the writer and overwrite the attribute with the new password
w = Writer.from_cursor(r) w = Writer.from_cursor(r)
@ -258,10 +263,10 @@ class ChangePassword(object):
# Check to see if the change has gone through # Check to see if the change has gone through
if not w.commit(): if not w.commit():
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Error ChangePassword] Could not write data for %s' % (datetime.now(), rdn) ) self.dispatch('ldap', '%s [Error ChangePassword] Could not write data for %s\n' % (datetime.now(), rdn) )
return "Could not write data for the user." return "Could not write data for the user."
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Info ChangePassword] Password changed for %s' % (datetime.now(), rdn) ) self.dispatch('ldap', '%s [Info ChangePassword] Password changed for %s\n' % (datetime.now(), rdn) )
return True return True
@ -274,23 +279,23 @@ class DeleteUser(object):
def delete_user(self, user): def delete_user(self, user):
LDAP_UID = user LDAP_UID = user
server = ldapservers() server = ldapservers()
conn = Connection(server, conf['LDAP']['LDAPMANAGER'], conf['LDAP']['LDAPMANAGERPASSWORD']) conn = Connection(server, config['LDAP']['LDAPMANAGER'], config['LDAP']['LDAPMANAGERPASSWORD'])
if not conn.bind(): if not conn.bind():
self.dispatch('ldap', '%s [Error DeleteUser] Could not connect to LDAP server.' % datetime.now() ) self.dispatch('ldap', '%s [Error DeleteUser] Could not connect to LDAP server.\n' % datetime.now() )
return "Could not connect to LDAP server." return "Could not connect to LDAP server."
# again, check whether the uid= is in ou=users or ou=customers # again, check whether the uid= is in ou=users or ou=customers
dn = user_or_customer(LDAP_UID) dn = user_or_customer(LDAP_UID)
if dn == False: if dn == False:
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Error DeleteUser] Could not find the user %s' % (datetime.now(), LDAP_UID) ) self.dispatch('ldap', '%s [Error DeleteUser] Could not find the user %s\n' % (datetime.now(), LDAP_UID) )
return "Could not find the user." return "Could not find the user."
# Check if the delete was successfull # Check if the delete was successfull
if not conn.delete(dn): if not conn.delete(dn):
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Error DeleteUser] Could not delete %s' % (datetime.now(), dn) ) self.dispatch('ldap', '%s [Error DeleteUser] Could not delete %s\n' % (datetime.now(), dn) )
return "Could not delete the user." return "Could not delete the user."
conn.unbind() conn.unbind()
self.dispatch('ldap', '%s [Info DeleteUser] Deleted %s' % (datetime.now(), dn) ) self.dispatch('ldap', '%s [Info DeleteUser] Deleted %s\n' % (datetime.now(), dn) )
return True return True