SpringSecurity: ifAnyGranted roles as property - jsf

how is it possible to pass the allowed roles as property?:
<sec:ifAnyGranted roles="#{item.allowedRolesToRender}">
Where Item is not a bean but the var of an dataList:
<rich:dataList value="${handler.itemlist}" var="item"
I tried to return array/comma-separated-string/list but it seems the the get method is never called. And i always get:
com.sun.facelets.FaceletException: roles must be given
at org.springframework.security.taglibs.facelets.IfAnyGrantedTag.apply(IfAnyGrantedTag.java:41)
Thanks

Use like this.
<sec:authorize ifAnyGranted="#{item.allowedRolesToRender}">
And allowedRolesToRender should be given as a comma-separated list of strings
Reference

Related

Load bundle string (in right language) when value depends on bean condition

I have a Problem with jsf and multiple languages. So my strings are in WEB_INF/classes/texte_<lang>.properties files. And are accessed for example like that
<h:outputLabel value="#{messages.err_text}"/>
which works fine.
The problem is, i have <h:outputLabel... element where i want to show an error message depending on the error. I would like something that works like this:
<h:outputLabel value="#{someBean.errMsg}/>
With a Bean like that
#ManagedBean()
#SessionScoped
public class SomeBean{
public String getErrMsg(){
if(something){
return "#{messages.err_text}"
}else if(somethingElse){
return "#{messages.err_text2}"
}else{
return "#{messages.err_text3}"
}
}
}
Just to be clear it doesn't work that way. I'm looking for a similar solution (or any solution) that works.
Thanks for reading.
Don't do it that way. The model shouldn't be aware of the view. Localization also doesn't strictly belong in the model. The model should instead prepare some state which the view has to be aware of.
One way would be:
public String getErrMsg(){
if (something) {
return "err_text";
} else if (somethingElse) {
return "err_text2";
} else {
return "err_text3";
}
}
<h:outputLabel value="#{messages[someBean.errMsg]}" />
Other way would be returning an enum as demonstrated in the following related questions: Localizing enum values in resource bundle and How to use enum values in f:selectItem(s).
The reason why what you have now is not working, is because the value attribute of the outputText is evaluated as a plain String, and not as an EL expression.
Going by what you are working with now, the best way to proceed is to inject the resource bundle directly into your bean:
#ManagedProperty("#{messages}")
ResourceBundle messages;
And then,
public String getErrMsg(){
if(something){
messages.getString("err_text");
}
}
In case you're not aware, traditionally, error messages are presented using the h:message component.
On an unrelated note to your original question, you should also know that it's not generally advisable to have processing logic buried in your getter. For one thing, the getter is called multiple times during the rendering of your page. Also for this to work properly, you should be able to guarantee that the value something will stay consistent across the entire lifecycle of a single JSF request

How can i assign Mule Variable Value to Property Key -Value lookup?

I want to find out the careerName from variable
I want to use that CareerName as Property Key. Example. If careerName came-up as apple, I have value setup against that key apple=ST\*214|ST\*210.
I have following line of code for Mule Choice Expression, which i tried with but i am not getting success here.
mule-esb.test1.properties
ftp.inbound.carriers.path='CareerName1/InBound/','CareerName2/InBound/','CareerName3/InBound','CareerName4/InBound/','apple/InBound/'
CareerName1=ST\*214|ST\*210
CareerName2=ST\*214|ST\*210
CareerName3=.\ST.214.\
CareerName4=ST\*214
apple=ST\*214
<context:property-placeholder location="mule-esb.${mule.env}.properties" />
<when expression="import java.util.regex.Pattern;Pattern p = Pattern.compile('${'+message.getInvocationProperty('careerName')+'}');return p.matcher(payload.toString()).find();" evaluator="groovy">
Looking for some alternatives or solution on this script.
MEL has extensive regex support, you shouldn't need to use Groovy. See: http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+Tips#MuleExpressionLanguageTips-RegexSupport
You need to load your properties in an Map that you can query from the registry but also use in the property placeholder resolver. So do this:
<util:properties id="configProperties"
location="classpath:mule-esb.${mule.env}.properties" />
<context:property-placeholder properties-ref="configProperties" />
With this in place, the following should work:
<when expression="#[regex(app.registry.configProperties[careerName])]">
define a spring bean globally configuring the key value pair properties for the bean.
the bean definition should accept the spring properties and a method which accept the key and returns the corresponding value.
sample bean definition as follows
<spring:bean id="entityMapper" name="entityMapper" class="com.xx.xx.commons.ClassNameXX">
<spring:property name="entities">
<spring:props>
<spring:prop key="CareerName1">${CareerName1}</spring:prop>
.
.
</spring:props>
</spring:property>
</spring:bean>
so in the flow level you can get value from bean with below expression.
#[app.registry.entityMapper.getEntity(message.getInvocationProperty('careerName'))]
where entityMapper will be the bean name and getEntity is the method defined in bean which accept the careerName and return the corresponding value.
hope this helps.
Dynamically you cant access the value directly from context placeholder.

