diff --git a/seahub/api2/endpoints/send_share_link_email.py b/seahub/api2/endpoints/send_share_link_email.py index 8cfbefcfa4..507812dee6 100644 --- a/seahub/api2/endpoints/send_share_link_email.py +++ b/seahub/api2/endpoints/send_share_link_email.py @@ -16,7 +16,7 @@ from seahub.utils import IS_EMAIL_CONFIGURED, \ is_valid_email, string2list, gen_shared_link, send_html_email, \ get_site_name from seahub.share.models import FileShare -from seahub.settings import REPLACE_FROM_EMAIL, ADD_REPLY_TO_HEADER +from seahub.settings import ADD_REPLY_TO_HEADER from seahub.profile.models import Profile logger = logging.getLogger(__name__) @@ -84,11 +84,6 @@ class SendShareLinkView(APIView): 'password': link.get_password(), } - if REPLACE_FROM_EMAIL: - from_email = useremail - else: - from_email = None # use default from email - if ADD_REPLY_TO_HEADER: reply_to = useremail else: @@ -107,7 +102,7 @@ class SendShareLinkView(APIView): # send email try: - send_html_email(title, template, c, from_email, [to_email], reply_to=reply_to) + send_html_email(title, template, c, None, [to_email], reply_to=reply_to) result['success'].append(to_email) except Exception as e: logger.error(e) diff --git a/seahub/api2/endpoints/send_upload_link_email.py b/seahub/api2/endpoints/send_upload_link_email.py index d0c63a68c4..c873d80541 100644 --- a/seahub/api2/endpoints/send_upload_link_email.py +++ b/seahub/api2/endpoints/send_upload_link_email.py @@ -15,7 +15,7 @@ from seahub.utils import IS_EMAIL_CONFIGURED, \ is_valid_email, string2list, gen_shared_upload_link, send_html_email, \ get_site_name from seahub.share.models import UploadLinkShare -from seahub.settings import REPLACE_FROM_EMAIL, ADD_REPLY_TO_HEADER +from seahub.settings import ADD_REPLY_TO_HEADER from seahub.profile.models import Profile logger = logging.getLogger(__name__) @@ -83,11 +83,6 @@ class SendUploadLinkView(APIView): 'password': link.get_password(), } - if REPLACE_FROM_EMAIL: - from_email = useremail - else: - from_email = None # use default from email - if ADD_REPLY_TO_HEADER: reply_to = useremail else: @@ -99,7 +94,7 @@ class SendUploadLinkView(APIView): # send email try: - send_html_email(title, template, c, from_email, [to_email], reply_to=reply_to) + send_html_email(title, template, c, None, [to_email], reply_to=reply_to) result['success'].append(to_email) except Exception as e: logger.error(e) diff --git a/seahub/settings.py b/seahub/settings.py index b62c756f79..4b4db2a782 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -823,9 +823,6 @@ SEADOC_SERVER_URL = 'http://127.0.0.1:7070' # Settings for Seahub Priv # ############################ -# Replace from email to current user instead of email sender. -REPLACE_FROM_EMAIL = False - # Add ``Reply-to`` header, see RFC #822. ADD_REPLY_TO_HEADER = False diff --git a/seahub/share/forms.py b/seahub/share/forms.py deleted file mode 100644 index 2d39f811e4..0000000000 --- a/seahub/share/forms.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (c) 2012-2016 Seafile Ltd. -from django import forms -from django.utils.translation import gettext_lazy as _ - -class RepoShareForm(forms.Form): - """ - Form for sharing repo to user or group. - """ - - email_or_group = forms.CharField(max_length=512) - repo_id = forms.CharField(max_length=36) - permission = forms.ChoiceField(choices=(('rw', 'read-write'), ('r', 'read-only'))) - -class FileLinkShareForm(forms.Form): - """ - Form for sharing file shared link to emails. - """ - - email = forms.CharField(max_length=512, error_messages={ - 'required': _("Email is required"), - 'max_length': _("Email is not longer than 512 characters"), - }) - file_shared_link = forms.CharField() - extra_msg = forms.CharField(required=False) - file_shared_name = forms.CharField() - file_shared_type = forms.CharField() - -class UploadLinkShareForm(forms.Form): - """ - Form for sharing upload link to emails. - """ - email = forms.CharField(max_length=512, error_messages={ - 'required': _("Email is required"), - 'max_length': _("Email is not longer than 512 characters"), - }) - shared_upload_link = forms.CharField() - extra_msg = forms.CharField(required=False) diff --git a/seahub/share/urls.py b/seahub/share/urls.py index f06a2ce5ea..0c9431048a 100644 --- a/seahub/share/urls.py +++ b/seahub/share/urls.py @@ -4,10 +4,8 @@ from django.urls import path from .views import * urlpatterns = [ - path('link/send/', send_shared_link, name='send_shared_link'), path('link/save/', save_shared_link, name='save_shared_link'), path('link/export-excel/', export_shared_link, name='export_shared_link'), - path('upload_link/send/', send_shared_upload_link, name='send_shared_upload_link'), path('ajax/private-share-dir/', ajax_private_share_dir, name='ajax_private_share_dir'), path('ajax/get-link-audit-code/', ajax_get_link_audit_code, name='ajax_get_link_audit_code'), ] diff --git a/seahub/share/views.py b/seahub/share/views.py index 921d2ea0fa..17a825bfc6 100644 --- a/seahub/share/views.py +++ b/seahub/share/views.py @@ -5,34 +5,27 @@ import json import logging from django.core.cache import cache -from django.http import HttpResponse, HttpResponseRedirect, Http404, \ - HttpResponseBadRequest -from django.utils.translation import gettext as _, activate +from django.http import HttpResponse, HttpResponseRedirect, Http404 +from django.utils.translation import gettext as _ from django.contrib import messages -from django.utils.html import escape import seaserv from seaserv import seafile_api from pysearpc import SearpcError -from seahub.share.forms import FileLinkShareForm, \ - UploadLinkShareForm from seahub.share.models import FileShare, UploadLinkShare from seahub.share.signals import share_repo_to_user_successful from seahub.auth.decorators import login_required, login_required_ajax from seahub.base.decorators import require_POST from seahub.contacts.signals import mail_sended from seahub.views import is_registered_user, check_folder_permission -from seahub.utils import string2list, IS_EMAIL_CONFIGURED, check_filename_with_rename, \ - is_valid_username, is_valid_email, send_html_email, is_org_context, \ - gen_token, normalize_cache_key, get_site_name, gen_shared_link +from seahub.utils import string2list, check_filename_with_rename, \ + is_valid_username, is_valid_email, is_org_context, \ + gen_token, normalize_cache_key, gen_shared_link from seahub.utils.mail import send_html_email_with_dj_template from seahub.utils.ms_excel import write_xls from seahub.utils.timeutils import datetime_to_isoformat_timestr -from seahub.settings import SITE_ROOT, REPLACE_FROM_EMAIL, \ - ADD_REPLY_TO_HEADER, SHARE_LINK_EMAIL_LANGUAGE, \ - SHARE_LINK_AUDIT_CODE_TIMEOUT -from seahub.profile.models import Profile +from seahub.settings import SITE_ROOT, SHARE_LINK_AUDIT_CODE_TIMEOUT # Get an instance of a logger logger = logging.getLogger(__name__) @@ -123,95 +116,6 @@ def share_to_user(request, repo, to_user, permission): return True -# share link -@login_required_ajax -def send_shared_link(request): - """ - Handle ajax post request to send file shared link. - """ - if not request.method == 'POST': - raise Http404 - - content_type = 'application/json; charset=utf-8' - - if not IS_EMAIL_CONFIGURED: - data = json.dumps({'error': _('Failed to send email, email service is not properly configured, please contact administrator.')}) - return HttpResponse(data, status=500, content_type=content_type) - - form = FileLinkShareForm(request.POST) - if form.is_valid(): - email = form.cleaned_data['email'] - file_shared_link = form.cleaned_data['file_shared_link'] - file_shared_name = form.cleaned_data['file_shared_name'] - file_shared_type = form.cleaned_data['file_shared_type'] - extra_msg = escape(form.cleaned_data['extra_msg']) - - to_email_list = string2list(email) - send_success, send_failed = [], [] - # use contact_email, if present - username = Profile.objects.get_contact_email_by_user(request.user.username) - for to_email in to_email_list: - if not is_valid_email(to_email): - send_failed.append(to_email) - continue - - if SHARE_LINK_EMAIL_LANGUAGE: - activate(SHARE_LINK_EMAIL_LANGUAGE) - - # Add email to contacts. - mail_sended.send(sender=None, user=request.user.username, - email=to_email) - - c = { - 'email': request.user.username, - 'to_email': to_email, - 'file_shared_link': file_shared_link, - 'file_shared_name': file_shared_name, - } - - if extra_msg: - c['extra_msg'] = extra_msg - - if REPLACE_FROM_EMAIL: - from_email = username - else: - from_email = None # use default from email - - if ADD_REPLY_TO_HEADER: - reply_to = username - else: - reply_to = None - - try: - if file_shared_type == 'f': - c['file_shared_type'] = _("file") - send_html_email(_('A file is shared to you on %s') % get_site_name(), - 'shared_link_email.html', - c, from_email, [to_email], - reply_to=reply_to - ) - else: - c['file_shared_type'] = _("directory") - send_html_email(_('A directory is shared to you on %s') % get_site_name(), - 'shared_link_email.html', - c, from_email, [to_email], - reply_to=reply_to) - - send_success.append(to_email) - except Exception: - send_failed.append(to_email) - - if len(send_success) > 0: - data = json.dumps({"send_success": send_success, "send_failed": send_failed}) - return HttpResponse(data, status=200, content_type=content_type) - else: - data = json.dumps({"error": _("Internal server error, or please check the email(s) you entered")}) - return HttpResponse(data, status=400, content_type=content_type) - else: - return HttpResponseBadRequest(json.dumps(form.errors), - content_type=content_type) - - @login_required def save_shared_link(request): """Save public share link to one's library. @@ -335,78 +239,6 @@ def export_shared_link(request): return response -@login_required_ajax -def send_shared_upload_link(request): - """ - Handle ajax post request to send shared upload link. - """ - if not request.method == 'POST': - raise Http404 - - content_type = 'application/json; charset=utf-8' - - if not IS_EMAIL_CONFIGURED: - data = json.dumps({'error': _('Failed to send email, email service is not properly configured, please contact administrator.')}) - return HttpResponse(data, status=500, content_type=content_type) - - form = UploadLinkShareForm(request.POST) - if form.is_valid(): - email = form.cleaned_data['email'] - shared_upload_link = form.cleaned_data['shared_upload_link'] - extra_msg = escape(form.cleaned_data['extra_msg']) - - to_email_list = string2list(email) - send_success, send_failed = [], [] - # use contact_email, if present - username = Profile.objects.get_contact_email_by_user(request.user.username) - for to_email in to_email_list: - if not is_valid_email(to_email): - send_failed.append(to_email) - continue - # Add email to contacts. - mail_sended.send(sender=None, user=request.user.username, - email=to_email) - - c = { - 'email': request.user.username, - 'to_email': to_email, - 'shared_upload_link': shared_upload_link, - } - - if extra_msg: - c['extra_msg'] = extra_msg - - if REPLACE_FROM_EMAIL: - from_email = username - else: - from_email = None # use default from email - - if ADD_REPLY_TO_HEADER: - reply_to = username - else: - reply_to = None - - try: - send_html_email(_('An upload link is shared to you on %s') % get_site_name(), - 'shared_upload_link_email.html', - c, from_email, [to_email], - reply_to=reply_to) - - send_success.append(to_email) - except Exception: - send_failed.append(to_email) - - if len(send_success) > 0: - data = json.dumps({"send_success": send_success, "send_failed": send_failed}) - return HttpResponse(data, status=200, content_type=content_type) - else: - data = json.dumps({"error": _("Internal server error, or please check the email(s) you entered")}) - return HttpResponse(data, status=400, content_type=content_type) - else: - return HttpResponseBadRequest(json.dumps(form.errors), - content_type=content_type) - - @login_required_ajax @require_POST def ajax_private_share_dir(request): diff --git a/tests/api/endpoints/test_send_share_link.py b/tests/api/endpoints/test_send_share_link.py index d9dbf17924..904878fce7 100644 --- a/tests/api/endpoints/test_send_share_link.py +++ b/tests/api/endpoints/test_send_share_link.py @@ -1,11 +1,10 @@ -#coding: UTF-8 +# coding: UTF-8 import json from mock import patch from django.core import mail from django.urls import reverse from django.test import override_settings -from seahub.utils import IS_EMAIL_CONFIGURED from seahub.share.models import FileShare from seahub.profile.models import Profile from seahub.profile.utils import refresh_cache @@ -16,7 +15,7 @@ class SendShareLinkApiTest(BaseTestCase): def setUp(self): fs = FileShare.objects.create_file_link(self.user.username, - self.repo.id, self.file, None) + self.repo.id, self.file, None) self.token = fs.token mail.outbox = [] @@ -47,7 +46,6 @@ class SendShareLinkApiTest(BaseTestCase): @patch('seahub.utils.IS_EMAIL_CONFIGURED', True) @patch('seahub.api2.endpoints.send_share_link_email.IS_EMAIL_CONFIGURED', True) - @patch('seahub.api2.endpoints.send_share_link_email.REPLACE_FROM_EMAIL', True) @patch('seahub.api2.endpoints.send_share_link_email.ADD_REPLY_TO_HEADER', True) def test_can_send_email_rewrite(self): self.login_as(self.user) @@ -63,17 +61,15 @@ class SendShareLinkApiTest(BaseTestCase): json_resp = json.loads(resp.content) assert json_resp['success'][0] == self.admin.email assert 'test.txt' in mail.outbox[0].body - assert mail.outbox[0].from_email == self.user.email assert mail.outbox[0].extra_headers['Reply-to'] == self.user.email @patch('seahub.utils.IS_EMAIL_CONFIGURED', True) @patch('seahub.api2.endpoints.send_share_link_email.IS_EMAIL_CONFIGURED', True) - @patch('seahub.api2.endpoints.send_share_link_email.REPLACE_FROM_EMAIL', True) @patch('seahub.api2.endpoints.send_share_link_email.ADD_REPLY_TO_HEADER', True) def test_can_send_email_rewrite_contact_email(self): self.login_as(self.user) nickname = 'Testuser' - contact_email= 'contact_email@test.com' + contact_email = 'contact_email@test.com' p = Profile.objects.add_or_update(self.user.email, nickname=nickname) p.contact_email = contact_email p.save() @@ -92,7 +88,6 @@ class SendShareLinkApiTest(BaseTestCase): json_resp = json.loads(resp.content) assert json_resp['success'][0] == self.admin.email assert 'test.txt' in mail.outbox[0].body - assert mail.outbox[0].from_email == contact_email assert mail.outbox[0].extra_headers['Reply-to'] == contact_email @patch('seahub.utils.IS_EMAIL_CONFIGURED', False) diff --git a/tests/api/endpoints/test_send_upload_link.py b/tests/api/endpoints/test_send_upload_link.py index e12f98f9ed..45613ecc2a 100644 --- a/tests/api/endpoints/test_send_upload_link.py +++ b/tests/api/endpoints/test_send_upload_link.py @@ -1,11 +1,10 @@ -#coding: UTF-8 +# coding: UTF-8 import json from mock import patch from django.core import mail from django.urls import reverse from django.test import override_settings -from seahub.utils import IS_EMAIL_CONFIGURED from seahub.share.models import UploadLinkShare from seahub.profile.models import Profile from seahub.profile.utils import refresh_cache @@ -16,7 +15,7 @@ class SendUploadLinkApiTest(BaseTestCase): def setUp(self): uls = UploadLinkShare.objects.create_upload_link_share(self.user.username, - self.repo.id, '/') + self.repo.id, '/') self.token = uls.token def tearDown(self): @@ -44,7 +43,6 @@ class SendUploadLinkApiTest(BaseTestCase): @patch('seahub.utils.IS_EMAIL_CONFIGURED', True) @patch('seahub.api2.endpoints.send_upload_link_email.IS_EMAIL_CONFIGURED', True) - @patch('seahub.api2.endpoints.send_upload_link_email.REPLACE_FROM_EMAIL', True) @patch('seahub.api2.endpoints.send_upload_link_email.ADD_REPLY_TO_HEADER', True) def test_can_send_email_rewrite(self): self.login_as(self.user) @@ -59,17 +57,15 @@ class SendUploadLinkApiTest(BaseTestCase): self.assertEqual(len(mail.outbox), 1) json_resp = json.loads(resp.content) assert json_resp['success'][0] == self.admin.email - assert mail.outbox[0].from_email == self.user.email assert mail.outbox[0].extra_headers['Reply-to'] == self.user.email @patch('seahub.utils.IS_EMAIL_CONFIGURED', True) @patch('seahub.api2.endpoints.send_upload_link_email.IS_EMAIL_CONFIGURED', True) - @patch('seahub.api2.endpoints.send_upload_link_email.REPLACE_FROM_EMAIL', True) @patch('seahub.api2.endpoints.send_upload_link_email.ADD_REPLY_TO_HEADER', True) def test_can_send_email_rewrite_contact_email(self): self.login_as(self.user) nickname = 'Testuser' - contact_email= 'contact_email@test.com' + contact_email = 'contact_email@test.com' p = Profile.objects.add_or_update(self.user.email, nickname=nickname) p.contact_email = contact_email p.save() @@ -87,7 +83,6 @@ class SendUploadLinkApiTest(BaseTestCase): self.assertEqual(len(mail.outbox), 1) json_resp = json.loads(resp.content) assert json_resp['success'][0] == self.admin.email - assert mail.outbox[0].from_email == contact_email assert mail.outbox[0].extra_headers['Reply-to'] == contact_email @patch('seahub.utils.IS_EMAIL_CONFIGURED', False) diff --git a/tests/seahub/share/views/test_send_shared_link.py b/tests/seahub/share/views/test_send_shared_link.py deleted file mode 100644 index b4e01cbdf3..0000000000 --- a/tests/seahub/share/views/test_send_shared_link.py +++ /dev/null @@ -1,80 +0,0 @@ -from mock import patch -from django.core import mail -from django.urls import reverse -from django.test import override_settings - -from seahub.profile.models import Profile -from seahub.profile.utils import refresh_cache -from seahub.test_utils import BaseTestCase - - -class SendSharedLinkTest(BaseTestCase): - def setUp(self): - mail.outbox = [] - - @override_settings(DEFAULT_FROM_EMAIL='from_noreply@seafile.com') - @patch('seahub.share.views.IS_EMAIL_CONFIGURED', True) - def test_can_send(self): - self.login_as(self.user) - - resp = self.client.post(reverse('send_shared_link'), { - 'email': self.user.email, - 'file_shared_link': 'http://xxx', - 'file_shared_name': 'xxx', - 'file_shared_type': 'd', - 'extra_msg': '' - }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - - self.assertEqual(200, resp.status_code) - self.assertEqual(len(mail.outbox), 1) - assert 'http://xxx' in mail.outbox[0].body - assert mail.outbox[0].from_email == 'from_noreply@seafile.com' - - @patch('seahub.share.views.REPLACE_FROM_EMAIL', True) - @patch('seahub.share.views.ADD_REPLY_TO_HEADER', True) - @patch('seahub.share.views.IS_EMAIL_CONFIGURED', True) - @patch('seahub.utils.IS_EMAIL_CONFIGURED', True) - def test_can_send_from_replyto_rewrite(self): - self.login_as(self.user) - - resp = self.client.post(reverse('send_shared_link'), { - 'email': self.user.email, - 'file_shared_link': 'http://xxx', - 'file_shared_name': 'xxx', - 'file_shared_type': 'd', - 'extra_msg': '' - }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - - self.assertEqual(200, resp.status_code) - self.assertEqual(len(mail.outbox), 1) - assert 'http://xxx' in mail.outbox[0].body - assert mail.outbox[0].from_email == self.user.email - assert mail.outbox[0].extra_headers['Reply-to'] == self.user.email - - @patch('seahub.share.views.REPLACE_FROM_EMAIL', True) - @patch('seahub.share.views.ADD_REPLY_TO_HEADER', True) - @patch('seahub.share.views.IS_EMAIL_CONFIGURED', True) - @patch('seahub.utils.IS_EMAIL_CONFIGURED', True) - def test_can_send_from_replyto_rewrite_contact_email(self): - self.login_as(self.user) - nickname = 'Testuser' - contact_email= 'contact_email@test.com' - p = Profile.objects.add_or_update(self.user.email, nickname=nickname) - p.contact_email = contact_email - p.save() - - refresh_cache(self.user.email) - - resp = self.client.post(reverse('send_shared_link'), { - 'email': self.user.email, - 'file_shared_link': 'http://xxx', - 'file_shared_name': 'xxx', - 'file_shared_type': 'd', - 'extra_msg': '' - }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - - self.assertEqual(200, resp.status_code) - self.assertEqual(len(mail.outbox), 1) - assert 'http://xxx' in mail.outbox[0].body - assert mail.outbox[0].from_email == contact_email - assert mail.outbox[0].extra_headers['Reply-to'] == contact_email diff --git a/tests/seahub/share/views/test_send_shared_upload_link.py b/tests/seahub/share/views/test_send_shared_upload_link.py deleted file mode 100644 index fb47a330df..0000000000 --- a/tests/seahub/share/views/test_send_shared_upload_link.py +++ /dev/null @@ -1,74 +0,0 @@ -from mock import patch -from django.core import mail -from django.urls import reverse -from django.test import override_settings - -from seahub.profile.models import Profile -from seahub.profile.utils import refresh_cache -from seahub.test_utils import BaseTestCase - - -class SendSharedUploadLinkTest(BaseTestCase): - def setUp(self): - mail.outbox = [] - - @override_settings(DEFAULT_FROM_EMAIL='from_noreply@seafile.com') - @patch('seahub.share.views.IS_EMAIL_CONFIGURED', True) - def test_can_send(self): - self.login_as(self.user) - - resp = self.client.post(reverse('send_shared_upload_link'), { - 'email': self.user.email, - 'shared_upload_link': 'http://xxx', - 'extra_msg': '' - }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - - self.assertEqual(200, resp.status_code) - self.assertEqual(len(mail.outbox), 1) - assert 'http://xxx' in mail.outbox[0].body - assert mail.outbox[0].from_email == 'from_noreply@seafile.com' - - @patch('seahub.share.views.REPLACE_FROM_EMAIL', True) - @patch('seahub.share.views.ADD_REPLY_TO_HEADER', True) - @patch('seahub.share.views.IS_EMAIL_CONFIGURED', True) - @patch('seahub.utils.IS_EMAIL_CONFIGURED', True) - def test_can_send_from_replyto_rewrite(self): - self.login_as(self.user) - - resp = self.client.post(reverse('send_shared_upload_link'), { - 'email': self.user.email, - 'shared_upload_link': 'http://xxx', - 'extra_msg': '' - }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - - self.assertEqual(200, resp.status_code) - self.assertEqual(len(mail.outbox), 1) - assert 'http://xxx' in mail.outbox[0].body - assert mail.outbox[0].from_email == self.user.email - assert mail.outbox[0].extra_headers['Reply-to'] == self.user.email - - @patch('seahub.share.views.REPLACE_FROM_EMAIL', True) - @patch('seahub.share.views.ADD_REPLY_TO_HEADER', True) - @patch('seahub.share.views.IS_EMAIL_CONFIGURED', True) - @patch('seahub.utils.IS_EMAIL_CONFIGURED', True) - def test_can_send_from_replyto_rewrite_contact_email(self): - self.login_as(self.user) - nickname = 'Testuser' - contact_email= 'contact_email@test.com' - p = Profile.objects.add_or_update(self.user.email, nickname=nickname) - p.contact_email = contact_email - p.save() - - refresh_cache(self.user.email) - - resp = self.client.post(reverse('send_shared_upload_link'), { - 'email': self.user.email, - 'shared_upload_link': 'http://xxx', - 'extra_msg': '' - }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') - - self.assertEqual(200, resp.status_code) - self.assertEqual(len(mail.outbox), 1) - assert 'http://xxx' in mail.outbox[0].body - assert mail.outbox[0].from_email == contact_email - assert mail.outbox[0].extra_headers['Reply-to'] == contact_email