Java Code Examples for jenkins.model.Jenkins#checkPermission()

The following examples show how to use jenkins.model.Jenkins#checkPermission() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FingerprintTestUtil.java    From docker-traceability-plugin with MIT License 6 votes vote down vote up
/**
 * A stub method, which emulates the submission of the image reference 
 * from the web interface
 * @param req Incoming request
 * @param rsp Response
 * @param imageId image id
 * @param jobName job name, to which the facet should be attached
 * @throws IOException Request processing error
 * @throws ServletException Servlet error
 */
public static void doTestSubmitBuildRef(StaplerRequest req, StaplerResponse rsp,
        @QueryParameter(required = true) String imageId,
        @QueryParameter(required = true) String jobName) throws IOException, ServletException {
    final Jenkins j = Jenkins.getInstance();
    if (j == null) {
        throw new IOException("Jenkins instance is not active");
    }
    j.checkPermission(Jenkins.ADMINISTER);
    
    final AbstractProject item = j.getItem(jobName, j, AbstractProject.class);
    final Run latest = item != null ? item.getLastBuild() : null;
    if (latest == null) {
        throw new IOException("Cannot find a project or run to modify"); 
    }
    
    DockerFingerprints.addFromFacet(null,imageId, latest);
    rsp.sendRedirect2(j.getRootUrl());
}
 
Example 2
Source File: FolderAuthorizationStrategyManagementLink.java    From folder-auth-plugin with MIT License 5 votes vote down vote up
/**
 * Get all {@link AbstractFolder}s in the system
 *
 * @return full names of all {@link AbstractFolder}s in the system
 */
@GET
@Nonnull
@Restricted(NoExternalUse.class)
public JSONArray doGetAllFolders() {
    Jenkins jenkins = Jenkins.get();
    jenkins.checkPermission(Jenkins.ADMINISTER);
    List<AbstractFolder> folders;

    try (ACLContext ignored = ACL.as(ACL.SYSTEM)) {
        folders = jenkins.getAllItems(AbstractFolder.class);
    }

    return JSONArray.fromObject(folders.stream().map(AbstractItem::getFullName).collect(Collectors.toList()));
}
 
Example 3
Source File: FolderAuthorizationStrategyManagementLink.java    From folder-auth-plugin with MIT License 5 votes vote down vote up
/**
 * Get all {@link Computer}s in the system
 *
 * @return all Computers in the system
 */
@Nonnull
@Restricted(NoExternalUse.class)
@SuppressWarnings("unused") // used by index.jelly
public List<Computer> getAllComputers() {
    Jenkins jenkins = Jenkins.get();
    jenkins.checkPermission(Jenkins.ADMINISTER);
    Computer[] computers;

    try (ACLContext ignored = ACL.as(ACL.SYSTEM)) {
        computers = jenkins.getComputers();
    }

    return Arrays.asList(computers);
}
 
Example 4
Source File: GitLabServer.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Looks up for Personal Access Token
 *
 * @return {@link PersonalAccessToken}
 */
public PersonalAccessToken getCredentials() {
    Jenkins jenkins = Jenkins.get();
    jenkins.checkPermission(CredentialsProvider.USE_OWN);
    return StringUtils.isBlank(credentialsId) ? null : CredentialsMatchers.firstOrNull(
        lookupCredentials(
            PersonalAccessToken.class,
            jenkins,
            ACL.SYSTEM,
            fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)).build()
        ), withId(credentialsId));
}
 
Example 5
Source File: GitLabServer.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
private PersonalAccessToken getCredentials(String serverUrl, String credentialsId) {
    Jenkins jenkins = Jenkins.get();
    jenkins.checkPermission(Jenkins.ADMINISTER);
    return StringUtils.isBlank(credentialsId) ? null : CredentialsMatchers.firstOrNull(
        lookupCredentials(
            PersonalAccessToken.class,
            jenkins,
            ACL.SYSTEM,
            fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)).build()),
        withId(credentialsId)
    );
}
 
Example 6
Source File: DockerTraceabilityRootAction.java    From docker-traceability-plugin with MIT License 5 votes vote down vote up
/**
 * Check permission.
 * Also prohibits the access if Jenkins has not been started yet.
 * @param p Permission to be checked
 * @throws AccessDeniedException Access denied
 */
private void checkPermission(Permission p) throws AccessDeniedException {
    final Jenkins j = Jenkins.getInstance();
    if (j == null) {
        throw new AccessDeniedException("Cannot retrieve Jenkins instance. "
                + "Probably, the service is starting or shutting down");
    }     
    j.checkPermission(p);
}
 
Example 7
Source File: GitLabPersonalAccessTokenCreator.java    From gitlab-branch-source-plugin with MIT License 4 votes vote down vote up
@SuppressWarnings("unused")
@RequirePOST
public FormValidation doCreateTokenByCredentials(
    @QueryParameter String serverUrl,
    @QueryParameter String credentialsId) {

    Jenkins jenkins = Jenkins.get();
    jenkins.checkPermission(Jenkins.ADMINISTER);
    if (isEmpty(credentialsId)) {
        return FormValidation.error("Please specify credentials to create token");
    }

    StandardUsernamePasswordCredentials credentials = firstOrNull(lookupCredentials(
        StandardUsernamePasswordCredentials.class,
        jenkins,
        ACL.SYSTEM,
        fromUri(defaultIfBlank(serverUrl, GitLabServer.GITLAB_SERVER_URL)).build()),
        withId(credentialsId));

    if (credentials == null) {
        credentials = firstOrNull(lookupCredentials(
            StandardUsernamePasswordCredentials.class,
            jenkins,
            Jenkins.getAuthentication(),
            fromUri(defaultIfBlank(serverUrl, GitLabServer.GITLAB_SERVER_URL)).build()),
            withId(credentialsId));
    }

    if (Objects.isNull(credentials)) {
        return FormValidation.error("Can't create GitLab token, credentials are null");
    }
    try {
        String tokenName = UUID.randomUUID().toString();
        String token = AccessTokenUtils.createPersonalAccessToken(
            defaultIfBlank(serverUrl, GitLabServer.GITLAB_SERVER_URL),
            credentials.getUsername(),
            Secret.toString(credentials.getPassword()),
            tokenName,
            GL_PLUGIN_REQUIRED_SCOPE
        );
        tokenName = getShortName(tokenName);
        createCredentials(serverUrl, token, credentials.getUsername(), tokenName);
        return FormValidation.ok("Created credentials with id %s ", tokenName);
    } catch (GitLabApiException e) {
        return FormValidation.error(e, "Can't create GL token - %s", e.getMessage());
    }
}