Shailendra Soni
Shailendra Soni

Reputation: 170

How to get Jenkins logged user

I have requirement to get logged user id information into Jenkins plugins.

I have created HelloWorldclass which extend hudson.tasks.Builder class for creating plugin. From this class, I am trying to get logged user in perform method.

I have tried various option but could not get logged user, every options return SYSTEM as user id.

User.current() :- Return SYSTEM on Jenkins console but on jenkins script console gives me perfect result but same code does not have me logged user information.

Yet, while I'm hunting around, I can see that the user id is always displaying in the top right corner of the UI, taunting me since it is the information I want, but seemingly not available.

Can anybody help to solve this problem.

Upvotes: 7

Views: 9325

Answers (3)

ayakout
ayakout

Reputation: 59

Using the json api data you can lookup the userName from the hudson.model.CauseAction

buildInfo = new groovy.json.JsonSlurper().parseText("${BUILD_URL}api/json".toURL().text)
for(element in buildInfo.actions) {
    if(element._class == "hudson.model.CauseAction") {
        println(element.causes[0].userName)
    }
}

Upvotes: 0

Shailendra Soni
Shailendra Soni

Reputation: 170

I think that is jenkins issue. If you class extends hudson.tasks.Builder then it won't give you logged use information by invoking method User.Current() instead it will give you SYSTEM user. But if you invoke same method from other classes which should be extend Jenkins class then it may get you logged user.

In my case, I have created another class which extend hudson.model.ManagementLink and annotated with hudson.Extension annotation. In this class User.Current() give me logged user.

Upvotes: 1

Gerold Broser
Gerold Broser

Reputation: 14762

User.current() gets you an User object representing the current user on which, if printed in Jenkins' Script Console, toString() is invoked. Which in turn most probably invokes getFullName() (or getDisplayName() – didn't check that in code and it's neither obvious from the API, it lacks proper description, nor the Manage User UI, since there is just one field: Full name).

User.current().getId() gets you the user's ID.

See hudson.model.User.

Upvotes: 0

Related Questions