Guest User

Untitled

a guest
Nov 1st, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. def login_view(request):
  2.     if request.method == 'OPTIONS':
  3.         return JsonResponse({})
  4.  
  5.     error = ""
  6.     session_key = ""
  7.     user_info = {
  8.         'first_name': '',
  9.         'last_name': ''
  10.     }
  11.  
  12.     try:
  13.         login_dict = json.loads(request.body.replace('\'', '"'))
  14.         username = login_dict['username']
  15.         password = login_dict['password']
  16.         user = auth.authenticate(username=username, password=password)
  17.         if user is not None:
  18.             if user.is_active:
  19.                 auth.login(request, user)
  20.             else:
  21.                 error = 'Account disabled!'
  22.         else:
  23.             error = 'Invalid credentials!'
  24.     except Exception as e:
  25.         error = 'Invalid credentials!'
  26.  
  27.     if error == "":
  28.         request.session.set_expiry(3600)
  29.         session_key = request.session.session_key
  30.         if user.first_name:
  31.             user_info['first_name'] = user.first_name
  32.         else:
  33.             user_info['first_name'] = 'Name'
  34.         if user.last_name:
  35.             user_info['last_name'] = user.last_name
  36.         else:
  37.             user_info['last_name'] = 'LastName'
  38.     if user is not None:
  39.         menu = get_user_menu(user)
  40.         response_params = {'error': error, 'session_key': session_key, 'user': user_info, 'menu': menu}
  41.     else:
  42.         response_params = {'error': error}
  43.     return JsonResponse(response_params, status=200)
  44.  
  45.  
  46.  
  47.  
  48. # LDAP authentication settings
  49. AUTHENTICATION_BACKENDS = (
  50.     'django_auth_ldap.backend.LDAPBackend',
  51.     'django.contrib.auth.backends.ModelBackend')
  52.  
  53. # Binding and connection options
  54. AUTH_LDAP_SERVER_URI = "ldap://2k8-dc1.hq.bc:389"
  55. AUTH_LDAP_BIND_DN = u"%s" % os.environ['LDAP_USER']
  56. AUTH_LDAP_BIND_PASSWORD = "%s" % os.environ['LDAP_PASSWORD']
  57. AUTH_LDAP_CONNECTION_OPTIONS = {
  58.     ldap.OPT_DEBUG_LEVEL: 0,
  59.     ldap.OPT_REFERRALS: 0
  60. }
  61.  
  62. # User and group search objects and types
  63. AUTH_LDAP_USER_SEARCH = LDAPSearch(u"OU=БАНК,DC=hq,DC=bc",
  64.     ldap.SCOPE_SUBTREE, "(&(objectClass=user)(sAMAccountname=%(user)s))")
  65. AUTH_LDAP_GROUP_SEARCH = LDAPSearch(u"OU=БАНК,DC=hq,DC=bc",
  66.     ldap.SCOPE_SUBTREE, "(objectClass=group)")
  67. AUTH_LDAP_GROUP_TYPE = NestedActiveDirectoryGroupType()
  68.  
  69. # Cache settings
  70. AUTH_LDAP_CACHE_GROUPS = True
  71. AUTH_LDAP_GROUP_CACHE_TIMEOUT = 300
  72.  
  73. # What to do once the user is authenticated
  74. AUTH_LDAP_USER_ATTR_MAP = {
  75.     "first_name": "givenName",
  76.     "last_name": "sn",
  77.     "email": "mail"
  78. }
  79. AUTH_LDAP_USER_FLAGS_BY_GROUP = {
  80.     "is_staff": [u"CN=stash-users,OU=stash,OU=applications,DC=hq,DC=bc"]
  81. }
  82. AUTH_LDAP_FIND_GROUP_PERMS = True
Add Comment
Please, Sign In to add comment