Java Code Examples for org.wso2.carbon.user.core.service.RealmService#getBootstrapRealm()

The following examples show how to use org.wso2.carbon.user.core.service.RealmService#getBootstrapRealm() . 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: APIManagerComponent.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private void setupSelfRegistration(APIManagerConfiguration config) throws APIManagementException {
    boolean enabled = Boolean.parseBoolean(config.getFirstProperty(APIConstants.SELF_SIGN_UP_ENABLED));
    if (!enabled) {
        return;
    }
    String role = config.getFirstProperty(APIConstants.SELF_SIGN_UP_ROLE);
    if (role == null) {
        // Required parameter missing - Throw an exception and interrupt startup
        throw new APIManagementException("Required subscriber role parameter missing " + "in the self sign up configuration");
    }
    try {
        RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
        UserRealm realm = realmService.getBootstrapRealm();
        UserStoreManager manager = realm.getUserStoreManager();
        if (!manager.isExistingRole(role)) {
            if (log.isDebugEnabled()) {
                log.debug("Creating subscriber role: " + role);
            }
            Permission[] subscriberPermissions = new Permission[] { new Permission("/permission/admin/login", UserMgtConstants.EXECUTE_ACTION), new Permission(APIConstants.Permissions.API_SUBSCRIBE, UserMgtConstants.EXECUTE_ACTION) };
            String superTenantName = ServiceReferenceHolder.getInstance().getRealmService().getBootstrapRealmConfiguration().getAdminUserName();
            String[] userList = new String[] { superTenantName };
            manager.addRole(role, userList, subscriberPermissions);
        }
    } catch (UserStoreException e) {
        throw new APIManagementException("Error while creating subscriber role: " + role + " - " + "Self registration might not function properly.", e);
    }
}
 
Example 3
Source File: StratosManagerServiceComponent.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * Create internal user role if not exists.
 *
 * @param componentContext
 * @throws UserStoreException
 * @throws UserManagerException
 */
private void createInternalUserRole(ComponentContext componentContext)
        throws UserStoreException, UserManagerException {
    RealmService realmService = ServiceReferenceHolder.getRealmService();
    UserRealm realm = realmService.getBootstrapRealm();
    UserStoreManager userStoreManager = realm.getUserStoreManager();
    UserRoleCreator.createInternalUserRole(userStoreManager);

    TenantUserRoleManager tenantUserRoleManager = new TenantUserRoleManager();
    componentContext.getBundleContext()
            .registerService(org.wso2.carbon.stratos.common.listeners.TenantMgtListener.class.getName(),
                    tenantUserRoleManager, null);
}