Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

106 righe
2.8 KiB

  1. from typing import Callable, Iterator, Optional
  2. from sqlalchemy import select
  3. from sqlalchemy.orm import Session
  4. #from ..models.user import User
  5. from models.user import User
  6. from .security import hash_password, manager
  7. import xml.dom.minidom
  8. import hashlib
  9. from .ldap import LDAPConsole
  10. # lc = None # LDAP disabilitato temporaneamente
  11. def getText( nodelist):
  12. rc = []
  13. for node in nodelist:
  14. if node.nodeType == node.TEXT_NODE:
  15. rc.append(node.data)
  16. return ''.join(rc)
  17. def check_is_admin(uidname):
  18. # use the parse() function to load and parse an XML file
  19. doc = xml.dom.minidom.parse("/conf/etc/useradmin/mnusers.xml");
  20. ssonode = doc.getElementsByTagName("useradmin")
  21. print ( "%d ssonode:" % ssonode.length)
  22. is_adminlevel= "";
  23. for skill in ssonode:
  24. binddn = getText(skill.getElementsByTagName("uid")[0].childNodes)
  25. #print(binddn)
  26. if binddn == uidname:
  27. is_adminlevel = getText (skill.getElementsByTagName("privilegi")[0].childNodes)
  28. #print (is_adminlevel)
  29. break
  30. return is_adminlevel
  31. @manager.user_loader()
  32. def get_user_by_name(
  33. name: str,
  34. #db: Optional[Session] = None,
  35. #session_provider: Callable[[], Iterator[Session]] = None
  36. ) -> Optional[User]:
  37. """
  38. Args:
  39. name: The name of the user
  40. Returns:
  41. The user object or none
  42. """
  43. #if db is None and session_provider is None:
  44. # raise ValueError("db and session_provider cannot both be None.")
  45. # if db is None:
  46. # db = next(session_provider())
  47. print (name)
  48. results = lc.doLdapGetUser("ou=users", username=name)
  49. print (results)
  50. #user = db.query(User).where(User.username == name).first()
  51. user=User()
  52. if results['authen'] == "yeah":
  53. print ('Welcome', results['authen'])
  54. user.username = name
  55. user.password = results['data']['ntPassword']
  56. else:
  57. print (' wrong!')
  58. print (check_is_admin(name))
  59. if check_is_admin(name) == "gruppo4":
  60. user.is_admin = True
  61. print (user)
  62. return user
  63. # def create_user(name: str, password: str, db: Session, is_admin: bool = False) -> User:
  64. # """
  65. # Creates and commits a new user object to the database
  66. # Args:
  67. # name: The name of the user
  68. # password: The plaintext password
  69. # db: The active db session
  70. # is_admin: Whether the user is a admin, defaults to false
  71. # Returns:
  72. # The newly created user.
  73. # """
  74. # hashed_pw = hash_password(password)
  75. # user = User(username=name, password=hashed_pw, is_admin=is_admin)
  76. # db.add(user)
  77. # db.commit()
  78. # return user
  79. # def create_post(text: str, owner: User, db: Session) -> Post:
  80. # post = Post(text=text, owner=owner)
  81. # db.add(post)
  82. # db.commit()
  83. # db.close()
  84. # return post