留言板

Liferay 6.1 templates (FreeMarker)

Traolly Xiong,修改在8 年前。

Liferay 6.1 templates (FreeMarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
Hello community,
Can anyone provide the good api code snippet within template freemarker to retrieve Active Directory groups associated to users within Liferay?

Also any other good features that can be done with freemarker or limitations would be great to know.

Much appreciated.

Thanks.
thumbnail
David H Nebinger,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Wow, no I don't think you're going to get much help there.

FM is used a lot for templating the portal pages, but you're not going to get much support for instantiating an LDAP connection with credentials to perform a query and retrieve results for display in FM...
Traolly Xiong,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
Hello David,
I didn't mean to establish a LDAP connection, but for when AD users along with their groups are imported into Liferay, does
templates have access to a set of apis to retrieve those info about the AD user from the Liferay db?

Hope that makes sense.

Thanks.
thumbnail
David H Nebinger,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
When ldap users and groups are both imported, they become true Liferay users and groups. So sure, you can access users and groups.

Sorry if I'm not following what you're going for here, but mixing FM and LDAP makes little sense, they are independent of each other completely.
Traolly Xiong,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
Cool so from what you are saying AD user groups imported into Liferay can be retrieved using the users and groups services apis.
That should do the trick as the templates allow number of instantiating of service objects to retrieve info from Liferay.

Again given that AD users / groups are imported in Liferay, the goal is to retrieve those info within Liferay templates.

We'll give it a go.

Thanks.
thumbnail
David H Nebinger,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
You have to have ldap configured to import users and groups, but otherwise from Liferay's perspective they're like any other user/group.
thumbnail
Jack Bakker,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Liferay Master 帖子: 978 加入日期: 10-1-3 最近的帖子
I don't know of a way to identify Liferay users as having come from any LDAP (like AD) as opposed to having been just entered directly into Liferay.
thumbnail
Jack Bakker,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Liferay Master 帖子: 978 加入日期: 10-1-3 最近的帖子
Jack Bakker:
I don't know of a way to identify Liferay users as having come from any LDAP (like AD) as opposed to having been just entered directly into Liferay.


PortalLDAPImporterImpl might be extended to set a user True/False custom field to flag if user came from LDAP.
thumbnail
David H Nebinger,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Jack Bakker:
PortalLDAPImporterImpl might be extended to set a user True/False custom field


Eek! Expandos!
Traolly Xiong,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
Hey guys,
Do you know if it's possible to extend template functionality to use custom classes or code outside it's scope?

Thanks.
thumbnail
David H Nebinger,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Yes, but with restrictions. The FM templates run within the portal context and the portal's class loader, so anything that you want to use would need to be visible in that scope.

For velocity it is sometimes necessary to inject objects into the velocity context for them to be available in FM templates; while I don't know for certain the same thing exists for FM, I would put my money on the side that feels it would be there.

If you want I could do some research there, but it's really not much more than checking how Liferay prepares for the invocation of the FM templating engine to see how it prepares the environment, so it's certainly something that would be within your own reach...
Traolly Xiong,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
When you say "Yes, but with restrictions" and knowing that FM is within the portal context and portal class loaders, do you mean other than the objects available on the left hand side of the template editor, one can explicitly initialize other objects (within the portal context) and use it within the templates? And what restrictions would they have exactly? And does this "Yes" mean FM can access custom classes with custom portlet plugins if done right?

With my knowledge with Freemarker and my Liferay skill set it would probably take me a little longer to figure that out. If you can point
me in the right direction with some tips I can go from there. I think this would be very helpful a lot of other developers who work
Liferay templates.

Thanks.
thumbnail
Jack Bakker,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Liferay Master 帖子: 978 加入日期: 10-1-3 最近的帖子
I recently had to get at com.liferay.portal.theme.NavItem in a velocity template and in portal-ext.properties had to add the following to allow access to what otherwise might have been restricted by default. For access from freemarker is similar.

velocity.engine.restricted.classes=
velocity.engine.restricted.variables=
freemarker.engine.restricted.classes=
freemarker.engine.restricted.variables=

Above four lines are really to allow granular control over what velocity or freemarker might have access to.
Traolly Xiong,修改在8 年前。

RE: Liferay 6.1 templates (FreeMarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
Thanks. In case others are in need, below a snippet on how it can be done with a function.

<#assign UserGroupLocalService = serviceLocator.findService("com.liferay.portal.service.UserGroupLocalService") />
<#assign userGroups = user.getUserGroupIds() />

<#-- function to see if user is associated with a certain user group -->
<#function validateGroup groupToCheck>
<#assign yesOrNo = "0" />
<#list userGroups as uGroup>
<#attempt>
<#assign userGroup = UserGroupLocalService.getUserGroup(uGroup) />
<#assign userGroupName = userGroup.getName() />
<#if userGroupName?contains(groupToCheck)>
<#assign yesOrNo = "1" />
<#return yesOrNo>
</#if>
<#recover>
recover: User not associated with user group<br>
</#recover>
</#list>
<#return yesOrNo>
</#function>