Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def login_view(request):
- if request.method == 'OPTIONS':
- return JsonResponse({})
- error = ""
- session_key = ""
- user_info = {
- 'first_name': '',
- 'last_name': ''
- }
- try:
- login_dict = json.loads(request.body.replace('\'', '"'))
- username = login_dict['username']
- password = login_dict['password']
- user = auth.authenticate(username=username, password=password)
- if user is not None:
- if user.is_active:
- auth.login(request, user)
- else:
- error = 'Account disabled!'
- else:
- error = 'Invalid credentials!'
- except Exception as e:
- error = 'Invalid credentials!'
- if error == "":
- request.session.set_expiry(3600)
- session_key = request.session.session_key
- if user.first_name:
- user_info['first_name'] = user.first_name
- else:
- user_info['first_name'] = 'Name'
- if user.last_name:
- user_info['last_name'] = user.last_name
- else:
- user_info['last_name'] = 'LastName'
- if user is not None:
- menu = get_user_menu(user)
- response_params = {'error': error, 'session_key': session_key, 'user': user_info, 'menu': menu}
- else:
- response_params = {'error': error}
- return JsonResponse(response_params, status=200)
- # LDAP authentication settings
- AUTHENTICATION_BACKENDS = (
- 'django_auth_ldap.backend.LDAPBackend',
- 'django.contrib.auth.backends.ModelBackend')
- # Binding and connection options
- AUTH_LDAP_SERVER_URI = "ldap://2k8-dc1.hq.bc:389"
- AUTH_LDAP_BIND_DN = u"%s" % os.environ['LDAP_USER']
- AUTH_LDAP_BIND_PASSWORD = "%s" % os.environ['LDAP_PASSWORD']
- AUTH_LDAP_CONNECTION_OPTIONS = {
- ldap.OPT_DEBUG_LEVEL: 0,
- ldap.OPT_REFERRALS: 0
- }
- # User and group search objects and types
- AUTH_LDAP_USER_SEARCH = LDAPSearch(u"OU=БАНК,DC=hq,DC=bc",
- ldap.SCOPE_SUBTREE, "(&(objectClass=user)(sAMAccountname=%(user)s))")
- AUTH_LDAP_GROUP_SEARCH = LDAPSearch(u"OU=БАНК,DC=hq,DC=bc",
- ldap.SCOPE_SUBTREE, "(objectClass=group)")
- AUTH_LDAP_GROUP_TYPE = NestedActiveDirectoryGroupType()
- # Cache settings
- AUTH_LDAP_CACHE_GROUPS = True
- AUTH_LDAP_GROUP_CACHE_TIMEOUT = 300
- # What to do once the user is authenticated
- AUTH_LDAP_USER_ATTR_MAP = {
- "first_name": "givenName",
- "last_name": "sn",
- "email": "mail"
- }
- AUTH_LDAP_USER_FLAGS_BY_GROUP = {
- "is_staff": [u"CN=stash-users,OU=stash,OU=applications,DC=hq,DC=bc"]
- }
- AUTH_LDAP_FIND_GROUP_PERMS = True
Add Comment
Please, Sign In to add comment