From 979e7d7ccfbf0d2cb3b747741598d5c76c0070a7 Mon Sep 17 00:00:00 2001 From: dhfelix Date: Wed, 20 Nov 2019 15:28:16 -0600 Subject: [PATCH] Add user consent to show his info --- pom.xml | 4 +- .../rdap/sql/SQLProviderConfiguration.java | 17 ++++ .../java/mx/nic/rdap/sql/UserConsentType.java | 5 ++ .../mx/nic/rdap/sql/model/EntityModel.java | 90 +++++++++++++++++++ src/main/resources/META-INF/sql/Entity.sql | 8 +- .../sql_provider_configuration.properties | 3 + src/test/java/mx/nic/rdap/DummyDataBatch.java | 1 + src/test/resources/META-INF/sql/Database.sql | 57 +++++++++++- 8 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 src/main/java/mx/nic/rdap/sql/UserConsentType.java diff --git a/pom.xml b/pom.xml index 9e85ff5..9c864d1 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 mx.nic.labs.reddog rdap-sql-provider - 1.4.2 + 1.5.0 jar mx.nic.labs.reddog:rdap-sql-provider @@ -49,7 +49,7 @@ mx.nic.labs.reddog rdap-core - 1.2.1 + 1.3.0 mx.nic.labs.reddog diff --git a/src/main/java/mx/nic/rdap/sql/SQLProviderConfiguration.java b/src/main/java/mx/nic/rdap/sql/SQLProviderConfiguration.java index 234f34f..2a8caaa 100644 --- a/src/main/java/mx/nic/rdap/sql/SQLProviderConfiguration.java +++ b/src/main/java/mx/nic/rdap/sql/SQLProviderConfiguration.java @@ -32,6 +32,7 @@ public class SQLProviderConfiguration { private static final String IS_REVERSE_IPV6_ENABLED_KEY = "is_reverse_ipv6_enabled"; private final static String USER_SQL_FILES_KEY = "sql_files_directory"; private final static String IS_NS_SHARING_NAME_ENABLED_KEY = "is_ns_sharing_name_enabled"; + private final static String USER_CONSENT_CONFIGURATION_TYPE = "user_consent_type"; /** * Default Path in the META-INF folder of the server, to be use for the user @@ -73,6 +74,11 @@ public class SQLProviderConfiguration { * Indicate if the nameserver-sharing-name conformance is active */ private static boolean isNsSharingNameEnabled; + + /** + * Indicate the type of user consent for contact information in entity + **/ + private static UserConsentType userConsentType; /** * Load properties configured from the server. The default properties are loaded @@ -90,6 +96,13 @@ public static void initForServer(Properties serverProperties) { isReverseIpv4Enabled = getBooleanProperty(serverProperties, IS_REVERSE_IPV4_ENABLED_KEY); isReverseIpv6Enabled = getBooleanProperty(serverProperties, IS_REVERSE_IPV6_ENABLED_KEY); isNsSharingNameEnabled = getBooleanProperty(serverProperties, IS_NS_SHARING_NAME_ENABLED_KEY); + + String userConsentTypeString = getStringProperty(serverProperties, USER_CONSENT_CONFIGURATION_TYPE); + try { + userConsentType = UserConsentType.valueOf(userConsentTypeString.toUpperCase()); + } catch (IllegalArgumentException e) { + throw new InvalidConfigurationException("Invalid value for property:" + USER_CONSENT_CONFIGURATION_TYPE); + } // checks if the user puts sql files outside of the project String property = serverProperties.getProperty(USER_SQL_FILES_KEY); @@ -254,4 +267,8 @@ public static boolean isReverseIpv6Enabled() { public static boolean isNsSharingNameEnabled() { return isNsSharingNameEnabled; } + + public static UserConsentType getUserConsentType() { + return userConsentType; + } } diff --git a/src/main/java/mx/nic/rdap/sql/UserConsentType.java b/src/main/java/mx/nic/rdap/sql/UserConsentType.java new file mode 100644 index 0000000..eb6898c --- /dev/null +++ b/src/main/java/mx/nic/rdap/sql/UserConsentType.java @@ -0,0 +1,5 @@ +package mx.nic.rdap.sql; + +public enum UserConsentType { + NONE, GLOBAL, ATTRIBUTES; +} diff --git a/src/main/java/mx/nic/rdap/sql/model/EntityModel.java b/src/main/java/mx/nic/rdap/sql/model/EntityModel.java index 865d16b..1f5ed8d 100644 --- a/src/main/java/mx/nic/rdap/sql/model/EntityModel.java +++ b/src/main/java/mx/nic/rdap/sql/model/EntityModel.java @@ -18,6 +18,9 @@ import mx.nic.rdap.core.db.Event; import mx.nic.rdap.core.db.IpNetwork; import mx.nic.rdap.core.db.PublicId; +import mx.nic.rdap.core.db.UserConsent; +import mx.nic.rdap.core.db.UserConsentGlobal; +import mx.nic.rdap.core.db.UserConsentByAttribute; import mx.nic.rdap.core.db.VCard; import mx.nic.rdap.db.exception.http.NotImplementedException; import mx.nic.rdap.db.struct.SearchResultStruct; @@ -52,6 +55,11 @@ public class EntityModel { private final static String SEARCH_BY_HANDLE_REGEX_QUERY = "searchByRegexHandle"; private final static String SEARCH_BY_NAME_REGEX_QUERY = "searchByRegexName"; + + private final static String GET_USER_GLOBAL_CONSENT = "userGlobalConsent"; + private final static String GET_USER_CONSENT_BY_ATTR = "userConsentByAttribute"; + + private static String GET_CONSENT; public static void loadQueryGroup(String schema) { try { @@ -60,6 +68,21 @@ public static void loadQueryGroup(String schema) { } catch (IOException e) { throw new RuntimeException("Error loading query group"); } + + switch (SQLProviderConfiguration.getUserConsentType()) { + case GLOBAL: + GET_CONSENT = GET_USER_GLOBAL_CONSENT; + break; + case ATTRIBUTES: + GET_CONSENT = GET_USER_CONSENT_BY_ATTR; + break; + case NONE: + default: + GET_CONSENT = null; + // nothing to do. + break; + } + } private static void setQueryGroup(QueryGroup qG) { @@ -127,6 +150,70 @@ private static void loadNestedObjects(Entity entity, Connection connection) thro // retrieve the entity roles List mainEntityRole = RoleModel.getMainEntityRole(entityId, connection); entity.getRoles().addAll(mainEntityRole); + + // retrieve the consent for this object; + entity.setConsent(getConsentById(entityId, connection)); + } + + private static UserConsent getConsentById(long entityId, Connection connection) throws SQLException { + if (GET_CONSENT == null) { + return null; + } + + String query = getQueryGroup().getQuery(GET_CONSENT); + if (SQLProviderConfiguration.isUserSQL() && query == null) { + return null; + } + + UserConsent result = null; + try (PreparedStatement statement = connection.prepareStatement(query);) { + statement.setLong(1, entityId); + logger.log(Level.INFO, "Executing QUERY: " + statement.toString()); + ResultSet rs = statement.executeQuery(); + if (!rs.next()) { + return null; + } + + // TODO handle result; + //result + switch (SQLProviderConfiguration.getUserConsentType()) { + case GLOBAL: + UserConsentGlobal userConsent = new UserConsentGlobal(); + userConsent.setGlobalConsent(rs.getBoolean("ugc_consent")); + result = userConsent; + break; + case ATTRIBUTES: + UserConsentByAttribute consentByAttr = new UserConsentByAttribute(); + + consentByAttr.setHandle(rs.getBoolean("uca_handle")); + consentByAttr.setName(rs.getBoolean("uca_name")); + consentByAttr.setCompanyName(rs.getBoolean("uca_companyName")); + consentByAttr.setCompanyURL(rs.getBoolean("uca_companyURL")); + consentByAttr.setEmail(rs.getBoolean("uca_email")); + consentByAttr.setVoice(rs.getBoolean("uca_voice")); + consentByAttr.setCellphone(rs.getBoolean("uca_cellphone")); + consentByAttr.setFax(rs.getBoolean("uca_fax")); + consentByAttr.setJobTitle(rs.getBoolean("uca_jobTitle")); + consentByAttr.setContactUri(rs.getBoolean("uca_contactUri")); + consentByAttr.setType(rs.getBoolean("uca_type")); + consentByAttr.setCountry(rs.getBoolean("uca_country")); + consentByAttr.setCountryCode(rs.getBoolean("uca_countryCode")); + consentByAttr.setCity(rs.getBoolean("uca_city")); + consentByAttr.setState(rs.getBoolean("uca_state")); + consentByAttr.setStreet1(rs.getBoolean("uca_street1")); + consentByAttr.setStreet2(rs.getBoolean("uca_street2")); + consentByAttr.setStreet3(rs.getBoolean("uca_street3")); + consentByAttr.setPostalCode(rs.getBoolean("uca_postalCode")); + + result = consentByAttr; + break; + default: + // Maybe Programming error? + break; + } + } + + return result; } private static EntityDbObj processResultSet(ResultSet resultSet, Connection connection) throws SQLException { @@ -235,6 +322,9 @@ private static void setNestedSimpleObjects(List entities, Connection con List eventList = EventModel.getByEntityId(entityId, connection); entity.getEvents().addAll(eventList); + // retrieve the consent for this object; + entity.setConsent(getConsentById(entityId, connection)); + if (isFirstNested) { List nestedEntities = getEntitiesByEntityId(entityId, connection, false); entity.getEntities().addAll(nestedEntities); diff --git a/src/main/resources/META-INF/sql/Entity.sql b/src/main/resources/META-INF/sql/Entity.sql index a0c0f39..3cdb843 100644 --- a/src/main/resources/META-INF/sql/Entity.sql +++ b/src/main/resources/META-INF/sql/Entity.sql @@ -47,4 +47,10 @@ SELECT EXISTS(SELECT 1 FROM {schema}.entity ent JOIN {schema}.entity_contact eco SELECT ent_id, ent_handle, ent_port43 FROM {schema}.entity e WHERE e.ent_handle REGEXP ? ORDER BY 1 LIMIT ?; #searchByRegexName -SELECT DISTINCT (ent.ent_id), ent.ent_handle, ent.ent_port43 FROM {schema}.entity ent JOIN {schema}.entity_contact eco ON eco.ent_id=ent.ent_id JOIN {schema}.vcard vca ON vca.vca_id=eco.vca_id WHERE vca.vca_name REGEXP ? ORDER BY 1 LIMIT ?; \ No newline at end of file +SELECT DISTINCT (ent.ent_id), ent.ent_handle, ent.ent_port43 FROM {schema}.entity ent JOIN {schema}.entity_contact eco ON eco.ent_id=ent.ent_id JOIN {schema}.vcard vca ON vca.vca_id=eco.vca_id WHERE vca.vca_name REGEXP ? ORDER BY 1 LIMIT ?; + +#userGlobalConsent +SELECT ugc_consent FROM {schema}.user_global_consent ugc WHERE ugc.ent_id = ?; + +#userConsentByAttribute +SELECT * FROM {schema}.user_consent_by_attributes uca WHERE uca.ent_id = ?; \ No newline at end of file diff --git a/src/main/resources/META-INF/sql_provider_configuration.properties b/src/main/resources/META-INF/sql_provider_configuration.properties index 2248f16..50b6111 100644 --- a/src/main/resources/META-INF/sql_provider_configuration.properties +++ b/src/main/resources/META-INF/sql_provider_configuration.properties @@ -14,3 +14,6 @@ is_reverse_ipv6_enabled = false #Optional. Boolean value to indicate if the user wants to enable the ns-sharing-name conformance. is_ns_sharing_name_enabled = false + +#Optional. Enum value (NONE, ATTRIBUTES, or GLOBAL) indicates the type of user consent this server supports. Default: NONE +user_consent_type = NONE \ No newline at end of file diff --git a/src/test/java/mx/nic/rdap/DummyDataBatch.java b/src/test/java/mx/nic/rdap/DummyDataBatch.java index f6b28c2..ac64cc6 100644 --- a/src/test/java/mx/nic/rdap/DummyDataBatch.java +++ b/src/test/java/mx/nic/rdap/DummyDataBatch.java @@ -212,6 +212,7 @@ private void createDomains(String domainName, String tld, Entity registrant, Ent VariantName vn = new VariantName(); String[] variantsString = variants.get(idnChar); String variantName = domainName + variantsString[i] + index + "." + tld; + vn.setUnicodeName(DomainLabel.nameToUnicode(variantName)); vn.setLdhName(DomainLabel.nameToASCII(variantName)); variant.getVariantNames().add(vn); } diff --git a/src/test/resources/META-INF/sql/Database.sql b/src/test/resources/META-INF/sql/Database.sql index 25a4a2f..1415b71 100644 --- a/src/test/resources/META-INF/sql/Database.sql +++ b/src/test/resources/META-INF/sql/Database.sql @@ -1,6 +1,6 @@ -- MySQL Script generated by MySQL Workbench --- 11/07/17 19:48:02 --- Model: New Model Version: 1.0 +-- 20/11/2019 00:00:00 +-- Model: New Model Version: 1.5.0 -- MySQL Workbench Forward Engineering SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; @@ -1605,6 +1605,59 @@ CREATE TABLE IF NOT EXISTS `rdap`.`entity_role` ( ENGINE = InnoDB COMMENT = 'This table contains the relation between an Entity and its role.'; +-- ----------------------------------------------------- +-- Table `rdap`.`user_consent_by_attributes` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `rdap`.`user_consent_by_attributes` ; + +CREATE TABLE IF NOT EXISTS `rdap`.`user_consent_by_attributes` ( + `ent_id` BIGINT NOT NULL, + `uca_handle` TINYINT NOT NULL, + `uca_name` TINYINT NOT NULL, + `uca_companyName` TINYINT NOT NULL, + `uca_companyURL` TINYINT NOT NULL, + `uca_email` TINYINT NOT NULL, + `uca_voice` TINYINT NOT NULL, + `uca_cellphone` TINYINT NOT NULL, + `uca_fax` TINYINT NOT NULL, + `uca_jobTitle` TINYINT NOT NULL, + `uca_contactUri` TINYINT NOT NULL, + `uca_type` TINYINT NOT NULL, + `uca_country` TINYINT NOT NULL, + `uca_countryCode` TINYINT NOT NULL, + `uca_city` TINYINT NOT NULL, + `uca_state` TINYINT NOT NULL, + `uca_street1` TINYINT NOT NULL, + `uca_street2` TINYINT NOT NULL, + `uca_street3` TINYINT NOT NULL, + `uca_postalCode` TINYINT NOT NULL, + PRIMARY KEY (`ent_id`), + UNIQUE INDEX `ent_id_UNIQUE` (`ent_id` ASC), + CONSTRAINT `fk_user_consent_by_attributes_entity` + FOREIGN KEY (`ent_id`) + REFERENCES `rdap`.`entity` (`ent_id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +COMMENT = 'Table for user consent by attribute for contact information'; + +-- ----------------------------------------------------- +-- Table `rdap`.`user_global_consent` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `rdap`.`user_global_consent` ; + +CREATE TABLE IF NOT EXISTS `rdap`.`user_global_consent` ( + `ent_id` BIGINT NOT NULL, + `ugc_consent` TINYINT NOT NULL, + PRIMARY KEY (`ent_id`), + UNIQUE INDEX `ent_id_UNIQUE` (`ent_id` ASC), + CONSTRAINT `fk_user_global_consent_entity` + FOREIGN KEY (`ent_id`) + REFERENCES `rdap`.`entity` (`ent_id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +COMMENT = 'Table for user global consent for contact information'; SET SQL_MODE=@OLD_SQL_MODE; SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;