Skip to content

Commit

Permalink
[department] added an option to control if libraries can be shared in… (
Browse files Browse the repository at this point in the history
haiwen#4778)

* [department] added an option to control if libraries can be shared into a department

* update seahub web api

Co-authored-by: lian <lian@seafile.com>
  • Loading branch information
llj and lian authored Jan 13, 2021
1 parent ae9874c commit 2db2c08
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
8 changes: 7 additions & 1 deletion frontend/src/components/dialog/share-to-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions frontend/src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 13 additions & 1 deletion seahub/api2/endpoints/dir_shared_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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, \
Expand All @@ -37,6 +37,7 @@
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__)
Expand Down Expand Up @@ -437,6 +438,17 @@ def put(self, request, repo_id, format=None):
})
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,
Expand Down
24 changes: 13 additions & 11 deletions seahub/api2/endpoints/shareable_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@
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,
Expand Down Expand Up @@ -60,12 +61,13 @@ def get(self, request):
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)
3 changes: 3 additions & 0 deletions seahub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@
# 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

Expand Down
2 changes: 1 addition & 1 deletion seahub/templates/base_for_react.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
libraryTemplates: (function () {
var libraryTemplates = [];
{% for template in library_templates %}
libraryTemplates.push("{{template}}");
libraryTemplates.push("{{template}}");
{% endfor %}
return libraryTemplates;
})(),
Expand Down
1 change: 1 addition & 0 deletions seahub/templates/react_app.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{% block extra_script %}
<script type="text/javascript">
Object.assign(app.pageOptions, {
enableShareToDepartment: {% if enable_share_to_department %} true {% else %} false {% endif %},
shareLinkExpireDaysDefault: {{ share_link_expire_days_default }},
shareLinkExpireDaysMin: {{ share_link_expire_days_min }},
shareLinkExpireDaysMax: {{ share_link_expire_days_max }},
Expand Down
1 change: 1 addition & 0 deletions seahub/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,4 +1211,5 @@ def react_fake_view(request, **kwargs):
'additional_about_dialog_links': ADDITIONAL_ABOUT_DIALOG_LINKS,
'enable_ocm': ENABLE_OCM,
'ocm_remote_servers': OCM_REMOTE_SERVERS,
'enable_share_to_department': settings.ENABLE_SHARE_TO_DEPARTMENT,
})

0 comments on commit 2db2c08

Please sign in to comment.