Example #1
0
def get_ecounting_user(username, password):
    from heliosauth.models import User

    is_valid, user_data = check_ecounting_credentials(username, password)
    user = None
    if not is_valid:
        return user

    try:
        user = User.get_by_type_and_id('password', username)
        user.institution = get_institution(user_data)
        user.info['name'] = username
        user.info['password'] = make_password(password)
        user.ecounting_account = True
        user.save()
    except User.DoesNotExist:
        if is_valid:
            user = create_user(username, password)
            user.admin_p = True
            user.info['name'] = user.user_id
            user.info['password'] = make_password(password)
            user.ecounting_account = True
            user.institution = get_institution(user_data)
            user.save()

    return user
Example #2
0
def get_ecounting_user(username, password):
  from heliosauth.models import User

  is_valid, user_data = check_ecounting_credentials(username, password)
  user = None
  if not is_valid:
    return user

  try:
    user = User.get_by_type_and_id('password', username)
    user.institution = get_institution(user_data)
    user.info['name'] = username
    user.info['password'] = make_password(password)
    user.ecounting_account = True
    user.save()
  except User.DoesNotExist:
    if is_valid:
      user = create_user(username, password)
      user.admin_p = True
      user.info['name'] = user.user_id
      user.info['password'] = make_password(password)
      user.ecounting_account = True
      user.institution = get_institution(user_data)
      user.save()

  return user
Example #3
0
def create_user(username, password, name = None, extra_info={}):
  from heliosauth.models import User

  try:
    user = User.get_by_type_and_id('password', username)
  except User.DoesNotExist:
    user = None

  if user:
    raise Exception('user exists')

  info = {'password' : make_password(password), 'name': name}
  info.update(extra_info)
  user = User.update_or_create(user_type='password', user_id=username, info=info)
  user.save()
  return user
Example #4
0
def password_forgotten_view(request):
  """
  forgotten password view and submit.
  includes return_url
  """
  from heliosauth.view_utils import render_template
  from heliosauth.models import User

  if request.method == "GET":
    return render_template(request, 'password/forgot', {'return_url': request.GET.get('return_url', '')})
  else:
    username = request.POST['username']
    return_url = request.POST['return_url']

    try:
      user = User.get_by_type_and_id('password', username)
    except User.DoesNotExist:
      return render_template(request, 'password/forgot', {'return_url': request.GET.get('return_url', ''), 'error': 'no such username'})

    body = """

This is a password reminder:

Your username: %s
Your password: %s

--
%s
""" % (user.user_id, user.info['password'], settings.SITE_TITLE)

    # FIXME: make this a task
    send_mail('password reminder', body, settings.SERVER_EMAIL, ["%s <%s>" % (user.info['name'], user.info['email'])], fail_silently=False)

    return HttpResponseRedirect(return_url)
Example #5
0
def create_user(username, password, name=None, extra_info={}):
    from heliosauth.models import User

    try:
        user = User.get_by_type_and_id('password', username)
    except User.DoesNotExist:
        user = None

    if user:
        raise Exception('user exists')

    info = {'password': make_password(password), 'name': name}
    info.update(extra_info)
    user = User.update_or_create(user_type='password',
                                 user_id=username,
                                 info=info)
    user.save()
    return user
Example #6
0
def password_forgotten_view(request):
    """
  forgotten password view and submit.
  includes return_url
  """
    from heliosauth.view_utils import render_template
    from heliosauth.models import User

    if request.method == "GET":
        return render_template(request, "password/forgot", {"return_url": request.GET.get("return_url", "")})
    else:
        username = request.POST["username"]
        return_url = request.POST["return_url"]

        try:
            user = User.get_by_type_and_id("password", username)
        except User.DoesNotExist:
            return render_template(
                request,
                "password/forgot",
                {"return_url": request.GET.get("return_url", ""), "error": "no such username"},
            )

        body = """

This is a password reminder:

Your username: %s
Your password: %s

--
%s
""" % (
            user.user_id,
            user.info["password"],
            settings.SITE_TITLE,
        )

        # FIXME: make this a task
        send_mail(
            "password reminder",
            body,
            settings.SERVER_EMAIL,
            ["%s <%s>" % (user.info["name"], user.info["email"])],
            fail_silently=False,
        )

        return HttpResponseRedirect(return_url)
