Skip to content

Commit e845880

Browse files
committed
feat(users): add profile
1 parent 61d514a commit e845880

File tree

10 files changed

+133
-11
lines changed

10 files changed

+133
-11
lines changed

src/main/java/config/security/SecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
4949
protected void configure(HttpSecurity http) throws Exception {
5050

5151
http.authorizeRequests()
52-
.antMatchers("/admin/users/**").hasRole("BLOG_ADMIN")
52+
.regexMatchers("/admin/users/\\b(?:(?!self)\\w)+\\b").hasRole("BLOG_ADMIN")
5353
.antMatchers("/admin/posts/**").hasRole("BLOG_CONTRIBUTOR")
5454
.antMatchers("/admin/signup").anonymous()
5555
.antMatchers("/admin/**").authenticated()

src/main/java/controllers/admin/SignupController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public String processSignup(@ModelAttribute @Valid User user, Errors errors, Red
5656
try {
5757
Role role = rolesRepository.findByName("ROLE_BLOG_CONTRIBUTOR");
5858
user.addRole(role);
59-
userService.registerNewUserAccount(user);
59+
userService.create(user);
6060
List<String> successMessages = new ArrayList();
6161
successMessages.add(messageSource.getMessage("message.signup.success", new Object[]{ user.getUsername() }, Locale.getDefault()));
6262
model.addFlashAttribute("successFlashMessages", successMessages);

src/main/java/controllers/admin/UsersController.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@
55
*/
66
package controllers.admin;
77

8+
import exceptions.UserAlredyExistsException;
9+
import java.util.ArrayList;
810
import java.util.List;
11+
import java.util.Locale;
12+
import javax.validation.Valid;
913
import models.User;
1014
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
1116
import org.springframework.stereotype.Controller;
1217
import org.springframework.ui.Model;
18+
import org.springframework.validation.Errors;
1319
import org.springframework.web.bind.annotation.GetMapping;
20+
import org.springframework.web.bind.annotation.ModelAttribute;
21+
import org.springframework.web.bind.annotation.PostMapping;
1422
import org.springframework.web.bind.annotation.RequestMapping;
23+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
1524
import services.UserService;
25+
import services.security.CurrentUser;
1626

1727
/**
1828
*
@@ -24,6 +34,31 @@ public class UsersController {
2434

2535
@Autowired
2636
private UserService userService;
37+
@Autowired
38+
private ReloadableResourceBundleMessageSource messageSource;
39+
40+
@GetMapping("/self")
41+
public String self(@CurrentUser User activeUser, Model model){
42+
model.addAttribute("user", activeUser);
43+
return "admin/user/self";
44+
}
45+
46+
@PostMapping("/self")
47+
public String self(@ModelAttribute @Valid User user, Errors errors, RedirectAttributes model){
48+
String viewName = "admin/user/self";
49+
if(!errors.hasErrors()){
50+
try{
51+
userService.update(user);
52+
List<String> successMessages = new ArrayList();
53+
successMessages.add(messageSource.getMessage("message.profile.save.success", new Object[]{ }, Locale.getDefault()));
54+
model.addFlashAttribute("successFlashMessages", successMessages);
55+
viewName = "redirect:/admin/user/self";
56+
}catch(UserAlredyExistsException e){
57+
errors.rejectValue("email", "user.exists");
58+
}
59+
}
60+
return viewName;
61+
}
2762

2863
@GetMapping("/all")
2964
public String all(Model model){

src/main/java/services/UserService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* @author sergio
1616
*/
1717
public interface UserService {
18-
void registerNewUserAccount(User user) throws UserAlredyExistsException;
18+
void create(User user) throws UserAlredyExistsException;
19+
void update(User user) throws UserAlredyExistsException;
1920
List<User> getAllUsers();
2021
void updateLastLoginAccess(String username, Date lastLoginAccess);
2122
User findUserByUsername(String username);

src/main/java/services/UserServiceImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,23 @@ public class UserServiceImpl implements UserService {
3333

3434
@Transactional
3535
@Override
36-
public void registerNewUserAccount(User user) throws UserAlredyExistsException {
36+
public void create(User user) throws UserAlredyExistsException {
3737
logger.debug("Registrando nuevo usuario: " + user.getUsername());
3838
if (userRepository.existsUserWithEmailOrUsername(user.getEmail(), user.getUsername()) > 0){
3939
throw new UserAlredyExistsException(user.getEmail(), user.getUsername());
4040
}
4141
user.setPassword(passwordEncoder.encode(user.getPasswordClear()));
4242
userRepository.save(user);
4343
}
44+
45+
@Transactional
46+
@Override
47+
public void update(User user) throws UserAlredyExistsException {
48+
if (userRepository.existsUserWithEmailOrUsername(user.getEmail(), user.getUsername()) > 0){
49+
throw new UserAlredyExistsException(user.getEmail(), user.getUsername());
50+
}
51+
userRepository.save(user);
52+
}
4453

4554
@Override
4655
public List<User> getAllUsers() {

src/main/webapp/WEB-INF/i18n/messages.properties

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ admin.post.create.title = Crear nuevo art\u00edculo
1010
admin.post.delete.title = Eliminar Art\u00edculo
1111
admin.post.edit.title = Editar Art\u00edculo
1212
admin.user.all.title = Todos los usuarios
13+
admin.user.profile.title = Perfil de {0}
1314
frontend.post.show.title = OhMyBlog! | {0}
1415
frontend.post.about.title = Acerca de este blog
1516
frontend.post.home.title = ohMyBlog! | Blog para los amantes de la web
@@ -23,6 +24,7 @@ admin.navigation.posts = Art\u00edculos
2324
admin.navigation.users = Usuarios
2425
admin.navigation.logout = Cerrar Sesi\u00f3n
2526
admin.navigation.back.to.blog = Regresar al blog
27+
admin.navigation.profile = Perfil
2628
###### Home Page #######
2729
home.title = Oh My Blog!
2830
home.subtitle = Blog para los amantes del desarrollo web
@@ -77,6 +79,15 @@ users.all.th.status = Estado
7779
users.all.th.posts = Art\u00edculos
7880
users.all.th.lastLoginAccess = \u00faltimo acceso
7981
users.all.not.found = Ning\u00fan usuario encontrado.
82+
#### User Profile ######
83+
users.profile.title = Perfil del Usuario.
84+
form.profile.username.label = Nombre de Usuario
85+
form.profile.username.placeholder = Nombre de usuario de la aplicaci\u00f3n
86+
form.profile.email.label = Correo electr\u00f3nico
87+
form.profile.email.placeholder = Introduce correo electr\u00f3nico
88+
form.profile.fullName.label = Nombre completo
89+
form.profile.fullName.placeholder = Introduce Nombre completo
90+
form.profile.save = Guardar
8091
#### Create Post Page #####
8192
post.create.title = Crear nuevo art\u00edculo.
8293
post.update.title = Modificar art\u00edculo.
@@ -85,6 +96,7 @@ post.delete.title = \u00bfDeseas eliminar este art\u00edculo?
8596
error.back.home = Regresar a la p\u00e1gina de inicio
8697
#### Flash Messages ####
8798
message.signup.success = Bienvenido {0}! Tu cuenta ha sido creada con \u00e9xito inicia sesi\u00f3n para continuar.
99+
message.profile.save.success = Datos del perfil guardados con exito.
88100
message.post.save.success = La informaci\u00f3n del post {0}, ha sido guardada con \u00e9xito.
89101
message.post.remove.success = El art\u00edculo ha sido eliminado con \u00e9xito.
90102
user.exists = Ya existe un usuario con este correo electr\u00f3nico o nombre de usuario.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<head>
5+
<title>TODO supply a title</title>
6+
<meta charset="UTF-8">
7+
</head>
8+
<body>
9+
<div th:fragment="form-errors" class="alert alert-danger" th:if="${#fields.hasErrors('*')}">
10+
<ul>
11+
<li th:each="err: ${#fields.errors('*')}" th:text="${err}">Input is incorrect</li>
12+
</ul>
13+
</div>
14+
</body>
15+
</html>

src/main/webapp/WEB-INF/templates/admin/fragment/post.html

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
34
<head>
45
<title>TODO supply a title</title>
56
<meta charset="UTF-8">
67
</head>
78
<body>
89
<form th:fragment="form(target, delete)" enctype="multipart/form-data" id="postForm" name="postForm" method="post" action="#" th:action="${target}" th:object="${post}">
9-
<div class="alert alert-danger" th:if="${#fields.hasErrors('*')}">
10-
<ul>
11-
<li th:each="err: ${#fields.errors('*')}" th:text="${err}">Input is incorrect</li>
12-
</ul>
13-
</div>
10+
<th:block th:replace="admin/fragment/alerts::form-errors"></th:block>
1411
<div class="row control-group">
1512
<div class="form-group col-xs-12 floating-label-form-group controls">
1613
<label th:text="#{form.post.title.label}">Title</label>

src/main/webapp/WEB-INF/templates/admin/layout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h1><a href="index.html">Oh My Blog! Admin</a></h1>
4141
<b class="caret"></b>
4242
</a>
4343
<ul class="dropdown-menu animated fadeInUp">
44-
<li><a href="profile.html">Profile</a></li>
44+
<li><a href="#" th:href="@{/admin/users/self}" th:text="#{admin.navigation.profile}">Profile</a></li>
4545
<li><a href="#" th:href="@{/admin/logout}" th:text="#{admin.navigation.logout}">Logout</a></li>
4646
</ul>
4747
</li>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<!--
3+
To change this license header, choose License Headers in Project Properties.
4+
To change this template file, choose Tools | Templates
5+
and open the template in the editor.
6+
-->
7+
<html xmlns="http://www.w3.org/1999/xhtml"
8+
xmlns:th="http://www.thymeleaf.org"
9+
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
10+
layout:decorator="admin/layout">
11+
<head>
12+
<title th:text="#{admin.user.profile.title(${user.fullName})}">User Profile</title>
13+
</head>
14+
<body>
15+
<div layout:fragment="main-content">
16+
<div class="content-box-large">
17+
<div class="panel-heading">
18+
<div class="panel-title text-capitalize" th:text="#{users.profile.title}">Create new Post</div>
19+
</div>
20+
<div class="panel-body">
21+
<form id="userProfile" name="userProfile" method="post" action="#" th:action="${target}" th:object="${user}">
22+
<th:block th:replace="admin/fragment/alerts::form-errors"></th:block>
23+
<div class="row control-group">
24+
<div class="form-group col-xs-12 floating-label-form-group controls">
25+
<label th:text="#{form.profile.username.label}">Username</label>
26+
<input type="text" th:field="*{username}" th:placeholder="#{form.profile.username.placeholder}" class="form-control" id="id" required />
27+
</div>
28+
</div>
29+
<div class="row control-group">
30+
<div class="form-group col-xs-12 floating-label-form-group controls">
31+
<label th:text="#{form.profile.email.label}">email</label>
32+
<input type="email" th:field="*{email}" th:placeholder="#{form.profile.email.placeholder}" class="form-control" id="email" required />
33+
</div>
34+
</div>
35+
<div class="row control-group">
36+
<div class="form-group col-xs-12 floating-label-form-group controls">
37+
<label th:text="#{form.profile.fullName.label}">full name</label>
38+
<input type="text" th:field="*{fullName}" th:placeholder="#{form.profile.fullName.placeholder}" class="form-control" id="fullName" required />
39+
</div>
40+
</div>
41+
<input type="hidden" th:field="*{id}"/>
42+
<br />
43+
<div class="row">
44+
<div class="form-group col-xs-12">
45+
<button type="submit" class="btn btn-success" th:value="#{form.profile.save}">Save</button>
46+
</div>
47+
</div>
48+
</form>
49+
</div>
50+
</div>
51+
</div>
52+
</body>
53+
</html>

0 commit comments

Comments
 (0)