use of org.wso2.carbon.identity.rest.api.server.challenge.v1.dto.ChallengeQuestionDTO in project carbon-identity-framework by wso2.
the class UserIdentityManagementAdminService method setChallengeQuestionsOfUser.
/**
* set challenges of user
*
* @param userName bean class that contains user and tenant Information
* @throws IdentityMgtServiceException if fails
*/
public void setChallengeQuestionsOfUser(String userName, UserChallengesDTO[] challengesDTOs) throws IdentityMgtServiceException {
if (challengesDTOs == null || challengesDTOs.length < 1) {
log.error("no challenges provided by user");
throw new IdentityMgtServiceException("no challenges provided by user");
}
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
String loggedInName = CarbonContext.getThreadLocalCarbonContext().getUsername();
if (userName != null && !userName.equals(loggedInName)) {
AuthorizationManager authzManager = null;
try {
authzManager = IdentityMgtServiceComponent.getRealmService().getTenantUserRealm(tenantId).getAuthorizationManager();
} catch (UserStoreException e) {
throw new IdentityMgtServiceException("Error occurred while retrieving AuthorizationManager for tenant " + tenantDomain, e);
}
boolean isAuthorized = false;
try {
isAuthorized = authzManager.isUserAuthorized(loggedInName, "/permission/admin/manage/identity/identitymgt/update", CarbonConstants.UI_PERMISSION_ACTION);
} catch (UserStoreException e) {
throw new IdentityMgtServiceException("Error occurred while checking access level for " + "user " + userName + " in tenant " + tenantDomain, e);
}
if (!isAuthorized) {
throw new IdentityMgtServiceException("Unauthorized access!! Possible elevation of privilege attack. " + "User " + loggedInName + " trying to change challenge questions for user " + userName);
}
} else if (userName == null) {
userName = loggedInName;
}
validateSecurityQuestionDuplicate(challengesDTOs);
ChallengeQuestionProcessor processor = IdentityMgtServiceComponent.getRecoveryProcessor().getQuestionProcessor();
try {
List<ChallengeQuestionDTO> challengeQuestionDTOs = processor.getAllChallengeQuestions();
for (UserChallengesDTO userChallengesDTO : challengesDTOs) {
boolean found = false;
for (ChallengeQuestionDTO challengeQuestionDTO : challengeQuestionDTOs) {
if (challengeQuestionDTO.getQuestion().equals(userChallengesDTO.getQuestion()) && challengeQuestionDTO.getQuestionSetId().equals(userChallengesDTO.getId())) {
found = true;
break;
}
}
if (!found) {
String errMsg = "Error while persisting user challenges for user : " + userName + ", because these user challengers are not registered with the tenant";
log.error(errMsg);
throw new IdentityMgtServiceException(errMsg);
}
}
processor.setChallengesOfUser(userName, tenantId, challengesDTOs);
} catch (IdentityException e) {
String errorMessage = "Error while persisting user challenges for user : " + userName;
log.error(errorMessage, e);
throw new IdentityMgtServiceException(errorMessage);
}
}
use of org.wso2.carbon.identity.rest.api.server.challenge.v1.dto.ChallengeQuestionDTO in project carbon-identity-framework by wso2.
the class UserIdentityManagementAdminService method getAllPromotedUserChallenge.
/**
* get all promoted user challenges
*
* @return array of user challenges
* @throws IdentityMgtServiceException if fails
*/
public UserChallengesSetDTO[] getAllPromotedUserChallenge() throws IdentityMgtServiceException {
ChallengeQuestionProcessor processor = IdentityMgtServiceComponent.getRecoveryProcessor().getQuestionProcessor();
List<UserChallengesSetDTO> challengeQuestionSetDTOs = new ArrayList<UserChallengesSetDTO>();
List<ChallengeQuestionDTO> questionDTOs = null;
try {
questionDTOs = processor.getAllChallengeQuestions();
} catch (IdentityException e) {
log.error("Error while loading user challenges", e);
throw new IdentityMgtServiceException("Error while loading user challenges");
}
Map<String, List<UserChallengesDTO>> listMap = new HashMap<String, List<UserChallengesDTO>>();
for (ChallengeQuestionDTO dto : questionDTOs) {
List<UserChallengesDTO> dtoList = listMap.get(dto.getQuestionSetId());
if (dtoList == null) {
dtoList = new ArrayList<UserChallengesDTO>();
}
UserChallengesDTO userChallengesDTO = new UserChallengesDTO();
userChallengesDTO.setId(dto.getQuestionSetId());
userChallengesDTO.setQuestion(dto.getQuestion());
userChallengesDTO.setOrder(dto.getOrder());
dtoList.add(userChallengesDTO);
listMap.put(dto.getQuestionSetId(), dtoList);
}
for (Map.Entry<String, List<UserChallengesDTO>> listEntry : listMap.entrySet()) {
UserChallengesSetDTO dto = new UserChallengesSetDTO();
dto.setId(listEntry.getKey());
List<UserChallengesDTO> dtoList = listEntry.getValue();
dto.setChallengesDTOs(dtoList.toArray(new UserChallengesDTO[dtoList.size()]));
challengeQuestionSetDTOs.add(dto);
}
return challengeQuestionSetDTOs.toArray(new UserChallengesSetDTO[challengeQuestionSetDTOs.size()]);
}
use of org.wso2.carbon.identity.rest.api.server.challenge.v1.dto.ChallengeQuestionDTO in project identity-api-server by wso2.
the class ServerChallengeService method getChallengeSetDTO.
private ChallengeSetDTO getChallengeSetDTO(String questionSetId, List<ChallengeQuestion> questions) {
ChallengeSetDTO challenge = new ChallengeSetDTO();
challenge.setQuestionSetId(questionSetId);
List<ChallengeQuestionDTO> questionDTOs = questions.stream().map(new ChallengeQuestionToExternal()).collect(Collectors.toList());
challenge.setQuestions(questionDTOs);
return challenge;
}
use of org.wso2.carbon.identity.rest.api.server.challenge.v1.dto.ChallengeQuestionDTO in project identity-api-server by wso2.
the class ChallengeQuestionToExternal method apply.
@Override
public ChallengeQuestionDTO apply(ChallengeQuestion challengeQuestion) {
ChallengeQuestionDTO question = new ChallengeQuestionDTO();
question.setLocale(challengeQuestion.getLocale());
question.setQuestion(challengeQuestion.getQuestion());
question.setQuestionId(challengeQuestion.getQuestionId());
return question;
}
use of org.wso2.carbon.identity.rest.api.server.challenge.v1.dto.ChallengeQuestionDTO in project carbon-identity-framework by wso2.
the class ChallengeQuestionProcessor method setChallengeQuestions.
/**
* @param questionDTOs
* @throws IdentityException
*/
public void setChallengeQuestions(ChallengeQuestionDTO[] questionDTOs) throws IdentityException {
Registry registry = null;
try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
registry = IdentityMgtServiceComponent.getRegistryService().getConfigSystemRegistry(tenantId);
if (!registry.resourceExists(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH)) {
Collection securityQuestionResource = registry.newCollection();
registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH, securityQuestionResource);
}
Resource identityMgtResource = registry.get(IdentityMgtConstants.IDENTITY_MANAGEMENT_PATH);
if (identityMgtResource != null) {
String questionCollectionPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS;
if (registry.resourceExists(questionCollectionPath)) {
registry.delete(questionCollectionPath);
}
Collection questionCollection = registry.newCollection();
registry.put(questionCollectionPath, questionCollection);
for (int i = 0; i < questionDTOs.length; i++) {
Resource resource = registry.newResource();
resource.addProperty("question", questionDTOs[i].getQuestion());
resource.addProperty("isPromoteQuestion", String.valueOf(questionDTOs[i].isPromoteQuestion()));
resource.addProperty("questionSetId", questionDTOs[i].getQuestionSetId());
registry.put(IdentityMgtConstants.IDENTITY_MANAGEMENT_QUESTIONS + RegistryConstants.PATH_SEPARATOR + "question" + i + RegistryConstants.PATH_SEPARATOR, resource);
}
}
} catch (RegistryException e) {
throw IdentityException.error("Error while setting challenge question.", e);
}
}
Aggregations