@Test public void testCreateWithPrivileges() throws SQLException { Attribute authentication = AttributeBuilder.build(OracleConstants.ORACLE_AUTHENTICATION_ATTR_NAME, OracleConstants.ORACLE_AUTH_LOCAL); Attribute name = new Name(TEST_USER); GuardedString password = new GuardedString("hello".toCharArray()); Attribute passwordAttribute = AttributeBuilder.buildPassword(password); try { SQLUtil.executeUpdateStatement(connector.getOrCreateAdminConnection(), "drop table MYTABLE"); } catch (SQLException e) { } SQLUtil.executeUpdateStatement(connector.getOrCreateAdminConnection(), "create table mytable(id number)"); Attribute privileges = AttributeBuilder.build(OracleConstants.ORACLE_PRIVS_ATTR_NAME, "CREATE SESSION", "SELECT ON MYTABLE"); final Uid uid = facade.create(ObjectClass.ACCOUNT, CollectionUtil.newSet(authentication, name, passwordAttribute, privileges), null); UserRecord record = userReader.readUserRecord(uid.getUidValue()); assertNotNull(record); assertEqualsIgnoreCase(TEST_USER, record.getUserName()); OracleRolePrivReader roleReader = new OracleRolePrivReader(connector.getOrCreateAdminConnection()); final List<String> privRead = roleReader.readAllPrivileges(uid.getUidValue()); assertThat(privRead).contains("CREATE SESSION"); assertThat(privRead).contains("SELECT ON " + testConf.getUserOwner() + ".MYTABLE"); }
public Uid create(ObjectClass oclass, Set<Attribute> attrs, OperationOptions options) { OracleConnectorHelper.checkObjectClass(oclass, cfg.getConnectorMessages()); Map<String, Attribute> map = AttributeUtil.toMap(attrs); checkCreateAttributes(map); String userName = OracleConnectorHelper.getStringValue(map, Name.NAME, cfg.getConnectorMessages()); new LocalizedAssert(cfg.getConnectorMessages()).assertNotBlank(userName, Name.NAME); LOG.info("Creating user : [{0}]", userName); OracleUserAttributes.Builder builder = new OracleUserAttributes.Builder(); builder.setUserName(userName); new OracleAttributesReader(cfg.getConnectorMessages()).readCreateAttributes(map, builder); OracleUserAttributes caAttributes = builder.build(); try { String createSQL = new OracleCreateOrAlterStBuilder(cfg).buildCreateUserSt(caAttributes); if (createSQL == null) { throw new ConnectorException( "No create SQL generated, probably not enough attributes"); } Attribute roles = AttributeUtil.find(OracleConstants.ORACLE_ROLES_ATTR_NAME, attrs); Attribute privileges = AttributeUtil.find(OracleConstants.ORACLE_PRIVS_ATTR_NAME, attrs); List<String> rolesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantRoles(userName, OracleConnectorHelper.castList(roles, String.class)); List<String> privilegesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantPrivileges(userName, OracleConnectorHelper.castList(privileges, String.class)); SQLUtil.executeUpdateStatement(adminConn, createSQL); OracleSpecifics.execStatemts(adminConn, rolesSQL); OracleSpecifics.execStatemts(adminConn, privilegesSQL); adminConn.commit(); LOG.info("User created : [{0}]", userName); } catch (Exception e) { SQLUtil.rollbackQuietly(adminConn); if (e instanceof SQLException) { SQLException sqle = (SQLException) e; if ("42000".equals(sqle.getSQLState()) && 1920 == sqle.getErrorCode()) { throw new AlreadyExistsException("User [" + userName + "] already exists"); } } throw new ConnectorException(cfg.getConnectorMessages().format( MSG_CREATE_OF_USER_FAILED, null, userName), e); } return new Uid(userName); }
OracleOperationCreate extends AbstractOracleOperation implements CreateOp { public Uid create(ObjectClass oclass, Set<Attribute> attrs, OperationOptions options) { OracleConnectorHelper.checkObjectClass(oclass, cfg.getConnectorMessages()); Map<String, Attribute> map = AttributeUtil.toMap(attrs); checkCreateAttributes(map); String userName = OracleConnectorHelper.getStringValue(map, Name.NAME, cfg.getConnectorMessages()); new LocalizedAssert(cfg.getConnectorMessages()).assertNotBlank(userName, Name.NAME); LOG.info("Creating user : [{0}]", userName); OracleUserAttributes.Builder builder = new OracleUserAttributes.Builder(); builder.setUserName(userName); new OracleAttributesReader(cfg.getConnectorMessages()).readCreateAttributes(map, builder); OracleUserAttributes caAttributes = builder.build(); try { String createSQL = new OracleCreateOrAlterStBuilder(cfg).buildCreateUserSt(caAttributes); if (createSQL == null) { throw new ConnectorException( "No create SQL generated, probably not enough attributes"); } Attribute roles = AttributeUtil.find(OracleConstants.ORACLE_ROLES_ATTR_NAME, attrs); Attribute privileges = AttributeUtil.find(OracleConstants.ORACLE_PRIVS_ATTR_NAME, attrs); List<String> rolesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantRoles(userName, OracleConnectorHelper.castList(roles, String.class)); List<String> privilegesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantPrivileges(userName, OracleConnectorHelper.castList(privileges, String.class)); SQLUtil.executeUpdateStatement(adminConn, createSQL); OracleSpecifics.execStatemts(adminConn, rolesSQL); OracleSpecifics.execStatemts(adminConn, privilegesSQL); adminConn.commit(); LOG.info("User created : [{0}]", userName); } catch (Exception e) { SQLUtil.rollbackQuietly(adminConn); if (e instanceof SQLException) { SQLException sqle = (SQLException) e; if ("42000".equals(sqle.getSQLState()) && 1920 == sqle.getErrorCode()) { throw new AlreadyExistsException("User [" + userName + "] already exists"); } } throw new ConnectorException(cfg.getConnectorMessages().format( MSG_CREATE_OF_USER_FAILED, null, userName), e); } return new Uid(userName); } }
OracleOperationCreate extends AbstractOracleOperation implements CreateOp { public Uid create(ObjectClass oclass, Set<Attribute> attrs, OperationOptions options) { OracleConnectorHelper.checkObjectClass(oclass, cfg.getConnectorMessages()); Map<String, Attribute> map = AttributeUtil.toMap(attrs); checkCreateAttributes(map); String userName = OracleConnectorHelper.getStringValue(map, Name.NAME, cfg.getConnectorMessages()); new LocalizedAssert(cfg.getConnectorMessages()).assertNotBlank(userName, Name.NAME); LOG.info("Creating user : [{0}]", userName); OracleUserAttributes.Builder builder = new OracleUserAttributes.Builder(); builder.setUserName(userName); new OracleAttributesReader(cfg.getConnectorMessages()).readCreateAttributes(map, builder); OracleUserAttributes caAttributes = builder.build(); try { String createSQL = new OracleCreateOrAlterStBuilder(cfg).buildCreateUserSt(caAttributes); if (createSQL == null) { throw new ConnectorException( "No create SQL generated, probably not enough attributes"); } Attribute roles = AttributeUtil.find(OracleConstants.ORACLE_ROLES_ATTR_NAME, attrs); Attribute privileges = AttributeUtil.find(OracleConstants.ORACLE_PRIVS_ATTR_NAME, attrs); List<String> rolesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantRoles(userName, OracleConnectorHelper.castList(roles, String.class)); List<String> privilegesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantPrivileges(userName, OracleConnectorHelper.castList(privileges, String.class)); SQLUtil.executeUpdateStatement(adminConn, createSQL); OracleSpecifics.execStatemts(adminConn, rolesSQL); OracleSpecifics.execStatemts(adminConn, privilegesSQL); adminConn.commit(); LOG.info("User created : [{0}]", userName); } catch (Exception e) { SQLUtil.rollbackQuietly(adminConn); if (e instanceof SQLException) { SQLException sqle = (SQLException) e; if ("42000".equals(sqle.getSQLState()) && 1920 == sqle.getErrorCode()) { throw new AlreadyExistsException("User [" + userName + "] already exists"); } } throw new ConnectorException(cfg.getConnectorMessages().format( MSG_CREATE_OF_USER_FAILED, null, userName), e); } return new Uid(userName); } OracleOperationCreate(OracleConfiguration cfg, Connection adminConn); }
OracleOperationCreate extends AbstractOracleOperation implements CreateOp { public Uid create(ObjectClass oclass, Set<Attribute> attrs, OperationOptions options) { OracleConnectorHelper.checkObjectClass(oclass, cfg.getConnectorMessages()); Map<String, Attribute> map = AttributeUtil.toMap(attrs); checkCreateAttributes(map); String userName = OracleConnectorHelper.getStringValue(map, Name.NAME, cfg.getConnectorMessages()); new LocalizedAssert(cfg.getConnectorMessages()).assertNotBlank(userName, Name.NAME); LOG.info("Creating user : [{0}]", userName); OracleUserAttributes.Builder builder = new OracleUserAttributes.Builder(); builder.setUserName(userName); new OracleAttributesReader(cfg.getConnectorMessages()).readCreateAttributes(map, builder); OracleUserAttributes caAttributes = builder.build(); try { String createSQL = new OracleCreateOrAlterStBuilder(cfg).buildCreateUserSt(caAttributes); if (createSQL == null) { throw new ConnectorException( "No create SQL generated, probably not enough attributes"); } Attribute roles = AttributeUtil.find(OracleConstants.ORACLE_ROLES_ATTR_NAME, attrs); Attribute privileges = AttributeUtil.find(OracleConstants.ORACLE_PRIVS_ATTR_NAME, attrs); List<String> rolesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantRoles(userName, OracleConnectorHelper.castList(roles, String.class)); List<String> privilegesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantPrivileges(userName, OracleConnectorHelper.castList(privileges, String.class)); SQLUtil.executeUpdateStatement(adminConn, createSQL); OracleSpecifics.execStatemts(adminConn, rolesSQL); OracleSpecifics.execStatemts(adminConn, privilegesSQL); adminConn.commit(); LOG.info("User created : [{0}]", userName); } catch (Exception e) { SQLUtil.rollbackQuietly(adminConn); if (e instanceof SQLException) { SQLException sqle = (SQLException) e; if ("42000".equals(sqle.getSQLState()) && 1920 == sqle.getErrorCode()) { throw new AlreadyExistsException("User [" + userName + "] already exists"); } } throw new ConnectorException(cfg.getConnectorMessages().format( MSG_CREATE_OF_USER_FAILED, null, userName), e); } return new Uid(userName); } OracleOperationCreate(OracleConfiguration cfg, Connection adminConn); Uid create(ObjectClass oclass, Set<Attribute> attrs, OperationOptions options); }
OracleOperationCreate extends AbstractOracleOperation implements CreateOp { public Uid create(ObjectClass oclass, Set<Attribute> attrs, OperationOptions options) { OracleConnectorHelper.checkObjectClass(oclass, cfg.getConnectorMessages()); Map<String, Attribute> map = AttributeUtil.toMap(attrs); checkCreateAttributes(map); String userName = OracleConnectorHelper.getStringValue(map, Name.NAME, cfg.getConnectorMessages()); new LocalizedAssert(cfg.getConnectorMessages()).assertNotBlank(userName, Name.NAME); LOG.info("Creating user : [{0}]", userName); OracleUserAttributes.Builder builder = new OracleUserAttributes.Builder(); builder.setUserName(userName); new OracleAttributesReader(cfg.getConnectorMessages()).readCreateAttributes(map, builder); OracleUserAttributes caAttributes = builder.build(); try { String createSQL = new OracleCreateOrAlterStBuilder(cfg).buildCreateUserSt(caAttributes); if (createSQL == null) { throw new ConnectorException( "No create SQL generated, probably not enough attributes"); } Attribute roles = AttributeUtil.find(OracleConstants.ORACLE_ROLES_ATTR_NAME, attrs); Attribute privileges = AttributeUtil.find(OracleConstants.ORACLE_PRIVS_ATTR_NAME, attrs); List<String> rolesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantRoles(userName, OracleConnectorHelper.castList(roles, String.class)); List<String> privilegesSQL = new OracleRolesAndPrivsBuilder(cfg.getCSSetup()).buildGrantPrivileges(userName, OracleConnectorHelper.castList(privileges, String.class)); SQLUtil.executeUpdateStatement(adminConn, createSQL); OracleSpecifics.execStatemts(adminConn, rolesSQL); OracleSpecifics.execStatemts(adminConn, privilegesSQL); adminConn.commit(); LOG.info("User created : [{0}]", userName); } catch (Exception e) { SQLUtil.rollbackQuietly(adminConn); if (e instanceof SQLException) { SQLException sqle = (SQLException) e; if ("42000".equals(sqle.getSQLState()) && 1920 == sqle.getErrorCode()) { throw new AlreadyExistsException("User [" + userName + "] already exists"); } } throw new ConnectorException(cfg.getConnectorMessages().format( MSG_CREATE_OF_USER_FAILED, null, userName), e); } return new Uid(userName); } OracleOperationCreate(OracleConfiguration cfg, Connection adminConn); Uid create(ObjectClass oclass, Set<Attribute> attrs, OperationOptions options);  }