Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 93268ba

Browse files
Displaying user profile
1 parent 5e97495 commit 93268ba

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

‎src/main/java/com/naturalprogrammer/spring5tutorial/config/SecurityConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ protected void configure(HttpSecurity http) throws Exception {
3838

3939
http
4040
.authorizeRequests()
41-
.mvcMatchers(HttpMethod.GET, "/").permitAll()
41+
.mvcMatchers(HttpMethod.GET, "/",
42+
"/users/*").permitAll()
4243
.mvcMatchers("/signup",
4344
"/forgot-password",
4445
"/reset-password/*").permitAll()

‎src/main/java/com/naturalprogrammer/spring5tutorial/controllers/UserController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.mail.MessagingException;
44

55
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
67
import org.springframework.web.bind.annotation.GetMapping;
78
import org.springframework.web.bind.annotation.PathVariable;
89
import org.springframework.web.bind.annotation.RequestMapping;
@@ -39,4 +40,11 @@ public String resendVerificationMail(@PathVariable("userId") User user,
3940
MyUtils.flash(redirectAttributes, "success", "verificationMailResent");
4041
return "redirect:/";
4142
}
43+
44+
@GetMapping("/{userId}")
45+
public String getById(@PathVariable Long userId, Model model) {
46+
47+
model.addAttribute(userService.fetchById(userId));
48+
return "user";
49+
}
4250
}

‎src/main/java/com/naturalprogrammer/spring5tutorial/services/UserService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ public interface UserService {
1616
void resendVerificationMail(User user) throws MessagingException;
1717
void forgotPassword(ForgotPasswordCommand forgotPasswordCommand);
1818
void resetPassword(String resetPasswordCode, String password);
19+
User fetchById(Long userId);
1920
}

‎src/main/java/com/naturalprogrammer/spring5tutorial/services/UserServiceImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,16 @@ public void resetPassword(String resetPasswordCode, String password) {
198198

199199
userRepository.save(u);
200200
}
201+
202+
@Override
203+
public User fetchById(Long userId) {
204+
205+
User user = userRepository.getOne(userId);
206+
MyUtils.validate(user != null, "userNotFound");
207+
208+
if (!isAdminOrSelfLoggedIn(user))
209+
user.setEmail("Confidential");
210+
211+
return user;
212+
}
201213
}

‎src/main/webapp/WEB-INF/jsp/includes/header.jsp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@
8282
<span class="caret"></span>
8383
</a>
8484
<ul class="dropdown-menu">
85+
<li>
86+
<a href="/users/<sec:authentication property='principal.id' />">
87+
<span class="glyphicon glyphicon-user"></span>
88+
Profile
89+
</a>
90+
</li>
8591
<li>
8692
<form:form id="logoutForm" action="/logout">
8793
</form:form>

‎src/main/webapp/WEB-INF/jsp/user.jsp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
2+
<%@include file="includes/header.jsp"%>
3+
4+
<div class="panel panel-primary">
5+
<div class="panel-heading">
6+
<h3 class="panel-title">Profile</h3>
7+
</div>
8+
<div class="panel-body">
9+
<dl class="dl-horizontal">
10+
<dt>Name</dt>
11+
<dd><c:out value="${user.name}" /></dd>
12+
<dt>Email</dt>
13+
<dd><c:out value="${user.email}" /></dd>
14+
<dt>Roles</dt>
15+
<dd><c:out value="${user.roles}" /></dd>
16+
</dl>
17+
</div>
18+
</div>
19+
20+
<%@include file="includes/footer.jsp"%>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /