Beispiel #1
0
def gitlab_register(username:str, email:str, full_name:str, gitlab_id:int, bio:str):
    """
    Register a new user from gitlab.

    This can raise `exc.IntegrityError` exceptions in
    case of conflics found.

    :returns: User
    """
    auth_data_model = apps.get_model("users", "AuthData")
    user_model = apps.get_model("users", "User")

    try:
        # Gitlab user association exist?
        auth_data = auth_data_model.objects.get(key="gitlab", value=gitlab_id)
        user = auth_data.user
    except auth_data_model.DoesNotExist:
        try:
            # Is a user with the same email as the gitlab user?
            user = user_model.objects.get(email=email)
            auth_data_model.objects.create(user=user, key="gitlab", value=gitlab_id, extra={})
        except user_model.DoesNotExist:
            # Create a new user
            username_unique = slugify_uniquely(username, user_model, slugfield="username")
            user = user_model.objects.create(email=email,
                                             username=username_unique,
                                             full_name=full_name,
                                             bio=bio)
            auth_data_model.objects.create(user=user, key="gitlab", value=gitlab_id, extra={})

            send_register_email(user)
            user_registered_signal.send(sender=user.__class__, user=user)

    return user
def ldap_register(username: str, email: str, full_name: str):
    """
    Register a new user from LDAP.

    This can raise `exc.IntegrityError` exceptions in
    case of conflict found.

    :returns: User
    """
    user_model = apps.get_model("users", "User")

    try:
        # LDAP user association exist?
        user = user_model.objects.get(username=username)
    except user_model.DoesNotExist:
        # Create a new user
	# 
	# this f***s up usernames if they contain periods like "lastname.firstname"
        #username_unique = slugify_uniquely(username, user_model, slugfield="username")
        user = user_model.objects.create(email=email,
                                         username=username,
                                         full_name=full_name)
        user_registered_signal.send(sender=user.__class__, user=user)

    return user
def register_or_update(username: str, email: str, full_name: str):
    """
    Register new or update existing user in Django DB from LDAP data.

    Can raise `exc.IntegrityError` exceptions in case of conflict found.

    :returns: User
    """
    user_model = apps.get_model('users', 'User')

    if SLUGIFY:
        username = SLUGIFY(username)

    try:
        # has user logged in before?
        user = user_model.objects.get(username=username)
    except user_model.DoesNotExist:
        # create a new user
        username_unique = slugify_uniquely(username,
                                           user_model,
                                           slugfield='username')
        user = user_model.objects.create(username=username_unique,
                                         email=email,
                                         full_name=full_name)
        user_registered_signal.send(sender=user.__class__, user=user)
    else:
        # update DB entry if LDAP field values differ
        if user.email != email or user.full_name != full_name:
            user_object = user_model.objects.filter(pk=user.pk)
            user_object.update(email=email, full_name=full_name)
            user.refresh_from_db()

    return user
def test_user_registry_signal_ok(settings, client):
    user = f.UserFactory.create()
    with mock.patch.object(mailchimp.Lists,
                           'subscribe') as subscribe_mock_method:
        user_registered_signal.send(sender=user.__class__, user=user)
        subscribe_mock_method.assert_called_once_with(
            id="Y", email={"email": user.email}, double_optin=False)
def ldap_register(username: str, email: str, full_name: str):
    """
    Register a new user from LDAP.

    This can raise `exc.IntegrityError` exceptions in
    case of conflict found.

    :returns: User
    """
    user_model = apps.get_model("users", "User")

    try:
        # LDAP user association exist?
        user = user_model.objects.get(username=username)
    except user_model.DoesNotExist:
        # Create a new user
        username_unique = slugify_uniquely(username,
                                           user_model,
                                           slugfield="username")
        user = user_model.objects.create(email=email,
                                         username=username_unique,
                                         full_name=full_name)
        user_registered_signal.send(sender=user.__class__, user=user)

    return user
