mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-23 22:50:51 +08:00
feat: allow user tag creation
This commit is contained in:
@@ -90,7 +90,7 @@ public class SecurityConfig {
|
||||
.requestMatchers(HttpMethod.GET, "/api/search/**").permitAll()
|
||||
.requestMatchers(HttpMethod.GET, "/api/users/**").permitAll()
|
||||
.requestMatchers(HttpMethod.POST, "/api/categories/**").hasAuthority("ADMIN")
|
||||
.requestMatchers(HttpMethod.POST, "/api/tags/**").hasAuthority("ADMIN")
|
||||
.requestMatchers(HttpMethod.POST, "/api/tags/**").authenticated()
|
||||
.requestMatchers(HttpMethod.DELETE, "/api/categories/**").hasAuthority("ADMIN")
|
||||
.requestMatchers(HttpMethod.DELETE, "/api/tags/**").hasAuthority("ADMIN")
|
||||
.requestMatchers("/api/admin/**").hasAuthority("ADMIN")
|
||||
|
||||
@@ -11,8 +11,10 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class TagService {
|
||||
private final TagRepository tagRepository;
|
||||
private final TagValidator tagValidator;
|
||||
|
||||
public Tag createTag(String name, String description, String icon, String smallIcon) {
|
||||
tagValidator.validate(name);
|
||||
Tag tag = new Tag();
|
||||
tag.setName(name);
|
||||
tag.setDescription(description);
|
||||
@@ -25,6 +27,7 @@ public class TagService {
|
||||
Tag tag = tagRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Tag not found"));
|
||||
if (name != null) {
|
||||
tagValidator.validate(name);
|
||||
tag.setName(name);
|
||||
}
|
||||
if (description != null) {
|
||||
|
||||
20
src/main/java/com/openisle/service/TagValidator.java
Normal file
20
src/main/java/com/openisle/service/TagValidator.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.exception.FieldException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Service
|
||||
public class TagValidator {
|
||||
private static final Pattern ALLOWED = Pattern.compile("^[A-Za-z0-9\\u4e00-\\u9fa5]+$");
|
||||
|
||||
public void validate(String name) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new FieldException("name", "Tag name cannot be empty");
|
||||
}
|
||||
if (!ALLOWED.matcher(name).matches()) {
|
||||
throw new FieldException("name", "Tag name must be letters or numbers");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user