Hi
I want to write some code to create tasklist filter programmatically.
I found a post from @DanielMeyer from 2014 that showed me a little bit how to do it (Link to Blogpost taskfilter
I had only to change a little thing (bold code) and it works:
taskService.createTaskQuery().taskAssigneeExpression("${ currentUser() }").active();
taskService.createTaskQuery().taskCandidateGroup(ANY);
// create a taskQuery
TaskQuery myTasksQuery = taskService.createTaskQuery().taskAssignee("studi1").active();
FilterService filterService= processEngine.getFilterService();
// save taskQuery as filter
Filter myTasksFilter = filterService.newTaskFilter("Studi1Tasks");
myTasksFilter.setOwner("studi1");
myTasksFilter.setQuery(myTasksQuery);
**String filterId =filterService.saveFilter(myTasksFilter).getId();**
// execute the filter
List<Task> tasks = filterService.listPage(filterId, 0, 20);
Now I want to create a filter that the current user sees his tasks. When I create the filter via tasklist view I use the following settings:
I tried the following but it does not work. I do not know how to set “accessible by all users” via code.
/***************Filter für currentUser************************/
taskService.createTaskQuery().taskAssigneeExpression("${ currentUser() }").active();
taskService.createTaskQuery().taskCandidateGroup(ANY);
// create a taskQuery
TaskQuery myTasksQuery2 = taskService.createTaskQuery().taskAssignee("${ currentUser() }").active();
FilterService filterService2= processEngine.getFilterService();
// save taskQuery as filter
Filter myTasksFilter2 = filterService2.newTaskFilter("CurrentUserList");
myTasksFilter2.setOwner("${ currentUser() }");
myTasksFilter2.setQuery(myTasksQuery2);
String filterId2 =filterService2.saveFilter(myTasksFilter2).getId();
List<Task> tasks2 = filterService2.listPage(filterId2, 0, 20);
Any ideas? Thank you,
Nicole
Hi @NickiMueller ,
As per the blog post,
the built-in expression currentUser() which returns the userId of the currently authenticated user (set through IdentityService.setAuthentication())
If JSF application is used then authenticated user can be set as described in below post
Hi @NickiMueller
This is the code I meant
identityService.setAuthentication(...);
Authentication should be handled by your application then passed to camunda using the above call. You can do that using servlet filter where you call the above setAuthentication() method “The filter should be mapped to the URL pattern of your jsf task forms”. That way the current authentication would be available in the current thread where jsf task form is running.
https://app.camunda.com/jira/browse/CAM-1117
Hi @hassang
I tried to make the filter “accessible for all users” (like here via tasklist view)
with this code:
TaskQuery myTasksQuery2 = taskService.createTaskQuery().taskAssignee("${currentUser()}").active();
FilterService filterService2= processEngine.getFilterService();
Filter myTasksFilter2 = filterService2.newTaskFilter("CurrentUserList");
myTasksFilter2.setOwner("${currentUser()}");
myTasksFilter2.setQuery(myTasksQuery2);
String filterId2 = filterService2.saveFilter(myTasksFilter2).getId();
// execute the filter
List<Task> tasks2 = filterService2.listPage(filterId2, 0, 20);
But it creates the following filter (that does not show the right tasks and is only visible for admin user):
By the way, I do not have an own tasklist, I use the camunda tasklist. So the process initiator is set by camunda tasklist.
I created a little process which I can execute with the admin user. The process executes the code above.
Any more ideas? Thank you a lot,
Nicole
Hi @NickiMueller ,
If I understood you correctly then your need is to create the filter programmatically and to be used by users through the tasklist app.
Below snip should create the intended filter
// create a taskQuery
TaskQuery myTasksQuery = taskService.createTaskQuery().taskAssigneeExpression("${ currentUser() }").active();
// save taskQuery as filter
Filter myTasksFilter = filterService.newTaskFilter("My Tasks");
myTasksFilter.setOwner("<ADMIN_USER>");
myTasksFilter.setQuery(myTasksQuery);
String filterId = filterService.saveFilter(myTasksFilter).getId();
To test it, you need to use it from within tasklist app.
Notice: executing the filter programmatically won’t work as intended.
Hi @NickiMueller ,
To make it accessible by all users , You need also to create the required Authorization either programmatically or through the admin app.
Hi @StephenOTT ,
Each permission added for a specific filter is actually a new created authorization where Read permission is assigned and resource Id is the filter Id (You could see that newly created authorization on camunda Admin)
See below link
https://docs.camunda.org/manual/7.5/webapps/tasklist/filters/
Permissions - Specify which users or groups can see the filter. You can set the filter as globally accessible by selecting the checkbox Accessible by all users. A permission that is set …
Hi @NickiMueller ,
Kindly find below a snip of creating the required authorization using Java API
authorization = authorizationService.createNewAuthorization(Authorization.AUTH_TYPE_GLOBAL);
authorization.setUserId(Authorization.ANY);
authorization.setResource(Resources.FILTER);
authorization.addPermission(Permissions.READ);
authorization.setResourceId(filterId);
authorizationService.saveAuthorization(authorization);
Edit: I had the time to do a simple test.
Below is the delegate code I used to create the required filter
import java.util.logging.Logger;
import org.camunda.bpm.engine.AuthorizationService;
import org.camunda.bpm.engine.FilterService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.authorization.Authorization;
import org.camunda.bpm.engine.authorization.Permissions;
import org.camunda.bpm.engine.authorization.Resources;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.filter.Filter;
import org.camunda.bpm.engine.task.TaskQuery;
public class CreateFilterDelegate implements JavaDelegate {
private final Logger LOGGER = Logger.getLogger(CreateFilterDelegate.class.getName());
@Override
public void execute(DelegateExecution execution) throws Exception {
TaskService taskService;
TaskQuery taskQuery;
FilterService filterService;
Filter taskFilter;
String filterId;
AuthorizationService authorizationService;
Authorization authorization;
taskService = execution.getProcessEngineServices().getTaskService();
taskQuery = taskService.createTaskQuery().taskAssigneeExpression("${ currentUser() }").active();
// save taskQuery as filter
filterService = execution.getProcessEngineServices().getFilterService();
taskFilter = filterService.newTaskFilter("Custom Filter");
taskFilter.setOwner("demo");
taskFilter.setQuery(taskQuery);
filterId = filterService.saveFilter(taskFilter).getId();
LOGGER.info("\nCreated Filter: " + filterId);
authorizationService = execution.getProcessEngineServices().getAuthorizationService();
authorization = authorizationService.createNewAuthorization(Authorization.AUTH_TYPE_GLOBAL);
authorization.setUserId(Authorization.ANY);
authorization.setResource(Resources.FILTER);
authorization.addPermission(Permissions.READ);
authorization.setResourceId(filterId);
authorizationService.saveAuthorization(authorization);
}
}
Notice: authorization should be of type Global (Authorization.AUTH_TYPE_GLOBAL)