더북(TheBook)

코멘트 컨트롤러에도 CheckUser 메서드를 추가해 보자.

▼ app/controllers/comment.go

type Comment struct {
    App
}
 
func (c Comment) CheckUser() revel.Result {
    if c.MethodName != "Destroy" {
        return nil
    }
     
    if c.CurrentUser == nil {
        c.Flash.Error("Please log in first")
        return c.Redirect(App.Login)
    }
     
    if c.CurrentUser.Role != "admin" {
        c.Response.Status = 401 // Unauthorized
        c.Flash.Error("You are not admin")
        return c.Redirect(App.Login)
    }
    return nil
}

코멘트 컨트롤러에서는 댓글 삭제만 권한을 확인한다.

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.