Example #7
0
def password_forgotten_view(request):
    """
  forgotten password view and submit.
  includes return_url
  """
    from heliosauth.view_utils import render_template
    from heliosauth.models import User

    if request.method == "GET":
        return render_template(
            request, 'password/forgot',
            {'return_url': request.GET.get('return_url', '')})
    else:
        username = request.POST['username']
        return_url = request.POST['return_url']

        try:
            user = User.get_by_type_and_id('password', username)
        except User.DoesNotExist:
            return render_template(
                request, 'password/forgot', {
                    'return_url': request.GET.get('return_url', ''),
                    'error': 'no such username'
                })

        body = """

This is a password reminder:

Your username: %s
Your password: %s

--
%s
""" % (user.user_id, user.info['password'], settings.SITE_TITLE)

        # FIXME: make this a task
        send_mail('password reminder',
                  body,
                  settings.SERVER_EMAIL,
                  ["%s <%s>" % (user.info['name'], user.info['email'])],
                  fail_silently=False)

        return HttpResponseRedirect(return_url)
Example #8
0
    def handle(self, **options):
        if options.get('create_institution'):
            if not options['param']:
                print("Provide the institution name")
                exit()

            name = options['param'].strip()
            Institution.objects.create(name=options['param'].strip())

        if options.get('remove_user'):
            if not options['param']:
                print("Provide a user id")
                exit()

            user = User.objects.get(pk=int(options['param'].strip()))
            print("User has %d elections objects which will be removed" % user.elections.count())
            confirm = input('Write "yes of course" if you are sure you want to remove \'%s\' ? ' % user.user_id)
            if confirm == "yes of course":
                user.delete()
            else:
                exit()
            print("User removed")

        if options.get("reset_password"):
            if not options['param']:
                print("Provide a user id and a password")
                exit()
            user = self.get_user(options['param'])
            password = getpass.getpass("Password:"******"Confirm password:"******"enable_sms"):
            if not options['param']:
                print("Provide a user id and sms backend sender id")
                exit()

            sender = options.get('enable_sms', 'ZEUS')
            creds = getpass.getpass("Credentials (e.g. username:pass):")
            username, password = creds.split(":")

            user = self.get_user(options['param'])
            if user.sms_data:
                backend = user.sms_data
            else:
                backend = SMSBackendData()
                backend.limit = options.get("sms_limit", 10)
                print("SMS deliveries limit is set to 10")

            backend.credentials = "%s:%s" % (username, password)
            backend.sender = sender
            backend.save()
            user.sms_data = backend
            user.save()

        if options.get("sms_limit"):
            user = self.get_user(options['param'])
            user.sms_data.limit = options.get("sms_limit")
            user.sms_data.save()

        if options.get('create_user'):
            username = options['param'].strip()
            superadmin = options.get('superuser', False)
            manager = options.get('manager', False)
            name = options.get('name', None)

            try:
                existing = User.objects.get(user_id=username)
            except User.DoesNotExist:
                existing = False

            if existing:
                print("User %s, already exists" % username)
                exit()

            inst_pk = options.get('institution')
            if not inst_pk:
                print("Please provide an institution id using --institution")
                exit()
            inst = Institution.objects.get(pk=int(inst_pk))

            password = getpass.getpass("Password:"******"Confirm password:"******"Passwords don't match")
                exit()

            newuser = User()
            newuser.user_type = "password"
            newuser.admin_p = True
            newuser.info = {'name': name or username, 'password':
                            make_password(password)}
            newuser.name = name
            newuser.user_id = username
            newuser.superadmin_p = superadmin
            newuser.management_p = manager
            newuser.institution = inst
            newuser.ecounting_account = False
            newuser.save()
            newuser.user_groups.set([UserGroup.objects.get(name="default")])