Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.gelecekbilimde.scienceplatform.common.util.validation.Username;
import org.gelecekbilimde.scienceplatform.user.model.enums.UserDegree;
import org.gelecekbilimde.scienceplatform.user.model.enums.UserGender;

Expand All @@ -28,6 +29,10 @@ public class RegisterRequest {
@Size(min = 2, max = 25)
private String lastname;

@NotBlank
@Username
private String username;

@NotBlank
@Email
@Size(min = 8, max = 255)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.gelecekbilimde.scienceplatform.auth.model.request.VerifyRequest;
import org.gelecekbilimde.scienceplatform.auth.port.RoleReadPort;
import org.gelecekbilimde.scienceplatform.auth.service.RegistrationService;
import org.gelecekbilimde.scienceplatform.user.exception.UsernameAlreadyTakenException;
import org.gelecekbilimde.scienceplatform.user.model.User;
import org.gelecekbilimde.scienceplatform.user.model.UserVerification;
import org.gelecekbilimde.scienceplatform.user.model.enums.UserStatus;
Expand Down Expand Up @@ -48,9 +49,14 @@ public void register(RegisterRequest request) {
Role role = roleReadPort.findByName(RoleName.USER)
.orElseThrow(() -> new RoleNotFoundByNameException(RoleName.USER.name()));

if (userReadPort.existsByUsername(request.getUsername())) {
throw new UsernameAlreadyTakenException(request.getUsername());
}

User user = User.builder()
.firstName(request.getFirstname())
.lastName(request.getLastname())
.username(request.getUsername())
.email(request.getEmail())
.birthDate(request.getBirthDate())
.biography(request.getBiography())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.gelecekbilimde.scienceplatform.common.util.validation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = UsernameValidator.class)
public @interface Username {

String message() default "must be valid";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.gelecekbilimde.scienceplatform.common.util.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.util.StringUtils;

class UsernameValidator implements ConstraintValidator<Username, String> {

private static final String USERNAME_REGEX = "^[a-zA-Z0-9]{3,20}$";

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (!StringUtils.hasText(value)) {
return true;
}

String lowerCasedValue = value.toLowerCase();

if (!value.trim().equals(value)) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("name must not start or end with whitespace")
.addConstraintViolation();
return false;
}

if (!lowerCasedValue.matches(USERNAME_REGEX)) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("Username must be 3-20 characters long and alphanumeric")
.addConstraintViolation();
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.gelecekbilimde.scienceplatform.user.exception;

import org.gelecekbilimde.scienceplatform.common.exception.AbstractConflictException;

import java.io.Serial;

public final class UsernameAlreadyTakenException extends AbstractConflictException {

@Serial
private static final long serialVersionUID = -3365493595680402581L;

public UsernameAlreadyTakenException(String username) {
super(String.format("'%s' username already taken", username));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class User extends BaseDomainModel {
private String password;
private String firstName;
private String lastName;
private String username;
private String avatarPath;
private String biography;
private LocalDate birthDate;
Expand All @@ -32,6 +33,11 @@ public class User extends BaseDomainModel {
private Role role;


public String getUsername() {
return username != null ? username.toLowerCase() : null;
}


public boolean isVerified() {
return this.status == UserStatus.VERIFIED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class UserEntity extends BaseEntity {
@Column(name = "last_name")
private String lastName;

@Column(name = "username")
private String username;

@Column(name = "avatar_path")
private String avatar;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface UserReadPort {

boolean existsByEmail(String email);

boolean existsByUsername(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public boolean existsByEmail(final String email) {
}


@Override
public boolean existsByUsername(String username) {
return userRepository.existsByUsername(username);
}


@Override
public User save(User user) {
final UserEntity userEntity = userToEntityMapper.map(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface UserRepository extends JpaRepository<UserEntity, String> {
Optional<UserEntity> findByEmail(String email);

boolean existsByEmail(String email);

boolean existsByUsername(String username);
}
1 change: 1 addition & 0 deletions src/main/resources/db/migration/V1__ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ create table if not exists gb_user
password varchar(255) not null,
first_name varchar(25) not null,
last_name varchar(25) not null,
username varchar(20) not null unique,
avatar_path varchar(255),
biography text,
birth_date date,
Expand Down
16 changes: 8 additions & 8 deletions src/main/resources/db/migration/V2__dml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -274,30 +274,30 @@ values (0, null, 'Bilim', 'Bilim kategorisidir.', 'bilim', 'flask-conical',
'gelecekbilimde', current_timestamp);


insert into gb_user (id, birth_date, email, gender, first_name, last_name, password,
insert into gb_user (id, birth_date, email, gender, first_name, last_name, username, password,
status, role_id, created_by, created_at)
values ('22afc9b4-807f-4eb2-b286-788631d1ed55', current_date, 'admin@gelecekbilimde.net',
'FEMALE', 'Test', 'Yönetici',
'FEMALE', 'Test', 'Yönetici', 'yönetici12',
'$2a$10$atVE.cT5YpEOS7ZLSoVdk.QKVyYBCgvNhvQEuCcXbEMpohYIjbZDG', 'VERIFIED',
'c147b5c2-87f7-4bb7-a165-368f639d8c3c', 'gelecekbilimde', current_timestamp);

insert into gb_user (id, birth_date, email, gender, first_name, last_name, password,
insert into gb_user (id, birth_date, email, gender, first_name, last_name, username, password,
status, role_id, created_by, created_at)
values ('99af408c-bec9-4cf2-a5ea-218b12b88a50', current_date, 'moderator@gelecekbilimde.net',
'FEMALE', 'Test', 'Moderatör',
'FEMALE', 'Test', 'Moderatör', 'moderatör12',
'$2a$10$atVE.cT5YpEOS7ZLSoVdk.QKVyYBCgvNhvQEuCcXbEMpohYIjbZDG', 'VERIFIED',
'1ed82a25-d348-4576-b4e6-1f2a7c430ca7', 'gelecekbilimde', current_timestamp);

insert into gb_user (id, birth_date, email, gender, first_name, last_name, password,
insert into gb_user (id, birth_date, email, gender, first_name, last_name, username, password,
status, role_id, created_by, created_at)
values ('fee95298-952d-4d1c-81dd-ae5a96b964e5', current_date, 'author@gelecekbilimde.net',
'MALE', 'Test', 'Yazar',
'MALE', 'Test', 'Yazar', 'yazar12',
'$2a$10$atVE.cT5YpEOS7ZLSoVdk.QKVyYBCgvNhvQEuCcXbEMpohYIjbZDG', 'VERIFIED',
'4d98a76c-9841-4aea-b296-2f27aa610b6c', 'gelecekbilimde', current_timestamp);

insert into gb_user (id, birth_date, email, gender, first_name, last_name, password,
insert into gb_user (id, birth_date, email, gender, first_name, last_name, username, password,
status, role_id, created_by, created_at)
values ('233d4054-e7b9-43ba-8b26-ca9254df78cd', current_date, 'user@gelecekbilimde.net',
'MALE', 'Test', 'Kullanıcı',
'MALE', 'Test', 'Kullanıcı', 'kullanıcı12',
'$2a$10$atVE.cT5YpEOS7ZLSoVdk.QKVyYBCgvNhvQEuCcXbEMpohYIjbZDG', 'VERIFIED',
'e3a1a32d-fcd7-46f0-bb2b-201df6b2b808', 'gelecekbilimde', current_timestamp);
Loading