Quantcast

Jelly views: Avoid displaying config section depending on user permissions

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Jelly views: Avoid displaying config section depending on user permissions

Nicky Ramone
Hi

In a plugin I'm writing, I need to hide a textbox in the project configuration depending on the user permission.
My @DataBoundConstructor is in a Notifier class. How can I retrieve the user permissions?

Thanks.
Cheers.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Jelly views: Avoid displaying config section depending on user permissions

Jesse Glick-4
On 08/15/2012 11:53 AM, Nicky Ramone wrote:
> In a plugin I'm writing, I need to hide a textbox in the project configuration depending on the user permission.
> My @DataBoundConstructor is in a Notifier class. How can I retrieve the user permissions?

"Making your plugin behave in secured Jenkins" [1] discusses h.hasPermission which I think is what you want.

[1] https://wiki.jenkins-ci.org/display/JENKINS/Making+your+plugin+behave+in+secured+Jenkins
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Jelly views: Avoid displaying config section depending on user permissions

Nicky Ramone
Thanks , it's what I was looking for.
However, I still don't understand the following snippet in the example found in your link:
<j:if test="${h.hasPermission(app.ADMINISTER)}">
  ...
</j:if>

I'm not clear on what 'app' is, although it looks like the Jenkins object.
I've tried doing this:
<j:if test="${h.hasPermission(it.myCustomPermission)}">
  ...
</j:if>
And I have defined the getMyCustomPermission() method, but it doesn't work.
Maybe the 'it' variable is not correctly parsed when being inside ${h...}?

On Wed, Aug 15, 2012 at 3:25 PM, Jesse Glick <[hidden email]> wrote:
On 08/15/2012 11:53 AM, Nicky Ramone wrote:
In a plugin I'm writing, I need to hide a textbox in the project configuration depending on the user permission.
My @DataBoundConstructor is in a Notifier class. How can I retrieve the user permissions?

"Making your plugin behave in secured Jenkins" [1] discusses h.hasPermission which I think is what you want.

[1] https://wiki.jenkins-ci.org/display/JENKINS/Making+your+plugin+behave+in+secured+Jenkins

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Jelly views: Avoid displaying config section depending on user permissions

Jesse Glick-4
On 08/15/2012 03:25 PM, Nicky Ramone wrote:
> I'm not clear on what 'app' is, although it looks like the Jenkins object.

Right.

> I have defined the getMyCustomPermission() method, but it doesn't work.
>
> Maybe the 'it' variable is not correctly parsed when being inside ${h...}?

It ought to work. 'it' will be bound to whatever object is the "subject" of the page, so make sure getMyCustomPermission is defined in the right class. Try inserting some
debugging fragments:

it=<j:out value="${it}"/>
it.myCustomPermission=<j:out value="${it.myCustomPermission}"/>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Jelly views: Avoid displaying config section depending on user permissions

Nicky Ramone
This is the result of the output with the debugging lines:
This is the result of the output with the debugging lines:
it=hudson.model.FreeStyleProject@3d2a58[dummy2]
it.myCustomPermission=

I'm kinda lost. I thought 'it' was supposed to be an instance of MyCustomNotifier.
So that's why it can't find the method I added to MyCustomNotifier.
How can I refer my own Permission object from within jelly?



On Wed, Aug 15, 2012 at 4:30 PM, Jesse Glick <[hidden email]> wrote:
On 08/15/2012 03:25 PM, Nicky Ramone wrote:
I'm not clear on what 'app' is, although it looks like the Jenkins object.

Right.


I have defined the getMyCustomPermission() method, but it doesn't work.

Maybe the 'it' variable is not correctly parsed when being inside ${h...}?

It ought to work. 'it' will be bound to whatever object is the "subject" of the page, so make sure getMyCustomPermission is defined in the right class. Try inserting some debugging fragments:

it=<j:out value="${it}"/>
it.myCustomPermission=<j:out value="${it.myCustomPermission}"/>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Jelly views: Avoid displaying config section depending on user permissions

