Skip to content

Commit

Permalink
Issue #1 feat: code merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindyadav108 committed Nov 2, 2017
2 parents 4d2518c + 6fe6a88 commit 4470fb1
Show file tree
Hide file tree
Showing 10 changed files with 667 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.sunbird.learner.actors.bulkupload.BulkUploadBackGroundJobActor;
import org.sunbird.learner.actors.bulkupload.BulkUploadManagementActor;
import org.sunbird.learner.actors.bulkupload.UserDataEncryptionDecryptionServiceActor;
import org.sunbird.learner.actors.client.ClientManagementActor;
import org.sunbird.learner.actors.geolocation.GeoLocationManagementActor;
import org.sunbird.learner.actors.skill.SkillmanagementActor;
import org.sunbird.learner.actors.fileuploadservice.FileUploadServiceActor;
Expand Down Expand Up @@ -81,6 +82,7 @@ public class RequestRouterActor extends UntypedAbstractActor {
private ActorRef badgesActor;
private ActorRef skillManagementActor;
private ActorRef tenantPrefManagementActor;
private ActorRef clientManagementActor;
private ActorRef geoLocationManagementActor;

private ExecutionContext ec;
Expand Down Expand Up @@ -117,6 +119,7 @@ public class RequestRouterActor extends UntypedAbstractActor {
"userDataEncryptionDecryptionServiceActor";
private static final String SKILL_MANAGEMENT_ACTOR = "skillManagementActor";
private static final String TENANT_PREFERENCE_MNGT_ACTOR = "tenantPreferenceManagementActor";
private static final String CLIENT_MANAGEMENT_ACTOR = "clientManagementActor";
private static final String GEO_LOCATION_MANAGEMENT_ACTOR="geoLocationManagementActor";


Expand Down Expand Up @@ -201,6 +204,7 @@ public RequestRouterActor() {
auditLogManagementActor = getContext().actorOf(Props.create(ActorAuditLogServiceImpl.class), AUDIT_LOG_MGMT_ACTOR);
skillManagementActor = getContext().actorOf(FromConfig.getInstance().props(Props.create(SkillmanagementActor.class)), SKILL_MANAGEMENT_ACTOR);
tenantPrefManagementActor = getContext().actorOf(FromConfig.getInstance().props(Props.create(TenantPreferenceManagementActor.class)), TENANT_PREFERENCE_MNGT_ACTOR);
clientManagementActor = getContext().actorOf(FromConfig.getInstance().props(Props.create(ClientManagementActor.class)), CLIENT_MANAGEMENT_ACTOR);
geoLocationManagementActor = getContext().actorOf(FromConfig.getInstance().props(Props.create(GeoLocationManagementActor.class)), GEO_LOCATION_MANAGEMENT_ACTOR);
ec = getContext().dispatcher();
initializeRouterMap();
Expand Down Expand Up @@ -344,6 +348,9 @@ private void initializeRouterMap() {
routerMap.put(ActorOperations.UPDATE_TENANT_PREFERENCE.getValue(), tenantPrefManagementActor);
routerMap.put(ActorOperations.GET_TENANT_PREFERENCE.getValue(), tenantPrefManagementActor);
routerMap.put(ActorOperations.UPDATE_TC_STATUS_OF_USER.getValue(), tenantPrefManagementActor);
routerMap.put(ActorOperations.REGISTER_CLIENT.getValue(), clientManagementActor);
routerMap.put(ActorOperations.UPDATE_CLIENT_KEY.getValue(), clientManagementActor);
routerMap.put(ActorOperations.GET_CLIENT_KEY.getValue(), clientManagementActor);
routerMap.put(ActorOperations.GET_GEO_LOCATION.getValue() ,geoLocationManagementActor);
routerMap.put(ActorOperations.CREATE_GEO_LOCATION.getValue() ,geoLocationManagementActor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import static org.sunbird.learner.util.Util.isNotNull;
import static org.sunbird.learner.util.Util.isNull;

import akka.actor.UntypedAbstractActor;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.velocity.VelocityContext;
import org.sunbird.cassandra.CassandraOperation;
import org.sunbird.common.Constants;
Expand Down Expand Up @@ -41,6 +44,8 @@
import org.sunbird.services.sso.SSOManager;
import org.sunbird.services.sso.SSOServiceFactory;

import akka.actor.UntypedAbstractActor;

/**
* This actor will handle course enrollment operation .
*
Expand Down Expand Up @@ -121,6 +126,8 @@ public void onReceive(Object message) throws Throwable {
getMediaTypes(actorMessage);
}else if (actorMessage.getOperation().equalsIgnoreCase(ActorOperations.FORGOT_PASSWORD.getValue())) {
forgotPassword(actorMessage);
}else if (actorMessage.getOperation().equalsIgnoreCase(ActorOperations.PROFILE_VISIBILITY.getValue())) {
profileVisibility(actorMessage);
}
else {
ProjectLogger.log("UNSUPPORTED OPERATION");
Expand All @@ -137,6 +144,215 @@ public void onReceive(Object message) throws Throwable {
}
}

/**
* This method will first check user exist with us or not.
* after that it will create private filed Map,
* for creating private field map it will take store value
* from ES and then a separate map for private field and remove those
* field from original map.
* if will user is sending some public field list as well then it will
* take private field values from another ES index and update values under
* original data.
* @param actorMessage
*/
private void profileVisibility(Request actorMessage) {
Map<String, Object> map = (Map) actorMessage.getRequest().get(JsonKey.USER);
String userId = (String)map.get(JsonKey.USER_ID);
List<String> privateList = (List)map.get(JsonKey.PRIVATE);
List<String> publicList = (List)map.get(JsonKey.PUBLIC);
Map<String, Object> esResult =
ElasticSearchUtil.getDataByIdentifier(ProjectUtil.EsIndex.sunbird.getIndexName(),
ProjectUtil.EsType.user.getTypeName(), userId);
if (esResult == null || esResult.size()==0) {
throw new ProjectCommonException(ResponseCode.userNotFound.getErrorCode(),
ResponseCode.userNotFound.getErrorMessage(), ResponseCode.CLIENT_ERROR.getResponseCode());
}
Map<String, Object> esPrivateResult = ElasticSearchUtil
.getDataByIdentifier(ProjectUtil.EsIndex.sunbird.getIndexName(),
ProjectUtil.EsType.userprofilevisibility.getTypeName(), userId);
Map<String,Object> responseMap = new HashMap<>();
if(privateList != null && privateList.size()>0) {
responseMap = handlePrivateVisibility(privateList, esResult,esPrivateResult);
}
if (responseMap != null && responseMap.size() > 0) {
Map<String, Object> privateDataMap =
(Map<String, Object>) responseMap.get(JsonKey.DATA);
if (privateDataMap != null && privateDataMap.size() >= esResult.size()) {
// this will indicate some extra private data is added
esPrivateResult = privateDataMap;
}
}
// now have a check for public field.
if (publicList != null && publicList.size() > 0) {
//this estype will hold all private data of user.
//now collecting values from private filed and it will update
//under original index with public field.
for (String field : publicList) {
if (esPrivateResult.containsKey(field)) {
esResult.put(field,esPrivateResult.get(field) );
} else {
ProjectLogger.log("field value not found inside private index =="+field);
}
}
}
Map<String,String> privateFieldMap = new HashMap<>();
if (privateList != null) {
for (String key : privateList) {
privateFieldMap.put(key, JsonKey.PRIVATE);
}
}
if (privateFieldMap.size()>0) {
updateCassandraWithPrivateFiled(userId, privateFieldMap);
esResult.put(JsonKey.PROFILE_VISIBILITY, privateFieldMap);
}
boolean updateResponse =true; //updateDataInES(esResult, privateFiledMap, userId);
Response response = new Response();
if (updateResponse) {
response.put(JsonKey.RESPONSE, JsonKey.SUCCESS);
} else {
response.put(JsonKey.RESPONSE, JsonKey.FAILURE);
}
sender().tell(response, self());
}


private Map<String,Object> handlePrivateVisibility(
List<String> privateFieldList, Map<String, Object> data,
Map<String, Object> oldPrivateData) {
Map<String, Object> privateFiledMap =
createPrivateFiledMap(data, privateFieldList);
privateFiledMap.putAll(oldPrivateData);
Map<String, String> privateField = new HashMap<>();
if (privateFieldList != null) {
for (String key : privateFieldList) {
privateField.put(key, JsonKey.PRIVATE);
}
}
// update old private field with new requested one.
Set<Entry<String, Object>> set = privateFiledMap.entrySet();
Iterator<Entry<String, Object>> itr = set.iterator();
while (itr.hasNext()) {
Entry<String, Object> entry = itr.next();
if (!privateField.containsKey(entry.getKey())) {
privateField.put(entry.getKey(), JsonKey.PRIVATE);
}
}

Map<String,Object> map = new HashMap<>();
map.put(JsonKey.PRIVATE, privateField);
map.put(JsonKey.DATA, privateFiledMap);
return map;
}


private void handlePublicVisibility (String userId, List<String> publicFieldList,Map<String,Object> data) {

}

/**
* This method will create a private field map and remove those filed from
* original map.
* @param map Map<String, Object> complete save data Map
* @param fields List<String> list of private fields
* @return Map<String, Object> map of private field with their original values.
*/
private Map<String, Object> createPrivateFiledMap(Map<String, Object> map,
List<String> fields) {
Map<String, Object> privateMap = new HashMap<>();
Map<String,List<String>> tempMap = new HashMap<>();
if(fields != null && fields.size()>0) {
for (String field : fields) {
// now if field contains {address.someField,education.someField,jobprofile.someField}
//then we need to remove those filed
if (field.contains(JsonKey.ADDRESS+".")){
tempMap = addPrivateField(JsonKey.ADDRESS, tempMap, field);
} else if (field.contains(JsonKey.EDUCATION+".")) {
tempMap = addPrivateField(JsonKey.EDUCATION, tempMap, field);
}else if (field.contains(JsonKey.JOB_PROFILE+".")) {
tempMap = addPrivateField(JsonKey.EDUCATION, tempMap, field);
}else {
privateMap.put(field, map.get(field));
map.remove(field);
}
}
}
return privateMap;
}


private Map<String, List<String>> addPrivateField(String key,
Map<String, List<String>> map, String privateField) {
if (map.containsKey(key)) {
List<String> list = (List) map.get(key);
list.add(privateField);
} else {
List<String> list = new ArrayList<>();
list.add(privateField);
map.put(key, list);
}
return map;
}

@SuppressWarnings("unchecked")
private void updatePrivateKey(List<String> keys, Map<String, Object> data,
Map<String, Object> privateMap, String attribute) {
if (keys == null || keys.size() == 0)
return;
List<Map<String, Object>> reqData =
(List<Map<String, Object>>) data.get(attribute);
List<Map<String, Object>> privateList = new ArrayList<>();
if (reqData != null && reqData.size() > 0) {
for (Map<String, Object> map : reqData) {
Map<String, Object> innerPrivateMap = new HashMap<>();
for (String key : keys) {
innerPrivateMap.put(key, map.get(key));
map.remove(key);
}
privateList.add(innerPrivateMap);
}
privateMap.put(attribute, privateList);
}
}



/**
* THis methods will update user private field under cassandra.
* @param userId Stirng
* @param privateFieldMap Map<String,String>
*/
private void updateCassandraWithPrivateFiled (String userId, Map<String,String> privateFieldMap) {
Util.DbInfo usrDbInfo = Util.dbInfoMap.get(JsonKey.USER_DB);
Map<String,Object> reqMap = new HashMap<>();
reqMap.put(JsonKey.ID, userId);
reqMap.put(JsonKey.PROFILE_VISIBILITY, privateFieldMap);
Response response = cassandraOperation.updateRecord(usrDbInfo.getKeySpace(), usrDbInfo.getTableName(),reqMap);
String val =(String)response.get(JsonKey.RESPONSE);
ProjectLogger.log("Private field updated under cassandra==" + val);
}

/**
* This method will first removed the remove the saved private data for the user and then
* it will create new private data for that user.
* @param dataMap Map<String, Object> allData
* @param privateDataMap Map<String, Object> only private data.
* @param userId String
* @return boolean
*/
private boolean updateDataInES(Map<String, Object> dataMap,
Map<String, Object> privateDataMap, String userId) {
boolean response = false;
ElasticSearchUtil.removeData(ProjectUtil.EsIndex.sunbird.getIndexName(),
ProjectUtil.EsType.userprofilevisibility.getTypeName(), userId);
ElasticSearchUtil.createData(ProjectUtil.EsIndex.sunbird.getIndexName(),
ProjectUtil.EsType.userprofilevisibility.getTypeName(), userId,
privateDataMap);
response =
ElasticSearchUtil.updateData(ProjectUtil.EsIndex.sunbird.getIndexName(),
ProjectUtil.EsType.user.getTypeName(), userId, dataMap);
return response;
}

/**
* This method will verify the loginId or email key against cassandra db.
* if user is found then it will send temporary password to user
Expand Down Expand Up @@ -674,6 +890,7 @@ private void updateUser(Request actorMessage) {
} else {
userMap.put(JsonKey.USER_ID, userMap.get(JsonKey.ID));
}
checkPhoneUniqueness(userMap,JsonKey.UPDATE);
if (null != userMap.get(JsonKey.EMAIL)) {
checkForEmailAndUserNameUniqueness(userMap, usrDbInfo);
}
Expand All @@ -697,7 +914,7 @@ private void updateUser(Request actorMessage) {
userMap.remove(JsonKey.LOGIN_ID);

if (isSSOEnabled) {
UpdateKeyCloakUserBase(userMap);
updateKeyCloakUserBase(userMap);
}
userMap.put(JsonKey.UPDATED_DATE, ProjectUtil.getFormattedDate());
userMap.put(JsonKey.UPDATED_BY, req.get(JsonKey.REQUESTED_BY));
Expand Down Expand Up @@ -992,9 +1209,17 @@ private void checkForEmailAndUserNameUniqueness(Map<String, Object> userMap, DbI
}
}

private void UpdateKeyCloakUserBase(Map<String, Object> userMap) {
private void updateKeyCloakUserBase(Map<String, Object> userMap) {
try {
String userId = ssoManager.updateUser(userMap);

if(!ProjectUtil.isStringNullOREmpty(userId) && null != userMap.get(JsonKey.PHONE)){
boolean bool = ssoManager.addAttributesToKeyCloak(JsonKey.MOBILE, (String) userMap.get(JsonKey.PHONE), userId);
if(!bool){
ProjectLogger.log("phone not saved for userId "+userId);
}
}

if (!(!ProjectUtil.isStringNullOREmpty(userId) && userId.equalsIgnoreCase(JsonKey.SUCCESS))) {
throw new ProjectCommonException(ResponseCode.userUpdationUnSuccessfull.getErrorCode(),
ResponseCode.userUpdationUnSuccessfull.getErrorMessage(),
Expand Down Expand Up @@ -1027,6 +1252,7 @@ private void createUser(Request actorMessage) {
Map<String, Object> req = actorMessage.getRequest();
Map<String, Object> requestMap = null;
Map<String, Object> userMap = (Map<String, Object>) req.get(JsonKey.USER);
checkPhoneUniqueness(userMap,JsonKey.CREATE);
Map<String, Object> emailTemplateMap = new HashMap<>(userMap);
if (userMap.containsKey(JsonKey.WEB_PAGES)) {
SocialMediaType
Expand Down Expand Up @@ -1166,6 +1392,12 @@ && isNotNull(esResult.get(JsonKey.CONTENT))
userId = responseMap.get(JsonKey.USER_ID);
accessToken = responseMap.get(JsonKey.ACCESSTOKEN);
if (!ProjectUtil.isStringNullOREmpty(userId)) {
if(null != userMap.get(JsonKey.PHONE)){
boolean bool = ssoManager.addAttributesToKeyCloak(JsonKey.MOBILE, (String) userMap.get(JsonKey.PHONE), userId);
if(!bool){
ProjectLogger.log("phone not saved for userId "+userId);
}
}
userMap.put(JsonKey.USER_ID, userId);
userMap.put(JsonKey.ID, userId);
} else {
Expand Down Expand Up @@ -1314,6 +1546,40 @@ && isNotNull(esResult.get(JsonKey.CONTENT))

}

private void checkPhoneUniqueness(Map<String,Object> userMap,String opType) {
String phone = (String) userMap.get(JsonKey.PHONE);
if(!ProjectUtil.isStringNullOREmpty(phone)){
try{
phone = encryptionService.encryptData(phone);
}catch(Exception e){
ProjectLogger.log("Exception occured while encrypting phone number ", e);
}
Map<String,Object> filters = new HashMap<>();
filters.put(JsonKey.ENC_PHONE, phone);
Map<String,Object> map = new HashMap<>();
map.put(JsonKey.FILTERS, filters);
SearchDTO searchDto = Util.createSearchDto(map);
Map<String, Object> result = ElasticSearchUtil.complexSearch(searchDto,
ProjectUtil.EsIndex.sunbird.getIndexName(), ProjectUtil.EsType.user.getTypeName());
List<Map<String, Object>> userMapList =
(List<Map<String, Object>>) result.get(JsonKey.CONTENT);
if(!userMapList.isEmpty()){
if(opType.equalsIgnoreCase(JsonKey.CREATE)){
throw new ProjectCommonException(ResponseCode.PhoneNumberInUse.getErrorCode(),
ResponseCode.PhoneNumberInUse.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}else{
Map<String,Object> user = userMapList.get(0);
if(!(((String)user.get(JsonKey.ID)).equalsIgnoreCase((String)userMap.get(JsonKey.ID)))){
throw new ProjectCommonException(ResponseCode.PhoneNumberInUse.getErrorCode(),
ResponseCode.PhoneNumberInUse.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
}
}
}
}

private void insertOrganisationDetails(Map<String, Object> userMap, DbInfo usrOrgDb) {

Map<String, Object> reqMap = new HashMap<>();
Expand Down
Loading

0 comments on commit 4470fb1

Please sign in to comment.