Package org.wso2.carbon.registry.core.session

Examples of org.wso2.carbon.registry.core.session.UserRegistry


        // embeddedRegistryService = new InMemoryEmbeddedRegistryService();
        // get the realm config to retrieve admin username, password
        RealmConfiguration realmConfig = ctx.getRealmService().getBootstrapRealmConfiguration();
       
        UserRegistry adminRegistry = embeddedRegistryService.
                getUserRegistry(realmConfig.getAdminUserName(), realmConfig.getAdminPassword());
        UserRealm adminRealm = adminRegistry.getUserRealm();

        adminRealm.getUserStoreManager().addUser("foo", "cce123", null, null, null);
        adminRealm.getUserStoreManager();

        adminRealm.getAuthorizationManager().
                authorizeUser("foo", RegistryConstants.ROOT_PATH, ActionConstants.PUT);
        adminRealm.getUserStoreManager().addUser("bar", "swe123", null, null, null);

        UserRegistry fooRegistry = embeddedRegistryService.getUserRegistry("foo", "cce123");
        UserRegistry barRegistry = embeddedRegistryService.getUserRegistry("bar", "swe123");

        String r1Content = "R1";
        Resource r1 = fooRegistry.newResource();
        r1.setContent(r1Content.getBytes());

        fooRegistry.put("/r1", r1);

        adminRegistry.applyTag("/r1", "java");
        fooRegistry.applyTag("/r1", "java");
        barRegistry.applyTag("/r1", "java");

        Tag[] tags1 = adminRegistry.getTags("/r1");
        assertEquals("There should be 3 taggings on resource '/r1'", 3, tags1[0].getTagCount());

        // owner of the /r1 removes a tag. all tags should be removed
        fooRegistry.removeTag("/r1", "java");
        Tag[] tags2 = adminRegistry.getTags("/r1");
        assertEquals("There should be 0 taggings on resource '/r1'", 0, tags2.length);

        adminRegistry.applyTag("/r1", "java");
        fooRegistry.applyTag("/r1", "java");
        barRegistry.applyTag("/r1", "java");

        // admin removes a tag. all tags should be removed
        adminRegistry.removeTag("/r1", "java");
        Tag[] tags3 = adminRegistry.getTags("/r1");
        assertEquals("There should be 0 taggings on resource '/r1'", 0, tags3.length);

        adminRegistry.applyTag("/r1", "java");
        fooRegistry.applyTag("/r1", "java");
        barRegistry.applyTag("/r1", "java");

        // normal user removes a tag. only the tag applied by the user is removed
        barRegistry.removeTag("/r1", "java");
        Tag[] tags4 = adminRegistry.getTags("/r1");
        assertEquals("There should be 2 taggings on resource '/r1'", 2, tags4[0].getTagCount());
    }
