-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83fc826
commit 0006e0d
Showing
11 changed files
with
189 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package by.petrovich.eshop.entity; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.OneToMany; | ||
import jakarta.persistence.Table; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Data | ||
@Entity | ||
@NoArgsConstructor | ||
@Table(name = "roles") | ||
public class Role { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "role_id") | ||
private Integer id; | ||
@Column(nullable = false, unique = true) | ||
private String name; | ||
@OneToMany(mappedBy = "role") | ||
private Set<User> users = new HashSet<>(); | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/by/petrovich/eshop/repository/RoleRepository.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,9 @@ | ||
package by.petrovich.eshop.repository; | ||
|
||
import by.petrovich.eshop.entity.Role; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface RoleRepository extends JpaRepository<Role, Integer> { | ||
Role findByName(String name); | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/by/petrovich/eshop/security/CustomUserDetails.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,59 @@ | ||
package by.petrovich.eshop.security; | ||
|
||
import by.petrovich.eshop.entity.Order; | ||
import by.petrovich.eshop.entity.Role; | ||
import by.petrovich.eshop.entity.User; | ||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Past; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.AuthorityUtils; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
public class CustomUserDetails extends User implements UserDetails { | ||
public CustomUserDetails(Integer userId, | ||
String name, | ||
String password, | ||
String email, | ||
LocalDate birthDate, | ||
BigDecimal balance, | ||
Set<Order> orders, | ||
Role role) { | ||
super(userId, name, password, email, birthDate, balance, orders, role); | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN"); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return super.getName(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,8 @@ | ||
INSERT INTO users (name, password, email, birth_date, balance) | ||
values ('Wade', 'Williams', '[email protected]', '01-01-1990', 0.5), | ||
('Dave', 'Harris', '[email protected]', '01-01-1990', 0.5), | ||
('Seth', 'Thomas', '[email protected]', '01-01-1990', 0.5), | ||
('Ivan', 'Robinson', '[email protected]', '01-01-1990', 0.5), | ||
('Riley', 'Walker', '[email protected]', '01-01-1990', 0.5), | ||
('Daisy', 'Scott', '[email protected]', '01-01-1990', 15.5), | ||
('Deborah', 'Nelson', '[email protected]', '01-01-1990', 0.5), | ||
('Stella', 'Morgan', '[email protected]', '01-01-1990', 0.5), | ||
('Debra', 'Cooper', '[email protected]', '01-01-1990', 0.5); | ||
INSERT INTO users (name, password, email, birth_date, balance, role_id) | ||
values ('Wade', 'Williams', '[email protected]', '01-01-1990', 0.5, 1), | ||
('Dave', 'Harris', '[email protected]', '01-01-1990', 0.5, 1), | ||
('Seth', 'Thomas', '[email protected]', '01-01-1990', 15.5, 1), | ||
('admin', '$2a$10$an1GB52whsATPp.1SulvUun3WnF.8c5k/4BgQwh4S/OpMr9wO.SDm', '[email protected]', '01-01-1990', 0.5, 2); | ||
|
||
INSERT INTO categories (name, rating) | ||
values ('category1', 1), | ||
|
@@ -77,4 +72,8 @@ VALUES (0.10, 1, CURRENT_TIMESTAMP), | |
INSERT INTO carts (price, created_at, user_id) | ||
VALUES (50.55, CURRENT_TIMESTAMP, 1), | ||
(10.99, CURRENT_TIMESTAMP, 2), | ||
(0.5, CURRENT_TIMESTAMP, 5); | ||
(0.5, CURRENT_TIMESTAMP, 3); | ||
|
||
INSERT INTO roles (name) | ||
VALUES ('ROLE_USER'), | ||
('ROLE_ADMIN'); |
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
23 changes: 23 additions & 0 deletions
23
src/test/by/petrovich/eshop/repository/RoleRepositoryTest.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,23 @@ | ||
package by.petrovich.eshop.repository; | ||
|
||
import by.petrovich.eshop.entity.Role; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@SpringBootTest | ||
class RoleRepositoryTest { | ||
@Autowired | ||
private RoleRepository repository; | ||
|
||
@Test | ||
void findByName() { | ||
Role expected = new Role(); | ||
expected.setName("user"); | ||
|
||
Role actual = repository.findByName(expected.getName()); | ||
assertEquals(actual, expected); | ||
} | ||
} |