Exemplo n.º 1
0
def openid_login_callback(request):
    #构造需要检查签名的内容
    OPENID_RESPONSE = dict(request.GET)
    SIGNED_CONTENT = []
    #import json
    #print json.dumps(OPENID_RESPONSE, indent=4)
    for k in OPENID_RESPONSE['openid.signed'][0].split(","):
        response_data = OPENID_RESPONSE["openid.%s" % k]
        SIGNED_CONTENT.append("%s:%s\n" % (k, response_data[0]))
    SIGNED_CONTENT = "".join(SIGNED_CONTENT).encode("UTF-8")
    # 使用associate请求获得的mac_key与SIGNED_CONTENT进行assoc_type hash,
    # 检查是否与OpenID Server返回的一致
    SIGNED_CONTENT_SIG = base64.b64encode(
        hmac.new(base64.b64decode(request.session.get('mac_key', '')),
                 SIGNED_CONTENT, hashlib.sha256).digest())

    if SIGNED_CONTENT_SIG != OPENID_RESPONSE['openid.sig'][0]:
        return '认证失败,请重新登录验证'

    request.session.pop('mac_key', None)
    email = request.GET.get('openid.sreg.email', '')
    fullname = request.GET.get('openid.sreg.fullname', '')
    next_url = request.GET.get('next', '/')

    login_user = User.objects.filter(username__iexact=email)
    if login_user.exists():
        login_user = login_user[0]
        login_user.set_password("sentry_netease_openid_pwd")
        login_user.name = fullname  # update by hzwangzhiwei @20160329
        login_user.save()
    else:
        #不存在数据,则增加数据数用户表
        login_user = User(username=email, name=fullname, email=email)
        login_user.set_password("sentry_netease_openid_pwd")
        login_user.save()  #save to db

    # 如果不存在将这个人加入到组织member表中
    if not OrganizationMember.objects.filter(
            user=login_user, organization=Organization.get_default()).exists():
        # 同时给他们默认的trace收集
        # 将用户到组织中
        orgMember = OrganizationMember(user=login_user,
                                       organization=Organization.get_default())
        orgMember.save()
        orgMember = OrganizationMember.objects.get(
            user=login_user, organization=Organization.get_default())
        # 保存组织者到第一个小组
        orgMemTeam = OrganizationMemberTeam(organizationmember=orgMember,
                                            team=Team.objects.get(id=1))
        orgMemTeam.save()

    # HACK: grab whatever the first backend is and assume it works
    login_user.backend = settings.AUTHENTICATION_BACKENDS[0]

    auth.login(request, login_user)
    # can_register should only allow a single registration
    request.session.pop('can_register', None)
    request.session.pop('needs_captcha', None)
    return HttpResponseRedirect(next_url)
def sentry_update_team_member(team, organization_member):
    model = sentry_find_team_member(team, organization_member)
    existing = model is not None
    if not existing:
        model = OrganizationMemberTeam()
        model.organizationmember = organization_member
        model.team = team
        model.save()

    return model, existing
Exemplo n.º 3
0
    def delete(self, request, organization, member_id, team_slug):
        """
        Leave a team

        Leave a team.
        """
        try:
            om = self._get_member(request, organization, member_id)
        except OrganizationMember.DoesNotExist:
            raise ResourceDoesNotExist

        if not self._can_access(request, om):
            return Response({'detail': ERR_INSUFFICIENT_ROLE}, status=400)

        try:
            team = Team.objects.get(
                organization=organization,
                slug=team_slug,
            )
        except Team.DoesNotExist:
            raise ResourceDoesNotExist

        try:
            omt = OrganizationMemberTeam.objects.get(
                team=team,
                organizationmember=om,
            )
        except OrganizationMemberTeam.DoesNotExist:
            # we need to create the row in order to handle superusers leaving a
            # team which they were never a member for (therefor there was never
            # a matching row)
            omt = OrganizationMemberTeam(
                team=team,
                organizationmember=om,
                # setting this to true ensures it gets saved below
                is_active=True,
            )

        if omt.is_active:
            omt.is_active = False
            omt.save()

            self.create_audit_entry(
                request=request,
                organization=organization,
                target_object=omt.id,
                target_user=om.user,
                event=AuditLogEntryEvent.MEMBER_LEAVE_TEAM,
                data=omt.get_audit_log_data(),
            )

        return Response(serialize(team, request.user,
                                  TeamWithProjectsSerializer()),
                        status=200)
    def delete(self, request, organization, member_id, team_slug):
        """
        Leave a team

        Leave a team.
        """
        try:
            om = self._get_member(request, organization, member_id)
        except OrganizationMember.DoesNotExist:
            raise ResourceDoesNotExist

        if not self._can_access(request, om):
            return Response({'detail': ERR_INSUFFICIENT_ROLE}, status=400)

        try:
            team = Team.objects.get(
                organization=organization,
                slug=team_slug,
            )
        except Team.DoesNotExist:
            raise ResourceDoesNotExist

        try:
            omt = OrganizationMemberTeam.objects.get(
                team=team,
                organizationmember=om,
            )
        except OrganizationMemberTeam.DoesNotExist:
            # we need to create the row in order to handle superusers leaving a
            # team which they were never a member for (therefor there was never
            # a matching row)
            omt = OrganizationMemberTeam(
                team=team,
                organizationmember=om,
                # setting this to true ensures it gets saved below
                is_active=True,
            )

        if omt.is_active:
            omt.is_active = False
            omt.save()

            self.create_audit_entry(
                request=request,
                organization=organization,
                target_object=omt.id,
                target_user=om.user,
                event=AuditLogEntryEvent.MEMBER_LEAVE_TEAM,
                data=omt.get_audit_log_data(),
            )

        return Response(serialize(
            team, request.user, TeamWithProjectsSerializer()), status=200)
    def post(self, request, organization, member_id, team_slug):
        """
        Join a team

        Join or request access to a team.

        If the user is already a member of the team, this will simply return
        a 204.

        If the user needs permission to join the team, an access request will
        be generated and the returned status code will be 202.
        """
        try:
            om = self._get_member(request, organization, member_id)
        except OrganizationMember.DoesNotExist:
            raise ResourceDoesNotExist

        if not self._can_access(request, om):
            return Response({'detail': ERR_INSUFFICIENT_ROLE}, status=400)

        try:
            team = Team.objects.get(
                organization=organization,
                slug=team_slug,
            )
        except Team.DoesNotExist:
            raise ResourceDoesNotExist

        if not om.has_global_access:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                )
            except OrganizationMemberTeam.DoesNotExist:
                # TODO(dcramer): this should create a pending request and
                # return a 202
                if not organization.flags.allow_joinleave:
                    omt, created = OrganizationAccessRequest.objects.get_or_create(
                        team=team,
                        member=om,
                    )
                    if created:
                        omt.send_request_email()
                    return Response(status=202)

                omt = OrganizationMemberTeam(
                    team=team,
                    organizationmember=om,
                    is_active=False,
                )

            if omt.is_active:
                return Response(status=204)
        else:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                )
            except OrganizationMemberTeam.DoesNotExist:
                # if the relationship doesnt exist, they're already a member
                return Response(status=204)

        omt.is_active = True
        omt.save()

        self.create_audit_entry(
            request=request,
            organization=organization,
            target_object=omt.id,
            target_user=om.user,
            event=AuditLogEntryEvent.MEMBER_JOIN_TEAM,
            data=omt.get_audit_log_data(),
        )

        return Response(serialize(
            team, request.user, TeamWithProjectsSerializer()), status=201)
    def delete(self, request, organization, member_id, team_slug):
        """
        Leave a team

        Leave a team.
        """
        try:
            om = self._get_member(request, organization, member_id)
        except OrganizationMember.DoesNotExist:
            raise ResourceDoesNotExist

        if not self._can_access(request, om):
            return Response({'detail': ERR_INSUFFICIENT_ROLE}, status=400)

        try:
            team = Team.objects.get(
                organization=organization,
                slug=team_slug,
            )
        except Team.DoesNotExist:
            raise ResourceDoesNotExist

        if not om.has_global_access:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                )
            except OrganizationMemberTeam.DoesNotExist:
                # if the relationship doesnt exist, they're already a member
                return Response(serialize(
                    team, request.user, TeamWithProjectsSerializer()), status=200)
        else:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                    is_active=True,
                )
            except OrganizationMemberTeam.DoesNotExist:
                omt = OrganizationMemberTeam(
                    team=team,
                    organizationmember=om,
                    is_active=True,
                )

        if omt.is_active:
            omt.is_active = False
            omt.save()

            self.create_audit_entry(
                request=request,
                organization=organization,
                target_object=omt.id,
                target_user=om.user,
                event=AuditLogEntryEvent.MEMBER_LEAVE_TEAM,
                data=omt.get_audit_log_data(),
            )

        return Response(serialize(
            team, request.user, TeamWithProjectsSerializer()), status=200)
Exemplo n.º 7
0
    def post(self, request, organization, member_id, team_slug):
        """
        Join a team

        Join or request access to a team.

        If the user is already a member of the team, this will simply return
        a 204.

        If the user needs permission to join the team, an access request will
        be generated and the returned status code will be 202.
        """
        try:
            om = OrganizationMember.objects.filter(
                organization=organization,
                id=member_id,
            ).select_related('user').get()
        except OrganizationMember.DoesNotExist:
            raise ResourceDoesNotExist

        if not self._can_access(request, om):
            return Response({'detail': ERR_INSUFFICIENT_ROLE}, status=400)

        try:
            team = Team.objects.get(
                organization=organization,
                slug=team_slug,
            )
        except Team.DoesNotExist:
            raise ResourceDoesNotExist

        if not om.has_global_access:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                )
            except OrganizationMemberTeam.DoesNotExist:
                # TODO(dcramer): this should create a pending request and
                # return a 202
                if not organization.flags.allow_joinleave:
                    omt, created = OrganizationAccessRequest.objects.get_or_create(
                        team=team,
                        member=om,
                    )
                    if created:
                        omt.send_request_email()
                    return Response(status=202)

                omt = OrganizationMemberTeam(
                    team=team,
                    organizationmember=om,
                    is_active=False,
                )

            if omt.is_active:
                return Response(status=204)
        else:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                )
            except OrganizationMemberTeam.DoesNotExist:
                # if the relationship doesnt exist, they're already a member
                return Response(status=204)

        omt.is_active = True
        omt.save()

        AuditLogEntry.objects.create(
            organization=organization,
            actor=request.user,
            ip_address=request.META['REMOTE_ADDR'],
            target_object=omt.id,
            target_user=request.user,
            event=AuditLogEntryEvent.MEMBER_JOIN_TEAM,
            data=omt.get_audit_log_data(),
        )

        return Response(serialize(team), status=201)
