Skip to content

Commit 88429b7

Browse files
Merge pull request #77 from PasinduYeshan/fix/wf-self-reg
Handle SELF_REGISTER_USER operation type in workflow
2 parents 182f19b + 4ee25e5 commit 88429b7

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

components/org.wso2.carbon.user.mgt.workflow/src/main/java/org/wso2/carbon/user/mgt/workflow/internal/IdentityWorkflowServiceComponent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.wso2.carbon.user.mgt.workflow.userstore.DeleteRoleWFRequestHandler;
4141
import org.wso2.carbon.user.mgt.workflow.userstore.DeleteUserWFRequestHandler;
4242
import org.wso2.carbon.user.mgt.workflow.userstore.RoleManagementActionListener;
43+
import org.wso2.carbon.user.mgt.workflow.userstore.SelfRegisterUserWFRequestHandler;
4344
import org.wso2.carbon.user.mgt.workflow.userstore.SetMultipleClaimsWFRequestHandler;
4445
import org.wso2.carbon.user.mgt.workflow.userstore.UpdateRoleNameWFRequestHandler;
4546
import org.wso2.carbon.user.mgt.workflow.userstore.UpdateRoleUsersWFRequestHandler;
@@ -152,6 +153,9 @@ protected void activate(ComponentContext context) {
152153
bundleContext.registerService(WorkflowRequestHandler.class.getName(), new DeleteRoleWFRequestHandler(), null);
153154
bundleContext.registerService(WorkflowRequestHandler.class.getName(), new UpdateRoleV2UsersWFRequestHandler(),
154155
null);
156+
bundleContext.registerService(WorkflowRequestHandler.class.getName(), new SelfRegisterUserWFRequestHandler(),
157+
null);
158+
155159
// todo: commenting out for a test failure
156160
// bundleContext.registerService(WorkflowRequestHandler.class.getName(), new ChangeCredentialWFRequestHandler(),
157161
// null);

components/org.wso2.carbon.user.mgt.workflow/src/main/java/org/wso2/carbon/user/mgt/workflow/userstore/AddUserWFRequestHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ public boolean isValidOperation(Entity[] entities) throws WorkflowException {
297297
ERROR_CODE_USER_WF_USER_ALREADY_EXISTS.getCode());
298298
// Check if user already exists in pending add user workflow.
299299
} else if (workflowService
300-
.entityHasPendingWorkflowsOfType(entity, UserStoreWFConstants.ADD_USER_EVENT)) {
300+
.entityHasPendingWorkflowsOfType(entity, UserStoreWFConstants.ADD_USER_EVENT) ||
301+
workflowService.entityHasPendingWorkflowsOfType(entity,
302+
UserStoreWFConstants.SELF_REGISTER_USER_EVENT)) {
301303
throw new WorkflowException(ERROR_CODE_USER_WF_ALREADY_EXISTS.getMessage(),
302304
ERROR_CODE_USER_WF_ALREADY_EXISTS.getCode());
303305
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.user.mgt.workflow.userstore;
20+
21+
import org.wso2.carbon.user.mgt.workflow.util.UserStoreWFConstants;
22+
23+
/**
24+
* User self registration workflow request handler.
25+
*/
26+
public class SelfRegisterUserWFRequestHandler extends AddUserWFRequestHandler {
27+
28+
private static final String FRIENDLY_NAME = "Self Register User";
29+
private static final String FRIENDLY_DESCRIPTION = "Triggered when a new user is self registered.";
30+
31+
@Override
32+
public String getEventId() {
33+
34+
return UserStoreWFConstants.SELF_REGISTER_USER_EVENT;
35+
}
36+
37+
@Override
38+
public String getFriendlyName() {
39+
40+
return FRIENDLY_NAME;
41+
}
42+
43+
@Override
44+
public String getDescription() {
45+
46+
return FRIENDLY_DESCRIPTION;
47+
}
48+
}

components/org.wso2.carbon.user.mgt.workflow/src/main/java/org/wso2/carbon/user/mgt/workflow/userstore/UserStoreActionListener.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.apache.commons.logging.LogFactory;
2424
import org.wso2.carbon.context.PrivilegedCarbonContext;
2525
import org.wso2.carbon.identity.core.AbstractIdentityUserOperationEventListener;
26+
import org.wso2.carbon.identity.core.context.IdentityContext;
27+
import org.wso2.carbon.identity.core.context.model.Flow;
2628
import org.wso2.carbon.identity.core.model.IdentityEventListenerConfig;
2729
import org.wso2.carbon.identity.core.util.IdentityCoreConstants;
2830
import org.wso2.carbon.identity.core.util.IdentityUtil;
@@ -100,12 +102,23 @@ public int getExecutionOrderId() {
100102
public boolean doPreAddUser(String userName, Object credential, String[] roleList, Map<String, String> claims,
101103
String profile, UserStoreManager userStoreManager) throws UserStoreException {
102104

103-
if (!isEnable() || isCalledViaIdentityMgtListners()
104-
|| !isEventAssociatedWithWorkflow(UserStoreWFConstants.ADD_USER_EVENT) || isJITProvisioningFlow()
105+
if (!isEnable() || isCalledViaIdentityMgtListners() || isJITProvisioningFlow()
105106
|| UserStoreWFUtils.isAgentUserStore(userStoreManager)) {
106107
return true;
107108
}
108109

110+
Flow flow = IdentityContext.getThreadLocalIdentityContext().getCurrentFlow();
111+
String eventType = UserStoreWFConstants.ADD_USER_EVENT;
112+
if (flow != null && Flow.Name.REGISTER.equals(flow.getName()) &&
113+
Flow.InitiatingPersona.USER.equals(flow.getInitiatingPersona())) {
114+
log.debug("User self registration flow is detected.");
115+
eventType = UserStoreWFConstants.SELF_REGISTER_USER_EVENT;
116+
}
117+
118+
if (!isEventAssociatedWithWorkflow(eventType)) {
119+
return true;
120+
}
121+
109122
if (claims != null && claims.containsKey(CLAIM_MANAGED_ORGANIZATION)) {
110123
// User add workflow is not supported for the shared users.
111124
return true;
@@ -114,7 +127,12 @@ public boolean doPreAddUser(String userName, Object credential, String[] roleLis
114127
validatePassword(credential, userName, roleList, claims, profile, userStoreManager);
115128

116129
try {
117-
AddUserWFRequestHandler addUserWFRequestHandler = new AddUserWFRequestHandler();
130+
AddUserWFRequestHandler addUserWFRequestHandler;
131+
if (UserStoreWFConstants.SELF_REGISTER_USER_EVENT.equals(eventType)) {
132+
addUserWFRequestHandler = new SelfRegisterUserWFRequestHandler();
133+
} else {
134+
addUserWFRequestHandler = new AddUserWFRequestHandler();
135+
}
118136
doPasswordPolicyValidation(userName, credential, userStoreManager, addUserWFRequestHandler);
119137
String domain = userStoreManager.getRealmConfiguration()
120138
.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
@@ -439,7 +457,7 @@ private void doPasswordPolicyValidation(String userName, Object credential, User
439457
// Check if add_user operation is engaged with a workflow or not.
440458
if (!addUserWFRequestHandler.isAssociated()) {
441459
/*
442-
This password policy pattern validation wil be done in later step from governance listeners.
460+
This password policy pattern validation will be done in later step from governance listeners.
443461
So skip this validation in this stage if workflows are not enabled for add user operation.
444462
*/
445463
return;

components/org.wso2.carbon.user.mgt.workflow/src/main/java/org/wso2/carbon/user/mgt/workflow/util/UserStoreWFConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class UserStoreWFConstants {
2727

2828

2929
public static final String ADD_USER_EVENT = "ADD_USER";
30+
public static final String SELF_REGISTER_USER_EVENT = "SELF_REGISTER_USER";
3031
public static final String ADD_ROLE_EVENT = "ADD_ROLE";
3132
public static final String DELETE_USER_EVENT = "DELETE_USER";
3233
public static final String DELETE_ROLE_EVENT = "DELETE_ROLE";

0 commit comments

Comments
 (0)