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.rest.cxf.service;
20  
21  import javax.ws.rs.core.Response;
22  import org.apache.commons.lang3.tuple.Triple;
23  import org.apache.syncope.common.lib.AnyOperations;
24  import org.apache.syncope.common.lib.SyncopeClientException;
25  import org.apache.syncope.common.lib.request.PasswordPatch;
26  import org.apache.syncope.common.lib.request.StatusR;
27  import org.apache.syncope.common.lib.request.UserCR;
28  import org.apache.syncope.common.lib.request.UserUR;
29  import org.apache.syncope.common.lib.to.ProvisioningResult;
30  import org.apache.syncope.common.lib.to.UserTO;
31  import org.apache.syncope.common.lib.types.ClientExceptionType;
32  import org.apache.syncope.common.rest.api.RESTHeaders;
33  import org.apache.syncope.common.rest.api.beans.ComplianceQuery;
34  import org.apache.syncope.common.rest.api.service.UserSelfService;
35  import org.apache.syncope.core.logic.SyncopeLogic;
36  import org.apache.syncope.core.logic.UserLogic;
37  import org.springframework.stereotype.Service;
38  
39  @Service
40  public class UserSelfServiceImpl extends AbstractService implements UserSelfService {
41  
42      protected final UserLogic logic;
43  
44      protected final SyncopeLogic syncopeLogic;
45  
46      public UserSelfServiceImpl(final UserLogic logic, final SyncopeLogic syncopeLogic) {
47          this.logic = logic;
48          this.syncopeLogic = syncopeLogic;
49      }
50  
51      @Override
52      public Response create(final UserCR createReq) {
53          if (!syncopeLogic.isSelfRegAllowed()) {
54              SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.DelegatedAdministration);
55              sce.getElements().add("Self registration forbidden by configuration");
56              throw sce;
57          }
58  
59          ProvisioningResult<UserTO> created = logic.selfCreate(createReq, isNullPriorityAsync());
60          return createResponse(created);
61      }
62  
63      @Override
64      public Response read() {
65          Triple<String, String, UserTO> self = logic.selfRead();
66          return Response.ok().
67                  header(RESTHeaders.RESOURCE_KEY, self.getRight().getKey()).
68                  header(RESTHeaders.OWNED_ENTITLEMENTS, self.getLeft()).
69                  header(RESTHeaders.DELEGATIONS, self.getMiddle()).
70                  entity(self.getRight()).
71                  build();
72      }
73  
74      @Override
75      public Response update(final UserUR updateReq) {
76          ProvisioningResult<UserTO> updated = logic.selfUpdate(updateReq, isNullPriorityAsync());
77          return modificationResponse(updated);
78      }
79  
80      @Override
81      public Response update(final UserTO user) {
82          Triple<String, String, UserTO> self = logic.selfRead();
83          return update(AnyOperations.diff(user, self.getRight(), false));
84      }
85  
86      @Override
87      public Response status(final StatusR statusR) {
88          ProvisioningResult<UserTO> updated = logic.selfStatus(statusR, isNullPriorityAsync());
89          return modificationResponse(updated);
90      }
91  
92      @Override
93      public Response delete() {
94          ProvisioningResult<UserTO> deleted = logic.selfDelete(isNullPriorityAsync());
95          return modificationResponse(deleted);
96      }
97  
98      @Override
99      public Response mustChangePassword(final PasswordPatch password) {
100         ProvisioningResult<UserTO> updated = logic.mustChangePassword(password, isNullPriorityAsync());
101         return modificationResponse(updated);
102     }
103 
104     @Override
105     public void compliance(final ComplianceQuery query) {
106         logic.compliance(query);
107     }
108 
109     @Override
110     public void requestPasswordReset(final String username, final String securityAnswer) {
111         if (!syncopeLogic.isPwdResetAllowed()) {
112             SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.DelegatedAdministration);
113             sce.getElements().add("Password reset forbidden by configuration");
114             throw sce;
115         }
116 
117         logic.requestPasswordReset(username, securityAnswer);
118     }
119 
120     @Override
121     public void confirmPasswordReset(final String token, final String password) {
122         if (!syncopeLogic.isPwdResetAllowed()) {
123             SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.DelegatedAdministration);
124             sce.getElements().add("Password reset forbidden by configuration");
125             throw sce;
126         }
127 
128         logic.confirmPasswordReset(token, password);
129     }
130 }