Hi,
I use Camunda 7.11. with JSF and EJB and Wildfly 16. My problem is that I try to get the user via
processEngine.getIdentityService().createUserQuery().userId(thmUid).singleResult();
and it works sometimes and sometimes not. (sometimes the user is null)
I tried to implement a custom tasklist. (based on the example from @Ingo_Richtsmeier) I added a login page which appears if a user is not logged in (I added an authenticationfilter).
AuthenticationFilter
public class AuthenticationFilter implements Filter{
public void init(FilterConfig arg0) throws ServletException {}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
HttpSession session = request.getSession(false);
String loginURI = request.getContextPath() + "/login.xhtml";
boolean loggedIn = session != null && session.getAttribute("user") != null;
boolean loginRequest = request.getRequestURI().equals(loginURI);
boolean resourceRequest = request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER);
if (loggedIn || loginRequest || resourceRequest) {
chain.doFilter(request, response);
} else {
response.sendRedirect(loginURI);
}
}
LoginBean.java checks if a userid can be found in another application (Rolemanager) and then it checks if the user exists in camunda.
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.camunda.bpm.engine.IdentityService;
import de.thm.mnd.ofv.businesslogic.thesis.ThesisWKUserLogic;
import de.thm.mnd.ofv.util.OFVLogger;
@Named
@RequestScoped
public class LoginBean {
@Inject
private ThesisWKUserLogic thesisWKUserLogic;
@Inject
private IdentityService identityService;
private String thmUid;
private String password;
private boolean userIsAdmin;
public void login() {
FacesContext context = FacesContext.getCurrentInstance();
ArrayList <String> userGroups = new ArrayList<String>();
if(thesisWKUserLogic.isUserExistingInRoleManager(thmUid)) {
if (thesisWKUserLogic.**isUserExistingInCamunda**(thmUid)) {
if(thesisWKUserLogic.isUserMemberOfAdminGroup(thmUid)) {
userGroups.add("camunda-admin");
userIsAdmin = true;
}
if(thesisWKUserLogic.isUserMemberOfSekiGroup(thmUid)) {
userGroups.add("SekiGroup");
}
if(thesisWKUserLogic.isUserMemberOfStudentGroup(thmUid)) {
userGroups.add("StudentGroup");
}
//identityService füllen, um in CDI Beans über identityService.getCurrentAuthentication().getUserId();
//den User auslesen zu können
identityService.setAuthentication(thmUid,userGroups);
//userName in Session setzen
context.getExternalContext().getSessionMap().put("user", thmUid);
}
else {
context.addMessage(null, new FacesMessage("Authentication Failed. User existiert nicht in Camunda."));
}
}
else {
context.addMessage(null, new FacesMessage("Authentication Failed. User existiert nicht in Rollenmanager."));
}
try {
if(userIsAdmin) {
OFVLogger.getLogger().finer("Login - User ist Admin, lade Admintasklist");
//TaskList für Admin laden
String requestContextPath = context.getExternalContext().getRequestContextPath();
String path = requestContextPath+"/tasklist/taskList_admin.xhtml";
context.getExternalContext().redirect(path);
}
else {
OFVLogger.getLogger().finer("Login - User ist kein Admin, lade normale Tasklist");
String requestContextPath = context.getExternalContext().getRequestContextPath();
String path = requestContextPath+"/tasklist/taskList.xhtml";
context.getExternalContext().redirect(path);
}
} catch (IOException e) {
e.printStackTrace();
}
}
ThesisWKUserLogic.java is stateless ejb that calls a method with the same name in a class “CamundaDelegate.java”:
public boolean isUserExistingInCamunda(String thmUid){
if(camundaDelegate.isUserExistingInCamunda(thmUid)) {
return true;
}
else return false;
}
CamundaDelegate.java:
public class CamundaDelegate implements Serializable{
private static final long serialVersionUID = 1L;
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
public boolean isUserExistingInCamunda(String thmUid) {
User camundaUser = processEngine.getIdentityService().createUserQuery().userId(thmUid).singleResult();
if(camundaUser!=null) {
return true;
}
return false;
}
Here is a description of what I am doing:
I call the page: http://localhost:8080/ofv2/login.xhtml and try to login with user studi1. That works. I press logout button (that calls the LoginBean again)
public void logout() {
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getSessionMap().remove("username");
context.getExternalContext().invalidateSession();
try {
context.getExternalContext().redirect("login.xhtml");
} catch (IOException e) {
e.printStackTrace();
}
}
Now I try to log in with my admin user. But it can not be found
If I now stop and start wildfly again and I try first to login with ofv2admin, it works.
When I then try to login with studi1, it works. But if I then try to login with ofv2admin, the user can not be found.
Do you have any ideas about this strange behaviour? And do I understand it right that
processEngine.getIdentityService().createUserQuery().userId(thmUid).singleResult();
executes a database query?
Thanks a lot,
Nicole