1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.logic;
20
21 import java.lang.reflect.Method;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import org.apache.commons.lang3.ArrayUtils;
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.syncope.common.lib.SyncopeClientException;
27 import org.apache.syncope.common.lib.to.RoleTO;
28 import org.apache.syncope.common.lib.types.ClientExceptionType;
29 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
30 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
31 import org.apache.syncope.core.persistence.api.dao.RoleDAO;
32 import org.apache.syncope.core.persistence.api.entity.Role;
33 import org.apache.syncope.core.provisioning.api.data.RoleDataBinder;
34 import org.apache.syncope.core.spring.security.AuthDataAccessor;
35 import org.springframework.security.access.prepost.PreAuthorize;
36 import org.springframework.transaction.annotation.Transactional;
37
38 public class RoleLogic extends AbstractTransactionalLogic<RoleTO> {
39
40 protected final RoleDataBinder binder;
41
42 protected final RoleDAO roleDAO;
43
44 public RoleLogic(final RoleDataBinder binder, final RoleDAO roleDAO) {
45 this.binder = binder;
46 this.roleDAO = roleDAO;
47 }
48
49 @PreAuthorize("hasRole('" + IdRepoEntitlement.ROLE_READ + "')")
50 @Transactional(readOnly = true)
51 public RoleTO read(final String key) {
52 Role role = roleDAO.find(key);
53 if (role == null) {
54 LOG.error("Could not find role '" + key + '\'');
55
56 throw new NotFoundException(key);
57 }
58
59 return binder.getRoleTO(role);
60 }
61
62 @PreAuthorize("hasRole('" + IdRepoEntitlement.ROLE_LIST + "')")
63 @Transactional(readOnly = true)
64 public List<RoleTO> list() {
65 return roleDAO.findAll().stream().map(binder::getRoleTO).collect(Collectors.toList());
66 }
67
68 @PreAuthorize("hasRole('" + IdRepoEntitlement.ROLE_CREATE + "')")
69 public RoleTO create(final RoleTO roleTO) {
70 return binder.getRoleTO(binder.create(roleTO));
71 }
72
73 @PreAuthorize("hasRole('" + IdRepoEntitlement.ROLE_UPDATE + "')")
74 public RoleTO update(final RoleTO roleTO) {
75 Role role = roleDAO.find(roleTO.getKey());
76 if (role == null) {
77 LOG.error("Could not find role '" + roleTO.getKey() + '\'');
78 throw new NotFoundException(roleTO.getKey());
79 }
80
81 return binder.getRoleTO(binder.update(role, roleTO));
82 }
83
84 @PreAuthorize("hasRole('" + IdRepoEntitlement.ROLE_DELETE + "')")
85 public RoleTO delete(final String key) {
86 if (AuthDataAccessor.GROUP_OWNER_ROLE.equals(key)) {
87 SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRole);
88 sce.getElements().add("This Role cannot be deleted");
89 throw sce;
90 }
91
92 Role role = roleDAO.find(key);
93 if (role == null) {
94 LOG.error("Could not find role '" + key + '\'');
95
96 throw new NotFoundException(key);
97 }
98
99 RoleTO deleted = binder.getRoleTO(role);
100 roleDAO.delete(key);
101 return deleted;
102 }
103
104 @PreAuthorize("isAuthenticated()")
105 public String getAnyLayout(final String key) {
106 Role role = roleDAO.find(key);
107 if (role == null) {
108 LOG.error("Could not find role '" + key + '\'');
109
110 throw new NotFoundException(key);
111 }
112
113 String consoleLayout = role.getAnyLayout();
114 if (StringUtils.isBlank(consoleLayout)) {
115 LOG.error("Could not find console layout for Role '" + key + '\'');
116
117 throw new NotFoundException("Console layout for role " + key);
118 }
119
120 return consoleLayout;
121 }
122
123 @PreAuthorize("hasRole('" + IdRepoEntitlement.ROLE_UPDATE + "')")
124 public void setAnyLayout(final String key, final String consoleLayout) {
125 Role role = roleDAO.find(key);
126 if (role == null) {
127 LOG.error("Could not find role '" + key + '\'');
128
129 throw new NotFoundException(key);
130 }
131
132 role.setAnyLayout(consoleLayout);
133 roleDAO.save(role);
134 }
135
136 @Override
137 protected RoleTO resolveReference(final Method method, final Object... args)
138 throws UnresolvedReferenceException {
139
140 String key = null;
141
142 if (ArrayUtils.isNotEmpty(args)) {
143 for (int i = 0; key == null && i < args.length; i++) {
144 if (args[i] instanceof String) {
145 key = (String) args[i];
146 } else if (args[i] instanceof RoleTO) {
147 key = ((RoleTO) args[i]).getKey();
148 }
149 }
150 }
151
152 if (key != null) {
153 try {
154 return binder.getRoleTO(roleDAO.find(key));
155 } catch (Throwable ignore) {
156 LOG.debug("Unresolved reference", ignore);
157 throw new UnresolvedReferenceException(ignore);
158 }
159 }
160
161 throw new UnresolvedReferenceException();
162 }
163 }