I want to give user access for parameters in Jenkins basused on user role.
so I am using Role-based Authorization Strategy and Active Choices parameter plugins and trying to do this and I successfully gave user access for parameters based on user name but not based on user roles.
I want to do the same based on user roles.
import jenkins.model.Jenkins
import hudson.model.User
// Get the current user
User currentUser = User.current()
// Get the username of the current user
String currentUsername = currentUser != null ? currentUser.fullName : null
// Define the available environments based on usernames
def envChoices = []
if (currentUsername == "DevUser1") {
envChoices += "DEV"
}
if (currentUsername == "QAUser1") {
envChoices += "QA"
}
// Check if the user has the role "admin" to list all parameters (DEV, QA, PROD) if (currentUsername == "Admin") { envChoices += ["DEV", "QA", "PROD"]
}
return envChoices
Also how to list user roles in Jenkins groovy script console does Role-based Authorization Strategy plugin have variables? I could not find it.
There is no method available that lists all roles a user belongs to.
You would also need to check if any of the groups a user belongs is in the admin role to be 100% correct. currentUser.getAuthorities() gives you the list of groups the user belongs to.
latest version of rolestategy plugin has 2 steps for pipeline jobs that allow to fetch the roles of the user that started the build. See Role-based Authorization Strategy