2
votes

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..

1
Don't render actions jsp in that case.Parkash Kumar
Your action are available through user_assign_site_role_actions.jsp If you can make it conditional via hiding for already assigned roles, then it can be useful.Parkash Kumar
How are you identifying that certain row has been assigned role?Parkash Kumar
The scenario is assign the site role for user. I will select respective user row and based on that user id in my action class I am getting the list of assigned site roles and keep it in one list and in the same method I am getting the list of all site roles and keep them in another list. And finally I am sending both list to this jsp pagePrasad
updated my question with full codePrasad

1 Answers

0
votes

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.