diff --git a/src/main/java/hudson/plugins/blazemeter/BlazemeterCredentialsBAImpl.java b/src/main/java/hudson/plugins/blazemeter/BlazemeterCredentialsBAImpl.java index dd6d03ca..23ba5966 100644 --- a/src/main/java/hudson/plugins/blazemeter/BlazemeterCredentialsBAImpl.java +++ b/src/main/java/hudson/plugins/blazemeter/BlazemeterCredentialsBAImpl.java @@ -22,15 +22,16 @@ import javax.validation.constraints.NotNull; import hudson.Extension; import hudson.Util; +import hudson.model.ModelObject; import hudson.plugins.blazemeter.utils.JenkinsBlazeMeterUtils; +import hudson.security.AccessControlled; import hudson.util.FormValidation; import hudson.util.Secret; import jenkins.model.Jenkins; +import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; -import java.util.Objects; - @SuppressWarnings("unused") // read resolved by extension plugins public class BlazemeterCredentialsBAImpl extends BaseStandardCredentials implements BlazemeterCredentials, StandardUsernamePasswordCredentials { @@ -104,48 +105,39 @@ public String getIconClassName() { return "icon-credentials-userpass"; } - public Boolean getAdministerStatus() { - return Objects.requireNonNull(Jenkins.getInstance()).hasPermission(Jenkins.ADMINISTER); - } - - public Boolean getManageCredentialsStatus() { - Jenkins jenkins = Objects.requireNonNull(Jenkins.getInstance()); - return jenkins.hasPermission(CredentialsProvider.CREATE) || - jenkins.hasPermission(CredentialsProvider.UPDATE) || - jenkins.hasPermission(CredentialsProvider.DELETE) || - jenkins.hasPermission(CredentialsProvider.MANAGE_DOMAINS) || - jenkins.hasPermission(CredentialsProvider.VIEW); - } - - public Boolean getProjectLevelCredentialsStatus() { - hudson.model.User currentUser = Objects.requireNonNull(hudson.model.User.current()); - return currentUser.hasPermission(CredentialsProvider.CREATE) || - currentUser.hasPermission(CredentialsProvider.UPDATE) || - currentUser.hasPermission(CredentialsProvider.DELETE) || - currentUser.hasPermission(CredentialsProvider.MANAGE_DOMAINS) || - currentUser.hasPermission(CredentialsProvider.VIEW); - } - - public Boolean isPrivilegedUser() { - return getAdministerStatus() || getManageCredentialsStatus() || getProjectLevelCredentialsStatus(); + public Boolean checkPermissions(AccessControlled aclHolder) { + return aclHolder.hasPermission(CredentialsProvider.CREATE) || + aclHolder.hasPermission(CredentialsProvider.UPDATE) || + aclHolder.hasPermission(CredentialsProvider.DELETE) || + aclHolder.hasPermission(CredentialsProvider.MANAGE_DOMAINS) || + aclHolder.hasPermission(CredentialsProvider.VIEW); } public FormValidation doValidate(@QueryParameter("username") final String username, - @QueryParameter("password") final String password) { - String decryptedPassword = Secret.fromString(password).getPlainText(); + @QueryParameter("password") final String password, + @AncestorInPath ModelObject context) { + + // Maybe a Folder + // Maybe be null in which case default to root Jenkins + AccessControlled aclHolder = context instanceof AccessControlled + ? (AccessControlled) context + : Jenkins.getInstance(); + + if(aclHolder == null) { + return FormValidation.ok(); + } + + checkPermissions(aclHolder); + try { - if (isPrivilegedUser()) { - JenkinsBlazeMeterUtils utils = BlazeMeterPerformanceBuilderDescriptor.getBzmUtils(username, decryptedPassword); - User.getUser(utils); - return FormValidation.ok("Successfully validated credentials."); - } else { - return FormValidation.error("You don't have required privileges to add/update credentials."); - } + JenkinsBlazeMeterUtils utils = BlazeMeterPerformanceBuilderDescriptor.getBzmUtils(username, + Secret.fromString(password).getPlainText()); + User.getUser(utils); + return FormValidation.ok(); } catch (Exception e) { return FormValidation.error(e.getMessage()); } } - } } \ No newline at end of file diff --git a/src/main/resources/hudson/plugins/blazemeter/BlazemeterCredentialsBAImpl/credentials.jelly b/src/main/resources/hudson/plugins/blazemeter/BlazemeterCredentialsBAImpl/credentials.jelly index 17ca1d08..30bc92a9 100644 --- a/src/main/resources/hudson/plugins/blazemeter/BlazemeterCredentialsBAImpl/credentials.jelly +++ b/src/main/resources/hudson/plugins/blazemeter/BlazemeterCredentialsBAImpl/credentials.jelly @@ -13,60 +13,19 @@ limitations under the License. --> - - - - - - - - - - - - - + + + + + + + + + - - - - - - - -

You don't have required privileges to add/update credentials.

- -
- - +
\ No newline at end of file