Example #1
0
 public void setUp() {
   super.setUp();
   if (embeddedRegistryService != null) {
     return;
   }
   try {
     embeddedRegistryService = ctx.getEmbeddedRegistryService();
     RealmUnawareRegistryCoreServiceComponent comp =
         new RealmUnawareRegistryCoreServiceComponent();
     comp.setRealmService(ctx.getRealmService());
     comp.registerBuiltInHandlers(embeddedRegistryService);
     // get the realm config to retrieve admin username, password
     RealmConfiguration realmConfig = ctx.getRealmService().getBootstrapRealmConfiguration();
     registry =
         embeddedRegistryService.getConfigUserRegistry(
             realmConfig.getAdminUserName(), realmConfig.getAdminPassword());
     systemRegistry = embeddedRegistryService.getConfigSystemRegistry();
   } catch (RegistryException e) {
     fail("Failed to initialize the registry. Caused by: " + e.getMessage());
   }
 }
  public void doUserRoleStuff() throws Exception {
    UserStoreManager admin = realm.getUserStoreManager();

    InputStream inStream =
        this.getClass()
            .getClassLoader()
            .getResource(JDBCRealmTest.JDBC_TEST_USERMGT_XML)
            .openStream();
    RealmConfigXMLProcessor realmConfigProcessor = new RealmConfigXMLProcessor();
    RealmConfiguration realmConfig = realmConfigProcessor.buildRealmConfiguration(inStream);

    admin.addRole("role2", null, null);
    admin.addRole("role3", null, null);
    admin.addRole("role4", null, null);
    assertEquals(6, admin.getRoleNames().length); // admin,everyone,role1,role2,role3,role4

    // Test delete role method
    assertTrue(admin.isExistingRole("role3"));
    admin.deleteRole("role3");
    admin.deleteRole("role4");
    assertFalse(admin.isExistingRole("role3"));
    admin.addRole("role3", null, null);
    admin.addRole("role4", null, null);

    // add users
    admin.addUser("saman", "pass1", null, null, null, false);
    admin.addUser("amara", "pass2", null, null, null, false);
    admin.addUser("sunil", "pass3", null, null, null, false);

    // update the ROLE list of USERS
    admin.updateRoleListOfUser("saman", null, new String[] {"role2"});
    admin.updateRoleListOfUser("saman", new String[] {"role2"}, new String[] {"role4", "role3"});
    try {
      admin.updateRoleListOfUser(null, null, new String[] {"role2"});
      fail("Exceptions at missing user name");
    } catch (Exception ex) {
      // expected user
      if (log.isDebugEnabled()) {
        log.debug("Expected error, hence ignored", ex);
      }
    }

    // Renaming Role
    admin.updateRoleName("role4", "role5");

    String[] rolesOfSaman = admin.getRoleListOfUser("saman");
    assertEquals(3, rolesOfSaman.length);

    String[] rolesOfisuru = admin.getRoleListOfUser("isuru");
    assertEquals(0, rolesOfisuru.length);

    admin.updateUserListOfRole("role2", new String[] {"saman"}, null);
    admin.updateUserListOfRole("role3", null, new String[] {"amara", "sunil"});

    String[] userOfRole5 = admin.getUserListOfRole("role5");
    assertEquals(1, userOfRole5.length);

    String[] userOfRole4 = admin.getUserListOfRole("role4");
    assertEquals(0, userOfRole4.length);

    try {
      admin.updateUserListOfRole("rolexx", null, new String[] {"amara", "sunil"});
      TestCase.assertTrue(false);
    } catch (Exception e) {
      // exptected error in negative testing
      if (log.isDebugEnabled()) {
        log.debug("Expected error, hence ignored", e);
      }
    }
    try {
      admin.updateUserListOfRole("role2", null, new String[] {"d"});
      TestCase.assertTrue(false);
    } catch (Exception e) {
      // exptected error in negative testing
      if (log.isDebugEnabled()) {
        log.debug("Expected error, hence ignored", e);
      }
    }

    try {
      admin.updateRoleListOfUser("saman", new String[] {"x"}, new String[] {"y"});
      TestCase.assertTrue(false);
    } catch (Exception e) {
      // exptected error in negative testing
      if (log.isDebugEnabled()) {
        log.debug("Expected error, hence ignored", e);
      }
    }

    try {
      admin.updateUserListOfRole(
          realmConfig.getAdminRoleName(), null, new String[] {realmConfig.getAdminUserName()});
      TestCase.assertTrue(false);
    } catch (Exception e) {
      // exptected error in negative testing
      if (log.isDebugEnabled()) {
        log.debug("Expected error, hence ignored", e);
      }
    }

    try {
      admin.updateRoleListOfUser(
          realmConfig.getAdminUserName(), new String[] {realmConfig.getAdminRoleName()}, null);
      TestCase.assertTrue(false);
    } catch (Exception e) {
      // exptected error in negative testing
      if (log.isDebugEnabled()) {
        log.debug("Expected error, hence ignored", e);
      }
    }

    try {
      admin.updateUserListOfRole(realmConfig.getEveryOneRoleName(), new String[] {"saman"}, null);
      TestCase.assertTrue(false);
    } catch (Exception e) {
      // exptected error in negative testing
      if (log.isDebugEnabled()) {
        log.debug("Expected error, hence ignored", e);
      }
    }

    try {
      admin.updateRoleListOfUser("sunil", new String[] {realmConfig.getEveryOneRoleName()}, null);
      TestCase.assertTrue(false);
    } catch (Exception e) {
      // exptected error in negative testing
      if (log.isDebugEnabled()) {
        log.debug("Expected error, hence ignored", e);
      }
    }

    try {
      admin.updateRoleName("role2", "role5");
      TestCase.assertTrue(false);
    } catch (Exception e) {
      // exptected error in negative testing
      if (log.isDebugEnabled()) {
        log.debug("Expected error, hence ignored", e);
      }
    }
  }
  // TODO get a factory or a stream writer - add more props
  public static OMElement serialize(RealmConfiguration realmConfig) {
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMElement rootElement =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_USER_MANAGER));
    OMElement realmElement =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_REALM));
    String realmName = realmConfig.getRealmClassName();

    OMAttribute propAttr =
        factory.createOMAttribute(
            UserCoreConstants.RealmConfig.ATTR_NAME_PROP_NAME, null, realmName);
    realmElement.addAttribute(propAttr);

    rootElement.addChild(realmElement);

    OMElement mainConfig =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_CONFIGURATION));
    realmElement.addChild(mainConfig);

    OMElement addAdmin =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_ADD_ADMIN));
    OMElement adminUser =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_ADMIN_USER));
    OMElement adminUserNameElem =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_USER_NAME));
    adminUserNameElem.setText(realmConfig.getAdminUserName());
    OMElement adminPasswordElem =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_PASSWORD));
    addAdmin.setText(UserCoreUtil.removeDomainFromName(realmConfig.getAddAdmin()));
    adminPasswordElem.setText(realmConfig.getAdminPassword());
    adminUser.addChild(adminUserNameElem);
    adminUser.addChild(adminPasswordElem);
    mainConfig.addChild(addAdmin);
    mainConfig.addChild(adminUser);

    OMElement adminRoleNameElem =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_ADMIN_ROLE));
    adminRoleNameElem.setText(UserCoreUtil.removeDomainFromName(realmConfig.getAdminRoleName()));
    mainConfig.addChild(adminRoleNameElem);

    OMElement systemUserNameElem =
        factory.createOMElement(
            new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_SYSTEM_USER_NAME));
    mainConfig.addChild(systemUserNameElem);

    // adding the anonymous user
    OMElement anonymousUserEle =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_ANONYMOUS_USER));
    OMElement anonymousUserNameElem =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_USER_NAME));
    OMElement anonymousPasswordElem =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_PASSWORD));
    anonymousUserEle.addChild(anonymousUserNameElem);
    anonymousUserEle.addChild(anonymousPasswordElem);
    mainConfig.addChild(anonymousUserEle);

    // adding the everyone role
    OMElement everyoneRoleNameElem =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_EVERYONE_ROLE));
    everyoneRoleNameElem.setText(
        UserCoreUtil.removeDomainFromName(realmConfig.getEveryOneRoleName()));
    mainConfig.addChild(everyoneRoleNameElem);

    // add the main config properties
    addPropertyElements(
        factory, mainConfig, null, realmConfig.getDescription(), realmConfig.getRealmProperties());
    // add the user store manager properties

    OMElement userStoreManagerElement =
        factory.createOMElement(
            new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_USER_STORE_MANAGER));
    realmElement.addChild(userStoreManagerElement);
    addPropertyElements(
        factory,
        userStoreManagerElement,
        realmConfig.getUserStoreClass(),
        realmConfig.getDescription(),
        realmConfig.getUserStoreProperties());

    RealmConfiguration secondaryRealmConfiguration = null;
    secondaryRealmConfiguration = realmConfig.getSecondaryRealmConfig();
    while (secondaryRealmConfiguration != null) {
      OMElement secondaryElement =
          factory.createOMElement(
              new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_USER_STORE_MANAGER));
      realmElement.addChild(secondaryElement);
      addPropertyElements(
          factory,
          secondaryElement,
          secondaryRealmConfiguration.getUserStoreClass(),
          secondaryRealmConfiguration.getDescription(),
          secondaryRealmConfiguration.getUserStoreProperties());
      secondaryRealmConfiguration = secondaryRealmConfiguration.getSecondaryRealmConfig();
    }

    // add the user authorization properties
    OMElement authorizerManagerElement =
        factory.createOMElement(new QName(UserCoreConstants.RealmConfig.LOCAL_NAME_ATHZ_MANAGER));
    realmElement.addChild(authorizerManagerElement);
    addPropertyElements(
        factory,
        authorizerManagerElement,
        realmConfig.getAuthorizationManagerClass(),
        realmConfig.getDescription(),
        realmConfig.getAuthzProperties());

    return rootElement;
  }