From d23a17424c2871a4819288d4c6d1fcad6594a09b Mon Sep 17 00:00:00 2001 From: zming <517046497@qq.com> Date: Fri, 25 Aug 2017 11:50:50 +0800 Subject: [PATCH] [API] background image of login page --- seahub/api2/endpoints/admin/login_bg_image.py | 59 +++++++++++++++++++ seahub/auth/views.py | 10 ++++ seahub/settings.py | 3 + seahub/templates/registration/login.html | 2 +- seahub/templates/sysadmin/settings.html | 5 ++ seahub/urls.py | 2 + seahub/views/sysadmin.py | 11 +++- 7 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 seahub/api2/endpoints/admin/login_bg_image.py diff --git a/seahub/api2/endpoints/admin/login_bg_image.py b/seahub/api2/endpoints/admin/login_bg_image.py new file mode 100644 index 0000000000..eba152893f --- /dev/null +++ b/seahub/api2/endpoints/admin/login_bg_image.py @@ -0,0 +1,59 @@ +# 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 SEAHUB_DATA_ROOT, MEDIA_ROOT + +logger = logging.getLogger(__name__) + +CUSTOM_LOGIN_BG_IMAGE_PATH = 'custom/login-bg.jpg' + + +class AdminLoginBgImage(APIView): + + authentication_classes = (TokenAuthentication, SessionAuthentication) + throttle_classes = (UserRateThrottle, ) + permission_classes = (IsAdminUser,) + + def post(self, request): + image_file = request.FILES.get('login_bg_image', None) + if not image_file: + error_msg = 'background image invalid' + return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + + if not os.path.exists(SEAHUB_DATA_ROOT): + os.makedirs(SEAHUB_DATA_ROOT) + + custom_dir = os.path.join(SEAHUB_DATA_ROOT, + os.path.dirname(CUSTOM_LOGIN_BG_IMAGE_PATH)) + if not os.path.exists(custom_dir): + os.makedirs(custom_dir) + + try: + custom_login_bg_image_file = os.path.join(SEAHUB_DATA_ROOT, + CUSTOM_LOGIN_BG_IMAGE_PATH) + # save login background image file to custom dir + with open(custom_login_bg_image_file, 'w') as fd: + fd.write(image_file.read()) + + custom_symlink = os.path.join(MEDIA_ROOT, + os.path.dirname(CUSTOM_LOGIN_BG_IMAGE_PATH)) + # create symlink for custom dir + if not os.path.exists(custom_symlink): + os.symlink(custom_dir, custom_symlink) + + except Exception as e: + logger.error(e) + error_msg = 'Internal Server Error' + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + + return Response({'success': True}) diff --git a/seahub/auth/views.py b/seahub/auth/views.py index 0fdd3aa7e1..03f8b938af 100644 --- a/seahub/auth/views.py +++ b/seahub/auth/views.py @@ -1,5 +1,6 @@ # Copyright (c) 2012-2016 Seafile Ltd. import hashlib +import os import re import logging from datetime import datetime @@ -32,6 +33,8 @@ from seahub.utils.ip import get_remote_ip from seahub.utils.file_size import get_quota_from_string from seahub.utils.two_factor_auth import two_factor_auth_enabled, handle_two_factor_auth from seahub.utils.user_permissions import get_user_role +from seahub.settings import LOGIN_BG_IMAGE_PATH, MEDIA_ROOT +from seahub.api2.endpoints.admin.login_bg_image import CUSTOM_LOGIN_BG_IMAGE_PATH from constance import config @@ -242,6 +245,12 @@ def login(request, template_name='registration/login.html', enable_krb5_login = getattr(settings, 'ENABLE_KRB5_LOGIN', False) enable_adfs_login = getattr(settings, 'ENABLE_ADFS_LOGIN', False) + login_bg_image_path = LOGIN_BG_IMAGE_PATH + # get path that background image of login page + custom_login_bg_image_file = os.path.join(MEDIA_ROOT, CUSTOM_LOGIN_BG_IMAGE_PATH) + if os.path.exists(custom_login_bg_image_file): + login_bg_image_path = CUSTOM_LOGIN_BG_IMAGE_PATH + return render_to_response(template_name, { 'form': form, redirect_field_name: redirect_to, @@ -252,6 +261,7 @@ def login(request, template_name='registration/login.html', 'enable_shib_login': enable_shib_login, 'enable_krb5_login': enable_krb5_login, 'enable_adfs_login': enable_adfs_login, + 'login_bg_image_path': login_bg_image_path, }, context_instance=RequestContext(request)) def login_simple_check(request): diff --git a/seahub/settings.py b/seahub/settings.py index 03e0783541..a6e80a9bde 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -422,6 +422,9 @@ 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 background image file of login page(relative to the media path) +LOGIN_BG_IMAGE_PATH = 'img/login-bg.jpg' + # 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/registration/login.html b/seahub/templates/registration/login.html index 2812b15648..52ac14e048 100644 --- a/seahub/templates/registration/login.html +++ b/seahub/templates/registration/login.html @@ -8,7 +8,7 @@