Skip to content

slug validation and search #1407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
slug validation
  • Loading branch information
dragonpoo committed Dec 27, 2024
commit 081791f8701a9f8f549094e6a8a04f22b7abc20c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public interface ApplicationRepository extends ReactiveMongoRepository<Applicati
* Find all agency applications
*/
Flux<Application> findByPublicToAllIsTrueAndAgencyProfileIsTrue();
Mono<Boolean> existsBySlug(String slug);

@Query("{ 'organizationId': ?0, 'slug': ?1 }")
Mono<Boolean> existsByOrganizationIdAndSlug(String organizationId, String slug);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.lowcoder.domain.permission.model.ResourceType;
import org.lowcoder.domain.permission.service.ResourcePermissionService;
import org.lowcoder.domain.user.repository.UserRepository;
import org.lowcoder.domain.util.SlugUtils;
import org.lowcoder.infra.annotation.NonEmptyMono;
import org.lowcoder.infra.mongo.MongoUpsertHelper;
import org.lowcoder.sdk.constants.FieldName;
Expand Down Expand Up @@ -349,15 +350,15 @@ public Mono<Map<String, Object>> getLiveDSLByApplicationId(String applicationId)

@Override
public Mono<Application> updateSlug(String applicationId, String newSlug) {
return repository.existsBySlug(newSlug).flatMap(exists -> {
return repository.findById(applicationId).flatMap(application -> repository.existsByOrganizationIdAndSlug(application.getOrganizationId(), newSlug).flatMap(exists -> {
if (!SlugUtils.validate(newSlug)) {
return Mono.error(new BizException(BizError.INVALID_SLUG, "Slug format is invalid"));
}
if (exists) {
return Mono.error(new BizException(BizError.DUPLICATE_ENTRY, "Slug already exists"));
}
return repository.findById(applicationId)
.flatMap(application -> {
application.setSlug(newSlug);
return repository.save(application);
});
});
application.setSlug(newSlug);
return repository.save(application);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.lowcoder.domain.organization.repository.OrganizationRepository;
import org.lowcoder.domain.user.model.User;
import org.lowcoder.domain.user.repository.UserRepository;
import org.lowcoder.domain.util.SlugUtils;
import org.lowcoder.infra.annotation.PossibleEmptyMono;
import org.lowcoder.infra.mongo.MongoUpsertHelper;
import org.lowcoder.sdk.config.CommonConfig;
Expand Down Expand Up @@ -292,6 +293,9 @@ private String buildCommonSettingsUpdateTimeKey(String key) {
@Override
public Mono<Organization> updateSlug(String organizationId, String newSlug) {
return repository.existsBySlug(newSlug).flatMap(exists -> {
if (!SlugUtils.validate(newSlug)) {
return Mono.error(new BizException(BizError.INVALID_SLUG, "Slug format is invalid"));
}
if (exists) {
return Mono.error(new BizException(BizError.DUPLICATE_ENTRY, "Slug already exists"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.lowcoder.domain.util;

public class SlugUtils {
public static Boolean validate(String slug) {
return slug.matches("^[a-zA-Z0-9_-]*$");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ public enum BizError {
ILLEGAL_BUNDLE_PERMISSION_ID(500, 6404),

//slug 6501 - 6501
DUPLICATE_ENTRY(403, 6501);
DUPLICATE_ENTRY(403, 6501),
INVALID_SLUG(403, 6502);

static {
checkDuplicates(values(), BizError::getBizErrorCode);
Expand Down