Wednesday, October 1, 2014

Working with Liferay User Roles


Liferay  have different types of roles for user. So whenever we develop portlet application we may get need to fetch user roles.
 
The following article will give you more about Liferay Roles


Generally we have following roles in Liferay
  1. Regular Roles/Portal Roles
  2. Organization Roles
  3. Site Roles
  4. Inherited Roles
Portal Role/Regular Roles

Liferay is providing Portal Role/Regular Role for portal level. It’s not specific to anything like Organization, Site or User Group. 

This role can be assigned to any user who belongs to any one of Organization, Community/Site or User Group.

Generally when we associate Portal Role/Regular Role s to any user then the association can be stored in Users_Roles Mapping Table.

To fetch Portal Role/Regular Role we can use RoleLocalServiceUtil.java class and these classes have many service methods which can fetch roles with respect to selected user.

We can use following ways to fetch user Portal Role/Regular Roles

Use RoleLocalSercviceUtil


List<Role> userRoles=RoleLocalServiceUtil.getUserRoles(themeDisplay.getUserId());


Use User Object 
 

List<Role> userRoles1=themeDisplay.getUser().getRoles();


Site Roles 

Site Role is one of the types in liferay which is only associated to Site users. This role only we can associate to site users. When we create site role we can use this site role to any used who belongs to any site liferay.

When we associate any site roles to user then association will be stored in UserGroupRole table.When ever we want get site roles then we have to use respective service class to access those roles like we need use UserGroupRoleLocalService.java class there we can find many service methods.


List<UserGroupRole> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(themeDisplay.getUserId());
List<UserGroupRole> siteRoles = new ArrayList<UserGroupRole>();
for (UserGroupRole userGroupRole : userGroupRoles) {
int roleType = userGroupRole.getRole().getType();
if (roleType == RoleConstants.TYPE_SITE) {
siteRoles.add(userGroupRole);
}
}


Organization Roles

Similar to site role organization role is used for organization users. This role can be associated to any user who belongs to any organization in portal.

When we associate any Organization roles to user then association will be stored in UserGroupRole table.When ever we want get Organization roles then we have to use respective service class to access those roles like we need use UserGroupRoleLocalService.java class there we can find many service methods.


List<UserGroupRole> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(themeDisplay.getUserId());
List<UserGroupRole> organizationRoles = new ArrayList<UserGroupRole>();
for (UserGroupRole userGroupRole : userGroupRoles) {
int roleType = userGroupRole.getRole().getType();
if (roleType == RoleConstants.TYPE_ORGANIZATION) {
organizationRoles.add(userGroupRole);
}
}



Inherited Roles

Inherited roles really not existed in the liferay but we can see these roles in the user my account page roles section .these roles specially appear when the use can be member of user group which is assigned a role.

We can say if any roles which associates with User Group and the user is member of respective user group then role can be visible as part of inherited roles section.

Simply we can say that user directly not associated with role instead of that User Group will be associated with role and the user will be member of User Group then the roles are become as inherited role to users who are belong to User Group.

Understanding inherited roles
  • Create User Group (Photography) and assign Users to Photography User Group
  • Create role called Photography Group Member
  • Assign User Group to Role( associate role to User Group)

Now all users who are belongs to User Group will be get Photography Group Member role as inherited role when we observe here Photography Group Member role is not directly associated with user but role is associated with Photography user group because of this Photography Group Member become as inherited role.

When we want fetch inherited roles first we need to find all User Groups of respective user so that we can fetch all inherited roles.


