Skip to content

Commit

Permalink
Add user consent to show his info
Browse files Browse the repository at this point in the history
  • Loading branch information
dhfelix committed Nov 20, 2019
1 parent c424eb7 commit 979e7d7
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>mx.nic.labs.reddog</groupId>
<artifactId>rdap-sql-provider</artifactId>
<version>1.4.2</version>
<version>1.5.0</version>
<packaging>jar</packaging>

<name>mx.nic.labs.reddog:rdap-sql-provider</name>
Expand Down Expand Up @@ -49,7 +49,7 @@
<dependency>
<groupId>mx.nic.labs.reddog</groupId>
<artifactId>rdap-core</artifactId>
<version>1.2.1</version>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mx.nic.labs.reddog</groupId>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/mx/nic/rdap/sql/SQLProviderConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -254,4 +267,8 @@ public static boolean isReverseIpv6Enabled() {
public static boolean isNsSharingNameEnabled() {
return isNsSharingNameEnabled;
}

public static UserConsentType getUserConsentType() {
return userConsentType;
}
}
5 changes: 5 additions & 0 deletions src/main/java/mx/nic/rdap/sql/UserConsentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mx.nic.rdap.sql;

public enum UserConsentType {
NONE, GLOBAL, ATTRIBUTES;
}
90 changes: 90 additions & 0 deletions src/main/java/mx/nic/rdap/sql/model/EntityModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -127,6 +150,70 @@ private static void loadNestedObjects(Entity entity, Connection connection) thro
// retrieve the entity roles
List<Role> 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 {
Expand Down Expand Up @@ -235,6 +322,9 @@ private static void setNestedSimpleObjects(List<Entity> entities, Connection con
List<Event> eventList = EventModel.getByEntityId(entityId, connection);
entity.getEvents().addAll(eventList);

// retrieve the consent for this object;
entity.setConsent(getConsentById(entityId, connection));

if (isFirstNested) {
List<Entity> nestedEntities = getEntitiesByEntityId(entityId, connection, false);
entity.getEntities().addAll(nestedEntities);
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/META-INF/sql/Entity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?;
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 = ?;
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/test/java/mx/nic/rdap/DummyDataBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
57 changes: 55 additions & 2 deletions src/test/resources/META-INF/sql/Database.sql
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 979e7d7

Please sign in to comment.