Ejemplo n.º 1
0
def user(request, action, login):
    users_file_hash = md5_for_file(settings.AUTH_FILE)
    hgweb = HGWeb(settings.HGWEB_CONFIG)
    tree = prepare_tree(
        modhg.repository.get_tree(hgweb.get_paths(), hgweb.get_collections()))
    model = {"tree": tree}
    is_write_access = _check_users_file(request)
    if action == "edit":
        # todo: check if login exists
        if request.method == "POST":
            form = EditUser(users_file_hash, request.POST)
            if form.is_valid() and is_write_access:
                password = form.cleaned_data['password2']
                users.update(settings.AUTH_FILE, login, password)
                messages.success(request, _("Password changed successfully."))
                return HttpResponseRedirect(
                    reverse("users", args=[action, login]))
        else:
            form = EditUser(users_file_hash)
        model["form"] = form
        model["login"] = login
        # sorting permissions dict by key
        permissions = users.permissions(login)
        permissions = sorted(permissions.items(), key=lambda elem: elem[0])
        model["permissions"] = SortedDict(permissions)
        return model
Ejemplo n.º 2
0
def user_index(request):
    is_w_access = _check_users_file(request)
    users_file_hash = md5_for_file(settings.AUTH_FILE)
    form = AddUser(users_file_hash)
    delete_user_form = FileHashForm(users_file_hash)
    hgweb = HGWeb(settings.HGWEB_CONFIG)
    tree = prepare_tree(
        modhg.repository.get_tree(hgweb.get_paths(), hgweb.get_collections()))
    model = {"tree": tree}
    if request.method == "POST":
        if "delete_user" in request.POST:
            delete_user_form = FileHashForm(users_file_hash, request.POST)
            if delete_user_form.is_valid() and is_w_access:
                login = request.POST.get("login")
                users.remove(settings.AUTH_FILE, login)
                messages.success(request, _("User '%s' was deleted.") % login)
                return HttpResponseRedirect(reverse('users_index'))
        elif "add_user" in request.POST:
            form = AddUser(users_file_hash, request.POST)
            if form.is_valid() and is_w_access:
                login = form.cleaned_data['login']
                password = form.cleaned_data['password2']
                users.add(settings.AUTH_FILE, login, password)
                messages.success(request, _("User '%s' was added.") % login)
                return HttpResponseRedirect(reverse('users_index'))
    user_list = users.login_list(settings.AUTH_FILE)
    model["delete_user_form"] = delete_user_form
    model["form"] = form
    model["users"] = user_list
    return model
