This article is intended to give you an overview of how can you assign a workflow task to organization role if the definition is applied to other than the organization site. You will need to know the basic concepts of Kaleo workflow in order to understand the concepts described here.
Before we get into the specific Liferay workflow scenario this blog is meant to address, let’s talk a little about Liferay workflow in general. Workflow in Liferay is most commonly used for blog posts, comments, wiki pages, message board messages, and user registration. In regards to Liferay content, workflow helps us to filter and monitor content before it goes live by assigning the task to a user or a role for the approval. The “out of the box” single approver workflow is most commonly used in Liferay. If the approver finds the content appropriate, they can accept the task, approve it, and the content is live. If the content is not appropriate, the approver can reject it. At that time, the workflow can do many things such as getting assigned back to the creator of the content. This helps maintain the integrity of the content within the site. Here are a few workflow scenarios.
Workflow assigned to a Regular Role
In a normal scenario, the approval of a workflow task is assigned to a regular role. This means every user in Liferay with that regular role has rights to accept the workflow task. This works well for organizations that have dedicated content editors & authors that work on many sites concurrently or if you only have one Liferay site. This is the most basic use case for managing content with Liferay workflow.
Workflow assigned to an Site Role
In another scenario, the content posted in a site is assigned to a site role. In this scenario, the workflow task can be approved/denied only by users who are the member of the same site where the content resides and have that site role. If other users have the same site role but are not the member of that Liferay site where the content exists, they will not be assigned to the workflow approval task. This was done intentionally to ensure only members of the site with the necessary role are assigned that workflow task. This works well if your content editors & authors have dedicated Liferay sites that they are responsible for maintaining.
Workflow assigned to an Organizational Role
The same principle applies to the organization roles as site roles. If the workflow assignment is set to an organization role, the workflow task will only get assigned to users with the specific organization role rights to accept the task and only if the workflow definition is applied to an application in an organization site. This is the less common scenario, I’d like to discuss further.
In this particular scenario, we would like to assign user registration workflow to be approved by the appropriate users within the organization the newly registered user is associated with. Therefore we need to assign the workflow task to an organization role even when the workflow definition is not in the organization site. The registration form will need to include some custom fields used to lookup the appropriate organization where they new user should reside. Then we need to assign the workflow task to the “Organization Administrator” role associated with that organization. There is no direct way to do this, but there is a workaround for achieving this goal.
First, you have to get the organization where the user should reside and where you want to assign the workflow task. We looked up the organization using custom fields on the registration form. For example, you can use a company name field & email address domain to lookup the correct organization in Liferay. Next, you need to add the user to the organization:
user = UserLocalServiceUtil.getUser(classPK);
UserLocalServiceUtil.addOrganizationUsers(organizationPk, classPK);
UserLocalServiceUtil.updateUser(user);
long classPK = GetterUtil.getLong((String) workflowContext.get(
WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
- The organizationPk is the primary key of the organization. This is the organization we will assign the user and it will be used for workflow assignment.
- The classPK is primary key of the context entry, which in this case will be the user’s primary key (aka userId).
Now to assign the task to an organization role, you need to get that role. But first, you have to set the group id of the workflowInstanceLink, workflowContext and serviceContext same as the organization’s groupId so that the organization role will get permission to view and approve the task:
WorkflowInstanceLink workflowInstanceLink =
WorkflowInstanceLinkLocalServiceUtil.getWorkflowInstanceLink(
serviceContext.getCompanyId(),
serviceContext.getScopeGroupId(),
className, classPK);
workflowInstanceLink.setGroupId(organization.getGroupId());
workflowContext.groupId = String.valueOf(organization.getGroupId());
serviceContext.setScopeGroupId(organization.getGroupId());
Role role = RoleLocalServiceUtil.getRole(organization.getCompanyId(),
"Organization Administrator");
roles = new ArrayList();
roles.add(role);
serviceContext.setAttribute("rolesAssignment",roles);
The “rolesAssignment” attribute contains the role that should be assigned to the workflow task. We can now add the following in the scripted assignment of the task to assign it to the organization role even if the workflow definition is not in the organization site:
roles = serviceContext.getAttribute("rolesAssignment");
user = null;
Summary
Liferay workflow is an efficient way to implement the approval based task to filter out the inappropriate content or users from your site. This blog post was written to show how can you assign the organization roles for approval of the workflow task even if the definition of the workflow is applied outside of an organization site.
If you have questions or need help, please engage with us via comments on this blog post, or reach out to us at https://www.xtivia.com/contact/ or [email protected].
Additional Reading
You can continue to explore Liferay DXP by checking out Publishing Typed Content With Liferay DXP’s Asset Publisher for utilizing the Asset Publisher to publish typed content, or Introducing Speedray Architecture Pattern for Liferay DXP to learn about the Speedray Architecture Pattern, or The Top 10 New Features in Liferay DXP 7 from a functional perspective, or Top 5 New Features in Liferay DXP UI Development and Creating JAX-RS REST Services in Liferay DXP from a development perspective, or Top 5 DevOps Features in Liferay DXP from a devops perspective.