View Full Code Here


    // Test each registry has different has different user stores
    public void testUserStores() throws RegistryException {
        RealmConfiguration realmConfig = ctx.getRealmService().getBootstrapRealmConfiguration();
        // first we will fill the user store for tenant 0
        UserRegistry registry1 =
                embeddedRegistryService.getUserRegistry(realmConfig.getAdminUserName(), 0);

        Resource r = registry1.newResource();
        registry1.put("/test2", r);

        UserRealm userRealm1 = registry1.getUserRealm();
        UserStoreManager userStoreManager1;
        try {
            userStoreManager1 = userRealm1.getUserStoreManager();
        } catch (UserStoreException e) {
            throw new RegistryException("Error in retrieving UserStoreManager.");
        }
        AuthorizationManager authorizationManager1;
        try {
            authorizationManager1 = userRealm1.getAuthorizationManager();
        } catch (UserStoreException e) {
            throw new RegistryException("Error in retrieving AuthorizationManager.");
        }

        // adding some users
        try {
            userStoreManager1.addUser("user1t0", "password", null, null, null);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding user user1t0.");
        }
        try {
            userStoreManager1.addUser("user2t0", "password", null, null, null);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding user user2t0.");
        }
        try {
            userStoreManager1.addUser("user3t0", "password", null, null, null);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding user user3t0.");
        }

        // adding more users we are going to add roles with
        try {
            userStoreManager1.addUser("user4t0", "password", null, null, null);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding user user4t0.");
        }
        try {
            userStoreManager1.addUser("user5t0", "password", null, null, null);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding user user5t0.");
        }
        try {
            userStoreManager1.addUser("user6t0", "password", null, null, null);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding user user6t0.");
        }

        // adding some roles
        try {
            userStoreManager1.addRole("role1t0", null, null);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding role role1t0.");
        }
        try {
            userStoreManager1.addRole("role2t0", null, null);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding role role2t0.");
        }
        try {
            userStoreManager1.addRole("role3t0", null, null);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding role role3t0.");
        }
//
//        // now assign authorizations to first set of users.
//        try {
//            authorizationManager1.authorizeUser("user1t0", "/test2", ActionConstants.PUT);
//        } catch (UserStoreException e) {
//            throw new RegistryException("Error in authorizing user1t0.");
//        }
//        try {
//            authorizationManager1.authorizeUser("user2t0", "/test2", ActionConstants.DELETE);
//        } catch (UserStoreException e) {
//            throw new RegistryException("Error in authorizing user2t0.");
//        }
//        try {
//            authorizationManager1.authorizeUser("user2t0", "/test2", "authorize");
//        } catch (UserStoreException e) {
//            throw new RegistryException("Error in authorizing user3t0.");
//        }

        // then assign roles to the second set of users.
        try {
            userStoreManager1.updateRoleListOfUser("user4t0", null, new String[]{"role1t0"});
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding user user4t0 to role1t0 role.");
        }
        try {
            userStoreManager1.updateRoleListOfUser("user5t0", null, new String[]{"role2t0"});
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding user user5t0 to role2t0 role.");
        }
        try {
            userStoreManager1.updateRoleListOfUser("user6t0", null, new String[]{"role3t0"});
        } catch (UserStoreException e) {
            throw new RegistryException("Error in adding user user6t0 to role3t0 role.");
        }

        // now giving authorizations to the role.
        try {
            authorizationManager1.authorizeRole("role1t0", "/test2", ActionConstants.PUT);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in authorizing role1t0.");
        }
        try {
            authorizationManager1.authorizeRole("role2t0", "/test2", ActionConstants.DELETE);
        } catch (UserStoreException e) {
            throw new RegistryException("Error in authorizing role2t0.");
        }
        try {
            authorizationManager1.authorizeRole("role3t0", "/test2", "authorize");
        } catch (UserStoreException e) {
            throw new RegistryException("Error in authorizing role3t0.");
        }

        // secondly we will check the user store from tenant 1 and verify no overlaps with tenant 0
        UserRegistry registry2 =
                embeddedRegistryService.getUserRegistry(realmConfig.getAdminUserName(), 1);

        UserRealm userRealm2 = registry2.getUserRealm();
        UserStoreManager userStoreManager2;
        try {
            userStoreManager2 = userRealm2.getUserStoreManager();
        } catch (UserStoreException e) {
            throw new RegistryException("Error in retrieving UserStoreManager.");
View Full Code Here

    }

    public void testClaims() throws RegistryException, UserStoreException {
        // first we will fill the user store for tenant 0
        RealmConfiguration realmConfig = ctx.getRealmService().getBootstrapRealmConfiguration();
        UserRegistry userRegistry1 =
                embeddedRegistryService.getUserRegistry(realmConfig.getAdminUserName(), 1);

        UserStoreManager userStoreManager = userRegistry1.getUserRealm().getUserStoreManager();

        Map<String, String> claimMap = new HashMap<String, String>();
        claimMap.put("http://wso2.org/claims/givenname", "admin123");
        claimMap.put("http://wso2.org/claims/emailaddress", "admin@wso2.org");

        userStoreManager.setUserClaimValues("admin", claimMap, "home-profile");


        Map<String, String> obtained = userStoreManager.getUserClaimValues("admin", new String[]{
                "http://wso2.org/claims/givenname", "http://wso2.org/claims/emailaddress"},
                "home-profile");
        assertEquals("The email should be same",
                obtained.get("http://wso2.org/claims/emailaddress"), "admin@wso2.org");
        assertEquals("The name should be same", obtained.get("http://wso2.org/claims/givenname"),
                "admin123");

        userStoreManager.addUser("another-admin", "another-admin123", null, null, null);

        // we will check another user in the same tenant have access to the claims
        UserRegistry userRegistry2 = embeddedRegistryService.getUserRegistry("another-admin", 1);
        UserStoreManager userStoreManager2 = userRegistry2.getUserRealm().getUserStoreManager();

        Map<String, String> userRegistryObtained =
                userStoreManager2.getUserClaimValues("admin", new String[]{
                        "http://wso2.org/claims/givenname", "http://wso2.org/claims/emailaddress"},
                        "home-profile");
View Full Code Here

    }

    // Do tenant-specific initialization.
    private static void initializeTenant(RegistryService registryService, int tenantId) {
        try {
            UserRegistry systemRegistry = registryService.getConfigSystemRegistry();
            if (systemRegistry.getRegistryContext() != null) {
                HandlerManager handlerManager =
                        systemRegistry.getRegistryContext().getHandlerManager();
                if (handlerManager instanceof HandlerLifecycleManager) {
                    ((HandlerLifecycleManager)handlerManager).init(tenantId);
                }
            }
            systemRegistry = registryService.getRegistry(
View Full Code Here

    }

    private void addPermissions(RegistryService registryService) throws Exception {

        try {
            UserRegistry registry = registryService.getGovernanceSystemRegistry();
            Map<String, String> map = new HashMap<String, String>();
            map.put(CarbonConstants.UI_PERMISSION_COLLECTION, "All Permissions");
            map.put(CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION, "Admin Permissions");
            map.put(UserMgtConstants.UI_PROTECTED_PERMISSION_ROOT, "Super Admin Permissions");
            map.put(UserMgtConstants.UI_ADMIN_PERMISSION_ROOT + "manage", "Manage");
            map.put(UserMgtConstants.UI_ADMIN_PERMISSION_ROOT + "monitor", "Monitor");
            map.put(UserMgtConstants.UI_ADMIN_PERMISSION_ROOT + "manage/modify", "Configure");
            map.put(UserMgtConstants.UI_ADMIN_PERMISSION_ROOT + "manage/add", "Add");
            map.put(UserMgtConstants.UI_ADMIN_PERMISSION_ROOT + "configure", "Configure");
            map.put(UserMgtConstants.UI_ADMIN_PERMISSION_ROOT + "configure/security", "Security");
            map.put(UserMgtConstants.UI_ADMIN_PERMISSION_ROOT + "login", "Login");

            for (Iterator<Map.Entry<String, String>> ite = map.entrySet().iterator(); ite.hasNext();) {
                Map.Entry<String, String> entry = ite.next();
                String resourcePath = entry.getKey();
                String displayName = entry.getValue();

                if (registry.resourceExists(resourcePath)) {
                    Resource resource = registry.get(resourcePath);
                    if (resource.getProperty(UserMgtConstants.DISPLAY_NAME) == null) {
                        resource.setProperty(UserMgtConstants.DISPLAY_NAME, displayName);
                        registry.put(resourcePath, resource);
                    }
                    continue;
                }

                Collection resource = registry.newCollection();
                resource.setProperty(UserMgtConstants.DISPLAY_NAME, displayName);
                registry.put(resourcePath, resource);

            }

            // realm is taken from the registry rather than realm service to fix
            // chrooted issues
            UserRealm realm = registry.getUserRealm();
            String adminRole = realm.getRealmConfiguration().getAdminRoleName();
            AuthorizationManager authMan = realm.getAuthorizationManager();
            if (!authMan.isRoleAuthorized(adminRole, CarbonConstants.UI_PERMISSION_COLLECTION,
                    UserMgtConstants.EXECUTE_ACTION)) {
                authMan.authorizeRole(adminRole, CarbonConstants.UI_PERMISSION_COLLECTION,
View Full Code Here

        this.indexStoragePath = indexStoragePath;

        // creates the the subscription intex
        // when creating subscriptions we going to add entries to this this resource
        try {
            UserRegistry userRegistry =
                    this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance().getTenantId());

            //create the topic storage path if it does not exists
            if (!userRegistry.resourceExists(this.topicStoragePath)) {
                userRegistry.put(this.topicStoragePath, userRegistry.newCollection());
            }
            //allow permissions to root topic
            //put the permissions to the user store. here we create the resource name from
            //the topic storage path of the registry.
            //here we allow permissions at each start up since some times user managers
            //may have changed.
            UserRealm userRealm =
                    EventBrokerHolder.getInstance().getRealmService().getTenantUserRealm(EventBrokerHolder.getInstance().getTenantId());
            for (String role : userRealm.getUserStoreManager().getRoleNames()) {
                userRealm.getAuthorizationManager().authorizeRole(
                        role, this.topicStoragePath, EventBrokerConstants.EB_PERMISSION_SUBSCRIBE);
                userRealm.getAuthorizationManager().authorizeRole(
                        role, this.topicStoragePath, EventBrokerConstants.EB_PERMISSION_PUBLISH);

            }
            // we need to create the index here only it is not exists.
            if (!userRegistry.resourceExists(this.indexStoragePath)) {
                userRegistry.put(this.indexStoragePath, userRegistry.newResource());
            }

        } catch (RegistryException e) {
            throw new EventBrokerConfigurationException("Can not access the registry ", e);
        } catch (UserStoreException e) {
View Full Code Here

     */
    public void addSubscription(Subscription subscription) throws EventBrokerException {


        try {
            UserRegistry userRegistry =
                    this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance().getTenantId());
            String resourcePath = getResourcePath(subscription.getId(), subscription.getTopicName());

            Resource resource = userRegistry.newResource();
            resource.setProperty(EventBrokerConstants.EB_RES_SUBSCRIPTION_URL, subscription.getEventSinkURL());
            resource.setProperty(EventBrokerConstants.EB_RES_EVENT_DISPATCHER_NAME, subscription.getEventDispatcherName());

            if (subscription.getExpires() != null){
               resource.setProperty(EventBrokerConstants.EB_RES_EXPIRS, ConverterUtil.convertToString(subscription.getExpires()));
            }
            resource.setProperty(EventBrokerConstants.EB_RES_OWNER, subscription.getOwner());
            resource.setProperty(EventBrokerConstants.EB_RES_TOPIC_NAME, subscription.getTopicName());
            resource.setProperty(EventBrokerConstants.EB_RES_CREATED_TIME, System.currentTimeMillis()+"");
            resource.setProperty(EventBrokerConstants.EB_RES_MODE, JavaUtil.getSubscriptionMode(subscription.getTopicName()));

            //set the other properties of the subscription.
            Map<String, String> properties = subscription.getProperties();
            for (String key : properties.keySet()){
                resource.setProperty(key, properties.get(key));
            }

            userRegistry.put(resourcePath, resource);

            // add the subscription index
            String fullPath = this.indexStoragePath;
            Resource topicIndexResource;
            if(userRegistry.resourceExists(fullPath)){
                topicIndexResource = userRegistry.get(fullPath);
                topicIndexResource.addProperty(subscription.getId(), subscription.getTopicName());
            }else{
               topicIndexResource = userRegistry.newResource();
               topicIndexResource.addProperty(subscription.getId(), subscription.getTopicName());               
            }
            userRegistry.put(fullPath, topicIndexResource);

        } catch (RegistryException e) {
            throw new EventBrokerException("Can not save to registry ", e);
        }

View Full Code Here

     */
    public List<Subscription> getAllSubscriptions() throws EventBrokerException {
        List<Subscription> subscriptions = new ArrayList<Subscription>();

        try {
            UserRegistry userRegistry =
                    this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance().getTenantId());
            if (userRegistry.resourceExists(this.indexStoragePath)) {
                Resource topicIndexResource = userRegistry.get(this.indexStoragePath);
                Properties savedSubscriptions = topicIndexResource.getProperties();

                Resource subscriptionResource = null;
                Subscription subscription = null;
                String subscriptionID = null;
                String topicName = null;
                for (Enumeration e = savedSubscriptions.propertyNames(); e.hasMoreElements();) {
                    subscriptionID = (String) e.nextElement();
                    // when the registry is remotely mount to antoher registry. then registry automatically added
                    // some propertiest stary with registry we need to skip them.
                    if (!subscriptionID.startsWith("registry")) {
                        topicName = topicIndexResource.getProperty(subscriptionID);
                        subscriptionResource = userRegistry.get(getResourcePath(subscriptionID, topicName));
                        subscription = JavaUtil.getSubscription(subscriptionResource);
                        subscription.setId(subscriptionID);
                        subscription.setTopicName(topicName);
                        subscription.setTenantId(EventBrokerHolder.getInstance().getTenantId());
                        subscriptions.add(subscription);
View Full Code Here

    }

    public Subscription getSubscription(String id) throws EventBrokerException {

        try {
            UserRegistry userRegistry =
                    this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance().getTenantId());
            Resource topicIndexResource = userRegistry.get(this.indexStoragePath);
            String subscriptionPath = getResourcePath(id, topicIndexResource.getProperty(id));
            if (subscriptionPath != null) {
                Resource subscriptionResource = userRegistry.get(subscriptionPath);
                Subscription subscription = JavaUtil.getSubscription(subscriptionResource);
                subscription.setTenantId(EventBrokerHolder.getInstance().getTenantId());
                return subscription;
            } else {
                return null;
View Full Code Here

    }

    public void renewSubscription(Subscription subscription) throws EventBrokerException {

         try {
            UserRegistry userRegistry =
                    this.registryService.getGovernanceSystemRegistry(EventBrokerHolder.getInstance().getTenantId());
            Resource topicIndexResource = userRegistry.get(this.indexStoragePath);
            String topicName = topicIndexResource.getProperty(subscription.getId());
            String subscriptionPath = getResourcePath(subscription.getId(), topicName);
            if (subscriptionPath != null) {
                Resource subscriptionResource = userRegistry.get(subscriptionPath);
                // Set the expires property only if it has been set again.
                if (subscription.getExpires() != null) {
                    subscriptionResource.setProperty(EventBrokerConstants.EB_RES_EXPIRS,
                            ConverterUtil.convertToString(subscription.getExpires()));
                }
                // There might be updated subscription properties. Set them too.
                Subscription currentSubscription = JavaUtil.getSubscription(subscriptionResource);
                Map<String, String> properties = currentSubscription.getProperties();
                for (String key : properties.keySet()) {
                    subscriptionResource.removeProperty(key);
                }
                properties = subscription.getProperties();
                for (String key : properties.keySet()) {
                    subscriptionResource.setProperty(key, properties.get(key));
                }
                userRegistry.put(subscriptionPath, subscriptionResource);
            } else {
                throw new EventBrokerException("Can not find the resouce to the subscription with" +
                        " id " + subscription.getId());
            }
        } catch (RegistryException e) {
View Full Code Here

TOP

Related Classes of org.wso2.carbon.registry.core.session.UserRegistry

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.