diff --git a/seahub/api2/endpoints/admin/license.py b/seahub/api2/endpoints/admin/license.py new file mode 100644 index 0000000000..a1adbf04d4 --- /dev/null +++ b/seahub/api2/endpoints/admin/license.py @@ -0,0 +1,41 @@ +# Copyright (c) 2012-2016 Seafile Ltd. +import os +import logging + +from rest_framework.authentication import SessionAuthentication +from rest_framework.permissions import IsAdminUser +from rest_framework.response import Response +from rest_framework.views import APIView +from rest_framework import status + +from seahub.api2.authentication import TokenAuthentication +from seahub.api2.throttling import UserRateThrottle +from seahub.api2.utils import api_error +from seahub.settings import LICENSE_PATH + +logger = logging.getLogger(__name__) + + +class AdminLicense(APIView): + authentication_classes = (TokenAuthentication, SessionAuthentication) + throttle_classes = (UserRateThrottle, ) + permission_classes = (IsAdminUser,) + + def post(self, request): + license_file = request.FILES.get('license', None) + if not license_file: + error_msg = 'license invalid.' + return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + + license_dir = os.path.dirname(LICENSE_PATH) + try: + if not os.path.exists(license_dir): + error_msg = 'path %s invalid.'% LICENSE_PATH + return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + + with open(LICENSE_PATH, 'w') as fd: + fd.write(license_file.read()) + except Exception as e: + logger.error(e) + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + return Response({'success': True}, status=status.HTTP_200_OK) diff --git a/seahub/settings.py b/seahub/settings.py index 4b2278b3f5..8664ce50d0 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -412,6 +412,9 @@ SITE_TITLE = 'Private Seafile' # Base name used in email sending SITE_NAME = 'Seafile' +# Path to the license file(relative to the media path) +LICENSE_PATH = os.path.join(PROJECT_ROOT, '../../seafile_license.txt') + # Path to the favicon file (relative to the media path) # tip: use a different name when modify it. FAVICON_PATH = 'img/favicon.ico' diff --git a/seahub/templates/snippets/web_settings_form.html b/seahub/templates/snippets/web_settings_form.html index 22dda8d6c5..91c8cb6b0d 100644 --- a/seahub/templates/snippets/web_settings_form.html +++ b/seahub/templates/snippets/web_settings_form.html @@ -40,3 +40,16 @@ {% endif %} + +{% if type == 'license' %} +
{% csrf_token %} +
{{ setting_display_name }}
+
+

{{ help_tip }}

+
+ + +
+
+
+{% endif %} diff --git a/seahub/templates/sysadmin/settings.html b/seahub/templates/sysadmin/settings.html index aa4e246f68..59cb1d443e 100644 --- a/seahub/templates/sysadmin/settings.html +++ b/seahub/templates/sysadmin/settings.html @@ -33,6 +33,10 @@ {% include "snippets/web_settings_form.html" %} {% endwith %} + {% with type="license" setting_display_name="License" setting_name="license" help_tip="seafile_license.txt" %} + {% include "snippets/web_settings_form.html" %} + {% endwith %} +

{% trans "User" %}

{% with type="checkbox" setting_name="ENABLE_SIGNUP" setting_val=config_dict.ENABLE_SIGNUP %} @@ -280,6 +284,7 @@ $('.web-setting-file-upload-input').change(function() { switch(input_name) { case 'logo': url = '{% url 'api-v2.1-admin-logo' %}'; break; case 'favicon': url = '{% url 'api-v2.1-admin-favicon' %}'; break; + case 'license': url = '{% url 'api-v2.1-admin-license' %}'; break; } fd.append(input_name, file); diff --git a/seahub/urls.py b/seahub/urls.py index da5ac9fa5b..9da4fbd412 100644 --- a/seahub/urls.py +++ b/seahub/urls.py @@ -71,6 +71,7 @@ from seahub.api2.endpoints.admin.logs import AdminLogs from seahub.api2.endpoints.admin.org_users import AdminOrgUsers, AdminOrgUser from seahub.api2.endpoints.admin.logo import AdminLogo from seahub.api2.endpoints.admin.favicon import AdminFavicon +from seahub.api2.endpoints.admin.license import AdminLicense # Uncomment the next two lines to enable the admin: #from django.contrib import admin @@ -295,6 +296,7 @@ urlpatterns = patterns( ## admin::logo url(r'^api/v2.1/admin/logo/$', AdminLogo.as_view(), name='api-v2.1-admin-logo'), url(r'^api/v2.1/admin/favicon/$', AdminFavicon.as_view(), name='api-v2.1-admin-favicon'), + url(r'^api/v2.1/admin/license/$', AdminLicense.as_view(), name='api-v2.1-admin-license'), (r'^avatar/', include('seahub.avatar.urls')), (r'^notification/', include('seahub.notifications.urls')), diff --git a/tests/api/endpoints/admin/test_license.py b/tests/api/endpoints/admin/test_license.py new file mode 100644 index 0000000000..9fbcf5ef58 --- /dev/null +++ b/tests/api/endpoints/admin/test_license.py @@ -0,0 +1,28 @@ +import os +import json +from tests.common.utils import urljoin +from tests.common.common import BASE_URL +from django.core.urlresolvers import reverse + +from seahub.settings import LICENSE_PATH +from seahub.test_utils import BaseTestCase + +class AdminLicenseTest(BaseTestCase): + def setUp(self): + self.login_as(self.admin) + + def test_update_license(self): + + license_dir = os.path.dirname(LICENSE_PATH) + if os.path.exists(LICENSE_PATH): + os.remove(LICENSE_PATH) + assert not os.path.exists(LICENSE_PATH) + + url = reverse('api-v2.1-admin-license') + url = urljoin(BASE_URL, url) + license_file = os.path.join(os.getcwd(), 'media/hello.hi') + with open(license_file) as f: + json_resp = self.client.post(url, {'license': f}) + json_resp = json.loads(json_resp.content) + assert json_resp['success'] == True + assert os.path.exists(LICENSE_PATH)