<%
User selUser=themeDisplay.getUser();
List<Group> allGroups = new ArrayList<Group>();
List<UserGroup> userGroups = selUser.getUserGroups();
List<Group> groups = selUser.getGroups();
List<Organization> organizations = selUser.getOrganizations();
allGroups.addAll(groups);
allGroups.addAll(GroupLocalServiceUtil.
getOrganizationsGroups(organizations));
allGroups.addAll(GroupLocalServiceUtil.
getOrganizationsRelatedGroups(organizations));
allGroups.addAll
(GroupLocalServiceUtil.getUserGroupsGroups(userGroups));
allGroups.addAll(GroupLocalServiceUtil.
getUserGroupsRelatedGroups(userGroups));
for(int i=0;i<allGroups.size();i++){
com.liferay.portal.model.Group group=allGroups.get(i);
List<Role> groupRoles = RoleLocalServiceUtil.getGroupRoles(group.getGroupId());
if (!groupRoles.isEmpty()) {
Role groupRole = groupRoles.get(0);
out.println(ListUtil.toString(groupRoles,
 Role.NAME_ACCESSOR));
}
}
%>


Note:

When we work with above code respective Java classes should be import.

The following is sample code snippets which is in JSP page


<%@page import="com.liferay.portal.kernel.util.ListUtil"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Organization"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="com.liferay.portal.model.UserGroup"%>
<%@page import="com.liferay.portal.model.Group"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.liferay.portal.model.RoleConstants"%>
<%@page import="com.liferay.portal.service.UserGroupRoleLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.UserGroupRole"%>
<%@page import="com.liferay.portal.model.Role"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.service.RoleLocalServiceUtil"%>
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<%
List<Role> userRoles=RoleLocalServiceUtil.getUserRoles(themeDisplay.getUserId());
List<Role> userRoles1=themeDisplay.getUser().getRoles();
for (Role role : userRoles) {
out.println(role.getName());
}
%>
<%
List<UserGroupRole> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(themeDisplay.getUserId());
List<UserGroupRole> organizationRoles = new ArrayList<UserGroupRole>();
for (UserGroupRole userGroupRole : userGroupRoles) {
int roleType = userGroupRole.getRole().getType();
if (roleType == RoleConstants.TYPE_ORGANIZATION) {
organizationRoles.add(userGroupRole);
out.println(userGroupRole.getRole().getName());
}
}
%>
<%
List<UserGroupRole> userGroupRoles1 = UserGroupRoleLocalServiceUtil.getUserGroupRoles(themeDisplay.getUserId());
List<UserGroupRole> siteRoles = new ArrayList<UserGroupRole>();
for (UserGroupRole userGroupRole : userGroupRoles1) {
int roleType = userGroupRole.getRole().getType();
if (roleType == RoleConstants.TYPE_SITE) {
siteRoles.add(userGroupRole);
out.println(userGroupRole.getRole().getName());
}
}
%>
<%
User selUser=themeDisplay.getUser();
List<Group> allGroups = new ArrayList<Group>();
List<UserGroup> userGroups = selUser.getUserGroups();
List<Group> groups = selUser.getGroups();
List<Organization> organizations = selUser.getOrganizations();
allGroups.addAll(groups);
allGroups.addAll(GroupLocalServiceUtil.
getOrganizationsGroups(organizations));
allGroups.addAll(GroupLocalServiceUtil.
getOrganizationsRelatedGroups(organizations));
allGroups.addAll(GroupLocalServiceUtil.
getUserGroupsGroups(userGroups));
allGroups.addAll(GroupLocalServiceUtil.
getUserGroupsRelatedGroups(userGroups));
for(int i=0;i<allGroups.size();i++){
com.liferay.portal.model.Group group=allGroups.get(i);
List<Role> groupRoles = RoleLocalServiceUtil.getGroupRoles(group.getGroupId());
if (!groupRoles.isEmpty()) {
Role groupRole = groupRoles.get(0);
out.println(ListUtil.toString(groupRoles,
 Role.NAME_ACCESSOR));
}
}
%>


Author

Monday, September 29, 2014

Why we need liferay in real web world / What are the main factors to choose liferay


Liferay is a portal technology which provides many features in the web world. Liferay portal developed in java technologies so that its pure java based portals.

Generally in any web application development we need to do many things when we start development from scratch.
Likewise we need to design system architecture, implement technologies, scalability, reliability, extensibility. So we need to take care all the above.

Assume we have some application already developed and if it requires customization and extensibility to add new features then we almost have application in our hand need to add changes for meeting the requirement, same thing liferay have done.

Liferay already developed portal application which covered almost all generalized webapplication needs so that we can use liferay portal to customize or extends the liferay portal according to our requirements.

As a developer we need to take care about reliability, scalability and security related things. In liferay we no need to worry for these as liferay already implemented all these features so that we can think only about our Customer requirements.

As we already know liferay is developed portlet technology which consists of multiple portlets to complete web page

Here portlet is fragment of dynamic content and each portal page have multiple portlets which loads dynamic content in the web browser to show complete web page.

The following are main reason to choose liferay in Real Web world

  • Ready to use Application
  • Easy customization and extending portal.
  • SAAS Based Portal
  • Robust Community
  • Open source CE and Enterprise Edition
  • Already developed Permission System and Security mechanisms
  • Support Multiple Server Environments and Operating Systems
  • Market Place for new portlet applications and Easy deployment
  • Robust Content Management System
  • Technology Flexibility in Development.


Ready to use Application:

Liferay is already developed potal application so we can simply installed and directly we can use it.
It already have more than 80 portlet applications so we can design our dynamic website with help of those portlet application.

Easy customization and extending portal :

Apart from generalized needs liferay also provides robust and easy mechanism to customize and extending portal so that we can full fill our requirement with help of customization or extending.
Liferay offers plugin mechanism so that we can customize or add new features to portal as per our needs. Customization achieved through liferay hook and ext plugins and extending portal achieved with help of plugin portlets.


SAAS Based Portal :

Liferay offers Software As A Service(SAAS) Mechanism so that we can install one portal instance but we can create any number of websites and each website has its own dynamic content and its own functionality. Liferay offers different ways to provide SAAS using Site, Organization and User Groups. Physically it is deployed in one application servers but virtually it seems to like many web applications. Each website has its own access point called domain so that as end user he always feels its individual website.


Robust Community :

Liferay has robus community so that we can get solutions for any challanges.Liferay providing forum discussion, wikis , blogs and documentation so that we can easyly expect solutions from community.There are many people are in the community.If any open source technologies we are going to use first we need to check community actiness. Liferay community is growing now days very rapidly.


Open source CE and Enterprise Edition :

Liferay is an open source technology so that anyone can use and experience the features. There are many people working for liferay community and releasing new version as per the new features they implemented. Liferay also provide Enterprise Edition, if anyone interested then they can purchase license too.

Already developed Permission System and Secuirty mechanisams :

Sometimes we need dynamic applications have different types of roles and permissions. This is one of the typical part in web application development but liferay already developed elegant permission system so that we can easily use it.

It is role based permission system and each user consist one or more roles and each role defied with set of permissions access the application accessibility against the user access.

Liferay also provides robust security mechanism and it also offer integrating with many of authentication implementation like Central Authentication Service(CAS)(Single Sign On And Single Sing Out) ,SAML(Security Assertion Markup Language) Integration and LDAP integration.


Support Multiple Server Environments and Operating Systems :

Liferay portal support multiple portal environments like Tomcat, Jboss, GlashFIsh, Weblogic and Websphear. And it also support any operating system like Windows and almost all Linux Flavours.


Market Place for new portlet applications and Easy deployment :

Liferay offers market place where we can get new portlet applications as for current needs in real web world. Liferay portal provides Admin Control panel where we can download and install new application from smooth user interface. So here any one can deploy portlet application without have development knowledge.


Robust Content Management System :

Liferay offer CMS features so that we can quickly design web content for page and update it. We have many features in CMS like structures and templates, work flow management.


Technolgy Flexibilty in Develoment :

To develop Liferay application, developer can choose any of the Java-J2EE technologies like Struts, Spring, JSF –etc or even we can implement with other technologies like PHP, vaadin, ruby, groovy –etc.

Note:
Apart from these liferay have many features. Please follow the liferay documentation so that we can understand more about liferay portal

http://www.liferay.com/documentation/liferay-portal/6.2/user-guide


Author



Thursday, June 26, 2014

Liferay 6.2 Web Content AUI Carousel Structure and Template / Image Slide Show

Introduction:

Liferay have very good content management system we can design and publish content in the web page very easy.

Along with that it’s also have structures and templates feature so that we can design common designs as structures and templates then we can reuse it many places with different data.

Same design if we want uses many places with different data then we will use structures and templates.

Liferay 6.2 structure templates have very user-friendly design to create structures and templates rather than liferay 6.1

But conceptually basic idea is same for liferay 6.2 and Liferay 6.1 but way of using they make simpler in Liferay 6.2 with very good User Interface.

Liferay 6.1 we have used velocity for coding in templates but in liferay 6.2 they have used free marker templates in addition velocity while designing templates and they have given very good UI and Template editor so that we can see all variables with respect to Structure which we have used for template.


Follow above tutorial then you can understand more about Structures and Templates in liferay web content. Same thing but way of usage is different in Liferay 6.2.


Objective:

We are going to create Carousel Image Slide show in website.

We will use AUI Carousel to design image slide show in liferay and we will use Web content 
Structures and templates feature to implement Image Slide Show


Assumptions:
  • In the image slide show we have many images so we able to upload or use many images
  • We need Slide show width and Height it should be configurable.
  • We need to specify time interval to change images so it should be configurable.
  • Each slide image have hyperlink when we click it should go to respective page and links should be configurable.

To consider all above now we will design structure and template to design web content.

Steps
  1. Create Structure
  2. Create Template and associate with Respective Structure
  3. Create web content by using Template
  4. Using Web content

Before start this we need to login as Admin

Liferay have default super admin so we need to login into liferay portal by following credentials


User Name:  test@liferay.com
Password:     test


Following is login screen




Once login as Admin go to Admin >> Site Administration


Once you go to Site Administration select Content Panel there you can click on web content



Create Structure

In the web content top navigation Manu you can see manage option menu there you can select 
structure link as follows




Once click on Structures then window pop up will be launch there you can enter your structure name and description


The following is Structure Creation Screen


In the bottom of pop up window you can see User Interface Design to Design Structure as follows



We have many controls so we can use all elements and we can design. Now we already have code for Image Slide show Structure so you simply click on source and in editor use following source code.


<root available-locales="en_US" default-locale="en_US">
<dynamic-element dataType="image" fieldNamespace="wcm" indexType="keyword" name="images" readOnly="false" repeatable="true" required="false" showLabel="true" type="wcm-image" width="">
<dynamic-element dataType="link-to-page" fieldNamespace="ddm" indexType="keyword" name="imagelink" readOnly="false" repeatable="false" required="false" showLabel="true" type="ddm-link-to-page" width="small">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Link to Page]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Image]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element dataType="number" fieldNamespace="ddm" indexType="keyword" name="containerwidth" readOnly="false" repeatable="false" required="false" showLabel="true" type="ddm-number" width="small">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Container Width]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element dataType="number" fieldNamespace="ddm" indexType="keyword" name="containerhight" readOnly="false" repeatable="false" required="false" showLabel="true" type="ddm-number" width="small">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Container Hight]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element dataType="number" fieldNamespace="ddm" indexType="keyword" name="intervaltime" readOnly="false" repeatable="false" required="false" showLabel="true" type="ddm-number" width="small">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Interval Time]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
</root>

The following screen after use source in editor



The following is Graphical view of Structure



Finally click on save button then your structure will be created


Finally close pop up window and go back to Web content Manage Screen

Create Template and associate with Respective Structure

Now we need create template and it will use above created structure. In the web content screen top navigation goes manage option menu there you can find Templates Link



Once we click on template then pop window will be open there we can see +Add to create new template.

Now we need to fill the name and description along with that we need select respective structure. In our case we already create structure i.e. Image Slide Show Structure we need to select that.

We already know we are using free market template so we need to select Language as Free Marker (ftl) and its default language, apart from that we can use velocity and XSLT



In the bottom of pop up you can see Template editor and its respective Structure variables /fields and other available variables was shown in left side.

In the structure we have used some fields all fields you can see left side as follows



We already have Free Marker Template code for Image Slide Show please use following code in Editor and save then template will be created.


<script>
AUI({ filter: 'raw' }).use('aui-carousel', function(A) {
new A.Carousel({
intervalTime:${intervaltime.getData()},
contentBox: '#myCarousel',
activeIndex:0,
height:${containerhight.getData()},
width:${containerwidth.getData()}
}).render();

});
</script>
<#if images.getSiblings()?has_content>
<div id="myCarousel">
<#list images.getSiblings() as cur_images>

<#if cur_images_index==0>
<a href="${cur_images.imagelink.getData()}">
<div class="carousel-item" style="background: url(${cur_images.getData()})width:${containerwidth.getData()}px; height:${containerhight.getData()}px;" class="carousel-item carousel-item-active";">
</div>
</a>
</#if>
<a href="${cur_images.imagelink.getData()}"> <div class="carousel-item" style="background: url(${cur_images.getData()});width:${containerwidth.getData()}px; height:${containerhight.getData()}px;" class="carousel-item""></div></a>
</#list>

</div>
</#if>



In the Template Design we use html and AUI Script to design template. And we will use stricture variable or fields to fill with dynamic content. These all design was made using Free Market Templates (FTL)

However use above template code in editor and finally save it then template will be create and it will be associated with one structure that is we created previously.

Note:

When we click in fields and variables in left side those will be added in the editor automatically where your cursor point in the editor. Templates editor is every flexible to code or design free marker templates in editor.

Each template should be associated with one structure and based in structure we need to code free market template to use structure variables and based on template design we will get input elements to design web content with dynamic data.

The following free marker template code in editor




Now finally save then your template will be created and it will be in the templates list as follows.



Now close pop up window and go back to web content screen.

Create web content by using Template

Well we are ready with Template and Structure and we will create web content using above template.

In the web content screen you can see +Add button option menu in the top navigation there you can see newly created template name that is Image Slide Show Template


Now you will get web content creation screen with selected template. We need to fill name for web content.

Based on template we need to fill the content and in our scenario we are using images element as repeatable so we can add more images by click + icon apposite to images file input element.

And we are using Height, Width and Interval we need to fill those values and it should be numbers and we can also select hyper link for each image


Once we fill the content in the web content template finally click on publish button then content will be available to use and it will be shown in web content list.


Using Web Content

We have done all steps now the content will be available to use. We will use web content display portlet to display web content in the pages.

Navigate to desirable page where you simply drag and drop web content display portlet in the page.

In the admin mode you can see left side + Icon click on the you can get the toggle there in the applications tab you can select Web content Display Portlet simply click on Add Link then blank web content display portlet will be added to page.


In the blank web content display portlet you can see toggle control in the bottom.


You can click on Select Web content there you can see list of web content articles.



You can select and click on respective web content and finally save it then web content will be displayed in portlet.


For better look and feel remove border for portlet then you can see image slide show as follows.


When we login as admin bottom of web content display portlet you can see edit controls to edit content and template. We can add more images, change height, width of slide show and we change slide interval time.


Download Image Slide Show Structure and Template Source 


Environment:

Liferay IDE 2.x+Eclipse (Kepler) +Liferay Plugins SDK 6.2+Tomcat 7.x Liferay Portal Bundle

Deployment and its Working.

You can source code in the template and Structure as said above in your portal.

I also given web content export file as lar. You simply drag and drop web content display portlet in the portlet configuration you can see export/import option and import downloaded lar file using file browses then web content will be created in your portal.

When we import lar file respective used template and structure will be created automatically.

Related Links



Author