From 2db2c0803232a1d651a04edd4835afc9997ca98c Mon Sep 17 00:00:00 2001 From: llj Date: Wed, 13 Jan 2021 14:11:35 +0800 Subject: [PATCH] =?UTF-8?q?[department]=20added=20an=20option=20to=20contr?= =?UTF-8?q?ol=20if=20libraries=20can=20be=20shared=20in=E2=80=A6=20(#4778)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [department] added an option to control if libraries can be shared into a department * update seahub web api Co-authored-by: lian --- .../src/components/dialog/share-to-group.js | 8 ++++++- frontend/src/utils/constants.js | 1 + seahub/api2/endpoints/dir_shared_items.py | 14 ++++++++++- seahub/api2/endpoints/shareable_groups.py | 24 ++++++++++--------- seahub/settings.py | 3 +++ seahub/templates/base_for_react.html | 2 +- seahub/templates/react_app.html | 1 + seahub/views/__init__.py | 1 + 8 files changed, 40 insertions(+), 14 deletions(-) diff --git a/frontend/src/components/dialog/share-to-group.js b/frontend/src/components/dialog/share-to-group.js index 097ada7d4e..565c874ede 100644 --- a/frontend/src/components/dialog/share-to-group.js +++ b/frontend/src/components/dialog/share-to-group.js @@ -2,7 +2,7 @@ import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { Button } from 'reactstrap'; import Select from 'react-select'; -import { gettext, isPro } from '../../utils/constants'; +import { gettext, isPro, enableShareToDepartment } from '../../utils/constants'; import { seafileAPI } from '../../utils/seafile-api.js'; import { Utils } from '../../utils/utils'; import toaster from '../toast'; @@ -135,6 +135,12 @@ class ShareToGroup extends React.Component { seafileAPI.shareableGroups().then((res) => { let options = []; for (let i = 0 ; i < res.data.length; i++) { + const item = res.data[i]; + if (item.parent_group_id != 0) { // it's a department + if (!enableShareToDepartment) { + continue; + } + } let obj = {}; obj.value = res.data[i].name; obj.id = res.data[i].id; diff --git a/frontend/src/utils/constants.js b/frontend/src/utils/constants.js index ba2e79cc63..bd10bb2a49 100644 --- a/frontend/src/utils/constants.js +++ b/frontend/src/utils/constants.js @@ -53,6 +53,7 @@ export const shareLinkExpireDaysDefault = window.app.pageOptions.shareLinkExpire export const uploadLinkExpireDaysMin = window.app.pageOptions.uploadLinkExpireDaysMin; export const uploadLinkExpireDaysMax = window.app.pageOptions.uploadLinkExpireDaysMax; export const uploadLinkExpireDaysDefault = window.app.pageOptions.uploadLinkExpireDaysDefault; +export const enableShareToDepartment = window.app.pageOptions.enableShareToDepartment; export const maxFileName = window.app.pageOptions.maxFileName; export const canPublishRepo = window.app.pageOptions.canPublishRepo; export const enableEncryptedLibrary = window.app.pageOptions.enableEncryptedLibrary; diff --git a/seahub/api2/endpoints/dir_shared_items.py b/seahub/api2/endpoints/dir_shared_items.py index 5a987a804c..01ccadd4c8 100644 --- a/seahub/api2/endpoints/dir_shared_items.py +++ b/seahub/api2/endpoints/dir_shared_items.py @@ -24,7 +24,7 @@ from seahub.api2.endpoints.utils import is_org_user from seahub.base.templatetags.seahub_tags import email2nickname, \ email2contact_email from seahub.base.accounts import User -from seahub.group.utils import is_group_member +from seahub.group.utils import is_group_member, is_group_admin from seahub.share.models import ExtraSharePermission, ExtraGroupsSharePermission from seahub.share.utils import is_repo_admin, share_dir_to_user, \ share_dir_to_group, update_user_dir_permission, \ @@ -37,6 +37,7 @@ from seahub.constants import PERMISSION_READ, PERMISSION_READ_WRITE, \ PERMISSION_ADMIN from seahub.utils.repo import get_available_repo_perms from seahub.avatar.templatetags.avatar_tags import api_avatar_url +from seahub.settings import ENABLE_SHARE_TO_DEPARTMENT logger = logging.getLogger(__name__) @@ -437,6 +438,17 @@ class DirSharedItemsEndpoint(APIView): }) continue + # is sharing repo to department + if group.parent_group_id != 0: + + if not ENABLE_SHARE_TO_DEPARTMENT or \ + ENABLE_SHARE_TO_DEPARTMENT and not is_group_admin(gid, username): + result['failed'].append({ + 'group_name': group.group_name, + 'error_msg': 'Permission denied.' + }) + continue + if self.has_shared_to_group(request, repo_id, path, gid): result['failed'].append({ 'group_name': group.group_name, diff --git a/seahub/api2/endpoints/shareable_groups.py b/seahub/api2/endpoints/shareable_groups.py index 560c90361f..0b46726f1c 100644 --- a/seahub/api2/endpoints/shareable_groups.py +++ b/seahub/api2/endpoints/shareable_groups.py @@ -13,26 +13,27 @@ from seahub.utils import is_org_context from seahub.utils.timeutils import timestamp_to_isoformat_timestr from seahub.api2.authentication import TokenAuthentication from seahub.api2.throttling import UserRateThrottle -from seahub.avatar.settings import GROUP_AVATAR_DEFAULT_SIZE + +from seahub.settings import ENABLE_SHARE_TO_DEPARTMENT from constance import config try: current_path = os.path.dirname(os.path.abspath(__file__)) - seafile_conf_dir = os.path.join(current_path, \ - '../../../../../conf') + seafile_conf_dir = os.path.join(current_path, '../../../../../conf') sys.path.append(seafile_conf_dir) from seahub_custom_functions import custom_get_groups CUSTOM_GET_GROUPS = True -except ImportError as e: +except ImportError: CUSTOM_GET_GROUPS = False + class ShareableGroups(APIView): authentication_classes = (TokenAuthentication, SessionAuthentication) permission_classes = (IsAuthenticated,) throttle_classes = (UserRateThrottle, ) - def _get_group_info(self, request, group, avatar_size): + def _get_group_info(self, request, group): isoformat_timestr = timestamp_to_isoformat_timestr(group.timestamp) group_info = { "id": group.id, @@ -60,12 +61,13 @@ class ShareableGroups(APIView): else: groups = ccnet_api.get_groups(username) - try: - avatar_size = int(request.GET.get('avatar_size', - GROUP_AVATAR_DEFAULT_SIZE)) - except ValueError: - avatar_size = GROUP_AVATAR_DEFAULT_SIZE + filtered_groups = [] + for group in groups: + if not ENABLE_SHARE_TO_DEPARTMENT and group.parent_group_id != 0: + continue + else: + filtered_groups.append(group) - result = [self._get_group_info(request, group, avatar_size) for group in groups] + result = [self._get_group_info(request, group) for group in filtered_groups] return Response(result) diff --git a/seahub/settings.py b/seahub/settings.py index 496477ce54..1db61efdbb 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -413,6 +413,9 @@ ENABLE_TERMS_AND_CONDITIONS = False # Enable or disable sharing to all groups ENABLE_SHARE_TO_ALL_GROUPS = False +# Enable or disable sharing to departments +ENABLE_SHARE_TO_DEPARTMENT = False + # interval for request unread notifications UNREAD_NOTIFICATIONS_REQUEST_INTERVAL = 3 * 60 # seconds diff --git a/seahub/templates/base_for_react.html b/seahub/templates/base_for_react.html index b3986bb66b..6d23422558 100644 --- a/seahub/templates/base_for_react.html +++ b/seahub/templates/base_for_react.html @@ -86,7 +86,7 @@ libraryTemplates: (function () { var libraryTemplates = []; {% for template in library_templates %} - libraryTemplates.push("{{template}}"); + libraryTemplates.push("{{template}}"); {% endfor %} return libraryTemplates; })(), diff --git a/seahub/templates/react_app.html b/seahub/templates/react_app.html index b7b20cd5db..6ed66e672b 100644 --- a/seahub/templates/react_app.html +++ b/seahub/templates/react_app.html @@ -8,6 +8,7 @@ {% block extra_script %}