1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-12 13:24:52 +00:00

remove repo share when delete custom share perm (#5241)

Co-authored-by: lian <lian@seafile.com>
This commit is contained in:
lian
2022-08-17 11:20:19 +08:00
committed by GitHub
parent 5e356c1b65
commit 1252e7b6ef
2 changed files with 56 additions and 26 deletions

View File

@@ -2,6 +2,7 @@ import React, { Fragment } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import cookie from 'react-cookies'; import cookie from 'react-cookies';
import moment from 'moment'; import moment from 'moment';
import { navigate } from '@reach/router';
import { gettext, siteRoot, username, isDocs, enableVideoThumbnail } from '../../utils/constants'; import { gettext, siteRoot, username, isDocs, enableVideoThumbnail } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api'; import { seafileAPI } from '../../utils/seafile-api';
import { Utils } from '../../utils/utils'; import { Utils } from '../../utils/utils';
@@ -135,6 +136,10 @@ class LibContentView extends React.Component {
const repoInfo = new RepoInfo(repoRes.data); const repoInfo = new RepoInfo(repoRes.data);
const isGroupOwnedRepo = repoInfo.owner_email.indexOf('@seafile_group') > -1; const isGroupOwnedRepo = repoInfo.owner_email.indexOf('@seafile_group') > -1;
this.setState({
currentRepoInfo: repoInfo,
});
if (repoInfo.permission.startsWith('custom-')) { if (repoInfo.permission.startsWith('custom-')) {
const permissionID = repoInfo.permission.split('-')[1]; const permissionID = repoInfo.permission.split('-')[1];
const permissionRes = await seafileAPI.getCustomPermission(repoID, permissionID); const permissionRes = await seafileAPI.getCustomPermission(repoID, permissionID);
@@ -143,14 +148,13 @@ class LibContentView extends React.Component {
this.isNeedUpdateHistoryState = false; this.isNeedUpdateHistoryState = false;
this.setState({ this.setState({
currentRepoInfo: repoInfo,
repoName: repoInfo.repo_name, repoName: repoInfo.repo_name,
libNeedDecrypt: repoInfo.lib_need_decrypt, libNeedDecrypt: repoInfo.lib_need_decrypt,
repoEncrypted: repoInfo.encrypted, repoEncrypted: repoInfo.encrypted,
isGroupOwnedRepo: isGroupOwnedRepo, isGroupOwnedRepo: isGroupOwnedRepo,
path: path path: path
}); });
if (!repoInfo.lib_need_decrypt) { if (!repoInfo.lib_need_decrypt) {
this.loadDirData(path); this.loadDirData(path);
} }
@@ -164,6 +168,11 @@ class LibContentView extends React.Component {
let errorMsg = gettext('Permission denied'); let errorMsg = gettext('Permission denied');
toaster.danger(errorMsg); toaster.danger(errorMsg);
} else if (error.response.status == 404) {
this.setState({
isDirentListLoading: false,
errorMsg: gettext('Library share permission not found.')
});
} else { } else {
this.setState({ this.setState({
isDirentListLoading: false, isDirentListLoading: false,
@@ -1747,6 +1756,22 @@ class LibContentView extends React.Component {
this.updateUsedRepoTags(); this.updateUsedRepoTags();
} }
handleSubmit = (e) => {
let options = {
'share_type': 'personal',
'from': this.state.currentRepoInfo.owner_email
};
seafileAPI.leaveShareRepo(this.props.repoID, options).then(res => {
navigate(siteRoot + 'shared-libs/');
}).catch((error) => {
let errorMsg = Utils.getErrorMsg(error, true);
toaster.danger(errorMsg);
});
e.preventDefault();
}
render() { render() {
if (this.state.libNeedDecrypt) { if (this.state.libNeedDecrypt) {
return ( return (
@@ -1759,6 +1784,15 @@ class LibContentView extends React.Component {
); );
} }
if (this.state.errorMsg) {
return (
<Fragment>
<p className="error mt-6 text-center">{this.state.errorMsg}</p>
<button type="submit" className="btn btn-primary submit" onClick={this.handleSubmit}>{gettext('Leave Share')}</button>
</Fragment>
)
}
if (!this.state.currentRepoInfo) { if (!this.state.currentRepoInfo) {
return ''; return '';
} }

View File

@@ -12,7 +12,8 @@ from seaserv import seafile_api
from seahub.api2.authentication import TokenAuthentication from seahub.api2.authentication import TokenAuthentication
from seahub.api2.throttling import UserRateThrottle from seahub.api2.throttling import UserRateThrottle
from seahub.api2.utils import api_error from seahub.api2.utils import api_error
from seahub.share.utils import is_repo_admin from seahub.share.utils import is_repo_admin, normalize_custom_permission_name
from seahub.utils.repo import get_repo_owner
from seahub.share.models import CustomSharePermissions from seahub.share.models import CustomSharePermissions
from seahub.views import check_folder_permission from seahub.views import check_folder_permission
@@ -107,14 +108,11 @@ class CustomSharePermissionView(APIView):
# main # main
try: try:
permission_obj = CustomSharePermissions.objects.get(id=permission_id) permission_obj = CustomSharePermissions.objects.get(id=permission_id)
if not permission_obj: except CustomSharePermissions.DoesNotExist:
return api_error(status.HTTP_404_NOT_FOUND, 'Permission %s not found.' % permission_id) error_msg = 'Permission %s not found.' % permission_id
res = permission_obj.to_dict() return api_error(status.HTTP_404_NOT_FOUND, error_msg)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
res = permission_obj.to_dict()
return Response({'permission': res}) return Response({'permission': res})
def put(self, request, repo_id, permission_id): def put(self, request, repo_id, permission_id):
@@ -169,6 +167,7 @@ class CustomSharePermissionView(APIView):
"""Delete a custom share permission """Delete a custom share permission
""" """
username = request.user.username username = request.user.username
# permission check # permission check
if not is_repo_admin(username, repo_id): if not is_repo_admin(username, repo_id):
return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.')
@@ -181,22 +180,19 @@ class CustomSharePermissionView(APIView):
try: try:
permission_obj = CustomSharePermissions.objects.get(id=permission_id) permission_obj = CustomSharePermissions.objects.get(id=permission_id)
except Exception as e: except CustomSharePermissions.DoesNotExist:
logger.error(e) error_msg = 'Permission %s not found.' % permission_id
error_msg = 'Internal Server Error' return api_error(status.HTTP_404_NOT_FOUND, error_msg)
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
if not permission_obj:
return api_error(status.HTTP_404_NOT_FOUND, 'custom permission %s not found.' % permission_id)
# main # delete related repo share
try: permission = normalize_custom_permission_name(permission_id)
permission_obj = CustomSharePermissions.objects.get(id=permission_id) repo_owner = get_repo_owner(request, repo_id)
if not permission_obj: share_items = seafile_api.list_repo_shared_to(repo_owner, repo_id)
return api_error(status.HTTP_404_NOT_FOUND, 'Permission %s not found.' % permission_id) for share in share_items:
permission_obj.delete() if share.perm == permission:
except Exception as e: seafile_api.remove_share(repo_id, repo_owner, share.user)
logger.error(e)
error_msg = 'Internal Server Error' # delete custom permission
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) permission_obj.delete()
return Response({'success': True}) return Response({'success': True})