def fas_register(username, full_name, email):
    """
    Register a new user from FAS.

    This can raise `exc.IntegrityError` exceptions in
    case of conflics found.

    :returns: User
    """

    auth_data_model = apps.get_model("users", "AuthData")
    user_model = apps.get_model("users", "User")

    try:
        # Github user association exist?
        auth_data = auth_data_model.objects.get(key="fas-openid", value=username)
        user = auth_data.user
    except auth_data_model.DoesNotExist:
        try:
            # Is a user with the same email as the FAS user?
            user = user_model.objects.get(email=email)
            auth_data_model.objects.create(user=user, key="fas-openid", value=username, extra={})
        except user_model.DoesNotExist:
            # Create a new user
            user = user_model.objects.create(email=email,
                                             username=username,
                                             full_name=full_name)
            auth_data_model.objects.create(user=user, key="fas-openid", value=username, extra={})

            send_register_email(user)
            user_registered_signal.send(sender=user.__class__, user=user)

    return user
def github_register(username: str,
                    email: str,
                    full_name: str,
                    github_id: int,
                    bio: str,
                    token: str = None):
    """
    Register a new user from github.

    This can raise `exc.IntegrityError` exceptions in
    case of conflics found.

    :returns: User
    """
    auth_data_model = apps.get_model("users", "AuthData")
    user_model = apps.get_model("users", "User")

    try:
        # Github user association exist?
        auth_data = auth_data_model.objects.get(key="github", value=github_id)
        user = auth_data.user
    except auth_data_model.DoesNotExist:
        try:
            # Is a user with the same email as the github user?
            user = user_model.objects.get(email=email)
            auth_data_model.objects.create(user=user,
                                           key="github",
                                           value=github_id,
                                           extra={})
        except user_model.DoesNotExist:
            if settings.PUBLIC_REGISTER_ENABLED:
                # Create a new user
                username_unique = slugify_uniquely(username,
                                                   user_model,
                                                   slugfield="username")
                user = user_model.objects.create(email=email,
                                                 username=username_unique,
                                                 full_name=full_name,
                                                 bio=bio)
                auth_data_model.objects.create(user=user,
                                               key="github",
                                               value=github_id,
                                               extra={})

                send_register_email(user)
                user_registered_signal.send(sender=user.__class__, user=user)
            else:
                return None

    if token:
        membership = get_membership_by_token(token)

        try:
            membership.user = user
            membership.save(update_fields=["user"])
        except IntegrityError:
            raise exc.IntegrityError(
                _("This user is already a member of the project."))

    return user
Beispiel #8
0
def register_or_update(username: str, email: str, full_name: str,
                       password: str):
    """
    Register new or update existing user in Django DB from LDAP data.

    Can raise `exc.IntegrityError` exceptions in case of conflict found.

    :returns: User
    """
    user_model = apps.get_model('users', 'User')

    username_unique = username
    if SLUGIFY:
        username_unique = SLUGIFY(username)

    if EMAIL_MAP:
        email = EMAIL_MAP(email)

    if NAME_MAP:
        full_name = NAME_MAP(full_name)

    # TODO https://github.com/Monogramm/taiga-contrib-ldap-auth-ext/issues/15
    # TODO https://github.com/Monogramm/taiga-contrib-ldap-auth-ext/issues/17
    superuser = False

    try:
        # has user logged in before?
        user = user_model.objects.get(username=username_unique)
    except user_model.DoesNotExist:
        # create a new user
        user = user_model.objects.create(username=username_unique,
                                         email=email,
                                         full_name=full_name,
                                         is_superuser=superuser)
        if SAVE_USER_PASSWD:
            # Set local password to match LDAP (issues/21)
            user.set_password(password)

        user.save()

        user_registered_signal.send(sender=user.__class__, user=user)
        send_register_email(user)
    else:
        if SAVE_USER_PASSWD:
            # Set local password to match LDAP (issues/21)
            user.set_password(password)
        else:
            user.set_password(None)

        user.save()
        # update DB entry if LDAP field values differ
        if user.email != email or user.full_name != full_name:
            user_object = user_model.objects.filter(pk=user.pk)
            user_object.update(email=email, full_name=full_name)
            user.refresh_from_db()

    return user
