use of org.wso2.carbon.user.core.UserStoreManager in project carbon-apimgt by wso2.
the class APIUtil method getClaims.
/**
* Returns the user claims for the given user.
*
* @param endUserName name of the user whose claims needs to be returned
* @param tenantId tenant id of the user
* @param dialectURI claim dialect URI
* @return claims map
* @throws APIManagementException
*/
public static SortedMap<String, String> getClaims(String endUserName, int tenantId, String dialectURI) throws APIManagementException {
SortedMap<String, String> claimValues;
try {
ClaimManager claimManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getClaimManager();
ClaimMapping[] claims = claimManager.getAllClaimMappings(dialectURI);
String[] claimURIs = claimMappingtoClaimURIString(claims);
UserStoreManager userStoreManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getUserStoreManager();
String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(endUserName);
claimValues = new TreeMap(userStoreManager.getUserClaimValues(tenantAwareUserName, claimURIs, null));
return claimValues;
} catch (UserStoreException e) {
throw new APIManagementException("Error while retrieving user claim values from user store", e);
}
}
use of org.wso2.carbon.user.core.UserStoreManager in project carbon-apimgt by wso2.
the class APIUtil method getListOfRoles.
/**
* Retrieves the role list of a user
*
* @param username A username
* @param username A username
* @throws APIManagementException If an error occurs
*/
public static String[] getListOfRoles(String username) throws APIManagementException {
if (username == null) {
throw new APIManagementException("Attempt to execute privileged operation as" + " the anonymous user");
}
String[] roles = null;
roles = getValueFromCache(APIConstants.API_USER_ROLE_CACHE, username);
if (roles != null) {
return roles;
}
String tenantDomain = MultitenantUtils.getTenantDomain(username);
try {
if (!org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
UserStoreManager manager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getUserStoreManager();
roles = manager.getRoleListOfUser(MultitenantUtils.getTenantAwareUsername(username));
} else {
roles = AuthorizationManager.getInstance().getRolesOfUser(MultitenantUtils.getTenantAwareUsername(username));
}
addToRolesCache(APIConstants.API_USER_ROLE_CACHE, username, roles);
return roles;
} catch (UserStoreException e) {
throw new APIManagementException("UserStoreException while trying the role list of the user " + username, e);
}
}
use of org.wso2.carbon.user.core.UserStoreManager in project identity-outbound-auth-sms-otp by wso2-extensions.
the class SMSOTPUtils method updateUserAttribute.
/**
* Update the mobile number (user attribute) in user's profile.
*
* @param username the Username
* @param attribute the Attribute
* @throws SMSOTPException
*/
public static void updateUserAttribute(String username, Map<String, String> attribute, String tenantDomain) throws SMSOTPException {
try {
// updating user attributes is independent from tenant association.not tenant association check needed here.
UserRealm userRealm;
// user is always in the super tenant.
userRealm = SMSOTPUtils.getUserRealm(tenantDomain);
if (userRealm == null) {
throw new SMSOTPException("The specified tenant domain " + tenantDomain + " does not exist.");
}
// check whether user already exists in the system.
SMSOTPUtils.verifyUserExists(username, tenantDomain);
UserStoreManager userStoreManager = userRealm.getUserStoreManager();
userStoreManager.setUserClaimValues(username, attribute, null);
} catch (UserStoreException | AuthenticationFailedException e) {
throw new SMSOTPException("Exception occurred while connecting to User Store: Authentication is failed. ", e);
}
}
use of org.wso2.carbon.user.core.UserStoreManager in project identity-outbound-auth-sms-otp by wso2-extensions.
the class SMSOTPAuthenticatorTest method testCheckWithBackUpCodes.
@Test
public void testCheckWithBackUpCodes() throws Exception {
mockStatic(IdentityTenantUtil.class);
context.setProperty(SMSOTPConstants.USER_NAME, "admin");
when(IdentityTenantUtil.getTenantId("carbon.super")).thenReturn(-1234);
when(IdentityTenantUtil.getRealmService()).thenReturn(realmService);
when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
when((AuthenticatedUser) context.getProperty(SMSOTPConstants.AUTHENTICATED_USER)).thenReturn(AuthenticatedUser.createLocalAuthenticatedUserFromSubjectIdentifier("admin"));
when(userRealm.getUserStoreManager().getUserClaimValue(MultitenantUtils.getTenantAwareUsername("admin"), SMSOTPConstants.SAVED_OTP_LIST, null)).thenReturn("12345,4568,1234,7896");
AuthenticatedUser user = (AuthenticatedUser) context.getProperty(SMSOTPConstants.AUTHENTICATED_USER);
Whitebox.invokeMethod(smsotpAuthenticator, "checkWithBackUpCodes", context, "1234", user);
}
use of org.wso2.carbon.user.core.UserStoreManager in project identity-outbound-auth-sms-otp by wso2-extensions.
the class SMSOTPAuthenticatorTest method testCheckWithInvalidBackUpCodes.
@Test(expectedExceptions = { AuthenticationFailedException.class })
public void testCheckWithInvalidBackUpCodes() throws Exception {
mockStatic(IdentityTenantUtil.class);
context.setProperty(SMSOTPConstants.USER_NAME, "admin");
when(IdentityTenantUtil.getTenantId("carbon.super")).thenReturn(-1234);
when(IdentityTenantUtil.getRealmService()).thenReturn(realmService);
when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
when((AuthenticatedUser) context.getProperty(SMSOTPConstants.AUTHENTICATED_USER)).thenReturn(AuthenticatedUser.createLocalAuthenticatedUserFromSubjectIdentifier("admin"));
when(userRealm.getUserStoreManager().getUserClaimValue(MultitenantUtils.getTenantAwareUsername("admin"), SMSOTPConstants.SAVED_OTP_LIST, null)).thenReturn("12345,4568,1234,7896");
AuthenticatedUser user = (AuthenticatedUser) context.getProperty(SMSOTPConstants.AUTHENTICATED_USER);
Whitebox.invokeMethod(smsotpAuthenticator, "checkWithBackUpCodes", context, "45698789", user);
}
Aggregations