Access value from a Map where Key is Enum Type in JSF Facelets

I have a Map defined in my controller as follows
Map<AddressTypeEnum,Address> addressMap = new HasMap<AddressTypeEnum,Address>();
where AddressTypeEnum is a Enum type with values as HOME,WORK
in facelets, I was trying to access the field of Address as follows
<p:inputText value="#{controller.addressMap['HOME'].addressLine1}"/>
It gives me error target not reachable as null. Is there any thing wrong with my facelets code
Thanks in advance
I have resolved the issue as follows.. May be useful to others
I used Ominifaces from BaluC
In the facelets page, I added the follwing line
<o:importConstants type="com.eplm.chits.entities.common.AddressTypeEnum" var="addressType"/>
Then I used
<p:inputText id="homeId" value="#{chitUserController.address[addressType.HOME].addressLine1}"/>
EL doesn't have built-in functionality to work with Enums. You can only test if Enum-typed variable holds some value like in: rendered="#{enumVar=='HOME'}", because in this case implicit call to enumVar.toString() is executed.
Probably, using Enum as the key for your Map, you want to enforce compile-time checking of your keys. You could use Map<String, Address> and addressMap.put(AddressTypeEnum.HOME.toString(), someAddress) instead.
From facelets template you then access addressMap either with
#{controller.addressMap['HOME'].addressLine1}
or with
#{controller.addressMap.HOME.addressLine1}.
Another solution would be to introduce controller method, as #sleske advises, which takes String parameter, gets Enum instance for this String and gets the value from the map:
Map<AddressTypeEnum, Address> addressMap;
...
public Address getAddress(String type) {
AddressTypeEnum addressType = AddressTypeEnum.valueOf(type);
return addressMap.get(addressType);
}
...and use it with #{controller.getAddress('HOME').addressLine1}
UPD: There is three-years-old StackOverflow question on the same problem: Passing a Enum value as a parameter from JSF (revisited)
If you're using Primefaces, it is possible to use the <p:importEnum /> tag, as described in: Primefaces's showcase:
<p:importEnum type="javax.faces.application.ProjectStage"
var="JsfProjectStages"
allSuffix="ALL_ENUM_VALUES"/>
<h:outputText value="#{JsfPRojectStages.Key}" />

How to obtain Role in Liferay Themes?

How to obtain Role of a logged in User in Liferay Themes? How to check if User belongs to a particular role?
UserLocalService has hasRoleUser method which can be used to find out if User belongs to a particular role.
Below code can be put in navigation.vm file under templates folder.
#set($UserLocalServiceUtil = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#if ($UserLocalServiceUtil.hasRoleUser(roleID, $user.getUserId())) // It takes roleID as input to check.
//Proceed with whatever you want to
#else
//Proceed with something else
Note: Instead of com.liferay.portal.service.UserLocalService, if you use com.liferay.portal.service.UserLocalServiceUtil,
as might be found in some resources like this then you will encounter below exception,
ERROR com.liferay.portal.kernel.bean.BeanLocatorException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'com.liferay.portal.service.UserLocalServiceUtil' is defined
com.liferay.portal.kernel.bean.BeanLocatorException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.liferay.portal.servi
ce.UserLocalServiceUtil' is defined
Another way is,
#set($role=$serviceLocator.findService("com.liferay.portal.service.RoleLocalService"))
$role.getUserRoles($user_id)
Just loop through the $user object defined in init.vm
#set ($user_roles = $user.getRoles())
#foreach($role in $user_roles)
$role.name<br />
#end

I can call my function in java bean but not using get and set

I have created a simple bean using this example from Per
http://per.lausten.dk/blog/2012/02/creating-your-first-managed-bean-for-xpages.html
And I can access the bean using
helloWorld.setSomeVariable("test") and
get the value using helloWorld.getSomeVariable()
But when I try it using EL language it doesn't work
helloWorld.SomeVariable
I get an error that SomeVariable doesn't exists in helloWorld
I'm probably doing some simple error, but I can't see what.
I believe the problem is that EL is likely case-sensitive the bean-style name of the property is "someVariable". Beans assume that you're using camel-case for the method names, so EL downcases the first letter after "set" and "get" for their translation.
EL seems to be case sensitive in my regards and your property is defined as someVariable and not SomeVariable. have you tried helloWorld.someVariable.

Resources