Search in sources :

Example 1 with EscalatedSecurityContext

use of com.atlassian.stash.user.EscalatedSecurityContext in project stash-codesearch-plugin by palantir.

the class GlobalSettingsServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!verifySysAdmin(req, resp)) {
        return;
    }
    // Parse arguments
    ArrayList<String> errors = new ArrayList<String>();
    boolean indexingEnabled = "on".equals(req.getParameter("indexingEnabled"));
    int maxConcurrentIndexing = 0;
    try {
        maxConcurrentIndexing = parseInt("Indexing Concurrency Limit", MAX_CONCURRENT_INDEXING_LB, MAX_CONCURRENT_INDEXING_UB, req.getParameter("maxConcurrentIndexing"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    int maxFileSize = 0;
    try {
        maxFileSize = parseInt("Max Filesize", MAX_FILE_SIZE_LB, MAX_FILE_SIZE_UB, req.getParameter("maxFileSize"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    int searchTimeout = 0;
    try {
        searchTimeout = parseInt("Search Timeout", SEARCH_TIMEOUT_LB, SEARCH_TIMEOUT_UB, req.getParameter("searchTimeout"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    String noHighlightExtensions = req.getParameter("noHighlightExtensions");
    int maxPreviewLines = 0;
    try {
        maxPreviewLines = parseInt("Preview Limit", MAX_PREVIEW_LINES_LB, MAX_PREVIEW_LINES_UB, req.getParameter("maxPreviewLines"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    int maxMatchLines = 0;
    try {
        maxMatchLines = parseInt("Match Limit", MAX_MATCH_LINES_LB, MAX_MATCH_LINES_UB, req.getParameter("maxMatchLines"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    int maxFragments = 0;
    try {
        maxFragments = parseInt("Fragment Limit", MAX_FRAGMENTS_LB, MAX_FRAGMENTS_UB, req.getParameter("maxFragments"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    int pageSize = 0;
    try {
        pageSize = parseInt("Page Size", PAGE_SIZE_LB, PAGE_SIZE_UB, req.getParameter("pageSize"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    double commitHashBoost = 0.0;
    try {
        commitHashBoost = parseDouble("Commit Hash Boost", COMMIT_HASH_BOOST_LB, COMMIT_HASH_BOOST_UB, req.getParameter("commitHashBoost"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    double commitSubjectBoost = 0.0;
    try {
        commitSubjectBoost = parseDouble("Commit Subject Boost", COMMIT_SUBJECT_BOOST_LB, COMMIT_SUBJECT_BOOST_UB, req.getParameter("commitSubjectBoost"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    double commitBodyBoost = 0.0;
    try {
        commitBodyBoost = parseDouble("Commit Body Boost", COMMIT_BODY_BOOST_LB, COMMIT_BODY_BOOST_UB, req.getParameter("commitBodyBoost"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    double fileNameBoost = 0.0;
    try {
        fileNameBoost = parseDouble("Filename Boost", FILE_NAME_BOOST_LB, FILE_NAME_BOOST_UB, req.getParameter("fileNameBoost"));
    } catch (IllegalArgumentException e) {
        errors.add(e.getMessage());
    }
    // Update settings object iff no parse errors
    GlobalSettings settings;
    if (errors.isEmpty()) {
        settings = settingsManager.setGlobalSettings(indexingEnabled, maxConcurrentIndexing, maxFileSize, searchTimeout, noHighlightExtensions, maxPreviewLines, maxMatchLines, maxFragments, pageSize, commitHashBoost, commitSubjectBoost, commitBodyBoost, fileNameBoost);
        // Trigger reindex is requested
        if ("true".equals(req.getParameter("reindex"))) {
            log.info("User {} submitted an async full reindex", req.getRemoteUser());
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        EscalatedSecurityContext esc = securityService.withPermission(Permission.SYS_ADMIN, "full reindex by sysadmin");
                        esc.call(new Operation<Void, Exception>() {

                            @Override
                            public Void perform() {
                                searchUpdater.reindexAll();
                                return null;
                            }
                        });
                    } catch (Exception e) {
                        log.warn("Caught exception while reindexing", e);
                    }
                }
            }).start();
        }
    } else {
        settings = settingsManager.getGlobalSettings();
    }
    renderPage(req, resp, settings, errors);
}
Also used : ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) AuthorisationException(com.atlassian.stash.exception.AuthorisationException) IOException(java.io.IOException) EscalatedSecurityContext(com.atlassian.stash.user.EscalatedSecurityContext)

Example 2 with EscalatedSecurityContext

use of com.atlassian.stash.user.EscalatedSecurityContext in project stash-codesearch-plugin by palantir.

the class RepositorySettingsServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!verifyLoggedIn(req, resp)) {
        return;
    }
    final Repository repository = getRepository(req);
    if (repository == null) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Repo not found.");
        return;
    }
    if (!verifyRepoAdmin(req, resp, repository)) {
        return;
    }
    // Parse arguments
    ArrayList<String> errors = new ArrayList<String>();
    String refRegex = req.getParameter("refRegex");
    try {
        Pattern.compile(refRegex);
    } catch (Exception e) {
        errors.add("Invalid regex: \"" + refRegex + "\"");
    }
    // Update settings object iff no parse errors
    RepositorySettings settings;
    if (errors.isEmpty()) {
        settings = settingsManager.setRepositorySettings(repository, refRegex);
        if ("true".equals(req.getParameter("reindex"))) {
            // Reindex is requested
            log.info("User {} submitted an async reindex for {}^{}", req.getRemoteUser(), repository.getProject().getKey(), repository.getSlug());
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        /* XXX: Is this really necessary?  Users of the
                             * configuration page are already repo admins, I
                             * think, and it doesn't look like reindexing does
                             * anything that needs this permission (although
                             * maybe you need to have permission to see all
                             * branches, which  a repo admin has?)
                             */
                        EscalatedSecurityContext esc = securityService.withPermission(Permission.REPO_ADMIN, "reindex by repo admin");
                        esc.call(new Operation<Void, Exception>() {

                            @Override
                            public Void perform() {
                                searchUpdater.reindexRepository(repository.getProject().getKey(), repository.getSlug());
                                return null;
                            }
                        });
                    } catch (Exception e) {
                        log.warn("Caught exception while reindexing", e);
                    }
                }
            }).start();
        }
    } else {
        settings = settingsManager.getRepositorySettings(repository);
    }
    renderPage(req, resp, repository, settings, errors);
}
Also used : Repository(com.atlassian.stash.repository.Repository) ArrayList(java.util.ArrayList) EscalatedSecurityContext(com.atlassian.stash.user.EscalatedSecurityContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthorisationException(com.atlassian.stash.exception.AuthorisationException)

Aggregations

AuthorisationException (com.atlassian.stash.exception.AuthorisationException)2 EscalatedSecurityContext (com.atlassian.stash.user.EscalatedSecurityContext)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ServletException (javax.servlet.ServletException)2 Repository (com.atlassian.stash.repository.Repository)1