Пример #1
0
def create_group_repo(request, group_id):
    """Create a repo and share it to current group"""

    content_type = 'application/json; charset=utf-8'

    def json_error(err_msg):
        result = {'error': [err_msg]}
        return HttpResponseBadRequest(json.dumps(result),
                                      content_type=content_type)
    group_id = int(group_id)
    if not get_group(group_id):
        return json_error(_(u'Failed to create: the group does not exist.'))

    # Check whether user belongs to the group.
    if not is_group_user(group_id, request.user.username):
        return json_error(_(u'Failed to create: you are not in the group.'))

    form = SharedRepoCreateForm(request.POST)
    if not form.is_valid():
        return json_error(form.errors)
    else:
        repo_name = form.cleaned_data['repo_name']
        repo_desc = form.cleaned_data['repo_desc']
        permission = form.cleaned_data['permission']
        encrypted = form.cleaned_data['encryption']
        passwd = form.cleaned_data['passwd']
        user = request.user.username

        org, base_template = check_and_get_org_by_group(group_id, user)
        if org:
            # create group repo in org context
            try:
                repo_id = create_org_repo(repo_name, repo_desc, user, passwd,
                                          org.org_id)
            except:
                repo_id = None
            if not repo_id:
                return json_error(_(u'Failed to create'))

            try:
                status = seafserv_threaded_rpc.add_org_group_repo(repo_id,
                                                                  org.org_id,
                                                                  group_id,
                                                                  user,
                                                                  permission)
            except SearpcError, e:
                status = -1
                
            # if share failed, remove the newly created repo
            if status != 0:
                seafserv_threaded_rpc.remove_repo(repo_id)
                return json_error(_(u'Failed to create: internal error.'))
            else:
                result = {'success': True}
                return HttpResponse(json.dumps(result),
                                    content_type=content_type)
        else:
Пример #2
0
def org_repo_share(request, url_prefix):
    """
    Share org repo to members or groups in current org.
    """
    if request.method != "POST":
        raise Http404

    org = get_user_current_org(request.user.username, url_prefix)
    if not org:
        return HttpResponseRedirect(reverse(myhome))

    form = RepoShareForm(request.POST)
    if not form.is_valid():
        # TODO: may display error msg on form
        raise Http404

    email_or_group = form.cleaned_data["email_or_group"]
    repo_id = form.cleaned_data["repo_id"]
    permission = form.cleaned_data["permission"]
    from_email = request.user.username

    # Test whether user is the repo owner
    if not validate_org_repo_owner(org.org_id, repo_id, request.user.username):
        return render_permission_error(
            request,
            _(u"Only the owner of this library has permission to share it."),
            extra_ctx={"org": org, "base_template": "org_base.html"},
        )

    share_to_list = string2list(email_or_group)
    for share_to in share_to_list:
        if share_to == "all":
            """ Share to public """

            try:
                seafserv_threaded_rpc.set_org_inner_pub_repo(org.org_id, repo_id, permission)
            except:
                msg = _(u"Failed to share to all members")
                messages.add_message(request, messages.ERROR, msg)
                continue

            msg = _(u'Shared to all members successfully, you can go check it at <a href="%s">Share</a>.') % (
                reverse("org_shareadmin", args=[org.url_prefix])
            )
            messages.add_message(request, messages.INFO, msg)
        elif share_to.find("@") == -1:
            """ Share repo to group """
            # TODO: if we know group id, then we can simplly call group_share_repo
            group_name = share_to

            # Get all org groups.
            groups = get_org_groups(org.org_id, -1, -1)
            find = False
            for group in groups:
                # for every group that user joined, if group name and
                # group creator matchs, then has finded the group
                if group.props.group_name == group_name:
                    seafserv_threaded_rpc.add_org_group_repo(repo_id, org.org_id, group.id, from_email, permission)
                    find = True
                    msg = _(
                        u'Shared to %(group)s successfully,you can go check it at <a href="%(share)s">Share</a>.'
                    ) % {"group": group_name, "share": reverse("org_shareadmin", args=[org.url_prefix])}

                    messages.add_message(request, messages.INFO, msg)
                    break
            if not find:
                msg = _(u"Failed to share to %s.") % group_name
                messages.add_message(request, messages.ERROR, msg)
        else:
            """ Share repo to user """
            # Test whether share_to is in this org
            if not org_user_exists(org.org_id, share_to):
                msg = _(u"Failed to share to %s: this user does not exist in the organization.") % share_to
                messages.add_message(request, messages.ERROR, msg)
                continue

            # Record share info to db.
            try:
                seafserv_threaded_rpc.add_share(repo_id, from_email, share_to, permission)
                msg = _(
                    u'Shared to %(share_to)s successfully,you can go check it at <a href="%(share)s">Share</a>.'
                ) % {"share_to": share_to, "share": reverse("org_shareadmin", args=[org.url_prefix])}
                messages.add_message(request, messages.INFO, msg)
            except SearpcError, e:
                msg = _(u"Failed to share to %s.") % share_to
                messages.add_message(request, messages.ERROR, msg)
                continue