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.flowable.task;
20  
21  import org.apache.commons.lang3.tuple.Pair;
22  import org.apache.syncope.common.lib.request.PasswordPatch;
23  import org.apache.syncope.common.lib.request.UserUR;
24  import org.apache.syncope.core.flowable.impl.FlowableRuntimeUtils;
25  import org.apache.syncope.core.persistence.api.dao.UserDAO;
26  import org.apache.syncope.core.persistence.api.entity.user.User;
27  import org.apache.syncope.core.provisioning.api.PropagationByResource;
28  import org.apache.syncope.core.provisioning.api.data.UserDataBinder;
29  import org.apache.syncope.core.workflow.api.WorkflowException;
30  import org.flowable.engine.delegate.DelegateExecution;
31  
32  public class PasswordReset extends FlowableServiceTask {
33  
34      protected final UserDataBinder dataBinder;
35  
36      protected final UserDAO userDAO;
37  
38      public PasswordReset(final UserDataBinder dataBinder, final UserDAO userDAO) {
39          this.dataBinder = dataBinder;
40          this.userDAO = userDAO;
41      }
42  
43      @Override
44      protected void doExecute(final DelegateExecution execution) {
45          User user = execution.getVariable(FlowableRuntimeUtils.USER, User.class);
46          String token = execution.getVariable(FlowableRuntimeUtils.TOKEN, String.class);
47          String password = execution.getVariable(FlowableRuntimeUtils.PASSWORD, String.class);
48  
49          if (!user.checkToken(token)) {
50              throw new WorkflowException(new IllegalArgumentException("Wrong token: " + token + " for " + user));
51          }
52  
53          user.removeToken();
54  
55          UserUR req = new UserUR.Builder(user.getKey()).
56                  password(new PasswordPatch.Builder().
57                          onSyncope(true).
58                          resources(userDAO.findAllResourceKeys(user.getKey())).
59                          value(password).build()).
60                  build();
61  
62          Pair<PropagationByResource<String>, PropagationByResource<Pair<String, String>>> propInfo =
63                  dataBinder.update(user, req);
64  
65          // report updated user and propagation by resource as result
66          execution.setVariable(FlowableRuntimeUtils.USER, user);
67          execution.setVariable(FlowableRuntimeUtils.USER_UR, req);
68          execution.setVariable(FlowableRuntimeUtils.PROP_BY_RESOURCE, propInfo.getLeft());
69          execution.setVariable(FlowableRuntimeUtils.PROP_BY_LINKEDACCOUNT, propInfo.getRight());
70      }
71  }