ldap search

This commit is contained in:
Tim Gröger 2019-12-22 23:09:18 +01:00
parent c6508fd516
commit 589ae3e3a8
2 changed files with 41 additions and 16 deletions

View File

@ -1,5 +1,5 @@
from flask import Blueprint, request, jsonify from flask import Blueprint, request, jsonify
from geruecht import BAR, db from geruecht import BAR, db, ldapController as ldap
from geruecht.routes import verifyAccessToken from geruecht.routes import verifyAccessToken
from geruecht.model.user import User from geruecht.model.user import User
from datetime import datetime from datetime import datetime
@ -90,26 +90,18 @@ def _getUsers():
return jsonify(retVal) return jsonify(retVal)
return jsonify({"error": "permission denied"}), 401 return jsonify({"error": "permission denied"}), 401
@baruser.route("/barGetUser", methods=['POST']) @baruser.route("/search", methods=['POST'])
def _getUser(): def _search():
""" Get specified User
This function returns the user with posted userID and them amount and credit.
Returns:
JSON-File with userID, amount and credit
or ERROR 401 Permission Denied
"""
token = request.headers.get("Token") token = request.headers.get("Token")
print(token)
accToken = verifyAccessToken(token, BAR) accToken = verifyAccessToken(token, BAR)
if accToken is not None: if accToken is not None:
data = request.get_json() data = request.get_json()
userID = data['userId']
user = User.query.filter_by(userID=userID) searchString = data['searchString']
month = user.getGeruecht().getMonth()
return jsonify({"userId": user.userID, "amount": month[1], "credit": month[0]}) retVal = ldap.searchUser(searchString)
return jsonify(retVal)
return jsonify({"error": "permission denied"}), 401 return jsonify({"error": "permission denied"}), 401

View File

@ -50,6 +50,39 @@ class LDAPController(metaclass=Singleton):
elif data == 'bar': elif data == 'bar':
return BAR return BAR
def __isUserInList(self, list, username):
help_list = []
for user in list:
help_list.append(user[1]['cn'][0].decode('utf-8'))
if username in help_list:
return True
return False
def searchUser(self, searchString):
self.connect()
name = searchString.split(" ")
name_result = []
if len(name) == 1:
name_result[0] = self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'givenName={}'.format(name[0]), ['cn', 'givenName', 'sn'])
name_result[1] = self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'sn={}'.format(name[0]),['cn', 'givenName', 'sn'])
else:
name_result[2] = self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE,
'givenName={}'.format(name[0]), ['cn', 'givenName', 'sn'])
name_result[3] = self.client.search_s('ou=user,{}'.format(self.dn), ldap.SCOPE_SUBTREE, 'sn={}'.format(name[0]),
['cn', 'givenName', 'sn'])
retVal = []
for user in name_result:
username = user[1]['cn'][0].decode('utf-8')
if not self.__isUserInList(retVal, username):
firstname = user[1]['givenName'][0].decode('utf-8')
lastname = user[1]['givenName'][0].decode('utf-8')
retVal.append({username: username, firstname: firstname, lastname: lastname})
return retVal
if __name__ == '__main__': if __name__ == '__main__':
a = LDAPController() a = LDAPController()