diff --git a/README.md b/README.md index e7bdcd3..1af99f8 100644 --- a/README.md +++ b/README.md @@ -57,11 +57,16 @@ by providing a user-friendly interface and incorporating the latest Java technol Show @@ -86,7 +91,7 @@ to populate the database tables. ### :key: Database structure -![DB_structure](src/main/resources/static/st/img/DB_structure.png) +![DB_structure](src/main/resources/static/img/DB_structure.png) ## :compass: roadmap: @@ -106,6 +111,8 @@ to populate the database tables. * [x] add the ability to view the list of products of each user order * [x] add admin page with user's list * [x] add Spring Security with In-Memory Authentication +* [x] add Spring Security authentication form Login custom page with Database +* [x] add Spring Security roles ## :handshake: Contact @@ -119,5 +126,6 @@ Use this section to mention useful resources and libraries that you have used in - [Shields.io](https://shields.io/) - [Awesome README](https://github.com/matiassingers/awesome-readme) -- [Emoji Cheat Sheet](https://github.com/ikatyang/emoji-cheat-sheet/blob/master/README.md#travel--places) +- [Templates for UI](https://www.w3schools.com/howto/default.asp) +- [Git Emoji for your commit messages](https://gitmoji.dev) - [Readme Template](https://github.com/othneildrew/Best-README-Template) \ No newline at end of file diff --git a/src/main/java/by/petrovich/eshop/entity/model/CustomUserDetail.java b/src/main/java/by/petrovich/eshop/entity/model/CustomUserDetail.java new file mode 100644 index 0000000..50a427f --- /dev/null +++ b/src/main/java/by/petrovich/eshop/entity/model/CustomUserDetail.java @@ -0,0 +1,74 @@ +package by.petrovich.eshop.entity.model; + +import by.petrovich.eshop.entity.User; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.Collection; +import java.util.Collections; + +public class CustomUserDetail implements UserDetails { + private final User user; + + public CustomUserDetail(User user) { + this.user = user; + } + + @Override + public Collection getAuthorities() { + return Collections.singletonList(new SimpleGrantedAuthority(user.getRole().getName())); + } + + @Override + public String getPassword() { + return user.getPassword(); + } + + @Override + public String getUsername() { + return user.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; + } + + public Integer getUserId() { + return user.getUserId(); + } + + public String getEmail() { + return user.getEmail(); + } + + public String getRole() { + return user.getRole().getName(); + } + + public BigDecimal getBalance() { + return user.getBalance(); + } + + public LocalDate getBirthDate() { + return user.getBirthDate(); + } +} diff --git a/src/main/java/by/petrovich/eshop/security/CustomUserDetails.java b/src/main/java/by/petrovich/eshop/security/CustomUserDetails.java deleted file mode 100644 index f68941f..0000000 --- a/src/main/java/by/petrovich/eshop/security/CustomUserDetails.java +++ /dev/null @@ -1,59 +0,0 @@ -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 orders, - Role role) { - super(userId, name, password, email, birthDate, balance, orders, role); - } - - @Override - public Collection getAuthorities() { - return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"); - } - - @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; - } -} diff --git a/src/main/java/by/petrovich/eshop/service/impl/UserServiceImpl.java b/src/main/java/by/petrovich/eshop/service/impl/UserServiceImpl.java index 8957ed0..66f039c 100644 --- a/src/main/java/by/petrovich/eshop/service/impl/UserServiceImpl.java +++ b/src/main/java/by/petrovich/eshop/service/impl/UserServiceImpl.java @@ -6,7 +6,7 @@ import by.petrovich.eshop.exceptions.UserNotFoundException; import by.petrovich.eshop.repository.RoleRepository; import by.petrovich.eshop.repository.UserRepository; -import by.petrovich.eshop.security.CustomUserDetails; +import by.petrovich.eshop.entity.model.CustomUserDetail; import by.petrovich.eshop.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.BadCredentialsException; @@ -58,16 +58,7 @@ public void register(RegistrationFormDto registrationFormDto) { public UserDetails loadUserByUsername(String name) throws UserNotFoundException { User user = userRepository.findByName(name).orElseThrow(() -> new UserNotFoundException("User not found")); - return new CustomUserDetails( - user.getUserId(), - user.getName(), - user.getPassword(), - user.getEmail(), - user.getBirthDate(), - user.getBalance(), - user.getOrders(), - user.getRole() - ); + return new CustomUserDetail(user); } private boolean isExist(String email) { diff --git a/src/main/resources/db/migration/V1__Create_Eshop_tables.sql b/src/main/resources/db/migration/V1__Create_Eshop_tables.sql index 2ec1c5f..f4a5e38 100644 --- a/src/main/resources/db/migration/V1__Create_Eshop_tables.sql +++ b/src/main/resources/db/migration/V1__Create_Eshop_tables.sql @@ -1,3 +1,12 @@ +-- ROLES +-- +CREATE TABLE IF NOT EXISTS roles +( + role_id SERIAL NOT NULL, + name varchar(20) NOT NULL, + PRIMARY KEY (role_id) +); + -- USERS -- CREATE TABLE IF NOT EXISTS users @@ -9,7 +18,9 @@ CREATE TABLE IF NOT EXISTS users birth_date date NOT NULL, balance NUMERIC(10, 2) DEFAULT '0.00', role_id INTEGER, - PRIMARY KEY (user_id) + PRIMARY KEY (user_id), + CONSTRAINT fk_role_id FOREIGN KEY (role_id) REFERENCES roles (role_id) + ); -- CATEGORIES @@ -58,13 +69,4 @@ CREATE TABLE IF NOT EXISTS carts user_id INTEGER, PRIMARY KEY (cart_id), CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (user_id) -); - --- ROLES --- -CREATE TABLE IF NOT EXISTS roles -( - role_id SERIAL NOT NULL, - name varchar(20) NOT NULL, - PRIMARY KEY (role_id) ); \ No newline at end of file diff --git a/src/main/resources/db/migration/V2__Populate_Eshop_tables.sql b/src/main/resources/db/migration/V2__Populate_Eshop_tables.sql index 4ffc73a..e162202 100644 --- a/src/main/resources/db/migration/V2__Populate_Eshop_tables.sql +++ b/src/main/resources/db/migration/V2__Populate_Eshop_tables.sql @@ -1,3 +1,7 @@ +INSERT INTO roles (name) +VALUES ('ROLE_USER'), + ('ROLE_ADMIN'); + INSERT INTO users (name, password, email, birth_date, balance, role_id) values ('Wade', '$2a$10$0ZrU1zZA2gx9lBm2MQ1Id.gN7Itm8a8Xi3AComXN2CZXZzHCclh..', 'williams@mail.com', '01-01-1990', 0.5, 1), ('Dave', '$2a$10$XtGZF/tA.hNaFsDN6MEKYOTcFS.52OFv0U0UZUWjCl34SgDJVenBG', 'harris@mail.com', '01-01-1990', 0.5, 1), @@ -65,15 +69,14 @@ VALUES ('product name1', 16.50, 5); INSERT INTO orders (price, user_id, created_at) -VALUES (0.10, 1, CURRENT_TIMESTAMP), - (0.20, 2, CURRENT_TIMESTAMP), +VALUES (100.00, 1, CURRENT_TIMESTAMP), + (19.99, 1, CURRENT_TIMESTAMP), + (200.00, 2, CURRENT_TIMESTAMP), + (25.00, 2, CURRENT_TIMESTAMP), + (50.00, 2, CURRENT_TIMESTAMP), (0.30, 3, 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, 3); - -INSERT INTO roles (name) -VALUES ('ROLE_USER'), - ('ROLE_ADMIN'); \ No newline at end of file + (0.5, CURRENT_TIMESTAMP, 3); \ No newline at end of file diff --git a/src/main/resources/static/img/DB_structure.png b/src/main/resources/static/img/DB_structure.png index a0f412a..0037636 100644 Binary files a/src/main/resources/static/img/DB_structure.png and b/src/main/resources/static/img/DB_structure.png differ diff --git a/src/main/resources/templates/include/topnav.html b/src/main/resources/templates/include/topnav.html index b38911a..08027bc 100644 --- a/src/main/resources/templates/include/topnav.html +++ b/src/main/resources/templates/include/topnav.html @@ -9,10 +9,9 @@  Log out  Profile  Cart - Admin
- Admin Role + Admin
diff --git a/src/main/resources/templates/order.html b/src/main/resources/templates/order.html index 063fc5c..dc25c12 100644 --- a/src/main/resources/templates/order.html +++ b/src/main/resources/templates/order.html @@ -14,7 +14,7 @@

Order

createdAt

-

Name

+

Name

diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html index 3fcfc2d..de092fd 100644 --- a/src/main/resources/templates/profile.html +++ b/src/main/resources/templates/profile.html @@ -14,7 +14,7 @@

User Profile Card

John name: - + id:
@@ -22,7 +22,7 @@
role: - +
balance: