Skip to content

Commit

Permalink
#164 페북 로그인 사용자의 경우 페북에 바로 댓글 달 수 있도록 한다.
Browse files Browse the repository at this point in the history
서버측과 간단한 클라이언트 측 작업 완료함.
  • Loading branch information
javajigi committed Nov 14, 2013
1 parent 9fa29c8 commit 69d17cb
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 135 deletions.
54 changes: 54 additions & 0 deletions src/main/java/net/slipp/domain/qna/TemporaryAnswer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.slipp.domain.qna;

import java.io.Serializable;

public class TemporaryAnswer implements Serializable {
private static final long serialVersionUID = -4198138126883127272L;

public static final String TEMPORARY_ANSWER_KEY = "temporaryAnswer";
public static final TemporaryAnswer EMPTY_ANSWER = new EmptyTemporaryAnswer();

private Long questionId;
private String temporaryAnswer;

public TemporaryAnswer() {
}

public TemporaryAnswer(Long questionId, String temporaryAnswer) {
this.questionId = questionId;
this.temporaryAnswer = temporaryAnswer;
}

public Long getQuestionId() {
return questionId;
}

public String getTemporaryAnswer() {
return temporaryAnswer;
}

public Answer createAnswer() {
return new Answer(this.temporaryAnswer);
}

public String generateUrl() {
return String.format("/questions/%d", this.questionId);
}

static class EmptyTemporaryAnswer extends TemporaryAnswer {
private static final long serialVersionUID = 1380330064986704887L;

public Answer createAnswer() {
return new Answer();
}

public String generateUrl() {
return "/";
}
}

@Override
public String toString() {
return "TemporaryAnswer [questionId=" + questionId + ", temporaryAnswer=" + temporaryAnswer + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

import javax.annotation.Resource;

import net.slipp.domain.qna.TemporaryAnswer;
import net.slipp.domain.user.SocialUser;
import net.slipp.service.user.SocialUserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionKey;
import org.springframework.social.connect.web.SignInAdapter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;

public class SlippSecuritySignInAdapter implements SignInAdapter {
private static Logger log = LoggerFactory.getLogger(SlippSecuritySignInAdapter.class);

public final static String SIGN_IN_DETAILS_SESSION_ATTRIBUTE_NAME = "net.slipp.social.security.signInDetails";

@Resource(name = "socialUserService")
Expand All @@ -22,6 +27,16 @@ public String signIn(String localUserId, Connection<?> connection, NativeWebRequ
SocialUser socialUser = socialUserService.findByUserIdAndConnectionKey(localUserId, connectionKey);
nativeWebRequest.setAttribute(SIGN_IN_DETAILS_SESSION_ATTRIBUTE_NAME, socialUser,
RequestAttributes.SCOPE_SESSION);
return SlippSecurityAuthenticationFilter.DEFAULT_AUTHENTICATION_URL + "?redirect=/questions/191";
TemporaryAnswer temporaryAnswer = getTemporaryAnswer(nativeWebRequest);
return SlippSecurityAuthenticationFilter.DEFAULT_AUTHENTICATION_URL + "?redirect=" + temporaryAnswer.generateUrl();
}

private TemporaryAnswer getTemporaryAnswer(NativeWebRequest nativeWebRequest) {
Object value = nativeWebRequest.getAttribute(TemporaryAnswer.TEMPORARY_ANSWER_KEY, RequestAttributes.SCOPE_SESSION);
log.debug("Temporary Answer : {}", value);
if (value == null) {
return TemporaryAnswer.EMPTY_ANSWER;
}
return (TemporaryAnswer)value;
}
}
3 changes: 0 additions & 3 deletions src/main/java/net/slipp/web/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import javax.annotation.Resource;

import net.slipp.domain.smalltalk.SmallTalk;
import net.slipp.domain.wiki.WikiPage;
import net.slipp.service.qna.QnaService;
import net.slipp.service.smalltalk.SmallTalkService;
Expand Down Expand Up @@ -48,8 +47,6 @@ public String home(Model model) {
}

private void productionMode(Model model) {
// List<SmallTalk> smallTalks = smallTalkService.getLastTalks();
// model.addAttribute("smallTalks", smallTalks);
if (isProductionMode()) {
model.addAttribute("pages", wikiService.findWikiPages());
}else{
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/slipp/web/qna/AnswerController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package net.slipp.web.qna;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.slipp.domain.qna.Answer;
import net.slipp.domain.qna.TemporaryAnswer;
import net.slipp.domain.user.SocialUser;
import net.slipp.service.qna.QnaService;
import net.slipp.service.tag.TagService;
Expand Down Expand Up @@ -30,13 +32,14 @@ public class AnswerController {
private TagService tagService;

@RequestMapping(value = "", method = RequestMethod.POST)
public String create(@LoginUser SocialUser loginUser, @PathVariable Long questionId, Answer answer)
public String create(@LoginUser SocialUser loginUser, @PathVariable Long questionId, Answer answer, HttpSession session)
throws Exception {
logger.debug("questionId :{}, answer : {}", questionId, answer);
qnaService.createAnswer(loginUser, questionId, answer);
session.removeAttribute(TemporaryAnswer.TEMPORARY_ANSWER_KEY);
return String.format("redirect:/questions/%d", questionId);
}

@RequestMapping(value = "{answerId}", method = RequestMethod.DELETE)
public String delete(@LoginUser SocialUser loginUser, @PathVariable Long questionId, @PathVariable Long answerId)
throws Exception {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/net/slipp/web/qna/ApiQuestionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

import javax.servlet.http.HttpSession;

import net.slipp.domain.qna.TemporaryAnswer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api/questions")
public class ApiQuestionController {
public static final String TEMPORARY_ANSWER_KEY = "temporaryAnswer";
private static Logger log = LoggerFactory.getLogger(ApiQuestionController.class);

@RequestMapping("/answers/tosession")
public @ResponseBody boolean setToSession(String contents, HttpSession session) {
log.debug("temporary answer : {}", contents);

session.setAttribute(TEMPORARY_ANSWER_KEY, contents);
@RequestMapping("/{id}/answers/logoutuser")
public @ResponseBody boolean answerWhenLogout(@PathVariable Long id, String contents, HttpSession session) {
TemporaryAnswer answer = new TemporaryAnswer(id, contents);
log.debug("TemporaryAnswer : {}", answer);
session.setAttribute(TemporaryAnswer.TEMPORARY_ANSWER_KEY, answer);
return true;
}
}
18 changes: 11 additions & 7 deletions src/main/java/net/slipp/web/qna/QuestionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.slipp.domain.qna.Answer;
import net.slipp.domain.qna.Question;
import net.slipp.domain.qna.QuestionDto;
import net.slipp.domain.qna.TemporaryAnswer;
import net.slipp.domain.user.SocialUser;
import net.slipp.service.qna.QnaService;
import net.slipp.service.tag.TagService;
Expand Down Expand Up @@ -89,18 +89,22 @@ public String show(@PathVariable Long id, Model model, HttpSession session) {
throw new AccessDeniedException(id + " question is deleted.");
}

Object value = session.getAttribute(ApiQuestionController.TEMPORARY_ANSWER_KEY);
if (value != null) {
model.addAttribute("answer", new Answer((String)value));
} else {
model.addAttribute("answer", new Answer());
}
model.addAttribute("answer", getTemporaryAnswer(session).createAnswer());
model.addAttribute("question", question);
model.addAttribute("tags", tagService.findPooledTags());
model.addAttribute("user", new UserForm());
return "qna/show";
}

private TemporaryAnswer getTemporaryAnswer(HttpSession session) {
Object value = session.getAttribute(TemporaryAnswer.TEMPORARY_ANSWER_KEY);
if (value == null) {
return TemporaryAnswer.EMPTY_ANSWER;
}

return (TemporaryAnswer)value;
}

@RequestMapping(value="{id}", method=RequestMethod.DELETE)
public String delete(@LoginUser SocialUser loginUser, @PathVariable Long id) {
qnaService.deleteQuestion(loginUser, id);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/slipp/web/user/UsersController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.slipp.domain.user.PasswordDto;
import net.slipp.domain.user.SocialUser;
Expand Down Expand Up @@ -48,10 +46,10 @@ public String login(Model model) {
}

@RequestMapping(value = "", method = RequestMethod.POST)
public String create(UserForm user, HttpServletRequest request, HttpServletResponse response) {
public String create(UserForm user, String redirect) {
SocialUser socialUser = userService.createSlippUser(user.getUserId(), user.getEmail());
autoLoginAuthenticator.login(socialUser.getEmail(), socialUser.getRawPassword());
return "redirect:/";
return "redirect:" + redirect;
}

@RequestMapping("/fblogout")
Expand Down
66 changes: 8 additions & 58 deletions webapp/WEB-INF/jsp/qna/show.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -41,56 +41,13 @@
<form:checkbox path="connected" /> 페이스북으로 전송하려면 체크하세요
</label>
</c:if>
</sec:authorize>
<button type="submit" class="btn-submit"><i class="icon-submit"></i> 작성완료</button>
</sec:authorize>
</div>
</fieldset>
</form:form>
<sec:authorize access="!hasRole('ROLE_USER')">
<div class="choose-login-type">
<div class="login-with-slipp">
<p class="login-with-slipp-text">SLiPP 계정으로 로그인하세요.</p>
<form id="authentication" action="/users/authenticate" method="POST">
<input type="hidden" name="redirect" value="/questions/${question.questionId}" />
<input id="authenticationId" name="authenticationId" class="inp-email" placeholder="이메일" type="text" value=""/>
<input id="authenticationPassword" name="authenticationPassword" class="inp-title" placeholder="비밀번호" type="password" value=""/>
<c:if test="${not empty param.login_error}">
<label class="error" style="">메일주소 혹은 비밀번호가 정확하지 않습니다.</label>
</c:if>
<p class="login-with-slipp-submit">
<label><input name="_spring_security_remember_me" class="inp-pw" type="checkbox" value="true"/> 자동로그인</label>
<button id="loginSubmitBtn" type="submit" class="login-with-slipp-submit-btn"><i class="icon-login"></i> 로그인</button>
</p>
</form>
</div>
<div class="divide-bar left"></div>
<div class="login-with-sns">
<p class="login-with-sns-text">또는, SNS 계정으로 로그인하세요.</p>
<form action="/signin/facebook" method="POST">
<input type="hidden" name="scope" value="publish_stream,user_groups" />
<input type="hidden" name="redirect" value="/questions/${question.questionId}" />
<button type="submit" class="btn-login-facebook"><i class="foundicon-facebook"></i> 페이스북</button>
</form>
<form action="/signin/twitter" method="POST">
<button type="submit" class="btn-login-twitter"><i class="foundicon-twitter"></i> 트위터</button>
</form>
<form action="/signin/google?scope=https://www.googleapis.com/auth/userinfo.profile" method="POST">
<button type="submit" class="btn-login-google"><i class="foundicon-google-plus"></i> 구글</button>
</form>
</div>
<div class="divide-bar right"></div>
<div class="sign-in-to-slipp">
<p class="sign-in-to-slipp-text">계정이 없다면 간단히 만들어보세요.</p>
<form:form modelAttribute="user" cssClass="form-write" action="/users" method="post">
<form:input path="email" class="inp_email" placeholder="이메일" />
<form:input path="userId" class="inp_pw" placeholder="닉네임" />
<p class="sign-in-to-slipp-notice">
- 등록한 메일로 임시 비밀번호를 보내드립니다. <br />
- 개인공간에서 비밀번호를 변경할 수 있습니다.</p>
<button type="submit" class="sign-in-to-slipp-btn"><i class="icon-signin"></i> 회원가입</button>
</form:form>
</div>
</div>
<slipp:loginform redirectUrl="/questions/${question.questionId}"/>
</sec:authorize>
<c:if test="${question.snsConnected}">
<div class="qna-comment-fb">
Expand Down Expand Up @@ -142,27 +99,20 @@
<script src="${url:resource('/javascripts/highlight.pack.js')}"></script>
<script>
hljs.initHighlightingOnLoad();
questionId = ${question.questionId};
</script>
<script src="${url:resource('/javascripts/qna/tagparser.js')}"></script>
<script src="${url:resource('/javascripts/qna/show.js')}"></script>
<script src="${url:resource('/javascripts/qna/write.js')}"></script>
<script src="${url:resource('/javascripts/qna/qna-set.js')}"></script>
<script src="${url:resource('/javascripts/qna/image.upload.js')}"></script>
<script src="${url:resource('/javascripts/jquery.markitup.js')}"></script>
<script src="${url:resource('/javascripts/support/slipp.validate.js')}"></script>
<script src="${url:resource('/javascripts/user/login.js')}"></script>
<sec:authorize access="!hasRole('ROLE_USER')">
<script src="${url:resource('/javascripts/qna/loginForm.js')}"></script>
</sec:authorize>
<script>
$(document).ready(function(){
function showFacebookComments(questionId) {
var url = '/api/facebooks/' + questionId + '/comments';
$.get(url,
function(response) {
$('.qna-facebook-comment').html(response);
return false;
}, 'html'
);
}
showFacebookComments(${question.questionId});
});
$('#contents').markItUp(mySettings);
</script>

43 changes: 1 addition & 42 deletions webapp/WEB-INF/jsp/users/login.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,7 @@
<div class="content-main">
<h1 class="login-content-title">Sustainable Life, Programming, Programmer</h1>
<p class="login-content-welcome">삶과 일의 균형을 맞추면서 행복하게 살 수 있는 세상을 꿈꿉니다.</p>
<div class="choose-login-type">
<div class="login-with-slipp">
<p class="login-with-slipp-text">SLiPP 계정으로 로그인하세요.</p>
<form id="authentication" action="/users/authenticate" method="POST">
<input id="authenticationId" name="authenticationId" class="inp-email" placeholder="이메일" type="text" value=""/>
<input id="authenticationPassword" name="authenticationPassword" class="inp-title" placeholder="비밀번호" type="password" value=""/>
<c:if test="${not empty param.login_error}">
<label class="error" style="">메일주소 혹은 비밀번호가 정확하지 않습니다.</label>
</c:if>
<p class="login-with-slipp-submit">
<label><input name="_spring_security_remember_me" class="inp-pw" type="checkbox" value="true"/> 자동로그인</label>
<button id="loginSubmitBtn" type="submit" class="login-with-slipp-submit-btn"><i class="icon-login"></i> 로그인</button>
</p>
</form>
</div>
<div class="divide-bar left"></div>
<div class="login-with-sns">
<p class="login-with-sns-text">또는, SNS 계정으로 로그인하세요.</p>
<form action="/signin/facebook" method="POST">
<input type="hidden" name="scope" value="publish_stream,user_groups" />
<button type="submit" class="btn-login-facebook"><i class="foundicon-facebook"></i> 페이스북</button>
</form>
<form action="/signin/twitter" method="POST">
<button type="submit" class="btn-login-twitter"><i class="foundicon-twitter"></i> 트위터</button>
</form>
<form action="/signin/google?scope=https://www.googleapis.com/auth/userinfo.profile" method="POST">
<button type="submit" class="btn-login-google"><i class="foundicon-google-plus"></i> 구글</button>
</form>
</div>
<div class="divide-bar right"></div>
<div class="sign-in-to-slipp">
<p class="sign-in-to-slipp-text">계정이 없다면 간단히 만들어보세요.</p>
<form:form modelAttribute="user" cssClass="form-write" action="/users" method="post">
<form:input path="email" class="inp_email" placeholder="이메일" />
<form:input path="userId" class="inp_pw" placeholder="닉네임" />
<p class="sign-in-to-slipp-notice">
- 등록한 메일로 임시 비밀번호를 보내드립니다. <br />
- 개인공간에서 비밀번호를 변경할 수 있습니다.</p>
<button type="submit" class="sign-in-to-slipp-btn"><i class="icon-signin"></i> 회원가입</button>
</form:form>
</div>
</div>
<slipp:loginform redirectUrl="/"/>
</div>
<div class="content-sub">
</div>
Expand Down
Loading

0 comments on commit 69d17cb

Please sign in to comment.