Exemplo n.º 1
0
    def post(self, request):

        itype = request.data.get('type', '').lower()
        if not itype or itype != 'guest':
            return api_error(status.HTTP_400_BAD_REQUEST, 'type invalid.')

        accepters = request.data.getlist('accepter', None)
        if not accepters:
            return api_error(status.HTTP_400_BAD_REQUEST, 'accepters invalid.')

        result = {}
        result['failed'] = []
        result['success'] = []

        for accepter in accepters:

            if not accepter.strip():
                continue

            accepter = accepter.lower()

            if not is_valid_email(accepter):
                result['failed'].append({
                    'email': accepter,
                    'error_msg': _('Email %s invalid.') % accepter
                    })
                continue

            if block_accepter(accepter):
                result['failed'].append({
                    'email': accepter,
                    'error_msg': _('The email address is not allowed to be invited as a guest.')
                    })
                continue

            if Invitation.objects.filter(inviter=request.user.username,
                    accepter=accepter).count() > 0:
                result['failed'].append({
                    'email': accepter,
                    'error_msg': _('%s is already invited.') % accepter
                    })
                continue

            try:
                User.objects.get(accepter)
                result['failed'].append({
                    'email': accepter,
                    'error_msg': _('User %s already exists.') % accepter
                    })
                continue
            except User.DoesNotExist:
                i = Invitation.objects.add(inviter=request.user.username,
                        accepter=accepter)
                i.send_to(email=accepter)
                result['success'].append(i.to_dict())

        return Response(result)
Exemplo n.º 2
0
    def post(self, request, format=None):
        # Send invitation.
        itype = request.data.get('type', '').lower()
        if not itype or itype != 'guest':
            return api_error(status.HTTP_400_BAD_REQUEST, 'type invalid.')

        accepter = request.data.get('accepter', '').lower()
        if not accepter:
            return api_error(status.HTTP_400_BAD_REQUEST, 'accepter invalid.')

        if not is_valid_email(accepter):
            return api_error(status.HTTP_400_BAD_REQUEST,
                             _('Email %s invalid.') % accepter)

        if block_accepter(accepter):
            return api_error(
                status.HTTP_400_BAD_REQUEST,
                _('The email address is not allowed to be invited as a guest.')
            )

        if Invitation.objects.filter(inviter=request.user.username,
                                     accepter=accepter).count() > 0:
            return api_error(status.HTTP_400_BAD_REQUEST,
                             _('%s is already invited.') % accepter)

        try:
            User.objects.get(accepter)
            user_exists = True
        except User.DoesNotExist:
            user_exists = False

        if user_exists:
            return api_error(status.HTTP_400_BAD_REQUEST,
                             _('User %s already exists.') % accepter)

        i = Invitation.objects.add(inviter=request.user.username,
                                   accepter=accepter)
        m = i.send_to(email=accepter)
        if m.status == STATUS.sent:
            return Response(i.to_dict(), status=201)
        else:
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR,
                             _('Internal Server Error'))
Exemplo n.º 3
0
    def post(self, request, format=None):
        # Send invitation.
        itype = request.data.get('type', '').lower()
        if not itype or itype != 'guest':
            return api_error(status.HTTP_400_BAD_REQUEST, 'type invalid.')

        accepter = request.data.get('accepter', '').lower()
        if not accepter:
            return api_error(status.HTTP_400_BAD_REQUEST, 'accepter invalid.')

        if not is_valid_email(accepter):
            return api_error(status.HTTP_400_BAD_REQUEST,
                             _('Email %s invalid.') % accepter)

        if block_accepter(accepter):
            return api_error(status.HTTP_400_BAD_REQUEST,
                             _('The email address is not allowed to be invited as a guest.'))

        if Invitation.objects.filter(inviter=request.user.username,
                                     accepter=accepter).count() > 0:
            return api_error(status.HTTP_400_BAD_REQUEST,
                             _('%s is already invited.') % accepter)

        try:
            User.objects.get(accepter)
            user_exists = True
        except User.DoesNotExist:
            user_exists = False

        if user_exists:
            return api_error(status.HTTP_400_BAD_REQUEST,
                             _('User %s already exists.') % accepter)

        i = Invitation.objects.add(inviter=request.user.username,
                                   accepter=accepter)
        m = i.send_to(email=accepter)
        if m.status == STATUS.sent:
            return Response(i.to_dict(), status=201)
        else:
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR,
                             _('Internal Server Error'))
