Exemplo n.º 1
0
    def get_or_create_user_by_social_medial_email(
            self, request: Request, user: authomatic.core.User) -> IUserModel:

        User = self.registry.queryUtility(IUserModel)

        dbsession = request.dbsession

        imported_data = self.import_social_media_user(user)
        email = imported_data["email"]

        user = self.get_existing_user(User, dbsession, email)

        if not user:
            user = self.create_blank_user(User, dbsession, email)

            request.registry.notify(UserCreated(request, user))

            self.update_first_login_social_data(user, imported_data)
            user.first_login = True

        else:
            user.first_login = False

        self.activate_user(dbsession, user)

        self.update_every_login_social_data(user, imported_data)
        return user
Exemplo n.º 2
0
def main(argv=sys.argv):

    if len(argv) < 3:
        usage(argv)

    config_uri = argv[1]
    request = init_websauna(config_uri)

    User = get_user_class(request.registry)
    dbsession = request.dbsession

    if len(argv) == 4:
        password = argv[3]
    else:
        password = getpass.getpass("Password:"******"Password (again):")

        if password != password2:
            sys.exit("Password did not match")

    with transaction.manager:
        u = User(email=argv[2], username=argv[2])
        u.password = password
        u.registration_source = "command_line"
        u.activated_at = now()
        dbsession.add(u)
        dbsession.flush()

        request.registry.notify(UserCreated(request, u))

        print("Created user #{}: {}, admin: {}".format(u.id, u.email,
                                                       u.is_admin()))
Exemplo n.º 3
0
    def sign_up(self, user_data: dict) -> Response:
        """Sign up a new user."""

        user_registry = get_user_registry(self.request)
        user = user_registry.sign_up(registration_source="email", user_data=user_data)

        # Notify site creator to initialize the admin for the first user
        self.request.registry.notify(UserCreated(self.request, user))

        settings = self.request.registry.settings

        require_activation = asbool(settings.get('websauna.require_activation', True))
        autologin = asbool(settings.get('websauna.autologin', False))

        if require_activation:
            self.create_email_activation(user)
        elif not autologin:
            messages.add(self.request, msg_id="msg-sign-up-complete", msg="Sign up complete. Welcome!", kind="success")

        self.request.registry.notify(NewRegistrationEvent(self.request, user, None, user_data))

        self.request.dbsession.flush()  # in order to get the id
        if autologin:
            login_service = get_login_service(self.request.registry)
            return login_service.authenticate(self.request, user)
        else:  # not autologin: user must log in just after registering.
            return render_to_response('login/waiting_for_activation.html', {"user": user}, request=self.request)
Exemplo n.º 4
0
def get_or_create_email_user(request: Request, email: str) -> User:
    """Fetch existing user or create new based on email."""
    dbsession = request.dbsession

    u = dbsession.query(User).filter_by(email=email).first()
    if u is not None:
        u.first_login = False
        return u

    u = User(email=email)
    u.registration_source = "email"
    u.activated_at = now()
    u.first_login = True

    dbsession.add(u)
    dbsession.flush()

    request.registry.notify(UserCreated(request, u))
    return u
Exemplo n.º 5
0
def create(
        request,
        username: str,
        email: str,
        password: t.Optional[str]=None,
        source: str='command_line',
        admin: bool=False
) -> IUserModel:
    """Create a new site user from command line.

    :param request:
    :param username: Username, usually an email.
    :param email: User's email.
    :param password: Password.
    :param source: Source of this user, in here, command_line.
    :param admin: Set this user to admin. The first user is always implicitly admin.
    :return: Newly created user.
    """
    User = get_user_class(request.registry)
    dbsession = request.dbsession
    u = dbsession.query(User).filter_by(email=email).first()
    if u is not None:
        return u

    u = User(email=email, username=username)
    dbsession.add(u)
    dbsession.flush()  # Make sure u.user_data is set

    if password:
        user_registry = get_user_registry(request)
        user_registry.set_password(u, password)

    u.registration_source = source
    u.activated_at = now()

    request.registry.notify(UserCreated(request, u))
    if admin:
        group = dbsession.query(Group).filter_by(name='admin').one_or_none()
        group.users.append(u)

    return u
Exemplo n.º 6
0
def create(request,
           username,
           email,
           password=None,
           source="command_line",
           admin=False):
    """Create a new site user from command line.

    :param request:
    :param username:
    :param email:
    :param password:
    :param source:
    :param admin: Set this user to admin. The first user is always implicitly admin.
    :return:
    """
    User = get_user_class(request.registry)
    dbsession = request.dbsession
    u = dbsession.query(User).filter_by(email=email).first()
    if u is not None:
        return u

    u = User(email=email, username=username)
    dbsession.add(u)
    dbsession.flush()  # Make sure u.user_data is set

    if password:
        user_registry = get_user_registry(request)
        user_registry.set_password(u, password)

    u.registration_source = source
    u.activated_at = now()

    request.registry.notify(UserCreated(request, u))

    if admin:
        group = dbsession.query(Group).filter_by(name="admin").one_or_none()
        group.users.append(u)

    return u
Exemplo n.º 7
0
    def get_or_create_user_by_social_medial_email(
            self, request: Request, user: authomatic.core.User) -> IUserModel:
        """Given a user information returned by Authomatic, return an existing User object from our database or create one if it does not exists.

        :param request: Pyramid request object.
        :param user: A user returned by Authomatic.
        :return: User object.
        """
        User = self.registry.queryUtility(IUserModel)
        dbsession = request.dbsession
        imported_data = self.import_social_media_user(user)
        email = imported_data['email']
        user = self.get_existing_user(User, dbsession, email)
        if not user:
            user = self.create_blank_user(User, dbsession, email)
            request.registry.notify(UserCreated(request, user))
            self.update_first_login_social_data(user, imported_data)
            user.first_login = True
        else:
            user.first_login = False
        self.activate_user(dbsession, user)
        self.update_every_login_social_data(user, imported_data)
        return user