From 4791789d8fff22ca24488abd586ddad140a18c28 Mon Sep 17 00:00:00 2001 From: Justin Field Date: Fri, 8 Feb 2019 14:52:30 -0800 Subject: [PATCH] Prevent sdbs with the same slug from being created (#186) --- .../com/nike/cerberus/dao/SafeDepositBoxDao.java | 4 ++++ .../nike/cerberus/mapper/SafeDepositBoxMapper.java | 2 ++ .../nike/cerberus/record/SafeDepositBoxRecord.java | 13 +++++++++++++ .../cerberus/service/SafeDepositBoxService.java | 5 +++-- .../nike/cerberus/mapper/SafeDepositBoxMapper.xml | 11 +++++++++++ .../migration/V1.6.5.0__add_sdb_name_slug.sql | 13 +++++++++++++ 6 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/com/nike/cerberus/migration/V1.6.5.0__add_sdb_name_slug.sql diff --git a/src/main/java/com/nike/cerberus/dao/SafeDepositBoxDao.java b/src/main/java/com/nike/cerberus/dao/SafeDepositBoxDao.java index 0854c2592..ca0629285 100644 --- a/src/main/java/com/nike/cerberus/dao/SafeDepositBoxDao.java +++ b/src/main/java/com/nike/cerberus/dao/SafeDepositBoxDao.java @@ -87,6 +87,10 @@ public boolean isPathInUse(final String path) { return safeDepositBoxMapper.countByPath(path) > 0; } + public boolean isSlugUnique(final String slug) { + return safeDepositBoxMapper.countBySdbNameSlug(slug) > 0; + } + public int createSafeDepositBox(final SafeDepositBoxRecord safeDepositBox) { return safeDepositBoxMapper.createSafeDepositBox(safeDepositBox); } diff --git a/src/main/java/com/nike/cerberus/mapper/SafeDepositBoxMapper.java b/src/main/java/com/nike/cerberus/mapper/SafeDepositBoxMapper.java index c7e6e51aa..429ae8472 100644 --- a/src/main/java/com/nike/cerberus/mapper/SafeDepositBoxMapper.java +++ b/src/main/java/com/nike/cerberus/mapper/SafeDepositBoxMapper.java @@ -52,6 +52,8 @@ List getIamAssumedRoleAssociatedSafeDepositBoxes(@Param("i int countByPath(@Param("path") String path); + int countBySdbNameSlug(@Param("sdbNameSlug") String sdbNameSlug); + int createSafeDepositBox(@Param("record") SafeDepositBoxRecord record); int updateSafeDepositBox(@Param("record") SafeDepositBoxRecord record); diff --git a/src/main/java/com/nike/cerberus/record/SafeDepositBoxRecord.java b/src/main/java/com/nike/cerberus/record/SafeDepositBoxRecord.java index f422416a7..96e6bc5fd 100644 --- a/src/main/java/com/nike/cerberus/record/SafeDepositBoxRecord.java +++ b/src/main/java/com/nike/cerberus/record/SafeDepositBoxRecord.java @@ -33,6 +33,8 @@ public class SafeDepositBoxRecord { private String path; + private String sdbNameSlug; + private String createdBy; private String lastUpdatedBy; @@ -86,6 +88,15 @@ public SafeDepositBoxRecord setPath(String path) { return this; } + public String getSdbNameSlug() { + return sdbNameSlug; + } + + public SafeDepositBoxRecord setSdbNameSlug(String sdbNameSlug) { + this.sdbNameSlug = sdbNameSlug; + return this; + } + public String getCreatedBy() { return createdBy; } @@ -132,6 +143,7 @@ public boolean equals(Object o) { if (id != null ? !id.equals(record.id) : record.id != null) return false; if (categoryId != null ? !categoryId.equals(record.categoryId) : record.categoryId != null) return false; if (name != null ? !name.equals(record.name) : record.name != null) return false; + if (sdbNameSlug != null ? !sdbNameSlug.equals(record.sdbNameSlug) : record.sdbNameSlug != null) return false; if (description != null ? !description.equals(record.description) : record.description != null) return false; if (path != null ? !path.equals(record.path) : record.path != null) return false; if (createdBy != null ? !createdBy.equals(record.createdBy) : record.createdBy != null) return false; @@ -149,6 +161,7 @@ public int hashCode() { result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (description != null ? description.hashCode() : 0); result = 31 * result + (path != null ? path.hashCode() : 0); + result = 31 * result + (sdbNameSlug != null ? sdbNameSlug.hashCode() : 0); result = 31 * result + (createdBy != null ? createdBy.hashCode() : 0); result = 31 * result + (lastUpdatedBy != null ? lastUpdatedBy.hashCode() : 0); result = 31 * result + (createdTs != null ? createdTs.hashCode() : 0); diff --git a/src/main/java/com/nike/cerberus/service/SafeDepositBoxService.java b/src/main/java/com/nike/cerberus/service/SafeDepositBoxService.java index 079a55b4c..262850041 100644 --- a/src/main/java/com/nike/cerberus/service/SafeDepositBoxService.java +++ b/src/main/java/com/nike/cerberus/service/SafeDepositBoxService.java @@ -286,9 +286,9 @@ public SafeDepositBoxV2 createSafeDepositBoxV2(final SafeDepositBoxV2 safeDeposi final Set iamRolePermissionSet = safeDepositBox.getIamPrincipalPermissions(); - final boolean isPathInUse = safeDepositBoxDao.isPathInUse(boxRecordToStore.getPath()); + final boolean isSlugUnique = safeDepositBoxDao.isSlugUnique(boxRecordToStore.getSdbNameSlug()); - if (isPathInUse) { + if (isSlugUnique) { throw ApiException.newBuilder() .withApiErrors(DefaultApiError.SDB_UNIQUE_NAME) .build(); @@ -455,6 +455,7 @@ private SafeDepositBoxRecord buildBoxToStore(final SafeDepositBoxV2 requestedBox boxToStore.setId(uuidSupplier.get()); boxToStore.setCategoryId(requestedBox.getCategoryId()); boxToStore.setName(requestedBox.getName()); + boxToStore.setSdbNameSlug(Slugger.toSlug(requestedBox.getName())); boxToStore.setDescription(requestedBox.getDescription()); boxToStore.setCreatedTs(dateTime); boxToStore.setLastUpdatedTs(dateTime); diff --git a/src/main/resources/com/nike/cerberus/mapper/SafeDepositBoxMapper.xml b/src/main/resources/com/nike/cerberus/mapper/SafeDepositBoxMapper.xml index 2fd73d38e..7ad8de47b 100644 --- a/src/main/resources/com/nike/cerberus/mapper/SafeDepositBoxMapper.xml +++ b/src/main/resources/com/nike/cerberus/mapper/SafeDepositBoxMapper.xml @@ -230,6 +230,15 @@ PATH = #{path} + + INSERT INTO SAFE_DEPOSIT_BOX ( ID, @@ -237,6 +246,7 @@ NAME, DESCRIPTION, PATH, + SDB_NAME_SLUG, CREATED_BY, LAST_UPDATED_BY, CREATED_TS, @@ -248,6 +258,7 @@ #{record.name}, #{record.description}, #{record.path}, + #{record.sdbNameSlug}, #{record.createdBy}, #{record.lastUpdatedBy}, #{record.createdTs}, diff --git a/src/main/resources/com/nike/cerberus/migration/V1.6.5.0__add_sdb_name_slug.sql b/src/main/resources/com/nike/cerberus/migration/V1.6.5.0__add_sdb_name_slug.sql new file mode 100644 index 000000000..c196f4544 --- /dev/null +++ b/src/main/resources/com/nike/cerberus/migration/V1.6.5.0__add_sdb_name_slug.sql @@ -0,0 +1,13 @@ +ALTER TABLE SAFE_DEPOSIT_BOX +ADD COLUMN SDB_NAME_SLUG varchar(255) NOT NULL; + +UPDATE SAFE_DEPOSIT_BOX +JOIN ( + select substr(PATH_TRAILING_PATH_REMOVED, LOCATE('/', PATH_TRAILING_PATH_REMOVED) + 1, LENGTH(PATH_TRAILING_PATH_REMOVED)) as SDB_NAME_SLUG, SDB_ID + from ( + select SUBSTR(PATH, 1, LENGTH(PATH) -1) as PATH_TRAILING_PATH_REMOVED, ID as SDB_ID + from SAFE_DEPOSIT_BOX + ) as PATH_TRAILING_PATH_REMOVED_VIRTUAL_TABLE +) as JOINED_VIRTUAL_TABLE on SDB_ID = SAFE_DEPOSIT_BOX.ID +SET SAFE_DEPOSIT_BOX.SDB_NAME_SLUG=(JOINED_VIRTUAL_TABLE.SDB_NAME_SLUG) +WHERE JOINED_VIRTUAL_TABLE.SDB_ID = SAFE_DEPOSIT_BOX.ID; \ No newline at end of file