diff --git a/media/css/seahub.css b/media/css/seahub.css index 596a85e8e9..20d14c1bf5 100644 --- a/media/css/seahub.css +++ b/media/css/seahub.css @@ -3533,16 +3533,20 @@ textarea:-moz-placeholder {/* for FF */ } /* user & group folder perm */ +#share-popup .tabs-panel, #folder-perm-tabs .tabs-panel { width:550px; } +#share-popup .share-permission-select, #folder-perm-tabs .perm-select { height:30px; } +#share-popup .submit, #folder-perm-tabs .submit { margin-top:2px; height:30px; } +#share-popup table, #folder-perm-tabs table { margin-top:0; } diff --git a/seahub/api2/endpoints/__init__.py b/seahub/api2/endpoints/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/seahub/api2/endpoints/dir_shared_items.py b/seahub/api2/endpoints/dir_shared_items.py new file mode 100644 index 0000000000..28e5d85bd9 --- /dev/null +++ b/seahub/api2/endpoints/dir_shared_items.py @@ -0,0 +1,399 @@ +import logging +import json +import os + +from django.http import HttpResponse +from pysearpc import SearpcError +from rest_framework import status +from rest_framework.authentication import SessionAuthentication +from rest_framework.permissions import IsAuthenticated +from rest_framework.throttling import UserRateThrottle +from rest_framework.views import APIView +import seaserv +from seaserv import seafile_api + +from seahub.api2.authentication import TokenAuthentication +from seahub.api2.permissions import IsRepoAccessible +from seahub.api2.utils import api_error +from seahub.base.templatetags.seahub_tags import email2nickname +from seahub.share.signals import share_repo_to_user_successful +from seahub.share.views import check_user_share_quota +from seahub.utils import (is_org_context, is_valid_username, + send_perm_audit_msg) + + +logger = logging.getLogger(__name__) +json_content_type = 'application/json; charset=utf-8' + +class DirSharedItemsEndpoint(APIView): + """Support uniform interface(list, share, unshare, modify) for sharing + library/folder to users/groups. + """ + authentication_classes = (TokenAuthentication, SessionAuthentication) + permission_classes = (IsAuthenticated, IsRepoAccessible) + throttle_classes = (UserRateThrottle, ) + + def list_user_shared_items(self, request, repo_id, path): + username = request.user.username + if path == '/': + share_items = seafile_api.list_repo_shared_to(username, repo_id) + else: + share_items = seafile_api.get_shared_users_for_subdir(repo_id, + path, username) + ret = [] + for item in share_items: + ret.append({ + "share_type": "user", + "user_info": { + "name": item.user, + "nickname": email2nickname(item.user), + }, + "permission": item.perm, + }) + return ret + + def list_group_shared_items(self, request, repo_id, path): + username = request.user.username + if path == '/': + share_items = seafile_api.list_repo_shared_group(username, repo_id) + else: + share_items = seafile_api.get_shared_groups_for_subdir(repo_id, + path, username) + ret = [] + for item in share_items: + ret.append({ + "share_type": "group", + "group_info": { + "id": item.group_id, + "name": seaserv.get_group(item.group_id).group_name, + }, + "permission": item.perm, + }) + return ret + + def handle_shared_to_args(self, request): + share_type = request.GET.get('share_type', None) + shared_to_user = False + shared_to_group = False + if share_type: + for e in share_type.split(','): + e = e.strip() + if e not in ['user', 'group']: + continue + if e == 'user': + shared_to_user = True + if e == 'group': + shared_to_group = True + else: + shared_to_user = True + shared_to_group = True + + return (shared_to_user, shared_to_group) + + def get_sub_repo_by_path(self, request, repo, path): + if path == '/': + raise Exception("Invalid path") + + # get or create sub repo + username = request.user.username + if is_org_context(request): + org_id = request.user.org.org_id + sub_repo = seaserv.seafserv_threaded_rpc.get_org_virtual_repo( + org_id, repo.id, path, username) + else: + sub_repo = seafile_api.get_virtual_repo(repo.id, path, username) + + return sub_repo + + def get_or_create_sub_repo_by_path(self, request, repo, path): + username = request.user.username + sub_repo = self.get_sub_repo_by_path(request, repo, path) + if not sub_repo: + name = os.path.basename(path) + # create a sub-lib, + # use name as 'repo_name' & 'repo_desc' for sub_repo + if is_org_context(request): + org_id = request.user.org.org_id + sub_repo_id = seaserv.seafserv_threaded_rpc.create_org_virtual_repo( + org_id, repo.id, path, name, name, username) + else: + sub_repo_id = seafile_api.create_virtual_repo(repo.id, path, + name, name, username) + sub_repo = seafile_api.get_repo(sub_repo_id) + + return sub_repo + + def get(self, request, repo_id, format=None): + """List shared items(shared to users/groups) for a folder/library. + """ + repo = seafile_api.get_repo(repo_id) + if not repo: + return api_error(status.HTTP_400_BAD_REQUEST, 'Repo not found.') + + shared_to_user, shared_to_group = self.handle_shared_to_args(request) + + path = request.GET.get('p', '/') + if seafile_api.get_dir_id_by_path(repo.id, path) is None: + return api_error(status.HTTP_400_BAD_REQUEST, 'Directory not found.') + + ret = [] + if shared_to_user: + ret += self.list_user_shared_items(request, repo_id, path) + + if shared_to_group: + ret += self.list_group_shared_items(request, repo_id, path) + + return HttpResponse(json.dumps(ret), status=200, + content_type=json_content_type) + + def post(self, request, repo_id, format=None): + """Update shared item permission. + """ + username = request.user.username + repo = seafile_api.get_repo(repo_id) + if not repo: + return api_error(status.HTTP_400_BAD_REQUEST, 'Repo not found.') + + shared_to_user, shared_to_group = self.handle_shared_to_args(request) + + permission = request.DATA.get('permission', 'r') + if permission not in ['r', 'rw']: + return api_error(status.HTTP_400_BAD_REQUEST, 'Bad permission') + + path = request.GET.get('p', '/') + if seafile_api.get_dir_id_by_path(repo.id, path) is None: + return api_error(status.HTTP_400_BAD_REQUEST, 'Directory not found.') + + if path == '/': + shared_repo = repo + else: + try: + sub_repo = self.get_sub_repo_by_path(request, repo, path) + if sub_repo: + shared_repo = sub_repo + else: + return api_error(status.HTTP_400_BAD_REQUEST, 'No sub repo found') + except SearpcError as e: + logger.error(e) + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Failed to get sub repo') + + if shared_to_user: + shared_to = request.GET.get('username') + if shared_to is None or not is_valid_username(shared_to): + return api_error(status.HTTP_400_BAD_REQUEST, 'Bad username.') + + if is_org_context(request): + org_id = request.user.org.org_id + seaserv.seafserv_threaded_rpc.org_set_share_permission( + org_id, shared_repo.id, username, shared_to, permission) + else: + seafile_api.set_share_permission(shared_repo.id, username, + shared_to, permission) + + send_perm_audit_msg('modify-repo-perm', username, shared_to, + shared_repo.id, path, permission) + + if shared_to_group: + gid = request.GET.get('group_id') + try: + gid = int(gid) + except ValueError: + return api_error(status.HTTP_400_BAD_REQUEST, 'Bad group id: %s' % gid) + group = seaserv.get_group(gid) + if not group: + return api_error(status.HTTP_400_BAD_REQUEST, 'Group not found: %s' % gid) + + if is_org_context(request): + org_id = request.user.org.org_id + seaserv.seafserv_threaded_rpc.set_org_group_repo_permission( + org_id, gid, shared_repo.id, permission) + else: + seafile_api.set_group_repo_permission(gid, shared_repo.id, + permission) + + send_perm_audit_msg('modify-repo-perm', username, gid, + shared_repo.id, path, permission) + + return HttpResponse(json.dumps({'success': True}), status=200, + content_type=json_content_type) + + def put(self, request, repo_id, format=None): + username = request.user.username + repo = seafile_api.get_repo(repo_id) + if not repo: + return api_error(status.HTTP_400_BAD_REQUEST, 'Repo not found.') + + path = request.GET.get('p', '/') + if seafile_api.get_dir_id_by_path(repo.id, path) is None: + return api_error(status.HTTP_400_BAD_REQUEST, 'Directory not found.') + + if path != '/': + try: + sub_repo = self.get_or_create_sub_repo_by_path(request, repo, path) + except SearpcError as e: + logger.error(e) + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Failed to get sub repo') + else: + sub_repo = None + + share_type = request.DATA.get('share_type') + if share_type != 'user' and share_type != 'group': + return api_error(status.HTTP_400_BAD_REQUEST, 'Bad share type') + + permission = request.DATA.get('permission', 'r') + if permission not in ['r', 'rw']: + return api_error(status.HTTP_400_BAD_REQUEST, 'Bad permission') + + shared_repo = repo if path == '/' else sub_repo + success, failed = [], [] + if share_type == 'user': + share_to_users = request.DATA.getlist('username') + for to_user in share_to_users: + if not check_user_share_quota(username, shared_repo, users=[to_user]): + return api_error(status.HTTP_403_FORBIDDEN, + 'Failed to share: No enough quota.') + + try: + if is_org_context(request): + org_id = request.user.org.org_id + seaserv.seafserv_threaded_rpc.org_add_share( + org_id, shared_repo.id, username, to_user, + permission) + else: + seafile_api.share_repo(shared_repo.id, username, + to_user, permission) + + # send a signal when sharing repo successful + share_repo_to_user_successful.send(sender=None, + from_user=username, + to_user=to_user, + repo=shared_repo) + success.append({ + "share_type": "user", + "user_info": { + "name": to_user, + "nickname": email2nickname(to_user), + }, + "permission": permission + }) + + send_perm_audit_msg('add-repo-perm', username, to_user, + shared_repo.id, path, permission) + except SearpcError as e: + logger.error(e) + failed.append(to_user) + continue + + if share_type == 'group': + group_ids = request.DATA.getlist('group_id') + for gid in group_ids: + try: + gid = int(gid) + except ValueError: + return api_error(status.HTTP_400_BAD_REQUEST, 'Bad group id: %s' % gid) + group = seaserv.get_group(gid) + if not group: + return api_error(status.HTTP_400_BAD_REQUEST, 'Group not found: %s' % gid) + + if not check_user_share_quota(username, shared_repo, groups=[group]): + return api_error(status.HTTP_403_FORBIDDEN, + 'Failed to share: No enough quota.') + + try: + if is_org_context(request): + org_id = request.user.org.org_id + seafile_api.add_org_group_repo(shared_repo.repo_id, + org_id, gid, username, + permission) + else: + seafile_api.set_group_repo(shared_repo.repo_id, gid, + username, permission) + + success.append({ + "share_type": "group", + "group_info": { + "id": gid, + "name": group.group_name, + }, + "permission": permission + }) + + send_perm_audit_msg('add-repo-perm', username, gid, + shared_repo.id, path, permission) + except SearpcError as e: + logger.error(e) + failed.append(group.group_name) + continue + + return HttpResponse(json.dumps({ + "success": success, + "failed": failed + }), status=200, content_type=json_content_type) + + def delete(self, request, repo_id, format=None): + username = request.user.username + repo = seafile_api.get_repo(repo_id) + if not repo: + return api_error(status.HTTP_400_BAD_REQUEST, 'Repo not found.') + + shared_to_user, shared_to_group = self.handle_shared_to_args(request) + + path = request.GET.get('p', '/') + if seafile_api.get_dir_id_by_path(repo.id, path) is None: + return api_error(status.HTTP_400_BAD_REQUEST, 'Directory not found.') + + if path == '/': + shared_repo = repo + else: + try: + sub_repo = self.get_sub_repo_by_path(request, repo, path) + if sub_repo: + shared_repo = sub_repo + else: + return api_error(status.HTTP_400_BAD_REQUEST, 'No sub repo found') + except SearpcError as e: + logger.error(e) + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Failed to get sub repo') + + if shared_to_user: + shared_to = request.GET.get('username') + if shared_to is None or not is_valid_username(shared_to): + return api_error(status.HTTP_400_BAD_REQUEST, 'Bad argument.') + + if is_org_context(request): + org_id = request.user.org.org_id + seaserv.seafserv_threaded_rpc.org_remove_share( + org_id, shared_repo.id, username, shared_to) + else: + seaserv.remove_share(shared_repo.id, username, shared_to) + + permission = seafile_api.check_permission_by_path(repo.id, path, + shared_to) + send_perm_audit_msg('delete-repo-perm', username, shared_to, + shared_repo.id, path, permission) + + if shared_to_group: + group_id = request.GET.get('group_id') + try: + group_id = int(group_id) + except ValueError: + return api_error(status.HTTP_400_BAD_REQUEST, 'Bad group id') + + # hacky way to get group repo permission + permission = '' + for e in seafile_api.list_repo_shared_group(username, shared_repo.id): + if e.group_id == group_id: + permission = e.perm + break + + if is_org_context(request): + org_id = request.user.org.org_id + seaserv.del_org_group_repo(shared_repo.id, org_id, group_id) + else: + seafile_api.unset_group_repo(shared_repo.id, group_id, username) + + send_perm_audit_msg('delete-repo-perm', username, group_id, + shared_repo.id, path, permission) + + return HttpResponse(json.dumps({'success': True}), status=200, + content_type=json_content_type) diff --git a/seahub/api2/urls.py b/seahub/api2/urls.py index a7f924f7ab..a59551f855 100644 --- a/seahub/api2/urls.py +++ b/seahub/api2/urls.py @@ -3,6 +3,7 @@ from django.conf.urls.defaults import * from .views import * from .views_misc import ServerInfoView from .views_auth import LogoutDeviceView, ClientLoginTokenView +from .endpoints.dir_shared_items import DirSharedItemsEndpoint urlpatterns = patterns('', @@ -40,6 +41,7 @@ urlpatterns = patterns('', url(r'^repos/(?P[-0-9-a-f]{36})/dir/$', DirView.as_view(), name='DirView'), url(r'^repos/(?P[-0-9-a-f]{36})/dir/sub_repo/$', DirSubRepoView.as_view()), url(r'^repos/(?P[-0-9-a-f]{36})/dir/share/$', DirShareView.as_view()), + url(r'^repos/(?P[-0-9-a-f]{36})/dir/shared_items/$', DirSharedItemsEndpoint.as_view(), name="api2-dir-shared-items"), url(r'^repos/(?P[-0-9-a-f]{36})/dir/download/$', DirDownloadView.as_view()), url(r'^repos/(?P[-0-9-a-f]{36})/thumbnail/$', ThumbnailView.as_view(), name='api2-thumbnail'), url(r'^starredfiles/', StarredFileView.as_view(), name='starredfiles'), diff --git a/seahub/api2/views.py b/seahub/api2/views.py index 082c391bff..d016bc1a99 100644 --- a/seahub/api2/views.py +++ b/seahub/api2/views.py @@ -64,6 +64,7 @@ from seahub.shortcuts import get_first_object_or_none from seahub.signals import repo_created, share_file_to_user_successful from seahub.share.models import PrivateFileDirShare, FileShare, OrgFileShare, \ UploadLinkShare +from seahub.share.signals import share_repo_to_user_successful from seahub.share.views import list_shared_repos from seahub.utils import gen_file_get_url, gen_token, gen_file_upload_url, \ check_filename_with_rename, is_valid_username, EVENTS_ENABLED, \ @@ -1851,7 +1852,10 @@ class FileDetailView(APIView): # get real path for sub repo real_path = repo.origin_path + path if repo.origin_path else path dirent = seafile_api.get_dirent_by_path(repo.store_id, real_path) - latest_contributor, last_modified = dirent.modifier, dirent.mtime + if dirent: + latest_contributor, last_modified = dirent.modifier, dirent.mtime + else: + latest_contributor, last_modified = None, 0 except SearpcError as e: logger.error(e) latest_contributor, last_modified = None, 0 @@ -2284,6 +2288,7 @@ class DirShareView(APIView): share_file_to_user_successful.send(sender=None, priv_share_obj=pfds) return HttpResponse(json.dumps({}), status=200, content_type=json_content_type) + class DirSubRepoView(APIView): authentication_classes = (TokenAuthentication, ) permission_classes = (IsAuthenticated,) diff --git a/seahub/auth/tokens.py b/seahub/auth/tokens.py index 1599f533c8..91bb013a89 100644 --- a/seahub/auth/tokens.py +++ b/seahub/auth/tokens.py @@ -57,12 +57,12 @@ class PasswordResetTokenGenerator(object): key_salt = "django.contrib.auth.tokens.PasswordResetTokenGenerator" # Ensure results are consistent across DB backends - try: - user_last_login = UserLastLogin.objects.get(username=user.email) - login_dt = user_last_login.last_login - except UserLastLogin.DoesNotExist: + user_last_login = UserLastLogin.objects.get_by_username(user.username) + if user_last_login is None: from seahub.utils.timeutils import dt login_dt = dt(user.ctime) + else: + login_dt = user_last_login.last_login login_timestamp = login_dt.replace(microsecond=0, tzinfo=None) value = (six.text_type(user.id) + user.enc_password + diff --git a/seahub/base/models.py b/seahub/base/models.py index 05103e7f5c..cdc4ada20f 100644 --- a/seahub/base/models.py +++ b/seahub/base/models.py @@ -120,7 +120,10 @@ class UserStarredFilesManager(models.Manager): real_path = sfile.repo.origin_path + sfile.path if sfile.repo.origin_path else sfile.path dirent = seafile_api.get_dirent_by_path(sfile.repo.store_id, real_path) - sfile.last_modified = dirent.mtime + if dirent: + sfile.last_modified = dirent.mtime + else: + sfile.last_modified = 0 except SearpcError as e: logger.error(e) sfile.last_modified = 0 @@ -152,18 +155,35 @@ class GroupEnabledModule(models.Model): module_name = models.CharField(max_length=20) ########## misc +class UserLastLoginManager(models.Manager): + def get_by_username(self, username): + """Return last login record for a user, delete duplicates if there are + duplicated records. + """ + try: + return self.get(username=username) + except UserLastLogin.DoesNotExist: + return None + except UserLastLogin.MultipleObjectsReturned: + dups = self.filter(username=username) + ret = dups[0] + for dup in dups[1:]: + dup.delete() + logger.warn('Delete duplicate user last login record: %s' % username) + return ret + class UserLastLogin(models.Model): username = models.CharField(max_length=255, db_index=True) last_login = models.DateTimeField(default=timezone.now) + objects = UserLastLoginManager() def update_last_login(sender, user, **kwargs): """ A signal receiver which updates the last_login date for the user logging in. """ - try: - user_last_login = UserLastLogin.objects.get(username=user.username) - except UserLastLogin.DoesNotExist: + user_last_login = UserLastLogin.objects.get_by_username(user.username) + if user_last_login is None: user_last_login = UserLastLogin(username=user.username) user_last_login.last_login = timezone.now() user_last_login.save() diff --git a/seahub/group/views.py b/seahub/group/views.py index 8073836400..6c710d1308 100644 --- a/seahub/group/views.py +++ b/seahub/group/views.py @@ -1396,7 +1396,10 @@ def group_wiki(request, group, page_name="home"): path = '/' + dirent.obj_name try: dirent = seafile_api.get_dirent_by_path(repo.id, path) - latest_contributor, last_modified = dirent.modifier, dirent.mtime + if dirent: + latest_contributor, last_modified = dirent.modifier, dirent.mtime + else: + latest_contributor, last_modified = None, 0 except SearpcError as e: logger.error(e) latest_contributor, last_modified = None, 0 diff --git a/seahub/share/views.py b/seahub/share/views.py index 81f69ca36a..4fc14e33fe 100644 --- a/seahub/share/views.py +++ b/seahub/share/views.py @@ -359,7 +359,7 @@ def ajax_repo_remove_share(request): @login_required def repo_remove_share(request): """ - If repo is shared from one person to another person, only these two peson + If repo is shared from one person to another person, only these two person can remove share. If repo is shared from one person to a group, then only the one share the repo and group staff can remove share. diff --git a/seahub/templates/js/templates.html b/seahub/templates/js/templates.html index 3472f87fec..6ee25009ef 100644 --- a/seahub/templates/js/templates.html +++ b/seahub/templates/js/templates.html @@ -325,8 +325,9 @@ <% if (user_perm == 'rw' && !repo_encrypted) { %>
  • {% trans "Upload Link" %}
  • <% } %> - <% if (!is_virtual && is_repo_owner) { %> -
  • {% trans "Private Share" %}
  • + <% if (!is_virtual && is_repo_owner) { %> {# dir private share #} +
  • {% trans "Share to user" %}
  • +
  • {% trans "Share to group" %}
  • <% } %> <% } %> @@ -420,28 +421,56 @@ <% if (!is_virtual && is_repo_owner) { %> <% if (!repo_encrypted) { %> -
    +
    <% } else { %> -
    {# enc lib only has 'dir-private-share' #} +
    <% } %> -
    -
    -
    - -
    -
    - -
    - -

    - -
    + + + + + + + + + + + +
    {% trans "User" %}{% trans "Permission" %}
    + + + +
    +

    - <% } %> - <% } %> + +
    + + + + + + + + + + + +
    {% trans "Group" %}{% trans "Permission" %}
    + + + +
    +

    +
    + <% } %> {# if (!is_virtual && is_repo_owner) #} + <% } %> {# if is_dir #}
    @@ -593,9 +622,9 @@