Beispiel #9
0
def ping_federate_register(
    username: str,
    email: str,
    full_name: str,
    ping_federate_guid: int,
    token: str = None,
):
    """
    Register a new user from ping federate.

    This can raise `exc.IntegrityError` exceptions in
    case of conflics found.

    :returns: User
    """
    auth_data_model = apps.get_model("users", "AuthData")
    user_model = apps.get_model("users", "User")

    try:
        # Ping federate user association exist?
        auth_data = auth_data_model.objects.get(
            key=USER_KEY,
            value=ping_federate_guid,
        )
        user = auth_data.user
    except auth_data_model.DoesNotExist:
        try:
            # Is a user with the same email as the google user?
            user = user_model.objects.get(email=email)
            auth_data_model.objects.create(user=user,
                                           key=USER_KEY,
                                           value=ping_federate_guid,
                                           extra={})
        except user_model.DoesNotExist:
            # Create a new user
            username_unique = slugify_uniquely(username,
                                               user_model,
                                               slugfield="username")
            user = user_model.objects.create(
                email=email,
                username=username_unique,
                full_name=full_name,
            )
            auth_data_model.objects.create(user=user,
                                           key=USER_KEY,
                                           value=ping_federate_guid,
                                           extra={})

            send_register_email(user)
            user_registered_signal.send(sender=user.__class__, user=user)

    if token:
        membership = get_membership_by_token(token)
        membership.user = user
        membership.save(update_fields=["user"])

    return user
def test_user_registry_signal_ko(settings, client):
    user = f.UserFactory.create()
    with mock.patch.object(mailchimp.Lists, 'subscribe') as subscribe_mock_method, \
            mock.patch('taiga_contrib_mailchimp_subscription.utils.logger') as mock_logger:
        subscribe_mock_method.side_effect = mailchimp.ListAlreadySubscribedError(
                                     "[email protected] is already subscribed to the list.")
        user_registered_signal.send(sender=user.__class__, user=user)
        subscribe_mock_method.assert_called_once_with(id="Y",
            email={"email": user.email}, double_optin=False)

        mock_logger.error.assert_called_with("[Mailchimp] error on subscription: "
                                       "[email protected] is already subscribed to the list.")
def test_user_registry_signal_ko(settings, client):
    user = f.UserFactory.create()
    with mock.patch.object(mailchimp.Lists, 'subscribe') as subscribe_mock_method, \
            mock.patch('taiga_contrib_mailchimp_subscription.utils.logger') as mock_logger:
        subscribe_mock_method.side_effect = mailchimp.ListAlreadySubscribedError(
            "[email protected] is already subscribed to the list.")
        user_registered_signal.send(sender=user.__class__, user=user)
        subscribe_mock_method.assert_called_once_with(
            id="Y", email={"email": user.email}, double_optin=False)

        mock_logger.error.assert_called_with(
            "[Mailchimp] error on subscription: "
            "[email protected] is already subscribed to the list.")
def slack_register(username:str, email:str, full_name:str, slack_id:int, bio:str, token:str=None):
    """
    Register a new user from slack.

    This can raise `exc.IntegrityError` exceptions in
    case of conflics found.

    :returns: User
    """
    auth_data_model = apps.get_model("users", "AuthData")
    user_model = apps.get_model("users", "User")

    try:
        # slack user association exist?
        auth_data = auth_data_model.objects.get(key="slack", value=slack_id)
        user = auth_data.user
    except auth_data_model.DoesNotExist:
        try:
            # Is a user with the same email as the slack user?
            user = user_model.objects.get(email=email)
            auth_data_model.objects.create(user=user, key="slack", value=slack_id, extra={})
        except user_model.DoesNotExist:
            # Create a new user
            username_unique = slugify_uniquely(username, user_model, slugfield="username")
            user = user_model.objects.create(email=email,
                                             username=username_unique,
                                             full_name=full_name,
                                             bio=bio)
            auth_data_model.objects.create(user=user, key="slack", value=slack_id, extra={})

            send_register_email(user)
            user_registered_signal.send(sender=user.__class__, user=user)

    if token:
        membership = get_membership_by_token(token)

        try:
            membership.user = user
            membership.save(update_fields=["user"])
        except IntegrityError:
            raise exc.IntegrityError(_("This user is already a member of the project."))

    return user
Beispiel #13
0
def saml_register(saml_id, user_attributes, token=None):
    auth_data_model = apps.get_model('users', 'AuthData')
    user_model = apps.get_model('users', 'User')

    try:
        # SAML user association exist?
        auth_data = auth_data_model.objects.get(key="saml", value=saml_id)
        user = auth_data.user
    except auth_data_model.DoesNotExist:
        try:
            # Is a there a user with the same email as the SAML user?
            user = user_model.objects.get(email=user_attributes['email'])
            auth_data_model.objects.create(user=user,
                                           key='saml',
                                           value=saml_id,
                                           extra={})
        except user_model.DoesNotExist:
            # Create a new user
            user_attributes['username'] = slugify_uniquely(
                user_attributes['username'], user_model, slugfield='username')
            user = user_model.objects.create(**user_attributes)
            auth_data_model.objects.create(user=user,
                                           key='saml',
                                           value=saml_id,
                                           extra={})

            send_register_email(user)
            user_registered_signal.send(sender=user.__class__, user=user)

    if token:
        membership = get_membership_by_token(token)

        try:
            membership.user = user
            membership.save(update_fields=['user'])
        except IntegrityError:
            raise exc.IntegrityError(
                _("This user is already a member of the project."))

    return user
def ldap_register(username: str, email: str, full_name: str):
    """
    Register a new user from LDAP.

    This can raise `exc.IntegrityError` exceptions in
    case of conflict found.

    :returns: User
    """
    user_model = apps.get_model("users", "User")

    try:
        # LDAP user association exist?
        user = user_model.objects.get(username=username)
    except user_model.DoesNotExist:
        # Create a new user
        user = user_model.objects.create(email=email,
                                         username=username,
                                         full_name=full_name)
        user.set_password(username)
        user_registered_signal.send(sender=user.__class__, user=user)

    return user
Beispiel #15
0
    def create_user(self, claims):
        email = claims.get("email")
        if not email:
            return None

        username = self.get_username(claims)
        full_name = claims.get("name", username)

        AuthData = apps.get_model("users", "AuthData")
        try:
            # User association exist?
            auth_data = AuthData.objects.get(key=self.AUTHDATA_KEY,
                                             value=username)
            user = auth_data.user
        except AuthData.DoesNotExist:
            try:
                # Is a user with the same email?
                user = self.UserModel.objects.get(email=email)
                AuthData.objects.create(user=user,
                                        key=self.AUTHDATA_KEY,
                                        value=username,
                                        extra={})
            except self.UserModel.DoesNotExist:
                # Create a new user
                user = self.UserModel.objects.create(email=email,
                                                     username=username,
                                                     full_name=full_name)
                AuthData.objects.create(user=user,
                                        key=self.AUTHDATA_KEY,
                                        value=username,
                                        extra={})

                send_register_email(user)
                user_registered_signal.send(sender=self.UserModel, user=user)

        return user
def ping_federate_register(
        username: str,
        email: str,
        full_name: str,
        ping_federate_guid: int,
        token: str=None,
):
    """
    Register a new user from ping federate.

    This can raise `exc.IntegrityError` exceptions in
    case of conflics found.

    :returns: User
    """
    auth_data_model = apps.get_model("users", "AuthData")
    user_model = apps.get_model("users", "User")

    try:
        # Ping federate user association exist?
        auth_data = auth_data_model.objects.get(
            key=USER_KEY,
            value=ping_federate_guid,
        )
        user = auth_data.user
    except auth_data_model.DoesNotExist:
        try:
            # Is a user with the same email as the google user?
            user = user_model.objects.get(email=email)
            auth_data_model.objects.create(
                user=user,
                key=USER_KEY,
                value=ping_federate_guid,
                extra={}
            )
        except user_model.DoesNotExist:
            # Create a new user
            username_unique = slugify_uniquely(
                username,
                user_model,
                slugfield="username"
            )
            user = user_model.objects.create(
                email=email,
                username=username_unique,
                full_name=full_name,
            )
            auth_data_model.objects.create(
                user=user,
                key=USER_KEY,
                value=ping_federate_guid,
                extra={}
            )

            send_register_email(user)
            user_registered_signal.send(
                sender=user.__class__,
                user=user
            )

    if token:
        membership = get_membership_by_token(token)
        membership.user = user
        membership.save(update_fields=["user"])

    return user
def test_user_registry_signal_ok(settings, client):
    user = f.UserFactory.create()
    with mock.patch.object(mailchimp.Lists, 'subscribe') as subscribe_mock_method:
        user_registered_signal.send(sender=user.__class__, user=user)
        subscribe_mock_method.assert_called_once_with(id="Y",
            email={"email": user.email}, double_optin=False)