Ejemplo n.º 1
0
    def test_simple(self):
        user = self.create_user()

        org = self.create_organization(owner=user)

        auth_provider = AuthProvider.objects.create(
            organization=org,
            provider='dummy',
        )
        auth_identity = AuthIdentity.objects.create(
            auth_provider=auth_provider,
            ident=user.email,
            user=user,
        )
        auth = Authenticator.objects.create(
            type=available_authenticators(ignore_backup=True)[0].type,
            user=user,
        )

        result = serialize(user, user, DetailedUserSerializer())
        assert result['id'] == six.text_type(user.id)
        assert result['has2fa'] is True
        assert len(result['emails']) == 1
        assert result['emails'][0]['email'] == user.email
        assert result['emails'][0]['is_verified']
        assert 'identities' in result
        assert len(result['identities']) == 1
        assert result['identities'][0]['id'] == six.text_type(auth_identity.id)
        assert result['identities'][0]['name'] == auth_identity.ident
        assert 'authenticators' in result
        assert len(result['authenticators']) == 1
        assert result['authenticators'][0]['id'] == six.text_type(auth.id)
Ejemplo n.º 2
0
    def test_simple(self):
        user = self.create_user()
        UserPermission.objects.create(user=user, permission="foo")

        org = self.create_organization(owner=user)

        auth_provider = AuthProvider.objects.create(organization=org,
                                                    provider="dummy")
        auth_identity = AuthIdentity.objects.create(
            auth_provider=auth_provider, ident=user.email, user=user)
        auth = Authenticator.objects.create(
            type=available_authenticators(ignore_backup=True)[0].type,
            user=user)

        result = serialize(user, user, DetailedUserSerializer())
        assert result["id"] == six.text_type(user.id)
        assert result["has2fa"] is True
        assert len(result["emails"]) == 1
        assert result["emails"][0]["email"] == user.email
        assert result["emails"][0]["is_verified"]
        assert "identities" in result
        assert len(result["identities"]) == 1
        assert result["identities"][0]["id"] == six.text_type(auth_identity.id)
        assert result["identities"][0]["name"] == auth_identity.ident
        assert "authenticators" in result
        assert len(result["authenticators"]) == 1
        assert result["authenticators"][0]["id"] == six.text_type(auth.id)
        assert result["permissions"] == ["foo"]
        assert result["canReset2fa"] is True

        self.create_organization(owner=user)
        result = serialize(user, user, DetailedUserSerializer())
        assert result["canReset2fa"] is False
Ejemplo n.º 3
0
    def test_simple(self):
        user = self.create_user()

        result = serialize(user)
        assert result['id'] == six.text_type(user.id)
        assert result['has2fa'] is False

        Authenticator.objects.create(
            user=user,
            type=available_authenticators(ignore_backup=True)[0].type,
        )

        result = serialize(user)
        assert result['id'] == six.text_type(user.id)
        assert result['has2fa'] is True
Ejemplo n.º 4
0
    def test_simple(self):
        user = self.create_user()

        result = serialize(user)
        assert result['id'] == six.text_type(user.id)
        assert result['has2fa'] is False

        Authenticator.objects.create(
            type=available_authenticators(ignore_backup=True)[0].type,
            user=user,
        )

        result = serialize(user)
        assert result['id'] == six.text_type(user.id)
        assert result['has2fa'] is True
        assert len(result['emails']) == 1
        assert result['emails'][0]['email'] == user.email
        assert result['emails'][0]['is_verified']
Ejemplo n.º 5
0
    def test_simple(self):
        user = self.create_user()

        result = serialize(user)
        assert result["id"] == six.text_type(user.id)
        assert result["has2fa"] is False

        Authenticator.objects.create(
            type=available_authenticators(ignore_backup=True)[0].type,
            user=user)

        result = serialize(user)
        assert result["id"] == six.text_type(user.id)
        assert result["has2fa"] is True
        assert len(result["emails"]) == 1
        assert result["emails"][0]["email"] == user.email
        assert result["emails"][0]["is_verified"]
        assert result["isSuperuser"] is False
    def get(self, request, organization):
        queryset = (OrganizationMember.objects.filter(
            Q(user__is_active=True) | Q(user__isnull=True),
            organization=organization,
            invite_status=InviteStatus.APPROVED.value,
        ).select_related("user").order_by("email", "user__email"))

        query = request.GET.get("query")

        if query:
            tokens = tokenize_query(query)
            for key, value in six.iteritems(tokens):
                if key == "email":
                    queryset = queryset.filter(
                        Q(email__in=value)
                        | Q(user__email__in=value)
                        | Q(user__emails__email__in=value))

                elif key == "scope":
                    queryset = queryset.filter(
                        role__in=[r.id for r in roles.with_any_scope(value)])

                elif key == "role":
                    queryset = queryset.filter(role__in=value)

                elif key == "isInvited":
                    isInvited = "true" in value
                    queryset = queryset.filter(user__isnull=isInvited)

                elif key == "ssoLinked":
                    ssoFlag = OrganizationMember.flags["sso:linked"]
                    ssoLinked = "true" in value
                    if ssoLinked:
                        queryset = queryset.filter(
                            flags=F("flags").bitor(ssoFlag))
                    else:
                        queryset = queryset.filter(
                            flags=F("flags").bitand(~ssoFlag))

                elif key == "has2fa":
                    has2fa = "true" in value
                    if has2fa:
                        types = [
                            a.type for a in available_authenticators(
                                ignore_backup=True)
                        ]
                        queryset = queryset.filter(
                            user__authenticator__isnull=False,
                            user__authenticator__type__in=types)
                    else:
                        queryset = queryset.filter(
                            user__authenticator__isnull=True)

                elif key == "query":
                    value = " ".join(value)
                    queryset = queryset.filter(
                        Q(email__icontains=value)
                        | Q(user__email__icontains=value)
                        | Q(user__name__icontains=value))
                else:
                    queryset = queryset.none()

        return self.paginate(
            request=request,
            queryset=queryset,
            on_results=lambda x: serialize(x, request.user),
            paginator_cls=OffsetPaginator,
        )