Exemplo n.º 8
0
    def delete(self, request, organization, member_id, team_slug):
        """
        Leave a team

        Leave a team.
        """
        try:
            om = OrganizationMember.objects.filter(
                organization=organization,
                id=member_id,
            ).select_related('user').get()
        except OrganizationMember.DoesNotExist:
            raise ResourceDoesNotExist

        if not self._can_access(request, om):
            return Response({'detail': ERR_INSUFFICIENT_ROLE}, status=400)

        try:
            team = Team.objects.get(
                organization=organization,
                slug=team_slug,
            )
        except Team.DoesNotExist:
            raise ResourceDoesNotExist

        if not om.has_global_access:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                )
            except OrganizationMemberTeam.DoesNotExist:
                # if the relationship doesnt exist, they're already a member
                return Response(status=204)
        else:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                    is_active=True,
                )
            except OrganizationMemberTeam.DoesNotExist:
                omt = OrganizationMemberTeam(
                    team=team,
                    organizationmember=om,
                    is_active=True,
                )

        if omt.is_active:
            omt.is_active = False
            omt.save()

            AuditLogEntry.objects.create(
                organization=organization,
                actor=request.user,
                ip_address=request.META['REMOTE_ADDR'],
                target_object=omt.id,
                target_user=request.user,
                event=AuditLogEntryEvent.MEMBER_LEAVE_TEAM,
                data=omt.get_audit_log_data(),
            )

        return Response(status=204)
Exemplo n.º 9
0
    def delete(self, request, organization, member_id, team_slug):
        """
        Leave a team

        Leave a team.
        """
        try:
            om = self._get_member(request, organization, member_id)
        except OrganizationMember.DoesNotExist:
            raise ResourceDoesNotExist

        if not self._can_access(request, om):
            return Response({'detail': ERR_INSUFFICIENT_ROLE}, status=400)

        try:
            team = Team.objects.get(
                organization=organization,
                slug=team_slug,
            )
        except Team.DoesNotExist:
            raise ResourceDoesNotExist

        if not om.has_global_access:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                )
            except OrganizationMemberTeam.DoesNotExist:
                # if the relationship doesnt exist, they're already a member
                return Response(serialize(team, request.user,
                                          TeamWithProjectsSerializer()),
                                status=200)
        else:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                    is_active=True,
                )
            except OrganizationMemberTeam.DoesNotExist:
                omt = OrganizationMemberTeam(
                    team=team,
                    organizationmember=om,
                    is_active=True,
                )

        if omt.is_active:
            omt.is_active = False
            omt.save()

            self.create_audit_entry(
                request=request,
                organization=organization,
                target_object=omt.id,
                target_user=om.user,
                event=AuditLogEntryEvent.MEMBER_LEAVE_TEAM,
                data=omt.get_audit_log_data(),
            )

        return Response(serialize(team, request.user,
                                  TeamWithProjectsSerializer()),
                        status=200)
    def delete(self, request, organization, member_id, team_slug):
        """
        Leave a team

        Leave a team.
        """
        try:
            om = OrganizationMember.objects.filter(
                organization=organization,
                id=member_id,
            ).select_related('user').get()
        except OrganizationMember.DoesNotExist:
            raise ResourceDoesNotExist

        if not self._can_access(request, om):
            return Response({'detail': ERR_INSUFFICIENT_ROLE}, status=400)

        try:
            team = Team.objects.get(
                organization=organization,
                slug=team_slug,
            )
        except Team.DoesNotExist:
            raise ResourceDoesNotExist

        if not om.has_global_access:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                )
            except OrganizationMemberTeam.DoesNotExist:
                # if the relationship doesnt exist, they're already a member
                return Response(status=204)
        else:
            try:
                omt = OrganizationMemberTeam.objects.get(
                    team=team,
                    organizationmember=om,
                    is_active=True,
                )
            except OrganizationMemberTeam.DoesNotExist:
                omt = OrganizationMemberTeam(
                    team=team,
                    organizationmember=om,
                    is_active=True,
                )

        if omt.is_active:
            omt.is_active = False
            omt.save()

            AuditLogEntry.objects.create(
                organization=organization,
                actor=request.user,
                ip_address=request.META['REMOTE_ADDR'],
                target_object=omt.id,
                target_user=request.user,
                event=AuditLogEntryEvent.MEMBER_LEAVE_TEAM,
                data=omt.get_audit_log_data(),
            )

        return Response(status=204)