tags:

views:

1199

answers:

2

I've written some code that checks to see if the user is logged in as an admin or not to limit who can make changes to other user's permissions, but the if tag is never true. I've made sure the correct data is there by echoing it out but nothing I've found so far has given me a solution.

<g:if test="${session.userPermissions == 'Administrator'}">
    <g:select id="permissions" name="permissions" from="${userInstance.constraints.permissions.inList}" value="${userInstance.permissions}" ></g:select>
</g:if>
<g:else>
    ${userInstance.permissions}
</g:else>
A: 

It ends up a groovy thing-

I used a criteria to get the user's login information. The criteria returns a list of user variables. I was setting session.userPermissions = user.permissions

In groovy you can access every list element's properties and retrieve them as a new list (example below). This was giving me the list variables in my session. It's a really cool feature, but not one you like to find on accident like this.

groovy> def demo = []
groovy> demo[0] = [a:1, b:2]
groovy> demo[1] = [a:3, b:4]
groovy> demo[2] = [a:5, b:6]
groovy> demo.a

Result: [1, 3, 5]
Noaki
Yep, actually this syntax is a shortcut for demo.collect { it.a }. This also works for method invocations on the list's elements: list*.myMethod() returns a list containing the return values of myMethod() invoked an all elements of the original list (so-called spread-dot operator). The collect-equivalent would be: list.collect { it.myMethod() }
Daniel Rinser
this could be a cause of hard to track bugs...hhmmm not great.
Chii
+3  A: 

try "java" String ways :D

<g:if test="${session.userPermissions.equals('Administrator')}">
....
</g:if>
nightingale2k1
Thanks for this suggestion! I was having the exact same problem in my .gsp files where two strings (or at least I thought were strings) x and y that would display equally as ${x} and %{y} would fail the == test. Using String.equals solved the problem and the mystery.
ראובן