Ejemplo n.º 1
0
def _get_demo_user(email_address):
    password = email_address

    try:
        user = User.objects.get(name=email_address)
    except User.DoesNotExist:
        pass
    else:
        if user.user_id.startswith("demo_"):
            user.info = {
                'name': email_address,
                'password': make_password(password)
            }
            user.save()
            return user, password

    try:
        inst = Institution.objects.get(name="DEMO")
    except Institution.DoesNotExist:
        return None, ''

    tries = 10
    while tries > 0:
        user_id = "demo_%d" % randint(1000, 1000000)
        try:
            User.objects.get(user_id=user_id)
        except User.DoesNotExist:
            break
        tries -= 1

    if tries <= 0:
        return None, ''

    demogroup = None
    try:
        demogroup = UserGroup.objects.get(name="demo")
    except UserGroup.DoesNotExist:
        pass
    newuser = User()
    newuser.user_type = "password"
    newuser.admin_p = True
    newuser.info = {'name': email_address, 'password': make_password(password)}
    newuser.name = email_address
    newuser.user_id = user_id
    newuser.superadmin_p = False
    newuser.institution = inst
    newuser.ecounting_account = False
    newuser.save()
    if demogroup:
        newuser.user_groups.add(demogroup)
        newuser.save()
    return newuser, password
Ejemplo n.º 2
0
Archivo: forms.py Proyecto: grnet/zeus
    def save(self, commit=True):
        instance = super(userForm, self).save(commit=False)

        try:
            User.objects.get(id=instance.id)
            if commit:
                instance.save()
                self.save_m2m()
            return instance, None

        except(User.DoesNotExist):
            instance.name = self.cleaned_data['name']
            password = random_password()
            instance.info = {'name': instance.name or instance.user_id,
                        'password': make_password(password)}
            instance.institution = self.cleaned_data['institution']
            if self.logged_user.superadmin_p:
                instance.management_p = self.cleaned_data.get('management_p')
            instance.admin_p = True
            instance.user_type = 'password'
            instance.superadmin_p = False
            instance.ecounting_account = False
            if commit:
                instance.save()
                self.save_m2m()
            return instance, password
Ejemplo n.º 3
0
def reset_password_confirmed(request):
    if request.method != 'POST':
        raise PermissionDenied
    uid = request.GET.get('uid')
    uid = sanitize_get_param(uid)
    logged_user = request.zeususer._user
    user = get_user(uid)
    if user:
        if can_do(logged_user, user):
            new_password = random_password()
            user.info['password'] = make_password(new_password)
            user.save()
            message = _("New password for user %(uid)s is "
                        "%(new_pass)s") % {
                            'uid': user.user_id,
                            'new_pass': new_password
                        }
            messages.info(request, message)
        else:
            message = _("You are not authorized to do this")
            messages.error(request, message)
    else:
        message = _("You didn't choose a user")
        messages.error(request, message)
        return redirect(reverse('list_users'))
    url = "%s?uid=%s" % (reverse('user_management'), str(user.id))
    return redirect(url)
Ejemplo n.º 4
0
    def save(self, commit=True):
        instance = super(userForm, self).save(commit=False)

        try:
            User.objects.get(id=instance.id)
            if commit:
                instance.save()
                self.save_m2m()
            return instance, None

        except (User.DoesNotExist):
            instance.name = self.cleaned_data['name']
            password = random_password()
            instance.info = {
                'name': instance.name or instance.user_id,
                'password': make_password(password)
            }
            instance.institution = self.cleaned_data['institution']
            if self.logged_user.superadmin_p:
                instance.management_p = self.cleaned_data.get('management_p')
            instance.admin_p = True
            instance.user_type = 'password'
            instance.superadmin_p = False
            instance.ecounting_account = False
            if commit:
                instance.save()
                self.save_m2m()
            return instance, password
