Esempio n. 1
0
def users_create(request):
    """
    Returns users list
    """
    form = forms.UserCreateForm(MultiDict(request.safe_json_body or {}),
                                csrf_context=request)
    if form.validate():
        log.info("registering user")
        # probably not needed in the future since this requires root anyways
        # lets keep this here in case we lower view permission in the future
        # if request.registry.settings['appenlight.disable_registration']:
        #     return HTTPUnprocessableEntity(body={'error': 'Registration is currently disabled.'})
        user = User()
        # insert new user here
        DBSession.add(user)
        form.populate_obj(user)
        UserService.regenerate_security_code(user)
        UserService.set_password(user, user.user_password)
        user.status = 1 if form.status.data else 0
        request.session.flash(_("User created"))
        DBSession.flush()
        return user.get_dict(exclude_keys=[
            "security_code_date",
            "notes",
            "security_code",
            "user_password",
        ])
    else:
        return HTTPUnprocessableEntity(body=form.errors_json)
Esempio n. 2
0
def users_create(request):
    """
    Returns users list
    """
    form = forms.UserCreateForm(MultiDict(request.safe_json_body or {}),
                                csrf_context=request)
    if form.validate():
        log.info('registering user')
        user = User()
        # insert new user here
        DBSession.add(user)
        form.populate_obj(user)
        user.regenerate_security_code()
        user.set_password(user.user_password)
        user.status = 1 if form.status.data else 0
        request.session.flash(_('User created'))
        DBSession.flush()
        return user.get_dict(exclude_keys=[
            'security_code_date', 'notes', 'security_code', 'user_password'
        ])
    else:
        return HTTPUnprocessableEntity(body=form.errors_json)
Esempio n. 3
0
def register(request):
    """
    Render register page with form
    Also handles oAuth flow for registration
    """
    login_url = request.route_url('ziggurat.routes.sign_in')
    if request.query_string:
        query_string = '?%s' % request.query_string
    else:
        query_string = ''
    referrer = '%s%s' % (request.path, query_string)

    if referrer in [login_url, '/register', '/register?sign_in=1']:
        referrer = '/'  # never use the login form itself as came_from
    sign_in_form = forms.SignInForm(came_from=request.params.get(
        'came_from', referrer),
                                    csrf_context=request)

    # populate form from oAuth session data returned by authomatic
    social_data = request.session.get('zigg.social_auth')
    if request.method != 'POST' and social_data:
        log.debug(social_data)
        user_name = social_data['user'].get('user_name', '').split('@')[0]
        form_data = {
            'user_name': user_name,
            'email': social_data['user'].get('email')
        }
        form_data['user_password'] = str(uuid.uuid4())
        form = forms.UserRegisterForm(MultiDict(form_data),
                                      csrf_context=request)
        form.user_password.widget.hide_value = False
    else:
        form = forms.UserRegisterForm(request.POST, csrf_context=request)
    if request.method == 'POST' and form.validate():
        log.info('registering user')
        # insert new user here
        if request.registry.settings['appenlight.disable_registration']:
            request.session.flash(_('Registration is currently disabled.'))
            return HTTPFound(location=request.route_url('/'))

        new_user = User()
        DBSession.add(new_user)
        form.populate_obj(new_user)
        new_user.regenerate_security_code()
        new_user.status = 1
        new_user.set_password(new_user.user_password)
        new_user.registration_ip = request.environ.get('REMOTE_ADDR')

        if social_data:
            handle_social_data(request, new_user, social_data)

        email_vars = {
            'user': new_user,
            'request': request,
            'email_title': "AppEnlight :: Start information"
        }
        UserService.send_email(request,
                               recipients=[new_user.email],
                               variables=email_vars,
                               template='/email_templates/registered.jinja2')
        request.session.flash(_('You have successfully registered.'))
        DBSession.flush()
        headers = security.remember(request, new_user.id)
        return HTTPFound(location=request.route_url('/'), headers=headers)
    settings = request.registry.settings
    social_plugins = {}
    if settings.get('authomatic.pr.twitter.key', ''):
        social_plugins['twitter'] = True
    if settings.get('authomatic.pr.google.key', ''):
        social_plugins['google'] = True
    if settings.get('authomatic.pr.github.key', ''):
        social_plugins['github'] = True
    if settings.get('authomatic.pr.bitbucket.key', ''):
        social_plugins['bitbucket'] = True

    return {
        "form": form,
        "sign_in_form": sign_in_form,
        "social_plugins": social_plugins
    }
Esempio n. 4
0
def register(request):
    """
    Render register page with form
    Also handles oAuth flow for registration
    """
    login_url = request.route_url("ziggurat.routes.sign_in")
    if request.query_string:
        query_string = "?%s" % request.query_string
    else:
        query_string = ""
    referrer = "%s%s" % (request.path, query_string)

    if referrer in [login_url, "/register", "/register?sign_in=1"]:
        referrer = "/"  # never use the login form itself as came_from
    sign_in_form = forms.SignInForm(
        came_from=request.params.get("came_from", referrer), csrf_context=request
    )

    # populate form from oAuth session data returned by authomatic
    social_data = request.session.get("zigg.social_auth")
    if request.method != "POST" and social_data:
        log.debug(social_data)
        user_name = social_data["user"].get("user_name", "").split("@")[0]
        form_data = {"user_name": user_name, "email": social_data["user"].get("email")}
        form_data["user_password"] = str(uuid.uuid4())
        form = forms.UserRegisterForm(MultiDict(form_data), csrf_context=request)
        form.user_password.widget.hide_value = False
    else:
        form = forms.UserRegisterForm(request.POST, csrf_context=request)
    if request.method == "POST" and form.validate():
        log.info("registering user")
        # insert new user here
        if request.registry.settings["appenlight.disable_registration"]:
            request.session.flash(_("Registration is currently disabled."))
            return HTTPFound(location=request.route_url("/"))

        new_user = User()
        DBSession.add(new_user)
        form.populate_obj(new_user)
        UserService.regenerate_security_code(new_user)
        new_user.status = 1
        UserService.set_password(new_user, new_user.user_password)
        new_user.registration_ip = request.environ.get("REMOTE_ADDR")

        if social_data:
            handle_social_data(request, new_user, social_data)

        email_vars = {
            "user": new_user,
            "request": request,
            "email_title": "AppEnlight :: Start information",
        }
        UserService.send_email(
            request,
            recipients=[new_user.email],
            variables=email_vars,
            template="/email_templates/registered.jinja2",
        )
        request.session.flash(_("You have successfully registered."))
        DBSession.flush()
        headers = security.remember(request, new_user.id)
        return HTTPFound(location=request.route_url("/"), headers=headers)
    settings = request.registry.settings
    social_plugins = {}
    if settings.get("authomatic.pr.twitter.key", ""):
        social_plugins["twitter"] = True
    if settings.get("authomatic.pr.google.key", ""):
        social_plugins["google"] = True
    if settings.get("authomatic.pr.github.key", ""):
        social_plugins["github"] = True
    if settings.get("authomatic.pr.bitbucket.key", ""):
        social_plugins["bitbucket"] = True

    return {
        "form": form,
        "sign_in_form": sign_in_form,
        "social_plugins": social_plugins,
    }