Skip to content

Commit

Permalink
fixed adding mutiple portlet permissions per portlet
Browse files Browse the repository at this point in the history
  • Loading branch information
ktor committed Aug 24, 2017
1 parent 3f43c6e commit 039dd8d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 64 deletions.
14 changes: 13 additions & 1 deletion db-setup-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.mimacom.liferay</groupId>
<version>1.1.1</version>
<version>1.1.2</version>
<artifactId>db-setup-core</artifactId>

<name>Liferay Portal DB Setup core</name>
Expand Down Expand Up @@ -179,6 +179,18 @@
</licenses>

<developers>
<developer>
<name>Ivan Mrva</name>
<email>[email protected]</email>
<organization>mimacom ag</organization>
<organizationUrl>http://www.mimacom.com</organizationUrl>
</developer>
<developer>
<name>Martin Ronckevic</name>
<email>[email protected]</email>
<organization>http://ronky.net</organization>
<organizationUrl>http://ronky.net</organizationUrl>
</developer>
<developer>
<name>Silvio Meier</name>
<email>[email protected]</email>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public static boolean setup(final Setup setup) {
if (!configuration.getCompany().isEmpty()) {
for (Company company : configuration.getCompany()) {
Long companyId = company.getCompanyid();
String companyWebId = company.getCompanywebid();
if (companyId == null) {
String companyWebId = company.getCompanywebid();
try {
companyId = CompanyLocalServiceUtil.getCompanyByWebId(companyWebId).getCompanyId();
} catch (PortalException | SystemException e) {
Expand All @@ -86,7 +86,6 @@ public static boolean setup(final Setup setup) {
}
}
long runAsUserId = configureThreadPermission(runAsUserEmail, companyId);

setupPortalInstance(setup, companyId, runAsUserId);

// iterate over group names or choose GUEST group for the company
Expand All @@ -102,6 +101,7 @@ public static boolean setup(final Setup setup) {
}
} else {
long companyId = PortalUtil.getDefaultCompanyId();

long runAsUserId = configureThreadPermission(runAsUserEmail, companyId);
setupPortalInstance(setup, companyId, runAsUserId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -39,10 +39,7 @@
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.mimacom.liferay.portal.setup.domain.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.*;

public final class SetupPermissions {

Expand All @@ -57,28 +54,50 @@ private SetupPermissions() {
public static void setupPortletPermissions(final PortletPermissions portletPermissions, long companyId) {

for (PortletPermissions.Portlet portlet : portletPermissions.getPortlet()) {

deleteAllPortletPermissions(portlet, companyId);
for (PortletPermissions.Portlet.ActionId actionId : portlet.getActionId()) {
for (Role role : actionId.getRole()) {
try {
String name = role.getName();
long roleId = RoleLocalServiceUtil.getRole(companyId, name).getRoleId();
ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId,
portlet.getPortletId(), ResourceConstants.SCOPE_COMPANY,
String.valueOf(companyId), roleId,
new String[]{actionId.getName()});
LOG.info("Set permission for action id " + actionId.getName() + " and role "
+ role.getName());

} catch (NestableException e) {
LOG.error("could not set permission to portlet :" + portlet.getPortletId(),
e);
}

Map<String, Set<String>> actionsPerRole = getActionsPerRole(portlet);
for (String roleName : actionsPerRole.keySet()) {
try {
long roleId = RoleLocalServiceUtil.getRole(companyId, roleName).getRoleId();
final Set<String> actionStrings = actionsPerRole.get(roleName);
final String[] actionIds = actionStrings.toArray(new String[actionStrings.size()]);

ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId,
portlet.getPortletId(), ResourceConstants.SCOPE_COMPANY,
String.valueOf(companyId), roleId, actionIds);
LOG.info("Set permission for role: " + roleName + " for action ids: " + actionIds);
} catch (NestableException e) {
LOG.error("Could not set permission to portlet :" + portlet.getPortletId(),
e);
}
}
}
}

/**
* @param portlet
* @return mapping of role name to action ids for the portlet
*/
private static Map<String, Set<String>> getActionsPerRole(PortletPermissions.Portlet portlet) {
Map<String, Set<String>> result = new HashMap<>();

for (PortletPermissions.Portlet.ActionId actionId : portlet.getActionId()) {
for (Role role : actionId.getRole()) {
final String roleName = role.getName();
Set<String> actions = result.get(roleName);
if (actions == null) {
actions = new HashSet<>();
result.put(roleName, actions);
}
actions.add(actionId.getName());
}
}

return result;
}

public static void addReadRight(final String roleName, final String className,
final String primaryKey, long companyId) throws SystemException, PortalException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
* #L%
*/

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.liferay.portal.NoSuchRoleException;
import com.liferay.portal.RequiredRoleException;
import com.liferay.portal.kernel.dao.orm.ObjectNotFoundException;
Expand All @@ -43,6 +38,11 @@
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public final class SetupRoles {
private static final Log LOG = LogFactoryUtil.getLog(SetupRoles.class);

Expand Down Expand Up @@ -93,50 +93,50 @@ private static void addRole(final com.mimacom.liferay.portal.setup.domain.Role r
}

public static void deleteRoles(final List<com.mimacom.liferay.portal.setup.domain.Role> roles,
final String deleteMethod, final long companyId) {
final String deleteMethod, final long companyId) {

switch (deleteMethod) {
case "excludeListed":
Map<String, com.mimacom.liferay.portal.setup.domain.Role> toBeDeletedRoles = convertRoleListToHashMap(
roles);
try {
for (Role role : RoleLocalServiceUtil.getRoles(-1, -1)) {
String name = role.getName();
if (!toBeDeletedRoles.containsKey(name)) {
try {
RoleLocalServiceUtil
.deleteRole(RoleLocalServiceUtil.getRole(companyId, name));
LOG.info("Deleting Role " + name);

} catch (Exception e) {
LOG.info("Skipping deletion fo system role " + name);
case "excludeListed":
Map<String, com.mimacom.liferay.portal.setup.domain.Role> toBeDeletedRoles = convertRoleListToHashMap(
roles);
try {
for (Role role : RoleLocalServiceUtil.getRoles(-1, -1)) {
String name = role.getName();
if (!toBeDeletedRoles.containsKey(name)) {
try {
RoleLocalServiceUtil
.deleteRole(RoleLocalServiceUtil.getRole(companyId, name));
LOG.info("Deleting Role " + name);

} catch (Exception e) {
LOG.info("Skipping deletion fo system role " + name);
}
}
}
} catch (SystemException e) {
LOG.error("problem with deleting roles", e);
}
} catch (SystemException e) {
LOG.error("problem with deleting roles", e);
}
break;
break;

case "onlyListed":
for (com.mimacom.liferay.portal.setup.domain.Role role : roles) {
String name = role.getName();
try {
RoleLocalServiceUtil.deleteRole(RoleLocalServiceUtil.getRole(companyId, name));
LOG.info("Deleting Role " + name);
case "onlyListed":
for (com.mimacom.liferay.portal.setup.domain.Role role : roles) {
String name = role.getName();
try {
RoleLocalServiceUtil.deleteRole(RoleLocalServiceUtil.getRole(companyId, name));
LOG.info("Deleting Role " + name);

} catch (RequiredRoleException e) {
LOG.info("Skipping deletion fo system role " + name);
} catch (RequiredRoleException e) {
LOG.info("Skipping deletion fo system role " + name);

} catch (PortalException | SystemException e) {
LOG.error("Unable to delete role.", e);
} catch (PortalException | SystemException e) {
LOG.error("Unable to delete role.", e);
}
}
}
break;
break;

default:
LOG.error("Unknown delete method : " + deleteMethod);
break;
default:
LOG.error("Unknown delete method : " + deleteMethod);
break;
}

}
Expand Down

0 comments on commit 039dd8d

Please sign in to comment.