Java Code Examples for org.wso2.carbon.user.mgt.stub.UserAdminStub#getRolesOfUser()

The following examples show how to use org.wso2.carbon.user.mgt.stub.UserAdminStub#getRolesOfUser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserSignUpWorkflowExecutor.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Method updates Roles users with subscriber role
 * @param serverURL
 * @param adminUsername
 * @param adminPassword
 * @param userName
 * @param role
 * @throws Exception
 */
protected static void updateRolesOfUser(String serverURL, String adminUsername,
                                        String adminPassword, String userName, String role)
                                                                                           throws Exception {
	if (log.isDebugEnabled()) {
		log.debug("Adding Subscriber role to " + userName);
	}

	String url = serverURL + "UserAdmin";
	RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
	UserRealm realm = realmService.getBootstrapRealm();
	UserStoreManager manager = realm.getUserStoreManager();
	if (!manager.isExistingRole(role)){
		log.error("Could not find role " + role + " in the user store");
		throw new Exception("Could not find role " + role + " in the user store");
	}

	UserAdminStub userAdminStub = new UserAdminStub(url);
	CarbonUtils.setBasicAccessSecurityHeaders(adminUsername, adminPassword, userAdminStub._getServiceClient());
	FlaggedName[] flaggedNames = userAdminStub.getRolesOfUser(userName, "*", -1);
	List<String> roles = new ArrayList<String>();
	if (flaggedNames != null) {
		for (FlaggedName flaggedName : flaggedNames) {
			if (flaggedName.getSelected()) {
				roles.add(flaggedName.getItemName());
			}
		}
	}
	roles.add(role);
	userAdminStub.updateRolesOfUser(userName, roles.toArray(new String[roles.size()]));
}
 
Example 2
Source File: UserSignUpWorkflowExecutor.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Method updates Roles users with list of roles
 * @param serverURL
 * @param adminUsername
 * @param adminPassword
 * @param userName
 * @param tenantID
 * @param role
 * @throws Exception
 */
protected static void updateRolesOfUser(String serverURL, String adminUsername,
                                        String adminPassword, String userName,
                                        List<String> roleList, String tenantDomain)
                                        		throws Exception {

	if (log.isDebugEnabled()) {
		log.debug("Adding roles to " + userName + "in " + tenantDomain + " Domain");
	}
	String url = serverURL + "UserAdmin";
	RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
	int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager()
			.getTenantId(tenantDomain);
	UserRealm realm = (UserRealm) realmService.getTenantUserRealm(tenantId);
	UserStoreManager manager = realm.getUserStoreManager();
	
	if(manager.isExistingUser(userName)) {
		// check whether given roles exist
		for (String role : roleList) {
			if (!manager.isExistingRole(role)) {
				log.error("Could not find role " + role + " in the user store");
				throw new Exception("Could not find role " + role + " in the user store");
			}
		}

		UserAdminStub userAdminStub = new UserAdminStub(url);
		CarbonUtils.setBasicAccessSecurityHeaders(adminUsername, adminPassword, userAdminStub._getServiceClient());
		
		FlaggedName[] flaggedNames = userAdminStub.getRolesOfUser(userName, "*", -1);
		List<String> roles = new ArrayList<String>();
		if (flaggedNames != null) {
			for (FlaggedName flaggedName : flaggedNames) {
				if (flaggedName.getSelected()) {
					roles.add(flaggedName.getItemName());
				}
			}
		}
		roles.addAll(roleList);
		userAdminStub.updateRolesOfUser(userName, roles.toArray(new String[roles.size()]));
	} else {
		log.error("User does not exist. Unable to approve user " + userName);
	} 
	
}