Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Prevent sdbs with the same slug from being created (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
fieldju authored Feb 8, 2019
1 parent be04fe5 commit 4791789
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/nike/cerberus/dao/SafeDepositBoxDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ List<SafeDepositBoxRecord> 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);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/nike/cerberus/record/SafeDepositBoxRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class SafeDepositBoxRecord {

private String path;

private String sdbNameSlug;

private String createdBy;

private String lastUpdatedBy;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ public SafeDepositBoxV2 createSafeDepositBoxV2(final SafeDepositBoxV2 safeDeposi

final Set<IamPrincipalPermission> 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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,23 @@
PATH = #{path}
</select>

<select id="countBySdbNameSlug" resultType="java.lang.Integer">
SELECT
COUNT(SDB_NAME_SLUG)
FROM
SAFE_DEPOSIT_BOX
WHERE
SDB_NAME_SLUG = #{sdbNameSlug}
</select>

<insert id="createSafeDepositBox" parameterType="SafeDepositBoxRecord">
INSERT INTO SAFE_DEPOSIT_BOX (
ID,
CATEGORY_ID,
NAME,
DESCRIPTION,
PATH,
SDB_NAME_SLUG,
CREATED_BY,
LAST_UPDATED_BY,
CREATED_TS,
Expand All @@ -248,6 +258,7 @@
#{record.name},
#{record.description},
#{record.path},
#{record.sdbNameSlug},
#{record.createdBy},
#{record.lastUpdatedBy},
#{record.createdTs},
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 4791789

Please sign in to comment.