-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
40 changed files
with
1,827 additions
and
15 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
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
44 changes: 44 additions & 0 deletions
44
backend/pium/src/main/java/com/official/pium/controller/AdminArgumentResolver.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,44 @@ | ||
package com.official.pium.controller; | ||
|
||
import com.official.pium.domain.Admin; | ||
import com.official.pium.domain.AdminAuth; | ||
import com.official.pium.exception.AuthorizationException; | ||
import com.official.pium.exception.AuthorizationException.NeedAdminException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpSession; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
|
||
public class AdminArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
private static final String SESSION_KEY = "PIUM_ADMIN_SESSION_ID"; | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(AdminAuth.class) && parameter.getParameterType().equals(Admin.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||
HttpSession session = request.getSession(false); | ||
|
||
if (session == null) { | ||
return null; | ||
} | ||
|
||
try { | ||
Admin admin = (Admin) session.getAttribute(SESSION_KEY); | ||
if (admin == null) { | ||
throw new NeedAdminException("관리자 권한이 필요합니다."); | ||
} | ||
return admin; | ||
} catch (Exception e) { | ||
throw new AuthorizationException("잘못된 세션정보입니다."); | ||
} | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
backend/pium/src/main/java/com/official/pium/controller/AdminPageController.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,130 @@ | ||
package com.official.pium.controller; | ||
|
||
import com.official.pium.domain.Admin; | ||
import com.official.pium.domain.AdminAuth; | ||
import com.official.pium.domain.DictionaryPlant; | ||
import com.official.pium.repository.DictionaryPlantRepository; | ||
import com.official.pium.service.AdminService; | ||
import com.official.pium.service.dto.AdminLoginRequest; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpSession; | ||
import jakarta.validation.Valid; | ||
import java.util.NoSuchElementException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.web.PageableDefault; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@Validated | ||
@Controller | ||
@RequiredArgsConstructor | ||
@RequestMapping("/admin") | ||
public class AdminPageController { | ||
|
||
private static final String REDIRECT_ADMIN_LOGIN = "redirect:/admin/login"; | ||
|
||
private final DictionaryPlantRepository dictionaryPlantRepository; | ||
private final AdminService adminService; | ||
|
||
@GetMapping("/**") | ||
public String adminPage(@AdminAuth Admin admin, Model model) { | ||
if (admin == null) { | ||
return REDIRECT_ADMIN_LOGIN; | ||
} | ||
|
||
model.addAttribute("admin", admin); | ||
return "/admin/index"; | ||
} | ||
|
||
@GetMapping("/dict") | ||
public String dictionaryPlants(@PageableDefault Pageable pageable, @AdminAuth Admin admin, Model model) { | ||
if (admin == null) { | ||
return REDIRECT_ADMIN_LOGIN; | ||
} | ||
|
||
Page<DictionaryPlant> dictionaryPlants = dictionaryPlantRepository.findAll(pageable); | ||
model.addAttribute("admin", admin); | ||
model.addAttribute("page", dictionaryPlants); | ||
model.addAttribute("plants", dictionaryPlants.getContent()); | ||
return "/admin/dict/list"; | ||
} | ||
|
||
@GetMapping("/dict/{id}") | ||
public String dictionaryPlant(@PathVariable Long id, @AdminAuth Admin admin, Model model) { | ||
if (admin == null) { | ||
return REDIRECT_ADMIN_LOGIN; | ||
} | ||
|
||
DictionaryPlant dictionaryPlant = dictionaryPlantRepository.findById(id) | ||
.orElseThrow(() -> new NoSuchElementException("일치하는 사전 식물이 존재하지 않습니다. id:" + id)); | ||
|
||
model.addAttribute("admin", admin); | ||
model.addAttribute("plant", dictionaryPlant); | ||
return "/admin/dict/plant"; | ||
} | ||
|
||
@GetMapping("/dict/create") | ||
public String dictionaryPlantCreateForm(@AdminAuth Admin admin, Model model) { | ||
if (admin == null) { | ||
return REDIRECT_ADMIN_LOGIN; | ||
} | ||
|
||
model.addAttribute("admin", admin); | ||
return "/admin/dict/create"; | ||
} | ||
|
||
@GetMapping("/dict/{id}/update") | ||
public String dictionaryPlantUpdateForm(@PathVariable Long id, @AdminAuth Admin admin, Model model) { | ||
if (admin == null) { | ||
return REDIRECT_ADMIN_LOGIN; | ||
} | ||
|
||
DictionaryPlant dictionaryPlant = dictionaryPlantRepository.findById(id) | ||
.orElseThrow(() -> new NoSuchElementException("일치하는 사전 식물이 존재하지 않습니다. id:" + id)); | ||
|
||
model.addAttribute("admin", admin); | ||
model.addAttribute("plant", dictionaryPlant); | ||
return "/admin/dict/update"; | ||
} | ||
|
||
@GetMapping("/dict/requests") | ||
public String dictionaryPlantRequests(@AdminAuth Admin admin, Model model) { | ||
if (admin == null) { | ||
return REDIRECT_ADMIN_LOGIN; | ||
} | ||
|
||
model.addAttribute("admin", admin); | ||
return "/admin/dict/requests"; | ||
} | ||
|
||
@GetMapping("/login") | ||
public String loginPage(Model model) { | ||
return "/admin/login"; | ||
} | ||
|
||
@PostMapping("/login") | ||
public String login(@RequestBody @Valid AdminLoginRequest admin, HttpSession httpSession) { | ||
adminService.login(admin, httpSession); | ||
return "redirect:/admin"; | ||
} | ||
|
||
@PostMapping("/logout") | ||
public ResponseEntity<Void> logout(HttpServletRequest request) { | ||
HttpSession session = request.getSession(false); | ||
|
||
if (session != null) { | ||
session.invalidate(); | ||
} | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
backend/pium/src/main/java/com/official/pium/domain/Admin.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,20 @@ | ||
package com.official.pium.domain; | ||
|
||
import java.util.Objects; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class Admin { | ||
|
||
private final String account; | ||
private final String password; | ||
private final String secondPassword; | ||
|
||
public boolean isValidate(String account, String password, String secondPassword) { | ||
return Objects.equals(this.account, account) | ||
&& Objects.equals(this.password, password) | ||
&& Objects.equals(this.secondPassword, secondPassword); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/pium/src/main/java/com/official/pium/domain/AdminAuth.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,12 @@ | ||
package com.official.pium.domain; | ||
|
||
import static java.lang.annotation.ElementType.PARAMETER; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface AdminAuth { | ||
} |
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
backend/pium/src/main/java/com/official/pium/exception/AuthorizationException.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 com.official.pium.exception; | ||
|
||
public class AuthorizationException extends RuntimeException { | ||
|
||
public AuthorizationException() { | ||
super(); | ||
} | ||
|
||
public AuthorizationException(final String message) { | ||
super(message); | ||
} | ||
|
||
public static class NeedAdminException extends AuthorizationException { | ||
|
||
public NeedAdminException() { | ||
super(); | ||
} | ||
|
||
public NeedAdminException(final String message) { | ||
super(message); | ||
} | ||
} | ||
} |
Oops, something went wrong.