Exemplo n.º 4
0
 def test_email_in_blacklist(self):
     assert block_accepter('*****@*****.**') is True
     assert block_accepter('*****@*****.**') is True
     assert block_accepter('*****@*****.**') is True
     assert block_accepter('*****@*****.**') is True
     assert block_accepter('*****@*****.**') is False
Exemplo n.º 5
0
    def post(self, request, repo_id, format=None):
        """ Batch add repo share invitations.
        """
        # argument check
        path = request.data.get('path', None)
        if not path:
            return api_error(status.HTTP_400_BAD_REQUEST, 'path invalid.')

        itype = request.data.get('type', '').lower()
        if not itype or itype != GUEST_USER:
            return api_error(status.HTTP_400_BAD_REQUEST, 'type invalid.')

        accepters = request.data.get('accepters', None)
        if not accepters or not isinstance(accepters, list):
            return api_error(status.HTTP_400_BAD_REQUEST, 'accepters invalid.')

        permission = request.data.get('permission', PERMISSION_READ)
        if permission not in (PERMISSION_READ, PERMISSION_READ_WRITE):
            return api_error(status.HTTP_400_BAD_REQUEST,
                             'permission invalid.')

        # recourse check
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            return api_error(status.HTTP_404_NOT_FOUND,
                             'Library %s not found.' % repo_id)

        if seafile_api.get_dir_id_by_path(repo.id, path) is None:
            return api_error(status.HTTP_404_NOT_FOUND,
                             'Folder %s not found.' % path)

        # permission check
        username = request.user.username
        if is_org_context(request):
            repo_owner = seafile_api.get_org_repo_owner(repo_id)
        else:
            repo_owner = seafile_api.get_repo_owner(repo_id)

        if username != repo_owner and not is_repo_admin(username, repo_id):
            error_msg = 'Permission denied.'
            return api_error(status.HTTP_403_FORBIDDEN, error_msg)

        # main
        result = {}
        result['failed'] = []
        result['success'] = []
        inviter_name = email2nickname(request.user.username)

        try:
            invitation_queryset = Invitation.objects.order_by(
                '-invite_time').filter(inviter=request.user.username,
                                       accept_time=None)
            shared_queryset = RepoShareInvitation.objects.list_by_repo_id_and_path(
                repo_id=repo_id, path=path)
        except Exception as e:
            logger.error(e)
            error_msg = 'Internal Server Error'
            return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

        for accepter in accepters:

            if not accepter.strip():
                continue

            accepter = accepter.lower()

            if not is_valid_email(accepter):
                result['failed'].append({
                    'email':
                    accepter,
                    'error_msg':
                    _('Email %s invalid.') % accepter
                })
                continue

            if block_accepter(accepter):
                result['failed'].append({
                    'email':
                    accepter,
                    'error_msg':
                    _('The email address is not allowed to be invited as a guest.'
                      )
                })
                continue

            try:
                user = User.objects.get(accepter)
                # user is active return exist
                if user.is_active is True:
                    result['failed'].append({
                        'email':
                        accepter,
                        'error_msg':
                        _('User %s already exists.') % accepter
                    })
                    continue
            except User.DoesNotExist:
                pass

            if invitation_queryset.filter(accepter=accepter).exists():
                invitation = invitation_queryset.filter(accepter=accepter)[0]
                invitation.expire_time = timezone.now() + timedelta(
                    hours=int(INVITATIONS_TOKEN_AGE))
                invitation.save()
            else:
                invitation = Invitation.objects.add(
                    inviter=request.user.username, accepter=accepter)

            if shared_queryset.filter(invitation=invitation).exists():
                result['failed'].append({
                    'email':
                    accepter,
                    'error_msg':
                    _('This item has been shared to %s.') % accepter
                })
                continue

            try:
                RepoShareInvitation.objects.add(invitation=invitation,
                                                repo_id=repo_id,
                                                path=path,
                                                permission=permission)
            except Exception as e:
                logger.error(e)
                result['failed'].append({
                    'email':
                    accepter,
                    'error_msg':
                    _('Internal Server Error'),
                })

            data = invitation.to_dict()
            data['permission'] = permission
            data['inviter_name'] = inviter_name

            result['success'].append(data)

            send_sucess = invitation.send_to(email=accepter)

            if not send_sucess:
                result['failed'].append({
                    'email':
                    accepter,
                    'error_msg':
                    _('Failed to send email, email service is not properly configured, please contact administrator.'
                      ),
                })

        return Response(result)
Exemplo n.º 6
0
    def post(self, request):

        itype = request.data.get('type', '').lower()
        if not itype or itype != 'guest':
            return api_error(status.HTTP_400_BAD_REQUEST, 'type invalid.')

        accepters = request.data.getlist('accepter', None)
        if not accepters:
            return api_error(status.HTTP_400_BAD_REQUEST, 'accepters invalid.')

        result = {}
        result['failed'] = []
        result['success'] = []

        for accepter in accepters:

            if not accepter.strip():
                continue

            accepter = accepter.lower()

            if not is_valid_email(accepter):
                result['failed'].append({
                    'email':
                    accepter,
                    'error_msg':
                    _('Email %s invalid.') % accepter
                })
                continue

            if block_accepter(accepter):
                result['failed'].append({
                    'email':
                    accepter,
                    'error_msg':
                    _('The email address is not allowed to be invited as a guest.'
                      )
                })
                continue

            if Invitation.objects.filter(inviter=request.user.username,
                                         accepter=accepter).count() > 0:
                result['failed'].append({
                    'email':
                    accepter,
                    'error_msg':
                    _('%s is already invited.') % accepter
                })
                continue

            try:
                user = User.objects.get(accepter)
                # user is active return exist
                if user.is_active is True:
                    result['failed'].append({
                        'email':
                        accepter,
                        'error_msg':
                        _('User %s already exists.') % accepter
                    })
                    continue
            except User.DoesNotExist:
                pass

            i = Invitation.objects.add(inviter=request.user.username,
                                       accepter=accepter)
            result['success'].append(i.to_dict())

            m = i.send_to(email=accepter)
            if m.status != STATUS.sent:
                result['failed'].append({
                    'email':
                    accepter,
                    'error_msg':
                    _('Failed to send email, email service is not properly configured, please contact administrator.'
                      ),
                })

        return Response(result)
Exemplo n.º 7
0
    def post(self, request):

        itype = request.data.get('type', '').lower()
        if not itype or itype != 'guest':
            return api_error(status.HTTP_400_BAD_REQUEST, 'type invalid.')

        accepters = request.data.getlist('accepter', None)
        if not accepters:
            return api_error(status.HTTP_400_BAD_REQUEST, 'accepters invalid.')

        result = {}
        result['failed'] = []
        result['success'] = []

        for accepter in accepters:

            if not accepter.strip():
                continue

            accepter = accepter.lower()

            if not is_valid_email(accepter):
                result['failed'].append({
                    'email': accepter,
                    'error_msg': _('Email %s invalid.') % accepter
                    })
                continue

            if block_accepter(accepter):
                result['failed'].append({
                    'email': accepter,
                    'error_msg': _('The email address is not allowed to be invited as a guest.')
                    })
                continue

            if Invitation.objects.filter(inviter=request.user.username,
                    accepter=accepter).count() > 0:
                result['failed'].append({
                    'email': accepter,
                    'error_msg': _('%s is already invited.') % accepter
                    })
                continue

            try:
                User.objects.get(accepter)
                result['failed'].append({
                    'email': accepter,
                    'error_msg': _('User %s already exists.') % accepter
                    })
                continue
            except User.DoesNotExist:
                i = Invitation.objects.add(inviter=request.user.username,
                        accepter=accepter)
                m = i.send_to(email=accepter)
                if m.status == STATUS.sent:
                    result['success'].append(i.to_dict())
                else:
                    result['failed'].append({
                        'email': accepter,
                        'error_msg': _('Internal Server Error'),
                    })

        return Response(result)