Ejemplo n.º 3
0
def repo(request, repo_path):
    hgweb = HGWeb(settings.HGWEB_CONFIG)
    tree = prepare_tree(
        modhg.repository.get_tree(hgweb.get_paths(), hgweb.get_collections()))
    is_raw_mode = False

    full_repository_path = repository.get_absolute_repository_path(repo_path)
    hgrc_path = os.path.join(full_repository_path, ".hg", "hgrc")
    _check_access_local_hgrc(request, hgrc_path)

    groups = hgweb.get_groups()
    hgweb_cfg_hash = md5_for_file(settings.HGWEB_CONFIG)
    edit_repo_form = CreateRepoForm(default_groups=groups,
                                    file_hash=hgweb_cfg_hash,
                                    initial={
                                        "name":
                                        os.path.split(full_repository_path)[1],
                                        "group":
                                        repository.get_group(repo_path),
                                        "file_hash":
                                        hgweb_cfg_hash
                                    })

    hgrc = HGWeb(hgrc_path, True)

    def _hgweb_or_hgrc_list(hgweb, hgrc, param_name):
        """
        checks if user list is not specified in the hgrc, tries to read it from hgweb
        """
        _list = _split_users(hgrc.get_web_key(param_name))
        if not _list:
            _list = _split_users(hgweb.get_web_key(param_name))
        return _list

    allow_read_list = _hgweb_or_hgrc_list(hgweb, hgrc, 'allow_read')
    allow_push_list = _hgweb_or_hgrc_list(hgweb, hgrc, 'allow_push')
    deny_read_list = _hgweb_or_hgrc_list(hgweb, hgrc, 'deny_read')
    deny_push_list = _hgweb_or_hgrc_list(hgweb, hgrc, 'deny_push')

    user_list = users.login_list(settings.AUTH_FILE)
    add_to_allow_read_list = [
        val for val in user_list if val.strip() not in allow_read_list
    ]
    add_to_allow_push_list = [
        val for val in user_list if val.strip() not in allow_push_list
    ]
    add_to_deny_read_list = [
        val for val in user_list if val.strip() not in deny_read_list
    ]
    add_to_deny_push_list = [
        val for val in user_list if val.strip() not in deny_push_list
    ]

    model = {
        "tree": tree,
        "global": False,
        "repo_path": repo_path,
        "add_to_allow_read_list": add_to_allow_read_list,
        "add_to_allow_push_list": add_to_allow_push_list,
        "add_to_deny_read_list": add_to_deny_read_list,
        "add_to_deny_push_list": add_to_deny_push_list,
        "allow_read_list": allow_read_list,
        "allow_push_list": allow_push_list,
        "deny_read_list": deny_read_list,
        "deny_push_list": deny_push_list
    }

    local_hgrc_hash = md5_for_file(hgrc_path)
    delete_repo_form = FileHashForm(local_hgrc_hash)
    repo_field_delete_form = FileHashForm(local_hgrc_hash)
    form = RepositoryForm(local_hgrc_hash)

    with open(hgrc_path, 'r') as f:
        hgrc_content = f.read()
    raw_mode_form = RawModeForm(local_hgrc_hash,
                                initial={
                                    "hgrc": hgrc_content,
                                    "file_hash": local_hgrc_hash
                                })

    if request.method == 'POST' and hgrc is not None:
        if 'save' in request.POST:
            form = RepositoryForm(local_hgrc_hash, request.POST)
            if form.is_valid():
                form.export_values(hgrc, request.POST)
                messages.success(request,
                                 _("Repository settings saved successfully."))
                return HttpResponseRedirect(
                    reverse("repository", args=[repo_path]))
        elif 'delete_field' in request.POST:
            repo_field_delete_form = FileHashForm(local_hgrc_hash,
                                                  request.POST)
            if repo_field_delete_form.is_valid():
                parameter = request.POST.get('parameter')
                hgrc.del_web_key(parameter)
                return HttpResponseRedirect(
                    reverse("repository", args=[repo_path]))
        elif 'sure_delete' in request.POST:
            delete_repo_form = FileHashForm(local_hgrc_hash, request.POST)
            if delete_repo_form.is_valid():
                try:
                    repository.delete(full_repository_path, repo_path)
                    messages.success(
                        request,
                        _("Repository '%s' deleted successfully.") % repo_path)
                except RepositoryException as e:
                    messages.warning(
                        request,
                        _("Repository '%(repo)s' was not deleted, cause: %(cause)."
                          ) % {
                              "repo": repo_path,
                              "cause": unicode(e)
                          })
                return HttpResponseRedirect(reverse("index"))
        elif 'save_repo' in request.POST:
            edit_repo_form = CreateRepoForm(groups, hgweb_cfg_hash,
                                            request.POST)
            if edit_repo_form.is_valid():
                return edit_repo_form.rename(request, repo_path, groups,
                                             full_repository_path)
        elif 'raw_save' in request.POST:
            is_raw_mode = True
            raw_mode_form = RawModeForm(local_hgrc_hash, request.POST)
            if raw_mode_form.is_valid():
                with open(hgrc_path, 'w') as f:
                    f.write(raw_mode_form.cleaned_data['hgrc'])
                messages.success(request,
                                 _("Repository settings saved successfully."))
                return HttpResponseRedirect(
                    reverse("repository", args=[repo_path]))

        # finally
        # local hgrc might be changed, recreate dependence forms
        local_hgrc_hash = md5_for_file(hgrc_path)
        repo_field_delete_form = FileHashForm(local_hgrc_hash)
        delete_repo_form = FileHashForm(local_hgrc_hash)

        # raw_mode form may have _errors set, so just update data in it
        with open(hgrc_path, 'r') as f:
            hgrc_content = f.read()
        raw_mode_form.data['hgrc'] = hgrc_content
        raw_mode_form.data['file_hash'] = local_hgrc_hash

        # re-set errors if any occurs in the is_valid method.
        errors = form._errors
        form = RepositoryForm(local_hgrc_hash)
        form._errors = errors

    form.set_default(hgweb, hgrc)
    model["form"] = form
    model["repo_field_delete_form"] = repo_field_delete_form
    model["delete_repo_form"] = delete_repo_form
    model["repo_form"] = edit_repo_form
    model["raw_mode_form"] = raw_mode_form
    model["is_raw_mode"] = is_raw_mode
    model["hgweb_repo_url"] = _hgweb_repo_url(
        full_repository_path) if _is_collection(
            repo_path, hgweb) else _hgweb_repo_url(repo_path)

    return model
