mirror of
https://github.com/retspen/webvirtcloud
synced 2026-07-01 17:15:42 +00:00
Added logging
This commit is contained in:
parent
418a563ba8
commit
557594769d
1 changed files with 34 additions and 15 deletions
|
|
@ -6,45 +6,61 @@ from accounts.models import UserAttributes, UserInstance, UserSSHKey
|
||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
from logs.models import Logs
|
from logs.models import Logs
|
||||||
import uuid
|
import uuid
|
||||||
import logging
|
|
||||||
|
|
||||||
#/srv/webvirtcloud/ldap/ldapbackend.py
|
#/srv/webvirtcloud/ldap/ldapbackend.py
|
||||||
class LdapAuthenticationBackend(ModelBackend):
|
class LdapAuthenticationBackend(ModelBackend):
|
||||||
|
|
||||||
def get_LDAP_user(self, username, password, filterString):
|
def get_LDAP_user(self, username, password, filterString):
|
||||||
logger.error("get_LDAP_user")
|
print('get_LDAP_user')
|
||||||
try:
|
try:
|
||||||
server = Server(settings.LDAP_URL, port=settings.LDAP_PORT,use_ssl=settings.USE_SSL,get_info=ALL)
|
server = Server(settings.LDAP_URL, port=settings.LDAP_PORT,
|
||||||
connection = Connection(server,settings.LDAP_MASTER_DN, settings.LDAP_MASTER_PW, auto_bind=True)
|
use_ssl=settings.USE_SSL,get_info=ALL)
|
||||||
|
connection = Connection(server,
|
||||||
|
settings.LDAP_MASTER_DN,
|
||||||
|
settings.LDAP_MASTER_PW, auto_bind=True)
|
||||||
|
|
||||||
connection.search(settings.LDAP_ROOT_DN, '(&({attr}={login})({filter}))'.format(attr=settings.LDAP_USER_UID_PREFIX, login=username,filter=filterString), attributes=[settings.LDAP_USER_UID_PREFIX])
|
connection.search(settings.LDAP_ROOT_DN,
|
||||||
|
'(&({attr}={login})({filter}))'.format(
|
||||||
|
attr=settings.LDAP_USER_UID_PREFIX,
|
||||||
|
login=username,
|
||||||
|
filter=filterString), attributes=[settings.LDAP_USER_UID_PREFIX])
|
||||||
|
|
||||||
if len(connection.response) == 0:
|
if len(connection.response) == 0:
|
||||||
|
print('get_LDAP_user-no response')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return connection.response[0]
|
return connection.response[0]
|
||||||
except:
|
except:
|
||||||
|
print('get_LDAP_user-error')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def authenticate(self, request, username=None, password=None, **kwargs):
|
def authenticate(self, request, username=None, password=None, **kwargs):
|
||||||
logger.error("authenticate")
|
print("authenticate")
|
||||||
# Get the user information from the LDAP if he can be authenticated
|
# Get the user information from the LDAP if he can be authenticated
|
||||||
isAdmin = False
|
isAdmin = False
|
||||||
isStaff = False
|
isStaff = False
|
||||||
if ldapAdmin
|
|
||||||
if get_LDAP_user(username, password, settings.LDAP_SEARCH_GROUP_FILTER_ADMINS) is None:
|
if self.get_LDAP_user(username, password, settings.LDAP_SEARCH_GROUP_FILTER_ADMINS) is None:
|
||||||
if get_LDAP_user(username, password, settings.LDAP_SEARCH_GROUP_FILTER_STAFF) is None:
|
print("authenticate-not admin")
|
||||||
if get_LDAP_user(username, password, settings.LDAP_SEARCH_GROUP_FILTER_USERS) is None:
|
if self.get_LDAP_user(username, password, settings.LDAP_SEARCH_GROUP_FILTER_STAFF) is None:
|
||||||
|
print("authenticate-not staff")
|
||||||
|
if self.get_LDAP_user(username, password, settings.LDAP_SEARCH_GROUP_FILTER_USERS) is None:
|
||||||
|
print("authenticate-not user")
|
||||||
return None
|
return None
|
||||||
else
|
else:
|
||||||
|
print("authenticate-user")
|
||||||
|
else:
|
||||||
isStaff = True
|
isStaff = True
|
||||||
else
|
print("authenticate-staff")
|
||||||
|
else:
|
||||||
isAdmin = True
|
isAdmin = True
|
||||||
isStaff = True
|
isStaff = True
|
||||||
|
print("authenticate-admin")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(username=username)
|
user = User.objects.get(username=username)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
|
print("authenticate-create new user")
|
||||||
user = User(username=username)
|
user = User(username=username)
|
||||||
user.is_staff = isStaff
|
user.is_staff = isStaff
|
||||||
user.is_superuser = isAdmin
|
user.is_superuser = isAdmin
|
||||||
|
|
@ -59,11 +75,14 @@ class LdapAuthenticationBackend(ModelBackend):
|
||||||
permission = Permission.objects.get(codename='clone_instances')
|
permission = Permission.objects.get(codename='clone_instances')
|
||||||
user.user_permissions.add(permission)
|
user.user_permissions.add(permission)
|
||||||
user.save()
|
user.save()
|
||||||
|
|
||||||
|
print("authenticate-user created")
|
||||||
return user
|
return user
|
||||||
|
|
||||||
def get_user(self, user_id):
|
def get_user(self, user_id):
|
||||||
logger.error("get_user")
|
print("get_user")
|
||||||
try:
|
try:
|
||||||
return User.objects.get(pk=user_id)
|
return User.objects.get(pk=user_id)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
|
print("get_user-user not found")
|
||||||
return None
|
return None
|
||||||
Loading…
Add table
Add a link
Reference in a new issue