AnsweredAssumed Answered

Activiti Spring Boot USer/Groups Memebership

Question asked by neohadoop on Jul 14, 2016
Please, Do not dismiss it as same old question!!

Questions:
1) With spring boot app, we are using Activiti for workflow, How to keep spring boot app User/Roles/Groups in sync with Activity Users/Group/Memberships?
2) We do not want to update Activiti User/Groups if there is an update on the Spring Boot App's Users/Roles. Is there a way around it?

Tools Being Used:
1) Spring Boot with JPA
2) Activiti 5.19 for workflow
3) Thymeleaf +Spring MVC (NO vaadin or NO Activiti Explorer forms)
4) MySQL 5.5

Business Requirements for Workflow:
1) Assign a task to group -> Group member claims and completes task -> Send to next group -> Complete the task -> End State
Status of document is managed with workflow.

Boot Starters:
<code>
<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
      <dependency>
         <groupId>org.activiti</groupId>
         <artifactId>activiti-spring-boot-starter-jpa</artifactId>
         <version>5.19.0</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
      <dependency>
         <groupId>org.activiti</groupId>
         <artifactId>activiti-spring-boot-starter-security</artifactId>
         <version>5.19.0</version>
      </dependency>
        <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-aop</artifactId>
      </dependency>
        <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-ldap</artifactId>
      </dependency>
      <dependency>
           <groupId>org.springframework.ldap</groupId>
           <artifactId>spring-ldap-core</artifactId>
           <version>2.1.0.RELEASE</version>
       </dependency>
<code>

Spring Boot APP User:

<java>
package XXX.YYYY.ZZZZZ.services.impl;

import XXX.YYYY.ZZZZZ.exceptions.NotSupportedException;
import XXX.YYYY.ZZZZZ.models.User;
import XXX.YYYY.ZZZZZ.models.UserRoleGroup;
import XXX.YYYY.ZZZZZ.repositories.UserRepository;
import XXX.YYYY.ZZZZZ.services.IUserService;
import XXX.YYYY.ZZZZZ.util.CurrentUserUtil;
import org.activiti.engine.IdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.persistence.NonUniqueResultException;
import java.io.Serializable;
import java.util.List;
import java.util.Map.Entry;

@Service("userService")
public class UserService implements IUserService {

   @Autowired
   private UserRepository repo;

   @Autowired
   private IdentityService identityService;

   @Override
   public User addUserGroup(User user) {
      UserRoleGroup urg = UserRoleGroup.getNewInstance();
      urg.setUser(user);
      urg.setGroup(user.getRole().getGroup());
      user.getGroups().add(urg);
      return user;
   }

   @Override
   public User create(User model) {
      User appUser = repo.saveAndFlush(model);
      //Add this new user to workflow
      addUserToWorkflow(appUser, false);
      return appUser;
   }

   @Override
   public boolean delete(Serializable key) {
      repo.delete((String) key);
      return true;
   }

   @Override
   public User findByUsername(String username){
      List<User> users = repo.findByUsername(username);
      if (users.size() > 1)
         throw new NonUniqueResultException("There is more than one User with username " + username);
      if (users.size() == 1)
         return users.get(0);
      return null;
   }
   
   
   private List<User> getExistingUsers(String username) {
      List<User> users = repo.findByUsername(username);
      return users;
   }

   
   @Override
   public User update(User model) {
      User updatedAppUser = repo.saveAndFlush(model);
      //Update the Workflow user
      addUserToWorkflow(updatedAppUser, true);
      return updatedAppUser;
   }
   
   //HOW to keep MY App user information and Group Memebership in Activiti in sync???
   private void addUserToWorkflow(User appUser, Boolean isUserUpdated){
      //Now create workflow membership, groups for the user
      if(appUser != null && appUser.getId() != null) {
         if (!isUserUpdated && identityService.createUserQuery().userId(appUser.getId().toString()).count() == 0) {

            org.activiti.engine.identity.User wfUser = identityService.newUser(appUser.getId().toString());
            wfUser.setFirstName(appUser.getFirstName());
            wfUser.setLastName(appUser.getLastName());
            identityService.saveUser(wfUser);
            if (appUser != null && appUser.getRole() != null && appUser.getRole().getGroup() != null) {
                  identityService.createMembership(appUser.getId().toString(), appUser.getRole().getGroup().getValue());
            }
         } else {
            org.activiti.engine.identity.User existingWFUser = identityService.createUserQuery().userId(appUser.getId()).singleResult();
            existingWFUser.setFirstName(appUser.getFirstName());
            existingWFUser.setLastName(appUser.getLastName());
            identityService.saveUser(existingWFUser);
            if (appUser != null && appUser.getRole() != null && appUser.getRole().getGroup() != null) {

               identityService.createMembership(appUser.getId().toString(), appUser.getRole().getGroup().getValue());
            }
         }
      }
   }
   
}
<java>


Thanks.

Outcomes