Ejemplo n.º 4
0
def hgweb(request):
    hgweb = HGWeb(settings.HGWEB_CONFIG)
    tree = prepare_tree(
        modhg.repository.get_tree(hgweb.get_paths(), hgweb.get_collections()))
    is_raw_mode = False

    allow_read_list = _split_users(hgweb.get_web_key('allow_read'))
    allow_push_list = _split_users(hgweb.get_web_key('allow_push'))
    deny_read_list = _split_users(hgweb.get_web_key('deny_read'))
    deny_push_list = _split_users(hgweb.get_web_key('deny_push'))

    user_list = users.login_list(settings.AUTH_FILE)
    add_to_allow_read_list = [
        val for val in user_list if val.strip() not in allow_read_list
    ]
    add_to_allow_push_list = [
        val for val in user_list if val.strip() not in allow_push_list
    ]
    add_to_deny_read_list = [
        val for val in user_list if val.strip() not in deny_read_list
    ]
    add_to_deny_push_list = [
        val for val in user_list if val.strip() not in deny_push_list
    ]

    model = {
        "tree": tree,
        "global": True,
        "add_to_allow_read_list": add_to_allow_read_list,
        "add_to_allow_push_list": add_to_allow_push_list,
        "add_to_deny_read_list": add_to_deny_read_list,
        "add_to_deny_push_list": add_to_deny_push_list,
        "allow_read_list": allow_read_list,
        "allow_push_list": allow_push_list,
        "deny_read_list": deny_read_list,
        "deny_push_list": deny_push_list
    }
    file_hash = md5_for_file(settings.HGWEB_CONFIG)

    form = RepositoryForm(file_hash)
    repo_field_delete_form = FileHashForm(file_hash)

    with open(settings.HGWEB_CONFIG, 'r') as f:
        hgrc_content = f.read()
    raw_mode_form = RawModeForm(file_hash, initial={"hgrc": hgrc_content})

    if request.method == 'POST':
        if 'save' in request.POST:
            form = RepositoryForm(file_hash, request.POST)
            if form.is_valid():
                form.export_values(hgweb, request.POST)
                messages.success(request,
                                 _("Global settings saved successfully."))
                return HttpResponseRedirect(reverse("hgweb"))
        elif 'raw_save' in request.POST:
            is_raw_mode = True
            raw_mode_form = RawModeForm(file_hash, request.POST)
            if raw_mode_form.is_valid():
                with open(settings.HGWEB_CONFIG, 'w') as f:
                    f.write(raw_mode_form.cleaned_data['hgrc'])
                messages.success(request,
                                 _("Global settings saved successfully."))
                return HttpResponseRedirect(reverse("hgweb"))

        # re-set errors if any occurs in the is_valid method.
        file_hash = md5_for_file(settings.HGWEB_CONFIG)
        repo_field_delete_form = FileHashForm(file_hash)

        # raw_mode form may have _errors set, so just update data in it
        with open(settings.HGWEB_CONFIG, 'r') as f:
            hgrc_content = f.read()
        raw_mode_form.data['hgrc'] = hgrc_content
        raw_mode_form.data['file_hash'] = file_hash

        # re-set errors if any occurs in the is_valid method.
        errors = form._errors
        form = RepositoryForm(file_hash)
        form._errors = errors

    form.set_default(hgweb, None)
    model["form"] = form
    model["repo_field_delete_form"] = repo_field_delete_form
    model["raw_mode_form"] = raw_mode_form
    model["is_raw_mode"] = is_raw_mode
    return model
Ejemplo n.º 5
0
def index(request):
    hgweb = HGWeb(settings.HGWEB_CONFIG)
    collections = hgweb.get_collections()
    _tree = modhg.repository.get_tree(hgweb.get_paths(), collections)
    tree = prepare_tree(_tree)
    groups = hgweb.get_groups()
    # unzipping collections
    unzipped_collections = zip(*hgweb.get_collections())
    if unzipped_collections:
        collection_names = unzipped_collections[0]
    else:
        collection_names = []
    hgweb_cfg_hash = md5_for_file(settings.HGWEB_CONFIG)
    create_repo_form = CreateRepoForm(groups, hgweb_cfg_hash)
    groups_form = ManageGroupsForm(hgweb_cfg_hash)
    delete_group_form = DeleteGroupForm(hgweb_cfg_hash)
    EDIT_GROUP_FORM_PREFIX = "edit_group"
    edit_group_form = ManageGroupsForm(hgweb_cfg_hash,
                                       prefix=EDIT_GROUP_FORM_PREFIX)

    model = {
        "tree":
        tree,
        "groups":
        _ext_groups_with_amount_of_repos_and_collection_flag(
            groups, collection_names, _tree),
        "default_path":
        settings.REPOSITORIES_ROOT
    }

    if request.method == 'POST':
        if "create_group" in request.POST:
            groups_form = ManageGroupsForm(hgweb_cfg_hash, request.POST)
            if groups_form.is_valid():
                return groups_form.create_group(request)
        elif "create_repo" in request.POST:
            create_repo_form = CreateRepoForm(groups, hgweb_cfg_hash,
                                              request.POST)
            if create_repo_form.is_valid():
                return create_repo_form.create_repository(request, groups)
        elif "delete_group" in request.POST:
            delete_group_form = DeleteGroupForm(hgweb_cfg_hash, request.POST)
            if delete_group_form.is_valid():
                return delete_group_form.delete_group(request, groups)
        elif "old_group_name" in request.POST:  # edit group request
            edit_group_form = ManageGroupsForm(hgweb_cfg_hash,
                                               request.POST,
                                               prefix=EDIT_GROUP_FORM_PREFIX)
            if edit_group_form.is_valid():
                return edit_group_form.edit_group(request, groups, hgweb)
            else:
                old_name = request.POST.get("old_group_name")
                old_path = request.POST.get("old_group_path")
                model["old_group_name"] = old_name
                model["old_group_path"] = old_path

    model["groups_form"] = groups_form
    model["edit_group_form"] = edit_group_form
    model["repo_form"] = create_repo_form
    model["delete_group_form"] = delete_group_form

    return model