Fakultas Ilmu Komputer UI

Commit 65454c75 authored by RYAN PUTRA BUDIANTO's avatar RYAN PUTRA BUDIANTO
Browse files

fix some bugs

parent 108b9123
Pipeline #134328 passed with stages
in 30 minutes and 16 seconds
......@@ -64,6 +64,7 @@ public class AdminController {
@GetMapping(path="/list-payment")
public String paymentList(Model model, Principal principal){
model.addAttribute("username",principal.getName());
model.addAttribute("user", userService.getUserByName(principal.getName()));
model.addAttribute("payments", paymentService.getAllPayments());
return "admin-payment-list";
......
......@@ -76,6 +76,7 @@ public class ProfileController {
@GetMapping(path="/profile")
public String profile(Principal principal, Model model){
model.addAttribute("username", principal.getName());
model.addAttribute("user", userService.getUserByName(principal.getName()));
return "profile";
}
......@@ -88,6 +89,7 @@ public class ProfileController {
@GetMapping(path="/edit-profile")
public String editProfile(Principal principal, Model model){
model.addAttribute("username", principal.getName());
model.addAttribute("user", userService.getUserByName(principal.getName()));
model.addAttribute("form", new EditProfileForm());
return "profile-edit";
......
......@@ -22,7 +22,9 @@ public class ArticleServiceImpl implements ArticleService {
private void createImageForArticle(Articles article, byte[] images){
try{
article.setImg(ImageConverter.getInstance().getString(images));
if (images.length > 0){
article.setImg(ImageConverter.getInstance().getString(images));
}
} catch (Exception e){
logger.error("Exception during image upload for article {}: {}", article.getArticleId(), e.getMessage());
}
......
......@@ -8,11 +8,15 @@ import id.ac.ui.cs.advprog.landiandfriends.repository.RoleRepository;
import id.ac.ui.cs.advprog.landiandfriends.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.List;
@Service
......@@ -89,6 +93,14 @@ public class UserServiceImpl implements UserService {
public void updateInfo(String username, String newUsername) {
var user = userRepository.findByUsername(username);
userRepository.updateInfo(newUsername,user.getUserId());
Collection<SimpleGrantedAuthority> nowAuthorities =
(Collection<SimpleGrantedAuthority>) SecurityContextHolder.getContext()
.getAuthentication()
.getAuthorities();
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(newUsername, nowAuthorities);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
public void updateResetPasswordToken(String token, String email) throws UsernameNotFoundException {
......
......@@ -16,7 +16,7 @@ admin.email = ${ADMIN_EMAIL}
spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
server.error.whitelabel.enabled=false
......
......@@ -73,8 +73,8 @@
<div style="width: 3%"></div>
<a href="/cart"><img src="https://i.imgur.com/EPveDOI.png" class = "cart-button" alt="cart" height="40"></a>
<div style="width: 3%"></div>
<a th:href ="${user.username.isBlank() ? '/login' : '/profile'}"><img src="https://i.imgur.com/KxpWso4.png" class = "signin-button" alt="sign in" height="40" style="padding-right: 10px"></a>
<a th:href ="${user.username.isBlank() ? '/login' : '/profile'}" class="sign-in-text" style="text-decoration: none; color: inherit" th:text="${user.username.isBlank()} ? 'Sign In' : ${user.username}"></a>
<a th:href ="${username.isBlank() ? '/login' : '/profile'}"><img src="https://i.imgur.com/KxpWso4.png" class = "signin-button" alt="sign in" height="40" style="padding-right: 10px"></a>
<a th:href ="${username.isBlank() ? '/login' : '/profile'}" class="sign-in-text" style="text-decoration: none; color: inherit" th:text="${username.isBlank()} ? 'Sign In' : ${username}"></a>
</div>
<div class = "container">
......
......@@ -99,8 +99,8 @@
method="GET">
<button class="btn btn-outline-primary" type="submit">Check This Out!</button>
</form></div>
<div th:if="${role != null}">
<div th:if="${role.equals('ADMIN') || role.equals('CURATOR')}">
<div th:if="${user.getRoles() != null}">
<div th:if="${user.getRoles().getName().equals('ADMIN') || user.getRoles().getName().equals('CURATOR')}">
<div id="container2">
<form
th:action="@{/curator/update/{id}(id = ${art.articleId})}"
......
......@@ -72,6 +72,7 @@
</button>
</form>
</div>
<a th:if="${username.isBlank()}" href="/register"><img src="https://i.imgur.com/Hzd2diL.png" class = "register-button" alt="register" height="40"></a>
<div style="width: 3%"></div>
<a href="/wishlist"><img src="https://i.imgur.com/HwTZHG8.png" class = "wishlist-button" alt="wishlist" height="40"></a>
<div style="width: 3%"></div>
......
......@@ -73,8 +73,8 @@
<div style="width: 3%"></div>
<a href="/cart"><img src="https://i.imgur.com/EPveDOI.png" class = "cart-button" alt="cart" height="40"></a>
<div style="width: 3%"></div>
<a th:href ="${user.username.isBlank() ? '/login' : '/profile'}"><img src="https://i.imgur.com/KxpWso4.png" class = "signin-button" alt="sign in" height="40" style="padding-right: 10px"></a>
<a th:href ="${user.username.isBlank() ? '/login' : '/profile'}" class="sign-in-text" style="text-decoration: none; color: inherit" th:text="${user.username.isBlank()} ? 'Sign In' : ${user.username}"></a>
<a th:href ="${username.isBlank() ? '/login' : '/profile'}"><img src="https://i.imgur.com/KxpWso4.png" class = "signin-button" alt="sign in" height="40" style="padding-right: 10px"></a>
<a th:href ="${username.isBlank() ? '/login' : '/profile'}" class="sign-in-text" style="text-decoration: none; color: inherit" th:text="${username.isBlank()} ? 'Sign In' : ${username}"></a>
</div>
<div class="container">
<form action="" th:action="@{/edit-profile}" th:object="${form}" method="post" id="input-form">
......
......@@ -74,8 +74,8 @@
<div style="width: 3%"></div>
<a href="/cart"><img src="https://i.imgur.com/EPveDOI.png" class = "cart-button" alt="cart" height="40"></a>
<div style="width: 3%"></div>
<a th:href ="${user.username.isBlank() ? '/login' : '/profile'}"><img src="https://i.imgur.com/KxpWso4.png" class = "signin-button" alt="sign in" height="40" style="padding-right: 10px"></a>
<a th:href ="${user.username.isBlank() ? '/login' : '/profile'}" class="sign-in-text" style="text-decoration: none; color: inherit" th:text="${user.username.isBlank()} ? 'Sign In' : ${user.username}"></a>
<a th:href ="${username.isBlank() ? '/login' : '/profile'}"><img src="https://i.imgur.com/KxpWso4.png" class = "signin-button" alt="sign in" height="40" style="padding-right: 10px"></a>
<a th:href ="${username.isBlank() ? '/login' : '/profile'}" class="sign-in-text" style="text-decoration: none; color: inherit" th:text="${username.isBlank()} ? 'Sign In' : ${username}"></a>
</div>
<div class = "container">
......@@ -109,9 +109,5 @@
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
......@@ -161,7 +161,7 @@ class AdminControllerTest {
}
@Test
@WithMockUser(authorities={"ADMIN"})
@WithMockUser(username = "name", authorities={"ADMIN"})
void whenGetPaymentListPage() throws Exception {
var user = new User();
user.setUsername("test");
......
......@@ -121,9 +121,11 @@ class ProfileControllerTest {
}
@Test
@WithMockUser(authorities = "ADMIN")
@WithMockUser(username = "name", authorities = "ADMIN")
void GetProfileShouldDisplayTheCorrectView() throws Exception {
when(userService.getUserByName(any())).thenReturn(new User());
var user = new User();
user.setUsername("name");
when(userService.getUserByName(any())).thenReturn(user);
mockMvc.perform(get("/profile"))
.andExpect(status().isOk())
.andExpect(handler().methodName("profile"))
......@@ -140,6 +142,7 @@ class ProfileControllerTest {
}
@Test
@WithMockUser(username = "name", authorities = "USER")
void editProfile() throws Exception {
mockMvc.perform(get("/edit-profile"))
.andExpect(status().isOk())
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment