|
18 | 18 | */
|
19 | 19 | package io.meeds.layout.service;
|
20 | 20 |
|
21 |
| -import org.apache.commons.lang3.StringUtils; |
22 | 21 | import org.springframework.beans.factory.annotation.Autowired;
|
23 | 22 | import org.springframework.stereotype.Service;
|
24 | 23 |
|
25 |
| -import org.exoplatform.container.ExoContainerContext; |
26 | 24 | import org.exoplatform.portal.config.UserACL;
|
27 | 25 | import org.exoplatform.portal.config.model.Page;
|
28 | 26 | import org.exoplatform.portal.config.model.PortalConfig;
|
29 | 27 | import org.exoplatform.portal.mop.SiteKey;
|
30 | 28 | import org.exoplatform.portal.mop.page.PageKey;
|
31 | 29 | import org.exoplatform.portal.mop.service.LayoutService;
|
32 |
| -import org.exoplatform.services.security.Authenticator; |
33 | 30 | import org.exoplatform.services.security.ConversationState;
|
34 |
| -import org.exoplatform.services.security.Identity; |
35 |
| -import org.exoplatform.services.security.IdentityConstants; |
36 |
| -import org.exoplatform.services.security.IdentityRegistry; |
| 31 | +import org.exoplatform.social.core.identity.model.Identity; |
37 | 32 | import org.exoplatform.social.core.manager.IdentityManager;
|
38 | 33 |
|
39 |
| -import jakarta.annotation.PostConstruct; |
40 |
| -import lombok.Setter; |
41 |
| -import lombok.SneakyThrows; |
42 |
| - |
43 | 34 | @Service
|
44 | 35 | public class LayoutAclService {
|
45 | 36 |
|
46 | 37 | @Autowired
|
47 |
| - private UserACL userAcl; |
48 |
| - |
49 |
| - @Autowired |
50 |
| - private LayoutService layoutService; |
| 38 | + private LayoutService layoutService; |
51 | 39 |
|
52 | 40 | @Autowired
|
53 |
| - private Authenticator authenticator; |
| 41 | + private IdentityManager identityManager; |
54 | 42 |
|
55 | 43 | @Autowired
|
56 |
| - private IdentityManager identityManager; |
57 |
| - |
58 |
| - @Setter |
59 |
| - private IdentityRegistry identityRegistry; |
60 |
| - |
61 |
| - @PostConstruct |
62 |
| - public void init() { |
63 |
| - // Can't be autowired from Kernel IoC, thus inject it once Spring Bean |
64 |
| - // initialized |
65 |
| - setIdentityRegistry(ExoContainerContext.getService(IdentityRegistry.class)); |
66 |
| - } |
| 44 | + private UserACL userAcl; |
67 | 45 |
|
68 | 46 | public boolean canAddSite(String username) {
|
69 |
| - ConversationState currentConversationState = ConversationState.getCurrent(); |
70 |
| - ConversationState.setCurrent(getConversationState(username)); |
71 |
| - try { |
72 |
| - return userAcl.hasCreatePortalPermission(); |
73 |
| - } finally { |
74 |
| - ConversationState.setCurrent(currentConversationState); |
75 |
| - } |
| 47 | + return userAcl.hasCreatePortalPermission(userAcl.getUserIdentity(username)); |
76 | 48 | }
|
77 | 49 |
|
78 | 50 | public boolean canEditSite(SiteKey siteKey, String username) {
|
79 | 51 | PortalConfig portalConfig = layoutService.getPortalConfig(siteKey);
|
80 | 52 | if (portalConfig == null) {
|
81 | 53 | return false;
|
82 | 54 | }
|
83 |
| - ConversationState currentConversationState = ConversationState.getCurrent(); |
84 |
| - ConversationState.setCurrent(getConversationState(username)); |
85 |
| - try { |
86 |
| - return userAcl.hasEditPermission(portalConfig); |
87 |
| - } finally { |
88 |
| - ConversationState.setCurrent(currentConversationState); |
89 |
| - } |
| 55 | + return userAcl.hasEditPermission(portalConfig, userAcl.getUserIdentity(username)); |
90 | 56 | }
|
91 | 57 |
|
92 | 58 | public boolean canViewSite(SiteKey siteKey, String username) {
|
93 | 59 | PortalConfig portalConfig = layoutService.getPortalConfig(siteKey);
|
94 | 60 | if (portalConfig == null) {
|
95 | 61 | return false;
|
96 | 62 | }
|
97 |
| - ConversationState currentConversationState = ConversationState.getCurrent(); |
98 |
| - ConversationState.setCurrent(getConversationState(username)); |
99 |
| - try { |
100 |
| - return userAcl.hasPermission(portalConfig); |
101 |
| - } finally { |
102 |
| - ConversationState.setCurrent(currentConversationState); |
103 |
| - } |
| 63 | + return userAcl.hasAccessPermission(portalConfig, userAcl.getUserIdentity(username)); |
104 | 64 | }
|
105 | 65 |
|
106 | 66 | public boolean canEditNavigation(SiteKey siteKey, String username) {
|
107 |
| - PortalConfig portalConfig = layoutService.getPortalConfig(siteKey); |
108 |
| - if (portalConfig == null) { |
109 |
| - return false; |
110 |
| - } |
111 |
| - |
112 |
| - ConversationState currentConversationState = ConversationState.getCurrent(); |
113 |
| - ConversationState.setCurrent(getConversationState(username)); |
114 |
| - try { |
115 |
| - return userAcl.hasEditPermission(portalConfig) || userAcl.hasEditPermissionOnNavigation(siteKey); |
116 |
| - } finally { |
117 |
| - ConversationState.setCurrent(currentConversationState); |
118 |
| - } |
| 67 | + return canEditSite(siteKey, username); |
119 | 68 | }
|
120 | 69 |
|
121 | 70 | public boolean canViewNavigation(SiteKey siteKey, PageKey pageKey, String username) {
|
122 |
| - PortalConfig portalConfig = layoutService.getPortalConfig(siteKey); |
123 |
| - if (portalConfig == null) { |
124 |
| - return false; |
125 |
| - } |
126 |
| - Page page = pageKey == null ? null : layoutService.getPage(pageKey); |
127 |
| - ConversationState currentConversationState = ConversationState.getCurrent(); |
128 |
| - ConversationState.setCurrent(getConversationState(username)); |
129 |
| - try { |
130 |
| - return userAcl.hasAccessPermission(portalConfig) && (page == null || userAcl.hasPermission(page)); |
131 |
| - } finally { |
132 |
| - ConversationState.setCurrent(currentConversationState); |
133 |
| - } |
| 71 | + return canViewSite(siteKey, username) && (pageKey == null || canViewPage(pageKey, username)); |
134 | 72 | }
|
135 | 73 |
|
136 | 74 | public boolean canViewPage(PageKey pageKey, String username) {
|
137 | 75 | Page page = layoutService.getPage(pageKey);
|
138 | 76 | if (page == null) {
|
139 | 77 | return false;
|
140 | 78 | }
|
141 |
| - |
142 |
| - ConversationState currentConversationState = ConversationState.getCurrent(); |
143 |
| - ConversationState.setCurrent(getConversationState(username)); |
144 |
| - try { |
145 |
| - return userAcl.hasPermission(page); |
146 |
| - } finally { |
147 |
| - ConversationState.setCurrent(currentConversationState); |
148 |
| - } |
| 79 | + return userAcl.hasAccessPermission(page, userAcl.getUserIdentity(username)); |
149 | 80 | }
|
150 | 81 |
|
151 | 82 | public boolean canEditPage(PageKey pageKey, String username) {
|
152 | 83 | Page page = layoutService.getPage(pageKey);
|
153 | 84 | if (page == null) {
|
154 | 85 | return false;
|
155 | 86 | }
|
156 |
| - |
157 |
| - ConversationState currentConversationState = ConversationState.getCurrent(); |
158 |
| - ConversationState.setCurrent(getConversationState(username)); |
159 |
| - try { |
160 |
| - return userAcl.hasEditPermission(page); |
161 |
| - } finally { |
162 |
| - ConversationState.setCurrent(currentConversationState); |
163 |
| - } |
| 87 | + return userAcl.hasEditPermission(page, userAcl.getUserIdentity(username)); |
164 | 88 | }
|
165 | 89 |
|
166 | 90 | public boolean isAdministrator(String username) {
|
167 |
| - ConversationState currentConversationState = ConversationState.getCurrent(); |
168 |
| - ConversationState.setCurrent(getConversationState(username)); |
169 |
| - try { |
170 |
| - return userAcl.isSuperUser() || userAcl.isUserInGroup(getAdministratorsGroup()); |
171 |
| - } finally { |
172 |
| - ConversationState.setCurrent(currentConversationState); |
173 |
| - } |
| 91 | + return userAcl.isAdministrator(userAcl.getUserIdentity(username)); |
174 | 92 | }
|
175 | 93 |
|
176 |
| - public boolean isMemberOf(String username, String expression) { |
177 |
| - ConversationState currentConversationState = ConversationState.getCurrent(); |
178 |
| - ConversationState.setCurrent(getConversationState(username)); |
179 |
| - try { |
180 |
| - return userAcl.hasPermission(expression); |
181 |
| - } finally { |
182 |
| - ConversationState.setCurrent(currentConversationState); |
183 |
| - } |
| 94 | + public boolean hasPermission(String username, String expression) { |
| 95 | + return userAcl.hasPermission(userAcl.getUserIdentity(username), expression); |
184 | 96 | }
|
185 | 97 |
|
186 | 98 | public String getAdministratorsGroup() {
|
187 | 99 | return userAcl.getAdminGroups();
|
188 | 100 | }
|
189 | 101 |
|
190 | 102 | public ConversationState getSuperUserConversationState() {
|
191 |
| - return new ConversationState(getUserIdentity(userAcl.getSuperUser())); |
| 103 | + return new ConversationState(userAcl.getUserIdentity(userAcl.getSuperUser())); |
192 | 104 | }
|
193 | 105 |
|
194 | 106 | public long getSuperUserIdentityId() {
|
195 |
| - org.exoplatform.social.core.identity.model.Identity userIdentity = |
196 |
| - identityManager.getOrCreateUserIdentity(userAcl.getSuperUser()); |
197 |
| - String id = userIdentity == null ? null : userIdentity.getId(); |
198 |
| - return id == null ? 0 : Long.parseLong(id); |
199 |
| - } |
200 |
| - |
201 |
| - private ConversationState getConversationState(String username) { |
202 |
| - return new ConversationState(getUserIdentity(username)); |
203 |
| - } |
204 |
| - |
205 |
| - @SneakyThrows |
206 |
| - private Identity getUserIdentity(String username) { |
207 |
| - if (StringUtils.isBlank(username) || IdentityConstants.ANONIM.equals(username)) { |
208 |
| - return null; |
209 |
| - } |
210 |
| - Identity identity = identityRegistry.getIdentity(username); |
211 |
| - if (identity != null) { |
212 |
| - return identity; |
213 |
| - } else { |
214 |
| - return authenticator.createIdentity(username); |
215 |
| - } |
| 107 | + Identity userIdentity = identityManager.getOrCreateUserIdentity(userAcl.getSuperUser()); |
| 108 | + return userIdentity == null ? 0l : Long.parseLong(userIdentity.getId()); |
216 | 109 | }
|
217 | 110 |
|
218 | 111 | }
|
0 commit comments