Skip to content

Commit 039dd8d

Browse files
committed
fixed adding mutiple portlet permissions per portlet
1 parent 3f43c6e commit 039dd8d

File tree

4 files changed

+95
-64
lines changed

4 files changed

+95
-64
lines changed

db-setup-core/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.mimacom.liferay</groupId>
7-
<version>1.1.1</version>
7+
<version>1.1.2</version>
88
<artifactId>db-setup-core</artifactId>
99

1010
<name>Liferay Portal DB Setup core</name>
@@ -179,6 +179,18 @@
179179
</licenses>
180180

181181
<developers>
182+
<developer>
183+
<name>Ivan Mrva</name>
184+
<email>ivan.mrva@mimacom.com</email>
185+
<organization>mimacom ag</organization>
186+
<organizationUrl>http://www.mimacom.com</organizationUrl>
187+
</developer>
188+
<developer>
189+
<name>Martin Ronckevic</name>
190+
<email>martin.ronky@gmail.com</email>
191+
<organization>http://ronky.net</organization>
192+
<organizationUrl>http://ronky.net</organizationUrl>
193+
</developer>
182194
<developer>
183195
<name>Silvio Meier</name>
184196
<email>silvio.meier@empa.ch</email>

db-setup-core/src/main/java/com/mimacom/liferay/portal/setup/LiferaySetup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public static boolean setup(final Setup setup) {
7676
if (!configuration.getCompany().isEmpty()) {
7777
for (Company company : configuration.getCompany()) {
7878
Long companyId = company.getCompanyid();
79-
String companyWebId = company.getCompanywebid();
8079
if (companyId == null) {
80+
String companyWebId = company.getCompanywebid();
8181
try {
8282
companyId = CompanyLocalServiceUtil.getCompanyByWebId(companyWebId).getCompanyId();
8383
} catch (PortalException | SystemException e) {
@@ -86,7 +86,6 @@ public static boolean setup(final Setup setup) {
8686
}
8787
}
8888
long runAsUserId = configureThreadPermission(runAsUserEmail, companyId);
89-
9089
setupPortalInstance(setup, companyId, runAsUserId);
9190

9291
// iterate over group names or choose GUEST group for the company
@@ -102,6 +101,7 @@ public static boolean setup(final Setup setup) {
102101
}
103102
} else {
104103
long companyId = PortalUtil.getDefaultCompanyId();
104+
105105
long runAsUserId = configureThreadPermission(runAsUserEmail, companyId);
106106
setupPortalInstance(setup, companyId, runAsUserId);
107107

db-setup-core/src/main/java/com/mimacom/liferay/portal/setup/core/SetupPermissions.java

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1313
* copies of the Software, and to permit persons to whom the Software is
1414
* furnished to do so, subject to the following conditions:
15-
*
15+
*
1616
* The above copyright notice and this permission notice shall be included in
1717
* all copies or substantial portions of the Software.
18-
*
18+
*
1919
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2020
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2121
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -39,10 +39,7 @@
3939
import com.liferay.portal.service.RoleLocalServiceUtil;
4040
import com.mimacom.liferay.portal.setup.domain.*;
4141

42-
import java.util.ArrayList;
43-
import java.util.HashMap;
44-
import java.util.List;
45-
import java.util.Set;
42+
import java.util.*;
4643

4744
public final class SetupPermissions {
4845

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

5956
for (PortletPermissions.Portlet portlet : portletPermissions.getPortlet()) {
57+
6058
deleteAllPortletPermissions(portlet, companyId);
61-
for (PortletPermissions.Portlet.ActionId actionId : portlet.getActionId()) {
62-
for (Role role : actionId.getRole()) {
63-
try {
64-
String name = role.getName();
65-
long roleId = RoleLocalServiceUtil.getRole(companyId, name).getRoleId();
66-
ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId,
67-
portlet.getPortletId(), ResourceConstants.SCOPE_COMPANY,
68-
String.valueOf(companyId), roleId,
69-
new String[]{actionId.getName()});
70-
LOG.info("Set permission for action id " + actionId.getName() + " and role "
71-
+ role.getName());
72-
73-
} catch (NestableException e) {
74-
LOG.error("could not set permission to portlet :" + portlet.getPortletId(),
75-
e);
76-
}
59+
60+
Map<String, Set<String>> actionsPerRole = getActionsPerRole(portlet);
61+
for (String roleName : actionsPerRole.keySet()) {
62+
try {
63+
long roleId = RoleLocalServiceUtil.getRole(companyId, roleName).getRoleId();
64+
final Set<String> actionStrings = actionsPerRole.get(roleName);
65+
final String[] actionIds = actionStrings.toArray(new String[actionStrings.size()]);
66+
67+
ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId,
68+
portlet.getPortletId(), ResourceConstants.SCOPE_COMPANY,
69+
String.valueOf(companyId), roleId, actionIds);
70+
LOG.info("Set permission for role: " + roleName + " for action ids: " + actionIds);
71+
} catch (NestableException e) {
72+
LOG.error("Could not set permission to portlet :" + portlet.getPortletId(),
73+
e);
7774
}
7875
}
7976
}
8077
}
8178

79+
/**
80+
* @param portlet
81+
* @return mapping of role name to action ids for the portlet
82+
*/
83+
private static Map<String, Set<String>> getActionsPerRole(PortletPermissions.Portlet portlet) {
84+
Map<String, Set<String>> result = new HashMap<>();
85+
86+
for (PortletPermissions.Portlet.ActionId actionId : portlet.getActionId()) {
87+
for (Role role : actionId.getRole()) {
88+
final String roleName = role.getName();
89+
Set<String> actions = result.get(roleName);
90+
if (actions == null) {
91+
actions = new HashSet<>();
92+
result.put(roleName, actions);
93+
}
94+
actions.add(actionId.getName());
95+
}
96+
}
97+
98+
return result;
99+
}
100+
82101
public static void addReadRight(final String roleName, final String className,
83102
final String primaryKey, long companyId) throws SystemException, PortalException {
84103

db-setup-core/src/main/java/com/mimacom/liferay/portal/setup/core/SetupRoles.java

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
* #L%
2727
*/
2828

29-
import java.util.HashMap;
30-
import java.util.List;
31-
import java.util.Locale;
32-
import java.util.Map;
33-
3429
import com.liferay.portal.NoSuchRoleException;
3530
import com.liferay.portal.RequiredRoleException;
3631
import com.liferay.portal.kernel.dao.orm.ObjectNotFoundException;
@@ -43,6 +38,11 @@
4338
import com.liferay.portal.service.RoleLocalServiceUtil;
4439
import com.liferay.portal.service.UserLocalServiceUtil;
4540

41+
import java.util.HashMap;
42+
import java.util.List;
43+
import java.util.Locale;
44+
import java.util.Map;
45+
4646
public final class SetupRoles {
4747
private static final Log LOG = LogFactoryUtil.getLog(SetupRoles.class);
4848

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

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

9898
switch (deleteMethod) {
99-
case "excludeListed":
100-
Map<String, com.mimacom.liferay.portal.setup.domain.Role> toBeDeletedRoles = convertRoleListToHashMap(
101-
roles);
102-
try {
103-
for (Role role : RoleLocalServiceUtil.getRoles(-1, -1)) {
104-
String name = role.getName();
105-
if (!toBeDeletedRoles.containsKey(name)) {
106-
try {
107-
RoleLocalServiceUtil
108-
.deleteRole(RoleLocalServiceUtil.getRole(companyId, name));
109-
LOG.info("Deleting Role " + name);
110-
111-
} catch (Exception e) {
112-
LOG.info("Skipping deletion fo system role " + name);
99+
case "excludeListed":
100+
Map<String, com.mimacom.liferay.portal.setup.domain.Role> toBeDeletedRoles = convertRoleListToHashMap(
101+
roles);
102+
try {
103+
for (Role role : RoleLocalServiceUtil.getRoles(-1, -1)) {
104+
String name = role.getName();
105+
if (!toBeDeletedRoles.containsKey(name)) {
106+
try {
107+
RoleLocalServiceUtil
108+
.deleteRole(RoleLocalServiceUtil.getRole(companyId, name));
109+
LOG.info("Deleting Role " + name);
110+
111+
} catch (Exception e) {
112+
LOG.info("Skipping deletion fo system role " + name);
113+
}
113114
}
114115
}
116+
} catch (SystemException e) {
117+
LOG.error("problem with deleting roles", e);
115118
}
116-
} catch (SystemException e) {
117-
LOG.error("problem with deleting roles", e);
118-
}
119-
break;
119+
break;
120120

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

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

131-
} catch (PortalException | SystemException e) {
132-
LOG.error("Unable to delete role.", e);
131+
} catch (PortalException | SystemException e) {
132+
LOG.error("Unable to delete role.", e);
133+
}
133134
}
134-
}
135-
break;
135+
break;
136136

137-
default:
138-
LOG.error("Unknown delete method : " + deleteMethod);
139-
break;
137+
default:
138+
LOG.error("Unknown delete method : " + deleteMethod);
139+
break;
140140
}
141141

142142
}

0 commit comments

Comments
 (0)