Jesse Glick-4
On 08/15/2012 03:52 PM, Nicky Ramone wrote:
> it=hudson.model.FreeStyleProject@3d2a58[dummy2]
>
> I'm kinda lost. I thought 'it' was supposed to be an instance of MyCustomNotifier.

If you are on the configure screen for a project, then 'it' would be the project. In general there _is_ no MyCustomNotifier instance yet when the configuration screen is
loaded - the user might be configuring your notifier for the first time on this web page! (In which case MyCustomNotifier is instantiated for the first time after they
click Save.) Confusing if you are used to Swing GUIs and the like, but makes sense in the context of a web app.

<j:invokeStatic .../> can be used to load the Permission as a static field from MyCustomNotifier.class.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Jelly views: Avoid displaying config section depending on user permissions

Nicky Ramone
Cool. I'll try to do it with invokeStatic until I can find a neater way.
Thanks very much for your help!
Regards.

On Wed, Aug 15, 2012 at 5:05 PM, Jesse Glick <[hidden email]> wrote:
On 08/15/2012 03:52 PM, Nicky Ramone wrote:
it=hudson.model.FreeStyleProject@3d2a58[dummy2]


I'm kinda lost. I thought 'it' was supposed to be an instance of MyCustomNotifier.

If you are on the configure screen for a project, then 'it' would be the project. In general there _is_ no MyCustomNotifier instance yet when the configuration screen is loaded - the user might be configuring your notifier for the first time on this web page! (In which case MyCustomNotifier is instantiated for the first time after they click Save.) Confusing if you are used to Swing GUIs and the like, but makes sense in the context of a web app.

<j:invokeStatic .../> can be used to load the Permission as a static field from MyCustomNotifier.class.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Jelly views: Avoid displaying config section depending on user permissions

stephenconnolly
In reply to this post by Jesse Glick-4
On 15 August 2012 21:05, Jesse Glick <[hidden email]> wrote:
On 08/15/2012 03:52 PM, Nicky Ramone wrote:
it=hudson.model.FreeStyleProject@3d2a58[dummy2]


I'm kinda lost. I thought 'it' was supposed to be an instance of MyCustomNotifier.

If you are on the configure screen for a project, then 'it' would be the project.

${instance} should be the instance of your notifier or null if not reconfiguring
${descriptor} should be the descriptor of your notifier, so just stick the getter in the DescriptorImpl and you should be fine, no need for j:invokeStatic
 
In general there _is_ no MyCustomNotifier instance yet when the configuration screen is loaded - the user might be configuring your notifier for the first time on this web page! (In which case MyCustomNotifier is instantiated for the first time after they click Save.) Confusing if you are used to Swing GUIs and the like, but makes sense in the context of a web app.

<j:invokeStatic .../> can be used to load the Permission as a static field from MyCustomNotifier.class.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Jelly views: Avoid displaying config section depending on user permissions

Jesse Glick-4
On 08/15/2012 05:51 PM, Stephen Connolly wrote:
> ${instance} should be the instance of your notifier or null if not reconfiguring
> ${descriptor} should be the descriptor of your notifier, so just stick the getter in the DescriptorImpl and you should be fine, no need for j:invokeStatic

Thanks for clarifying. I added some tips to the wiki [1] since without static typing it is pretty hard to figure out what is possible in Jelly files.

[1] https://wiki.jenkins-ci.org/pages/diffpages.action?pageId=37323473&originalId=63145441
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Jelly views: Avoid displaying config section depending on user permissions

Nicky Ramone
Stephen, thanks very much to you too. I did it with DescriptorImpl and I'm fine now.

On Wed, Aug 15, 2012 at 7:08 PM, Jesse Glick <[hidden email]> wrote:
On 08/15/2012 05:51 PM, Stephen Connolly wrote:
${instance} should be the instance of your notifier or null if not reconfiguring
${descriptor} should be the descriptor of your notifier, so just stick the getter in the DescriptorImpl and you should be fine, no need for j:invokeStatic

Thanks for clarifying. I added some tips to the wiki [1] since without static typing it is pretty hard to figure out what is possible in Jelly files.

[1] https://wiki.jenkins-ci.org/pages/diffpages.action?pageId=37323473&originalId=63145441

Loading...