Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. from django import forms
  2. from django.contrib.auth.forms import AuthenticationForm
  3.  
  4. GROUP = (
  5. ('', '--------------'),
  6. ('red_server'),
  7. ('blue_server'),
  8. )
  9.  
  10. class LoginForm(AuthenticationForm):
  11. group = forms.ChoiceField(choices=GROUP, required=True,
  12. help_text="Choose one of the groups to login." )
  13. field_order = ['group', 'username', 'password']
  14.  
  15. from django.contrib.auth.models import User
  16. from django.conf import settings
  17.  
  18. def client_for(username, password=None, group=None):
  19. """
  20. Get a connection for the given user.
  21. """
  22.  
  23. auth_server = settings.AUTH_SERVER[group]
  24. # here goes the server authentication code
  25.  
  26. class UnixBackend(object):
  27. """
  28. Authenticate the given user as a Unix account on a remote host.
  29. """
  30.  
  31. def authenticate(self, username=None, password=None):
  32. try:
  33. client_for(username, password, group)
  34. except paramiko.AuthenticationException:
  35. return None
  36. try:
  37. user = User.objects.get(username=username)
  38. except User.DoesNotExist:
  39. # Create a new user if the user does not exist yet
  40. user = User(username=username)
  41. user.is_staff = False
  42. user.is_active = True
  43. user.save()
  44. return user
  45.  
  46. def get_user(self, user_id):
  47. try:
  48. return User.objects.get(pk=user_id)
  49. except User.DoesNotExist:
  50. return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement