def test_get_membership_by_token(self): with self.assertRaises(exc.NotFound): services.get_membership_by_token("invalidtoken") project = create_project(1, self.user1) membership = add_membership(project, self.user1, "back") membership.token = "foobar" membership.save() m = services.get_membership_by_token("foobar") self.assertEqual(m.id, membership.id)
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
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 openid_register(username: str, email: str, full_name: str, openid_id: int, token: str = None): """ Register a new user from openid. 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: # openid user association exist? auth_data = auth_data_model.objects.get(key="openid", value=openid_id) user = auth_data.user except auth_data_model.DoesNotExist: try: # Is a user with the same email as the openid user? user = user_model.objects.get(email=email) auth_data_model.objects.create(user=user, key="openid", value=openid_id, extra={}) except user_model.DoesNotExist: raise exc.IntegrityError( _("This user doesn't exists. Please contact with an admin of this Taiga." )) 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 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
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 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