From 3349f2e5677009e38e5ae57a780a1e9e172f82a2 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sat, 23 Jun 2012 16:15:59 -0300 Subject: [PATCH] Audith login operations. --- .../java/com/wisemapping/dao/UserManager.java | 5 +- .../com/wisemapping/dao/UserManagerImpl.java | 8 +- .../com/wisemapping/model/AccessAuditory.java | 59 +++++++++++++ .../java/com/wisemapping/model/UserLogin.java | 83 ------------------- .../security/AuthenticationProvider.java | 1 + .../security/UserDetailsService.java | 20 +++-- .../com/wisemapping/service/UserService.java | 4 +- .../wisemapping/service/UserServiceImpl.java | 27 +++--- ...erLogin.hbm.xml => AccessAuditory.hbm.xml} | 10 ++- .../main/webapp/WEB-INF/wisemapping-dao.xml | 2 +- .../webapp/WEB-INF/wisemapping-security.xml | 2 +- .../src/main/webapp/jsp/accountSettings.jsp | 2 + .../src/test/sql/hsql/create-schemas.sql | 11 ++- .../src/test/sql/mysql/create-schemas.sql | 18 ++-- .../src/test/sql/mysql/v2.0-to-v3.0.sql | 22 ++++- 15 files changed, 146 insertions(+), 128 deletions(-) create mode 100755 wise-webapp/src/main/java/com/wisemapping/model/AccessAuditory.java delete mode 100755 wise-webapp/src/main/java/com/wisemapping/model/UserLogin.java rename wise-webapp/src/main/resources/com/wisemapping/model/{UserLogin.hbm.xml => AccessAuditory.hbm.xml} (50%) diff --git a/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java b/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java index aedcfff5..a721824c 100644 --- a/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java +++ b/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java @@ -20,7 +20,8 @@ package com.wisemapping.dao; import com.wisemapping.model.Collaborator; import com.wisemapping.model.User; -import com.wisemapping.model.UserLogin; +import com.wisemapping.model.AccessAuditory; +import org.jetbrains.annotations.NotNull; import java.util.List; @@ -36,7 +37,7 @@ public interface UserManager { void createUser(User user); - void auditLogin(UserLogin userLogin); + void auditLogin(@NotNull AccessAuditory accessAuditory); void updateUser(User user); diff --git a/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java b/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java index 5018090d..2bf88c47 100644 --- a/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java @@ -21,7 +21,7 @@ package com.wisemapping.dao; import com.wisemapping.model.Collaboration; import com.wisemapping.model.Collaborator; import com.wisemapping.model.User; -import com.wisemapping.model.UserLogin; +import com.wisemapping.model.AccessAuditory; import org.jetbrains.annotations.NotNull; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.security.authentication.encoding.PasswordEncoder; @@ -121,9 +121,9 @@ public class UserManagerImpl getHibernateTemplate().flush(); } - public void auditLogin(UserLogin userLogin) { - assert userLogin != null : "userLogin is null"; - getHibernateTemplate().save(userLogin); + public void auditLogin(@NotNull AccessAuditory accessAuditory) { + assert accessAuditory != null : "accessAuditory is null"; + getHibernateTemplate().save(accessAuditory); } public void updateUser(@NotNull User user) { diff --git a/wise-webapp/src/main/java/com/wisemapping/model/AccessAuditory.java b/wise-webapp/src/main/java/com/wisemapping/model/AccessAuditory.java new file mode 100755 index 00000000..939dc951 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/model/AccessAuditory.java @@ -0,0 +1,59 @@ +/* +* Copyright [2011] [wisemapping] +* +* Licensed under WiseMapping Public License, Version 1.0 (the "License"). +* It is basically the Apache License, Version 2.0 (the "License") plus the +* "powered by wisemapping" text requirement on every single page; +* you may not use this file except in compliance with the License. +* You may obtain a copy of the license at +* +* http://www.wisemapping.org/license +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package com.wisemapping.model; + +import org.jetbrains.annotations.NotNull; + +import java.io.Serializable; +import java.util.Calendar; + +public class AccessAuditory + implements Serializable { + + private int id; + private Calendar loginDate = null; + private User user = null; + + public AccessAuditory() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public void setLoginDate(@NotNull Calendar loginDate) { + this.loginDate = loginDate; + } + + public Calendar getLoginDate() { + return loginDate; + } + + public void setUser(@NotNull User user) { + this.user = user; + } + + public User getUser() { + return this.user; + } +} \ No newline at end of file diff --git a/wise-webapp/src/main/java/com/wisemapping/model/UserLogin.java b/wise-webapp/src/main/java/com/wisemapping/model/UserLogin.java deleted file mode 100755 index 8a7e432c..00000000 --- a/wise-webapp/src/main/java/com/wisemapping/model/UserLogin.java +++ /dev/null @@ -1,83 +0,0 @@ -/* -* Copyright [2011] [wisemapping] -* -* Licensed under WiseMapping Public License, Version 1.0 (the "License"). -* It is basically the Apache License, Version 2.0 (the "License") plus the -* "powered by wisemapping" text requirement on every single page; -* you may not use this file except in compliance with the License. -* You may obtain a copy of the license at -* -* http://www.wisemapping.org/license -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -package com.wisemapping.model; - -import java.io.Serializable; -import java.util.Calendar; - -public class UserLogin - implements Serializable -{ - - private int id; - private Calendar loginDate = null; - private String email = null; - - public UserLogin() - { - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public void setLoginDate(Calendar loginDate) - { - this.loginDate = loginDate; - } - - public Calendar getLoginDate() - { - return loginDate; - } - - public void setEmail(String email) - { - this.email = email; - } - - public String getEmail() - { - return email; - } -/* - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - final UserLogin userLogin = (UserLogin) o; - - if (loginDate.equals(userLogin.loginDate)) return false; - if (email.equals(userLogin.email)) return false; - - return true; - } - - public int hashCode() { - int result; - result = (loginDate!= null ? loginDate.hashCode() : 0); - result = 29 * result + (email != null ? email.hashCode() : 0); - return result; - } - */ -} \ No newline at end of file diff --git a/wise-webapp/src/main/java/com/wisemapping/security/AuthenticationProvider.java b/wise-webapp/src/main/java/com/wisemapping/security/AuthenticationProvider.java index 48454c66..9d0ce501 100644 --- a/wise-webapp/src/main/java/com/wisemapping/security/AuthenticationProvider.java +++ b/wise-webapp/src/main/java/com/wisemapping/security/AuthenticationProvider.java @@ -26,6 +26,7 @@ public class AuthenticationProvider implements org.springframework.security.auth if (user == null || credentials == null || !encoder.isPasswordValid(user.getPassword(), credentials, null)) { throw new BadCredentialsException("Username/Password does not match for " + auth.getPrincipal()); } + userDetailsService.getUserService().auditLogin(user); return new UsernamePasswordAuthenticationToken(userDetails, credentials, userDetails.getAuthorities()); } diff --git a/wise-webapp/src/main/java/com/wisemapping/security/UserDetailsService.java b/wise-webapp/src/main/java/com/wisemapping/security/UserDetailsService.java index d5285236..92dbf19a 100644 --- a/wise-webapp/src/main/java/com/wisemapping/security/UserDetailsService.java +++ b/wise-webapp/src/main/java/com/wisemapping/security/UserDetailsService.java @@ -19,23 +19,27 @@ package com.wisemapping.security; import com.wisemapping.dao.UserManager; +import com.wisemapping.model.User; +import com.wisemapping.service.UserService; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.dao.DataAccessException; import org.springframework.security.core.userdetails.UsernameNotFoundException; public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService { - private UserManager userManager; + private UserService userService; private String adminUser; @Override public UserDetails loadUserByUsername(@NotNull String email) throws UsernameNotFoundException, DataAccessException { - final com.wisemapping.model.User model = userManager.getUserBy(email); + final User user = userService.getUserBy(email); - if (model != null) { - return new UserDetails(model, isAdmin(email)); + if (user != null) { + return new UserDetails(user, isAdmin(email)); } else { throw new UsernameNotFoundException(email); } @@ -45,12 +49,12 @@ public class UserDetailsService return email != null && adminUser != null && email.trim().endsWith(adminUser); } - public UserManager getUserManager() { - return userManager; + public UserService getUserService() { + return userService; } - public void setUserManager(UserManager userManager) { - this.userManager = userManager; + public void setUserService(UserService userManager) { + this.userService = userManager; } public String getAdminUser() { diff --git a/wise-webapp/src/main/java/com/wisemapping/service/UserService.java b/wise-webapp/src/main/java/com/wisemapping/service/UserService.java index 199d28a2..76b1f964 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/UserService.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/UserService.java @@ -40,7 +40,7 @@ public interface UserService { public void resetPassword(@NotNull String email) throws InvalidUserEmailException; - public User reloadUser(final User user); - public void deleteUser(@NotNull User user); + + public void auditLogin(@NotNull User user); } diff --git a/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java b/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java index 96dcc460..df30dd56 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java @@ -20,16 +20,13 @@ package com.wisemapping.service; import com.wisemapping.dao.UserManager; import com.wisemapping.exceptions.WiseMappingException; -import com.wisemapping.mail.Mailer; import com.wisemapping.mail.NotificationService; +import com.wisemapping.model.AccessAuditory; import com.wisemapping.model.Collaborator; import com.wisemapping.model.User; -import org.apache.log4j.Logger; import org.jetbrains.annotations.NotNull; import java.util.Calendar; -import java.util.HashMap; -import java.util.Map; import java.util.UUID; public class UserServiceImpl @@ -38,6 +35,7 @@ public class UserServiceImpl private MindmapService mindmapService; private NotificationService notificationService; + @Override public void activateAccount(long code) throws InvalidActivationCodeException { final User user = userManager.getUserByActivationCode(code); @@ -51,10 +49,7 @@ public class UserServiceImpl } } - public User reloadUser(final User user) { - return this.getUserBy(user.getId()); - } - + @Override public void resetPassword(@NotNull String email) throws InvalidUserEmailException { final User user = userManager.getUserBy(email); @@ -66,8 +61,6 @@ public class UserServiceImpl // Send an email with the new temporal password ... notificationService.resetPassword(user, password); - - } else { throw new InvalidUserEmailException("The email '" + email + "' does not exists."); } @@ -90,10 +83,19 @@ public class UserServiceImpl return lo + i; } + @Override public void deleteUser(@NotNull User user) { userManager.deleteUser(user); } + @Override + public void auditLogin(@NotNull User user) { + final AccessAuditory accessAuditory = new AccessAuditory(); + accessAuditory.setUser(user); + accessAuditory.setLoginDate(Calendar.getInstance()); + userManager.auditLogin(accessAuditory); + } + public User createUser(@NotNull User user, boolean emailConfirmEnabled) throws WiseMappingException { final UUID uuid = UUID.randomUUID(); user.setCreationDate(Calendar.getInstance()); @@ -127,23 +129,28 @@ public class UserServiceImpl return user; } + @Override public void changePassword(@NotNull User user) { notificationService.passwordChanged(user); userManager.updateUser(user); } + @Override public User getUserBy(String email) { return userManager.getUserBy(email); } + @Override public User getUserByUsername(String username) { return userManager.getUserByUsername(username); } + @Override public User getUserBy(long id) { return userManager.getUserBy(id); } + @Override public void updateUser(@NotNull User user) { userManager.updateUser(user); } diff --git a/wise-webapp/src/main/resources/com/wisemapping/model/UserLogin.hbm.xml b/wise-webapp/src/main/resources/com/wisemapping/model/AccessAuditory.hbm.xml similarity index 50% rename from wise-webapp/src/main/resources/com/wisemapping/model/UserLogin.hbm.xml rename to wise-webapp/src/main/resources/com/wisemapping/model/AccessAuditory.hbm.xml index 16118c30..c9eb20da 100755 --- a/wise-webapp/src/main/resources/com/wisemapping/model/UserLogin.hbm.xml +++ b/wise-webapp/src/main/resources/com/wisemapping/model/AccessAuditory.hbm.xml @@ -5,12 +5,16 @@ - - + + - + \ No newline at end of file diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-dao.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-dao.xml index 4bddf1a8..88f47431 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-dao.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-dao.xml @@ -28,7 +28,7 @@ com/wisemapping/model/MindMap.hbm.xml com/wisemapping/model/Collaboration.hbm.xml com/wisemapping/model/CollaborationProperties.hbm.xml - com/wisemapping/model/UserLogin.hbm.xml + com/wisemapping/model/AccessAuditory.hbm.xml com/wisemapping/model/MindMapHistory.hbm.xml diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-security.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-security.xml index db78e509..f243e1fd 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-security.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-security.xml @@ -68,7 +68,7 @@ - + diff --git a/wise-webapp/src/main/webapp/jsp/accountSettings.jsp b/wise-webapp/src/main/webapp/jsp/accountSettings.jsp index 6d0bc8fd..6e7f815f 100755 --- a/wise-webapp/src/main/webapp/jsp/accountSettings.jsp +++ b/wise-webapp/src/main/webapp/jsp/accountSettings.jsp @@ -53,6 +53,8 @@