View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.logic;
20  
21  import java.lang.reflect.Method;
22  import java.util.List;
23  import org.apache.commons.lang3.ArrayUtils;
24  import org.apache.syncope.common.lib.to.SecurityQuestionTO;
25  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
26  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
27  import org.apache.syncope.core.persistence.api.dao.SecurityQuestionDAO;
28  import org.apache.syncope.core.persistence.api.dao.UserDAO;
29  import org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion;
30  import org.apache.syncope.core.persistence.api.entity.user.User;
31  import org.apache.syncope.core.provisioning.api.data.SecurityQuestionDataBinder;
32  import org.springframework.security.access.prepost.PreAuthorize;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  public class SecurityQuestionLogic extends AbstractTransactionalLogic<SecurityQuestionTO> {
36  
37      protected final SecurityQuestionDAO securityQuestionDAO;
38  
39      protected final UserDAO userDAO;
40  
41      protected final SecurityQuestionDataBinder binder;
42  
43      public SecurityQuestionLogic(
44              final SecurityQuestionDAO securityQuestionDAO,
45              final UserDAO userDAO,
46              final SecurityQuestionDataBinder binder) {
47  
48          this.securityQuestionDAO = securityQuestionDAO;
49          this.userDAO = userDAO;
50          this.binder = binder;
51      }
52  
53      @PreAuthorize("isAuthenticated()")
54      @Transactional(readOnly = true)
55      public List<SecurityQuestionTO> list() {
56          return securityQuestionDAO.findAll().stream().map(binder::getSecurityQuestionTO).toList();
57      }
58  
59      @PreAuthorize("hasRole('" + IdRepoEntitlement.SECURITY_QUESTION_READ + "')")
60      @Transactional(readOnly = true)
61      public SecurityQuestionTO read(final String key) {
62          SecurityQuestion securityQuestion = securityQuestionDAO.findById(key).
63                  orElseThrow(() -> new NotFoundException("SecurityQuestion " + key));
64  
65          return binder.getSecurityQuestionTO(securityQuestion);
66      }
67  
68      @PreAuthorize("hasRole('" + IdRepoEntitlement.SECURITY_QUESTION_CREATE + "')")
69      public SecurityQuestionTO create(final SecurityQuestionTO securityQuestionTO) {
70          return binder.getSecurityQuestionTO(securityQuestionDAO.save(binder.create(securityQuestionTO)));
71      }
72  
73      @PreAuthorize("hasRole('" + IdRepoEntitlement.SECURITY_QUESTION_UPDATE + "')")
74      public SecurityQuestionTO update(final SecurityQuestionTO securityQuestionTO) {
75          SecurityQuestion securityQuestion = securityQuestionDAO.findById(securityQuestionTO.getKey()).
76                  orElseThrow(() -> new NotFoundException("SecurityQuestion " + securityQuestionTO.getKey()));
77  
78          binder.update(securityQuestion, securityQuestionTO);
79          securityQuestion = securityQuestionDAO.save(securityQuestion);
80  
81          return binder.getSecurityQuestionTO(securityQuestion);
82      }
83  
84      @PreAuthorize("hasRole('" + IdRepoEntitlement.SECURITY_QUESTION_DELETE + "')")
85      public SecurityQuestionTO delete(final String key) {
86          SecurityQuestion securityQuestion = securityQuestionDAO.findById(key).
87                  orElseThrow(() -> new NotFoundException("SecurityQuestion " + key));
88  
89          SecurityQuestionTO deleted = binder.getSecurityQuestionTO(securityQuestion);
90          securityQuestionDAO.deleteById(key);
91          return deleted;
92      }
93  
94      @PreAuthorize("hasRole('" + IdRepoEntitlement.ANONYMOUS + "')")
95      public SecurityQuestionTO readByUser(final String username) {
96          if (username == null) {
97              throw new NotFoundException("Null username");
98          }
99          User user = userDAO.findByUsername(username).
100                 orElseThrow(() -> new NotFoundException("User " + username));
101 
102         if (user.getSecurityQuestion() == null) {
103             LOG.error("Could not find security question for user '{}'", username);
104 
105             throw new NotFoundException("Security question for user " + username);
106         }
107 
108         return binder.getSecurityQuestionTO(user.getSecurityQuestion());
109     }
110 
111     @Override
112     protected SecurityQuestionTO resolveReference(final Method method, final Object... args)
113             throws UnresolvedReferenceException {
114 
115         String key = null;
116 
117         if (ArrayUtils.isNotEmpty(args)) {
118             for (int i = 0; key == null && i < args.length; i++) {
119                 if (args[i] instanceof String string) {
120                     key = string;
121                 } else if (args[i] instanceof SecurityQuestionTO securityQuestionTO) {
122                     key = securityQuestionTO.getKey();
123                 }
124             }
125         }
126 
127         if (key != null) {
128             try {
129                 return binder.getSecurityQuestionTO(securityQuestionDAO.findById(key).orElseThrow());
130             } catch (Throwable ignore) {
131                 LOG.debug("Unresolved reference", ignore);
132                 throw new UnresolvedReferenceException(ignore);
133             }
134         }
135 
136         throw new UnresolvedReferenceException();
137     }
138 }