Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handled roles from user_roles table for user lookup method #96

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -656,5 +656,6 @@ public final class JsonKey {
public static final String EMAIL_CAPS = "Email";
public static final String MENTORING_ROLES = "mentoring.roles";
public static final String MENTORING = "mentoring";
public static final String USERID= "userid";
private JsonKey() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.sunbird.cassandra.CassandraOperation;
import org.sunbird.common.ElasticSearchHelper;
import org.sunbird.dao.user.UserDao;
import org.sunbird.dao.user.UserLookupDao;
Expand All @@ -22,6 +23,7 @@
import org.sunbird.dto.SearchDTO;
import org.sunbird.exception.ProjectCommonException;
import org.sunbird.exception.ResponseCode;
import org.sunbird.helper.ServiceFactory;
import org.sunbird.keys.JsonKey;
import org.sunbird.logging.LoggerUtil;
import org.sunbird.model.adminutil.AdminUtilRequestData;
Expand Down Expand Up @@ -55,6 +57,7 @@ public class UserServiceImpl implements UserService {
private final OrgService orgService = OrgServiceImpl.getInstance();
private final UserRoleService userRoleService = UserRoleServiceImpl.getInstance();
private final ObjectMapper mapper = new ObjectMapper();
private final CassandraOperation cassandraOperation = ServiceFactory.getInstance();

public static UserService getInstance() {
if (userService == null) {
Expand Down Expand Up @@ -211,6 +214,7 @@ public List<Map<String, Object>> searchUserNameInUserLookup(
public Response userLookUpByKey(
String key, String value, List<String> fields, RequestContext context) {
Response response;
Map<String, List<String>> userRoleMap = new HashMap<>();
if (JsonKey.ID.equalsIgnoreCase(key)) {
List<String> ids = new ArrayList<>(2);
ids.add(value);
Expand All @@ -226,6 +230,28 @@ record -> {
ids.add((String) record.get(JsonKey.USER_ID));
});
response = userDao.getUserPropertiesById(ids, fields, context);
if (fields.contains(JsonKey.ROLES)) {
Response cassandraResponse = cassandraOperation.getRecordsByProperties(
JsonKey.SUNBIRD, JsonKey.USER_ROLES,
Collections.singletonMap(JsonKey.USERID, ids),
Arrays.asList(JsonKey.ROLE, JsonKey.USERID), context);

((List<Map<String, Object>>) cassandraResponse.getResult().get(JsonKey.RESPONSE)).stream()
.filter(userRole -> userRole.get(JsonKey.ROLE) != null && !((String) userRole.get(JsonKey.ROLE)).isEmpty())
.forEach(userRole ->
userRoleMap.computeIfAbsent((String) userRole.get(JsonKey.USER_ID), k -> new ArrayList<>())
.add((String) userRole.get(JsonKey.ROLE))
);

((List<Map<String, Object>>) response.getResult().get(JsonKey.RESPONSE)).stream()
.forEach(user -> {
if (userRoleMap.containsKey((String) user.get(JsonKey.ID))) {
user.put(JsonKey.ROLES, userRoleMap.get((String) user.get(JsonKey.ID)));
}
UserUtility.decryptUserDataFrmES(user);
});
}
return response;
}
for (Map<String, Object> userMap :
(List<Map<String, Object>>) response.getResult().get(JsonKey.RESPONSE)) {
Expand Down
Loading