Monday, 22 September 2014

Remove user from usergroup programmatically in liferay, delete user from user group in liferay

In this post I am describing how to remove a user from a user group.
If there is a group and a user is a member of a group then we can delete a member from that group.
In this example I am printing number of members in a group with member details, beside of each user details one delete link is there. If user click on it then javascript function will executes and then by using ajax call we are calling serveResource method and passing userid and groupid.

my jsp page

userremovegroup.jsp

<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="com.liferay.portal.kernel.util.GetterUtil"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ page import="javax.portlet.PortletSession" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />

<%@page import="com.liferay.portal.service.AddressLocalServiceUtil"%>
<portlet:resourceURL var='myInfo' id='myInfo' />


<aui:script>

   
   function deleteMemberFromGroupFunc(userId,groupId)
   {
    var myInfoUrl = "<%= myInfo.toString() %>";
var A = AUI();
   
    var io = A.io.request(myInfoUrl, {
method: 'GET',
dataType: "json",
data:
{
    userIdVal : userId,
    groupIdVal : groupId,
    groupMemberDeleteVal : "true"
},
on: {
success: function() 
{
location.reload();
},
failure: function() 
{
}
}
});
alert("uid: "+userId+"     gid: "+groupId)  ;  
   }
   
</aui:script>







<%

User userr = themeDisplay.getUser();
List<UserGroup> userUserGroups = UserGroupLocalServiceUtil.getUserUserGroups(userr.getUserId());
if(!userUserGroups.isEmpty())
{
Iterator userUserGroupItr = userUserGroups.iterator();
UserGroup userUserGroup = (UserGroup)userUserGroupItr.next();
long classPK = userUserGroup.getUserGroupId();
String className= UserGroup.class.getName();
List<Address> addressList= AddressLocalServiceUtil.getAddresses(themeDisplay.getCompanyId(), UserGroup.class.getName(), classPK);
Iterator addressListIterator = addressList.iterator();


while(addressListIterator.hasNext()){
 Address addressDetailList =(Address)addressListIterator.next();
 %>


 Street1:<%=addressDetailList.getStreet1()%><br />
 Street2:<%=addressDetailList.getStreet2()%><br />
 City:<%=addressDetailList.getCity()%><br /> 
 Zip:<%=addressDetailList.getZip()%><br />
 
 
 <%
}

List<User> userList=UserLocalServiceUtil.getUserGroupUsers(userUserGroup.getUserGroupId());
 Iterator userListIterator = userList.iterator();
 %>
<table class="table table-striped table-hover" >
<thead>
  <th>Name</th>
  <th>Email</th>
  <th>Gender</th>
  <th>Action</th>
</thead>
 <%
 while(userListIterator.hasNext()){
 User userIdListDetails =(User)userListIterator.next();
 //System.out.println("the userids from"+userIdListDetails.getUserId());
 //long userId = userIdList.getUserId();
 User user1 = UserLocalServiceUtil.fetchUser(userIdListDetails.getUserId());
 %>
<tr>
<td><%= user1.getFullName()%></td>
<td><%= user1.getEmailAddress()%></td>
<td><%= user1.getMale() %></td>
<td>
<a href="#" onclick="deleteMemberFromGroupFunc(<%= userIdListDetails.getUserId() %>,<%= userUserGroup.getUserGroupId() %>);">Delete user</a>
</td>
</tr>
 <%
 
}
}
%><%
 

%>
</table>




action class 

DeleteAction.java

public class DeleteAction extends MVCPortlet {

public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException {
boolean deleteGroupMember = Boolean.parseBoolean(PortalUtil.getOriginalServletRequest(httpReq).getParameter("groupMemberDeleteVal"));
long companyId = PortalUtil.getCompanyId(resourceRequest);
long userIdVal = 0l; 
long groupIdVal = 0l;
if(deleteGroupMember) { try{ userIdVal=Long.parseLong(PortalUtil.getOriginalServletRequest(httpReq).getParameter("userIdVal")); groupIdVal=Long.parseLong(PortalUtil.getOriginalServletRequest(httpReq).getParameter("groupIdVal")); 
 }
 catch(NumberFormatException e) 
 { 
 System.out.println("err by asif"); 
 }
 System.out.println("@@ userIdVal: "+userIdVal+"\n@@ groupIdVal: "+groupIdVal); 
 try
 {
 UserLocalServiceUtil.deleteUserGroupUser(groupIdVal, userIdVal); 
 }
 catch (PortalException e) 
{
 System.out.println("portalexception"); e.printStackTrace(); 
 } 
 catch( SystemException e) 
 { 
 System.out.println("systemexception"); 
 e.printStackTrace(); 
 }
 }
}
}

No comments:

Post a Comment