Search code examples
liferayhookliferay-6liferay-themeliferay-aui

Can it possible to make liferay-ui:search-container rows as non editable in Life ray?


I have a page where I need to set assign the site roles for user in my custom portlet. So I am able to get the "User existing roles" in one list and all "Available site roles" in another list. So how can I do conditioning Or any validation that I need to make non editable for the rows which user has been assigned. Let's say( we have four site type roles i.e, Site Administrator, Site Owner, Site Member and Site Content Reviewer Now the respective user has already assigned with Site Administrator role. So Now In the search container rows I need to make the Site Administrator row as non editable because the user has already has this role). My code is as follows,

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@include file="/html/users/init.jsp"%>

    <portletefineObjects />
             <%
List<Role> siteRoleList = (List) request.getAttribute("allsiteRolesList");   
List<Role> existingRoles = (List) request.getAttribute("existingRoles");     
String sel_userID = renderRequest.getParameter("sel_userID");
String backURL = ParamUtil.getString(request, "backURL");
            %>
            <%! 
                    List<Role> roles = null;
                    int totalNoOfroles=0;
                    String value = null;
            %>
            <%
            totalNoOfroles = siteRoleList.size();        
            roles = siteRoleList;
            %> 

    <liferay-ui:header backURL="<%= backURL %>" title="Available Site Roles" />

            <liferay-ui:search-container delta="5" emptyResultsMessage="no-site-roles-were-found" rowChecker="<%= new RowChecker(renderResponse) %>" >
            <liferay-ui:search-container-results
                    results="<%= ListUtil.subList(roles,searchContainer.getStart(),searchContainer.getEnd()) %>"
            total="<%= totalNoOfroles %>">    
        </liferay-ui:search-container-results>
            <liferay-ui:search-container-row className="com.liferay.portal.model.Role" keyProperty="roleId"        modelVar="role">

                    <liferay-ui:search-container-row-parameter name="roleIds" value="<%= role.getRoleId() %>"></liferay-ui:search-container-row-parameter>
                    <liferay-ui:search-container-row-parameter name="userIds" value="<%= sel_userID %>"></liferay-ui:search-container-row-parameter>
                    <liferay-ui:search-container-column-text name="title" value="<%= role.getName()%>" />
            <liferay-ui:search-container-column-text name="type" value="<%= role.getTypeLabel() %>">
            </liferay-ui:search-container-column-text>

            <liferay-ui:search-container-column-text name="description" value="<%= role.getDescription() %>">
            </liferay-ui:search-container-column-text>

                    <liferay-ui:search-container-column-jsp align="right" path="/html/users/user_assign_site_role_actions.jsp" />

            </liferay-ui:search-container-row>
            <liferay-ui:search-iterator />
    </liferay-ui:search-container>
    <liferay-ui:search-container delta="5" emptyResultsMessage="no-users-were-found" />

Action Class:

 public void assignUserSiteRoles(ActionRequest request, ActionResponse response) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException{
                    String sel_userID = ParamUtil.getString(request, "selectedId");
                    long userid = Long.valueOf(sel_userID);
                    String backURL = ParamUtil.getString(request, "backURL");
                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
                    long companyId = themeDisplay.getCompanyId();
                    long mySite =  themeDisplay.getSiteGroupId();
                    List<Role> allsiteRolesList = new ArrayList<Role>();
List<Role> existing roles = new ArrayList<Role>(); 


                    List<UserGroupRole> userGroupRoleList = UserGroupRoleLocalServiceUtil.getUserGroupRoles(userid, mySite);
                                if (userGroupRoleList != null) {
                                for (UserGroupRole userGroupRole : userGroupRoleList) {
                                    /* Get Role object based on userGroupRole.getRoleId() */
                                    Role role = RoleLocalServiceUtil.getRole(userGroupRole.getRoleId());
                                    if(role.getTypeLabel().equalsIgnoreCase("Site"))
                                {
                                 existingroles.add(role);             
                                }

                                }
                            }

                    List<Role> rolesList = RoleLocalServiceUtil.getRoles(companyId);
                    if (rolesList != null) {
                for (Role role : rolesList) {  
                                if(role.getTypeLabel().equalsIgnoreCase("Site"))
                                {
                                        allsiteRolesList.add(role);
                                }
                    }
                }


                    request.setAttribute("allsiteRolesList", allsiteRolesList);
                    response.setRenderParameter("sel_userID", sel_userID);
                    response.setRenderParameter("backURL", backURL);
                    response.setRenderParameter("mvcPath","/html/users/assign_user_site_roles.jsp");
              }

In my code, siteRoleList has list of all available roles and existingRoles list has roles which has already assigned for that respective user. So how can make editable for only those rows which user does's have that roles.

actionJSP

<%@ include file="/html/users/init.jsp" %>

<%
ResultRow resultRow = (ResultRow)request.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW);
Role role = (Role)resultRow.getObject();
String rowUserId = (String)resultRow.getParameter("userIds");
%>

<liferay-ui:icon-menu>  

  <portlet:actionURL name="UserSiteRoleAssign" var="UserSiteRoleAssign">
          <portlet:param name="selectedId" value="<%=String.valueOf(role.getRoleId()) %>" />
          <portle:pgaram name="rowUserId" value="<%= rowUserId %>" />
  </portlet:actionURL>
  <liferay-ui:icon iconCssClass="icon-signin" message="Assign Role" url="<%= UserSiteRoleAssign.toString() %>" />
</liferay-ui:icon-menu>

Any suggestions please..


Solution

  • I am not sure how are you evaluating condition to check that certain user has that specific role.

    Anyway, in you actionJSP, if you can manage to identify that selected user has that particular role through checking in existingRoles list as following, should do the trick for you:

    <%
    ResultRow resultRow = (ResultRow)request.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW);
    Role role = (Role)resultRow.getObject();
    String rowUserId = (String)resultRow.getParameter("userIds");
    
    /* Get user existingRoles list here. */
    boolean hasSiteRole = false;
    
    for(Role userRole : existingRoles){
        if(userRole.getRoleId() == role.getRoleId())
            hasSiteRole = true;
    }
    
    
    %>
    
    <liferay-ui:icon-menu>
        <portlet:actionURL name="UserSiteRoleAssign" var="UserSiteRoleAssign">
            <portlet:param name="selectedId" value="<%=String.valueOf(role.getRoleId()) %>" />
            <portle:pgaram name="rowUserId" value="<%= rowUserId %>" />
        </portlet:actionURL>
    
        <%
            String url = !hasSiteRole ? UserSiteRoleAssign.toString() : "javascript: void();";
            String styleClass = !hasSiteRole ? "icon-signin" : "icon-signin-diabled";
        %>
    
        <liferay-ui:icon iconCssClass="<%=styleClass %>" message="Assign Role" url="<%=url %>" />
    </liferay-ui:icon-menu>
    

    Based on hasSiteRole property, you can control href, class or rendering for the icon. You can also check that on your main view as well. Hide icon and style class as disabled in case if certain role already exists for the user in existingRoles list.

    Note: This is just an idea, you have to give it concrete implementation.