use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.
the class APIUtil method createRole.
/**
* Creates a role with a given set of permissions for the specified tenant
*
* @param roleName role name
* @param permissions a set of permissions to be associated with the role
* @param tenantId id of the tenant
* @throws APIManagementException
*/
public static void createRole(String roleName, Permission[] permissions, int tenantId) throws APIManagementException {
try {
RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
UserRealm realm;
org.wso2.carbon.user.api.UserRealm tenantRealm;
UserStoreManager manager;
if (tenantId < 0) {
realm = realmService.getBootstrapRealm();
manager = realm.getUserStoreManager();
} else {
tenantRealm = realmService.getTenantUserRealm(tenantId);
manager = tenantRealm.getUserStoreManager();
}
if (!manager.isExistingRole(roleName)) {
if (log.isDebugEnabled()) {
log.debug("Creating role: " + roleName);
}
String tenantAdminName = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getRealmConfiguration().getAdminUserName();
String[] userList = new String[] { tenantAdminName };
manager.addRole(roleName, userList, permissions);
}
} catch (UserStoreException e) {
throw new APIManagementException("Error while creating role: " + roleName, e);
}
}
use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.
the class APIUtil method getAllTenantsWithSuperTenant.
public static List<Tenant> getAllTenantsWithSuperTenant() throws UserStoreException {
Tenant[] tenants = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getAllTenants();
ArrayList<Tenant> tenantArrayList = new ArrayList<Tenant>();
Collections.addAll(tenantArrayList, tenants);
Tenant superAdminTenant = new Tenant();
superAdminTenant.setDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
superAdminTenant.setId(org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_ID);
superAdminTenant.setAdminName(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME);
tenantArrayList.add(superAdminTenant);
return tenantArrayList;
}
use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.
the class APIUtil method isUserExist.
/**
* Check whether user is exist
*
* @param username A username
* @throws APIManagementException If an error occurs
*/
public static boolean isUserExist(String username) throws APIManagementException {
if (username == null) {
throw new APIManagementException("Attempt to execute privileged operation as the anonymous user");
}
String tenantDomain = MultitenantUtils.getTenantDomain(username);
String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(username);
try {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
UserStoreManager manager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getUserStoreManager();
return manager.isExistingUser(tenantAwareUserName);
} catch (UserStoreException e) {
throw new APIManagementException("UserStoreException while trying the user existence " + username, e);
}
}
use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.
the class APIUtil method getTenantAdminUserName.
public static String getTenantAdminUserName(String tenantDomain) throws APIManagementException {
try {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
String adminUserName = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getRealmConfiguration().getAdminUserName();
if (!tenantDomain.contentEquals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
return adminUserName.concat("@").concat(tenantDomain);
}
return adminUserName;
} catch (UserStoreException e) {
throw new APIManagementException("Error in getting tenant admin username", e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.
the class ApplicationThrottleController method createThrottleContext.
private static ThrottleContext createThrottleContext(MessageContext synCtx, ThrottleDataHolder dataHolder, String applicationId, String policyKeyApplication) {
// Object entryValue = synCtx.getEntry(APPLICATION_THROTTLE_POLICY_KEY);
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
AuthenticationContext authContext = APISecurityUtils.getAuthenticationContext(synCtx);
// extract the subscriber username from the auth Context
String subscriber = authContext.getSubscriber();
// get the tenant Domain from the subscriber
String tenantDomain = MultitenantUtils.getTenantDomain(subscriber);
int tenantId;
// get the tenant domain id from the tenant domain name
try {
tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
} catch (UserStoreException e) {
handleException("Unable to Find the tenant ID using tenant: " + tenantDomain, e);
return null;
}
Object entryValue = lookup(policyKeyApplication, tenantId);
if (entryValue == null || !(entryValue instanceof OMElement)) {
handleException("Unable to load throttling policy using key: " + policyKeyApplication);
}
try {
Throttle throttle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy((OMElement) entryValue));
ThrottleContext context = throttle.getThrottleContext(ThrottleConstants.ROLE_BASED_THROTTLE_KEY);
dataHolder.addThrottleContext(applicationId, context);
return context;
} catch (ThrottleException e) {
handleException("Error processing the throttling policy", e);
}
return null;
}
Aggregations