Skip to content

Commit

Permalink
fix: 사이트 및 컴포넌트에 인증 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyihyeon committed Feb 6, 2024
1 parent a2e31b2 commit 46767a4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import com.pickax.status.page.server.util.SecurityUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.pickax.status.page.server.dto.request.ComponentCreateRequestDto;
import com.pickax.status.page.server.dto.reseponse.component.ComponentListResponseDto;
Expand All @@ -28,9 +30,10 @@ public class ComponentController {
private final ComponentService componentService;

@PostMapping("/{siteId}/components")
public void createComponent(@PathVariable Long siteId, @RequestBody @Valid ComponentCreateRequestDto request) {
Long loggedInUserId = 1L;
this.componentService.createComponent(siteId, request, loggedInUserId);
public ResponseEntity<Void> createComponent(@PathVariable Long siteId, @RequestBody @Valid ComponentCreateRequestDto request) {
Long loggedInUserId = SecurityUtil.getCurrentUserId();
this.componentService.createComponent(siteId, request, loggedInUserId);
return new ResponseEntity<>(HttpStatus.OK);
}

@GetMapping("/{siteId}/components/active")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pickax.status.page.server.controller;

import com.pickax.status.page.server.dto.reseponse.site.SiteResponseDto;
import com.pickax.status.page.server.util.SecurityUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -44,9 +45,7 @@ public void verifySite(@PathVariable long siteId) {

@GetMapping
public ResponseEntity<List<DefaultSite>> sitesByUserId() {
// 로그인 기능이 없으므로 임시로 생성
Long loggedInUserId = 1L;

Long loggedInUserId = SecurityUtil.getCurrentUserId();
return ResponseEntity.ok(this.siteService.findAllByUserId(loggedInUserId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ public boolean hasValidatedOwnerByMetaTag() {
return SiteRegistrationStatus.COMPLETED.equals(this.siteRegistrationStatus);
}

public boolean ownerEqualsBy(Long requesterId) {
return this.getUser().getId().equals(requesterId);
}

public void cancel() {
siteRegistrationStatus = SiteRegistrationStatus.CANCELED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public interface SiteRepositoryCustom {
Optional<SiteResponseDto> getSiteResponse(long siteId);

List<Site> findByUserId(Long userId);

Optional<Site> findBySiteIdAndUserId(Long siteId, Long userId);

List<Site> findAllByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,23 @@ public List<Site> findByUserId(Long userId) {
.where(site.user.id.eq(userId))
.fetch();
}

@Override
public Optional<Site> findBySiteIdAndUserId(Long siteId, Long userId) {
return Optional.ofNullable(queryFactory
.selectFrom(site)
.where(
site.id.eq(siteId),
site.user.id.eq(userId)
)
.fetchOne());
}

@Override
public List<Site> findAllByUserId(Long userId) {
return queryFactory
.selectFrom(site)
.where(site.user.id.eq(userId))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ public class ComponentService {

@Transactional
public void createComponent(Long siteId, ComponentCreateRequestDto request, Long loggedInUserId) {
Site site = this.siteRepository.findById(siteId).orElseThrow(() -> new EntityNotFoundException("존재하지 않는 사이트 입니다."));
// 로그인 기능이 생기기 전까지 주석처리 합니다.
// if (!site.ownerEqualsBy(loggedInUserId)) {
// throw new IllegalArgumentException("해당 사이트의 정당한 소유자가 아닙니다.");
// }
Site site = this.siteRepository.findBySiteIdAndUserId(siteId, loggedInUserId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_SITE));

Component component = Component.create(request.getName(),
request.getDescription(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,8 @@ private void checkMetaTag(Site site, MetaTag metaTag) {
throw new CustomException(ErrorCode.INVALID_META_TAG);
}

/**
* TODO. 로그인 API 붙일 때 유저아이디로 조회
*
* @param loggedInUserId
* @return
*/
public List<DefaultSite> findAllByUserId(Long loggedInUserId) {
// List<Site> sitesFromRepository = this.siteRepository.findAllByUserId(loggedInUserId);
List<Site> sitesFromRepository = this.siteRepository.findAll();
List<Site> sitesFromRepository = this.siteRepository.findAllByUserId(loggedInUserId);
List<DefaultSite> sites = new ArrayList<>();

sitesFromRepository.forEach(
Expand Down

0 comments on commit 46767a4

Please sign in to comment.