Ejemplo n.º 5
0
def _get_demo_user(email_address):
    password = email_address

    try:
        user = User.objects.get(name=email_address)
    except User.DoesNotExist:
        pass
    else:
        if user.user_id.startswith("demo_"):
            user.info = {'name': email_address,
                         'password': make_password(password)}
            user.save()
            return user, password

    try:
        inst = Institution.objects.get(name="DEMO")
    except Institution.DoesNotExist:
        return None, ''

    tries = 10
    while tries > 0:
        user_id = "demo_%d" % randint(1000, 1000000)
        try:
            User.objects.get(user_id=user_id)
        except User.DoesNotExist:
            break

    if tries <= 0:
        return None, ''

    newuser = User()
    newuser.user_type = "password"
    newuser.admin_p = True
    newuser.info = {'name': email_address,
                    'password': make_password(password)}
    newuser.name = email_address
    newuser.user_id = user_id
    newuser.superadmin_p = False
    newuser.institution = inst
    newuser.ecounting_account = False
    newuser.save()
    return newuser, password
Ejemplo n.º 6
0
def main(institution_name, username, password):
    inst, created = Institution.objects.get_or_create(name=institution_name)

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

    user.user_type = "password"
    user.name = username
    user.superadmin_p = True
    user.management_p = True
    user.institution = inst
    user.ecounting_account = False
    user.info = {"name": username, "password": make_password(password)}
    user.save()
Ejemplo n.º 7
0
Archivo: init.py Proyecto: grnet/zeus
def main(institution_name, username, password):
    inst, created = Institution.objects.get_or_create(name=institution_name)

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

    user.user_type = "password"
    user.name = username
    user.superadmin_p = True
    user.management_p = True
    user.institution = inst
    user.ecounting_account = False
    user.info = {
        "name": username,
        "password": make_password(password)
    }
    if not user.pk:
        user.save()
    user.user_groups = UserGroup.objects.all()
    user.save()
Ejemplo n.º 8
0
def reset_password_confirmed(request):
    uid = request.GET.get('uid')
    uid = sanitize_get_param(uid)
    logged_user = request.zeususer._user
    user = get_user(uid)
    if user:
        if can_do(logged_user, user):
            new_password = random_password()
            user.info['password'] = make_password(new_password)
            user.save()
            message = _("New password for user %(uid)s is "
                        "%(new_pass)s") % {'uid': user.user_id,
                                           'new_pass': new_password}
            messages.info(request, message)
        else:
            message = _("You are not authorized to do this")
            messages.error(request, message)
    else:
        message = _("You didn't choose a user")
        messages.error(request, message)
        return redirect(reverse('list_users'))
    url = "%s?uid=%s" % (reverse('user_management'),
                                   str(user.id))
    return redirect(url)
Ejemplo n.º 9
0
    def handle(self, *args, **options):
        reload(sys)
        sys.setdefaultencoding('utf-8')

        if options.get('create_institution'):
            if not len(args):
                print "Provide the institution name"
                exit()

            name = args[0].strip()
            Institution.objects.create(name=args[0].strip())

        if options.get('remove_user'):
            if not len(args):
                print "Provide a user id"
                exit()

            user = User.objects.get(pk=int(args[0].strip()))
            print "User has %d elections objects which will be removed" % user.elections.count()
            confirm = raw_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 len(args):
                print "Provide a user id and a password"
                exit()
            user = self.get_user(args[0])
            password = getpass.getpass("Password:"******"Confirm password:"******"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.institution = inst
            newuser.ecounting_account = False
            newuser.save()
Ejemplo n.º 10
0
    def handle(self, *args, **options):
        reload(sys)
        sys.setdefaultencoding('utf-8')

        if options.get('create_institution'):
            if not len(args):
                print "Provide the institution name"
                exit()

            name = args[0].strip()
            Institution.objects.create(name=args[0].strip())

        if options.get('remove_user'):
            if not len(args):
                print "Provide a user id"
                exit()

            user = User.objects.get(pk=int(args[0].strip()))
            print "User has %d elections objects which will be removed" % user.elections.count(
            )
            confirm = raw_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 len(args):
                print "Provide a user id and a password"
                exit()
            user = self.get_user(args[0])
            password = getpass.getpass("Password:"******"Confirm password:"******"enable_sms"):
            if not len(args):
                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(args[0])
            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(args[0])
            user.sms_data.limit = options.get("sms_limit")
            user.sms_data.save()

        if options.get('create_user'):
            username = args[0].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()