-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from gelecekbilimde/GBS-55/username-create
GBS-55 | Username Created
- Loading branch information
Showing
12 changed files
with
114 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/gelecekbilimde/scienceplatform/common/util/validation/Username.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...ain/java/org/gelecekbilimde/scienceplatform/common/util/validation/UsernameValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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 (value.length() <= 3 || value.length() >= 20) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate("must be between 3 and 20 characters long") | ||
.addConstraintViolation(); | ||
return false; | ||
} | ||
|
||
if (!lowerCasedValue.matches(USERNAME_REGEX)) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate("must be alphanumeric") | ||
.addConstraintViolation(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...java/org/gelecekbilimde/scienceplatform/user/exception/UsernameAlreadyTakenException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, '[email protected]', | ||
'FEMALE', 'Test', 'Yönetici', | ||
'FEMALE', 'Test', 'Yönetici', 'administrator12', | ||
'$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, '[email protected]', | ||
'FEMALE', 'Test', 'Moderatör', | ||
'FEMALE', 'Test', 'Moderatör', 'moderator12', | ||
'$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, '[email protected]', | ||
'MALE', 'Test', 'Yazar', | ||
'MALE', 'Test', 'Yazar', 'author12', | ||
'$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, '[email protected]', | ||
'MALE', 'Test', 'Kullanıcı', | ||
'MALE', 'Test', 'Kullanıcı', 'users12', | ||
'$2a$10$atVE.cT5YpEOS7ZLSoVdk.QKVyYBCgvNhvQEuCcXbEMpohYIjbZDG', 'VERIFIED', | ||
'e3a1a32d-fcd7-46f0-bb2b-201df6b2b808', 'gelecekbilimde', current_timestamp); |