You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rave.apache.org by mf...@apache.org on 2011/04/26 04:41:00 UTC
svn commit: r1096643 - in /incubator/rave/trunk: ./ rave-portal/
rave-portal/src/main/java/org/apache/rave/portal/model/
rave-portal/src/main/java/org/apache/rave/portal/model/util/
rave-portal/src/main/java/org/apache/rave/portal/repository/ rave-port...
Author: mfranklin
Date: Tue Apr 26 02:40:59 2011
New Revision: 1096643
URL: http://svn.apache.org/viewvc?rev=1096643&view=rev
Log:
Added Spring Security form login
Updated User object with properties required to implement UserDetails
Implemented User Repository
Implemented User Service
Added:
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/User.java
- copied, changed from r1096551, incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Person.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/UserRepository.java
- copied, changed from r1096551, incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PersonRepository.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaUserRepository.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/util/
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/util/JpaUtil.java
incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext-security.xml
incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java
- copied, changed from r1096551, incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaPageRepositoryTest.java
incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/UserServiceTest.java
Removed:
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Person.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PersonRepository.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/PersonService.java
Modified:
incubator/rave/trunk/pom.xml
incubator/rave/trunk/rave-portal/pom.xml
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Page.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Widget.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/InternationalString.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PageRepository.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaPageRepository.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/PageService.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/UserService.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultPageService.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultUserService.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/util/LocalizationUtils.java
incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/HomeController.java
incubator/rave/trunk/rave-portal/src/main/resources/META-INF/persistence.xml
incubator/rave/trunk/rave-portal/src/main/resources/initial_data.sql
incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/web.xml
incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/InternationalStringTest.java
incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaPageRepositoryTest.java
incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/PageServiceTest.java
incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/HomeControllerTest.java
Modified: incubator/rave/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/pom.xml?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/pom.xml (original)
+++ incubator/rave/trunk/pom.xml Tue Apr 26 02:40:59 2011
@@ -91,6 +91,16 @@
<version>${org.springframework.version}</version>
</dependency>
<dependency>
+ <groupId>org.springframework.security</groupId>
+ <artifactId>spring-security-web</artifactId>
+ <version>${org.springframework.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.security</groupId>
+ <artifactId>spring-security-config</artifactId>
+ <version>${org.springframework.version}</version>
+ </dependency>
+ <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
Modified: incubator/rave/trunk/rave-portal/pom.xml
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/pom.xml?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/pom.xml (original)
+++ incubator/rave/trunk/rave-portal/pom.xml Tue Apr 26 02:40:59 2011
@@ -90,6 +90,16 @@ if needed you can specify it on the comm
<artifactId>spring-orm</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.springframework.security</groupId>
+ <artifactId>spring-security-web</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework.security</groupId>
+ <artifactId>spring-security-config</artifactId>
+ </dependency>
+
<!--Persistence-->
<dependency>
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Page.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Page.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Page.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Page.java Tue Apr 26 02:40:59 2011
@@ -22,7 +22,7 @@ import javax.persistence.*;
import java.util.List;
/**
- * A page, which consists of regions, and which may be owned by a {@link Person} (note the ownership will likely need to
+ * A page, which consists of regions, and which may be owned by a {@link User} (note the ownership will likely need to
* become more flexible to enable things like group ownership in the future).
*/
@Entity
@@ -42,7 +42,7 @@ public class Page {
@ManyToOne
@JoinColumn(name = "owner_id")
- private Person owner;
+ private User owner;
@Basic @Column(name="render_sequence")
private Long renderSequence;
@@ -82,15 +82,15 @@ public class Page {
}
/**
- * Gets the {@link Person} that owns the page
+ * Gets the {@link User} that owns the page
*
- * @return Valid {@link Person}
+ * @return Valid {@link User}
*/
- public Person getOwner() {
+ public User getOwner() {
return owner;
}
- public void setOwner(Person owner) {
+ public void setOwner(User owner) {
this.owner = owner;
}
Copied: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/User.java (from r1096551, incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Person.java)
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/User.java?p2=incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/User.java&p1=incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Person.java&r1=1096551&r2=1096643&rev=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Person.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/User.java Tue Apr 26 02:40:59 2011
@@ -18,35 +18,134 @@
*/
package org.apache.rave.portal.model;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.userdetails.UserDetails;
+
import javax.persistence.*;
+import java.util.Collection;
/**
- * A person
+ * {@inheritDoc}
+ *
+ * A user of the system
*/
@Entity
-@Table(name="person")
-@SequenceGenerator(name="personIdSeq", sequenceName = "person_id_seq")
-public class Person {
- @Id @Column(name="user_id")
- @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "personIdSeq")
- private String userId;
+@Table(name = "user")
+@SequenceGenerator(name = "userIdSeq", sequenceName = "user_id_seq")
+@NamedQueries({
+ @NamedQuery(name="User.getByUsername", query = "select u from User u where u.username = :username")
+})
+public class User implements UserDetails {
+ @Id @Column(name = "user_id")
+ @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userIdSeq")
+ private Long userId;
+
+ @Basic @Column(name = "username")
+ private String username;
+
+ @Basic @Column(name = "password")
+ private String password;
+
+ @Basic @Column(name = "expired")
+ private boolean expired;
+
+ @Basic @Column(name = "locked")
+ private boolean locked;
+
+ @Basic @Column(name = "enabled")
+ private boolean enabled;
- public Person() {
+ public User() {
}
- public Person(String userId) {
+ public User(Long userId) {
this.userId = userId;
}
+ public User(Long userId, String username) {
+ this.userId = userId;
+ this.username = username;
+ }
+
/**
* Gets the unique identifier for this user.
+ *
* @return The unique identifier for this user.
*/
- public String getUserId() {
+ public Long getUserId() {
return userId;
}
- public void setUserId(String userId) {
+ public void setUserId(Long userId) {
this.userId = userId;
}
+
+ //TODO: Add GrantedAuthorities to user
+
+ @Override
+ public Collection<GrantedAuthority> getAuthorities() {
+ return null;
+ }
+
+ //TODO:Setup Hashing and Salting of Passwords
+
+ /**
+ * Gets the password stored in the database
+ *
+ * @return password as String
+ */
+ @Override
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ @Override
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ @Override
+ public boolean isAccountNonLocked() {
+ return !locked;
+ }
+
+ public void setLocked(boolean locked) {
+ this.locked = locked;
+ }
+
+ @Override
+ public boolean isCredentialsNonExpired() {
+ return !expired;
+ }
+
+ /**
+ * Synchronized with password expiration {@see isCredentialsNonExpired}
+ *
+ * @return <code>true</code> if the user's is not expired valid (ie non-expired), <code>false</code> if no longer valid
+ */
+ @Override
+ public boolean isAccountNonExpired() {
+ return isCredentialsNonExpired();
+ }
+
+ public void setExpired(boolean expired) {
+ this.expired = expired;
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return enabled;
+ }
+
+ public void setEnabled(boolean enabled) {
+ this.enabled = enabled;
+ }
}
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Widget.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Widget.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Widget.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/Widget.java Tue Apr 26 02:40:59 2011
@@ -18,14 +18,7 @@
*/
package org.apache.rave.portal.model;
-import javax.persistence.Basic;
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.SequenceGenerator;
-import javax.persistence.Table;
+import javax.persistence.*;
/**
* A widget
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/InternationalString.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/InternationalString.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/InternationalString.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/model/util/InternationalString.java Tue Apr 26 02:40:59 2011
@@ -18,11 +18,11 @@
*/
package org.apache.rave.portal.model.util;
+import org.apache.rave.portal.util.LocalizationUtils;
+
import java.util.ArrayList;
import java.util.List;
-import org.apache.rave.portal.util.LocalizationUtils;
-
/**
* A String that has been internationalized into several Locales. This class is used as a replacement for the String
* type whenever a String needs to be i18nn-capable. An instance of the InternationalString interface composes within it
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PageRepository.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PageRepository.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PageRepository.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PageRepository.java Tue Apr 26 02:40:59 2011
@@ -23,5 +23,5 @@ import org.apache.rave.portal.model.Page
import java.util.List;
public interface PageRepository {
- List<Page> getAllPages(String userId);
+ List<Page> getAllPages(Long userId);
}
\ No newline at end of file
Copied: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/UserRepository.java (from r1096551, incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PersonRepository.java)
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/UserRepository.java?p2=incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/UserRepository.java&p1=incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PersonRepository.java&r1=1096551&r2=1096643&rev=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/PersonRepository.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/UserRepository.java Tue Apr 26 02:40:59 2011
@@ -18,5 +18,9 @@
*/
package org.apache.rave.portal.repository;
-public interface PersonRepository {
+import org.apache.rave.portal.model.User;
+
+public interface UserRepository {
+ User getById(long userId);
+ User getByUsername(String username);
}
\ No newline at end of file
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaPageRepository.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaPageRepository.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaPageRepository.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaPageRepository.java Tue Apr 26 02:40:59 2011
@@ -26,6 +26,7 @@ import org.springframework.stereotype.Re
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
+import javax.persistence.TypedQuery;
import java.util.List;
@Repository
@@ -36,8 +37,8 @@ public class JpaPageRepository implement
@Override
@SuppressWarnings("unchecked")
- public List<Page> getAllPages(String userId) {
- Query query = manager.createNamedQuery("Page.getByUserId");
+ public List<Page> getAllPages(Long userId) {
+ TypedQuery<Page> query = manager.createNamedQuery("Page.getByUserId", Page.class);
query.setParameter("userId", userId);
return query.getResultList();
}
Added: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaUserRepository.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaUserRepository.java?rev=1096643&view=auto
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaUserRepository.java (added)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/JpaUserRepository.java Tue Apr 26 02:40:59 2011
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.apache.rave.portal.repository.impl;
+
+import org.apache.rave.portal.model.User;
+import org.apache.rave.portal.repository.UserRepository;
+import org.springframework.stereotype.Repository;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+import javax.persistence.TypedQuery;
+
+import static org.apache.rave.portal.repository.impl.util.JpaUtil.getSingleResult;
+
+/**
+ */
+@Repository
+public class JpaUserRepository implements UserRepository{
+
+ @PersistenceContext
+ private EntityManager manager;
+
+ @Override
+ public User getById(long userId) {
+ return manager.find(User.class, userId);
+ }
+
+ @Override
+ public User getByUsername(String username) {
+ TypedQuery<User> query = manager.createNamedQuery("User.getByUsername", User.class);
+ query.setParameter("username", username);
+ return getSingleResult(query.getResultList());
+ }
+}
Added: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/util/JpaUtil.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/util/JpaUtil.java?rev=1096643&view=auto
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/util/JpaUtil.java (added)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/util/JpaUtil.java Tue Apr 26 02:40:59 2011
@@ -0,0 +1,23 @@
+package org.apache.rave.portal.repository.impl.util;
+
+import org.springframework.dao.IncorrectResultSizeDataAccessException;
+
+import java.util.List;
+
+/**
+ * @author mfranklin
+ * Date: 4/25/11
+ * Time: 10:13 PM
+ */
+public class JpaUtil {
+ public static <T> T getSingleResult(List<T> list) {
+ switch(list.size()) {
+ case 0:
+ return null;
+ case 1:
+ return list.get(0);
+ default:
+ throw new IncorrectResultSizeDataAccessException(1);
+ }
+ }
+}
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/PageService.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/PageService.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/PageService.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/PageService.java Tue Apr 26 02:40:59 2011
@@ -29,5 +29,5 @@ public interface PageService {
* @param userId The user to retrieve pages for.
* @return A non null possible empty list of pages for the given user.
*/
- List<Page> getAllPages(String userId);
+ List<Page> getAllPages(long userId);
}
\ No newline at end of file
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/UserService.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/UserService.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/UserService.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/UserService.java Tue Apr 26 02:40:59 2011
@@ -19,20 +19,26 @@
package org.apache.rave.portal.service;
-import org.apache.rave.portal.model.Person;
+import org.apache.rave.portal.model.User;
+import org.springframework.security.core.userdetails.UserDetailsService;
-public interface UserService {
+public interface UserService extends UserDetailsService {
/**
* Get the currently authenticated user.
*
* @return The authenticated user.
*/
- Person getAuthenticatedUser();
+ User getAuthenticatedUser();
/**
* Set the currently authenticated user to the user with a given userId.
- *
- * @param userId
+ *
+ * @param userId the unique id of the user
+ */
+ void setAuthenticatedUser(long userId);
+
+ /**
+ * Un-sets the currently authenticated user
*/
- void setAuthenticatedUser(String userId);
+ void clearAuthenticatedUser();
}
\ No newline at end of file
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultPageService.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultPageService.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultPageService.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultPageService.java Tue Apr 26 02:40:59 2011
@@ -37,7 +37,7 @@ public class DefaultPageService implemen
}
@Override
- public List<Page> getAllPages(String userId) {
+ public List<Page> getAllPages(long userId) {
return pageRepository.getAllPages(userId);
}
}
\ No newline at end of file
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultUserService.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultUserService.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultUserService.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/service/impl/DefaultUserService.java Tue Apr 26 02:40:59 2011
@@ -4,10 +4,10 @@
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
+ * "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
@@ -19,43 +19,91 @@
package org.apache.rave.portal.service.impl;
-import java.util.Random;
-
-import org.apache.rave.portal.model.Person;
+import org.apache.rave.portal.model.User;
+import org.apache.rave.portal.repository.UserRepository;
import org.apache.rave.portal.service.UserService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataAccessException;
+import org.springframework.security.authentication.AbstractAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.context.SecurityContextImpl;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
-@Service
+/**
+ *
+ */
+@Service(value = "userService")
public class DefaultUserService implements UserService {
- private String userId;
+ protected static final Logger log = LoggerFactory.getLogger(DefaultUserService.class);
+
+ private UserRepository userRepository;
+
+ @Autowired
+ public DefaultUserService(UserRepository userRepository) {
+ this.userRepository = userRepository;
+
+ }
+
+ @Override
+ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
+ log.debug("loadUserByUsername called with: " + username);
+ final User user = userRepository.getByUsername(username);
+ if(user == null) {
+ throw new UsernameNotFoundException("User with username '" + username + "' was not found!");
+ }
+ return user;
+ }
@Override
- public Person getAuthenticatedUser() {
- //TODO: Returning random mock data until we hook in real authentication
- String requestUserId;
- Person person = new Person();
- if (this.userId == null) {
- Random random = new Random();
- switch (random.nextInt(3)) {
- case 1:
- requestUserId = "john.doe";
- break;
- case 2:
- requestUserId = "jane.doe";
- break;
- default:
- requestUserId = "canonical";
- break;
- }
+ public User getAuthenticatedUser() {
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+
+ if (authentication != null && authentication.getPrincipal() instanceof User) {
+ return (User) authentication.getPrincipal();
} else {
- requestUserId = this.userId;
+ throw new SecurityException("Could not get the authenticated user!");
+ }
+ }
+
+ @Override
+ public void setAuthenticatedUser(long userId) {
+ final User user = userRepository.getById(userId);
+ if(user == null) {
+ throw new UsernameNotFoundException("User with id '" + userId + "' was not found!");
}
- person.setUserId(requestUserId);
- return person;
+ SecurityContext securityContext = createContext(user);
+ SecurityContextHolder.setContext(securityContext);
}
-
+
@Override
- public void setAuthenticatedUser(String userId) {
- this.userId = userId;
+ public void clearAuthenticatedUser() {
+ SecurityContextHolder.clearContext();
+ }
+
+ private SecurityContext createContext(final User user) {
+ SecurityContext securityContext = new SecurityContextImpl();
+ securityContext.setAuthentication(new AbstractAuthenticationToken(user.getAuthorities()) {
+ @Override
+ public Object getCredentials() {
+ return "N/A";
+ }
+
+ @Override
+ public Object getPrincipal() {
+ return user;
+ }
+
+ @Override
+ public boolean isAuthenticated() {
+ return true;
+ }
+ });
+ return securityContext;
}
}
\ No newline at end of file
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/util/LocalizationUtils.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/util/LocalizationUtils.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/util/LocalizationUtils.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/util/LocalizationUtils.java Tue Apr 26 02:40:59 2011
@@ -18,18 +18,17 @@
*/
package org.apache.rave.portal.util;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Comparator;
-import java.util.List;
-
+import com.ibm.icu.util.GlobalizationPreferences;
+import com.ibm.icu.util.ULocale;
import org.apache.commons.lang.ArrayUtils;
import org.apache.rave.portal.model.util.LocalizedString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.ibm.icu.util.GlobalizationPreferences;
-import com.ibm.icu.util.ULocale;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
/**
* Utilities for localization (adapted from org.apache.wookie.w3c.util.LocalizationUtils.java)
Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/HomeController.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/HomeController.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/HomeController.java (original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/HomeController.java Tue Apr 26 02:40:59 2011
@@ -19,7 +19,7 @@
package org.apache.rave.portal.web.controller;
import org.apache.rave.portal.model.Page;
-import org.apache.rave.portal.model.Person;
+import org.apache.rave.portal.model.User;
import org.apache.rave.portal.service.PageService;
import org.apache.rave.portal.service.UserService;
import org.apache.rave.portal.web.util.ModelKeys;
@@ -49,20 +49,9 @@ public class HomeController {
@RequestMapping(value = "/")
public String getHome(Model model) {
- Person user = userService.getAuthenticatedUser();
+ User user = userService.getAuthenticatedUser();
List<Page> pages = pageService.getAllPages(user.getUserId());
model.addAttribute(ModelKeys.PAGES, pages);
return "home";
}
-
- @RequestMapping(value = "/login")
- public String getLogin(@RequestParam("userId") String userId, Model model) {
- // TODO: implement a real login system, currently returning a random user if no "login" has been performed
- if (userId.equals("canonical") || userId.equals("jane.doe") || userId.equals("john.doe")) {
- userService.setAuthenticatedUser(userId);
- } else {
- model.addAttribute(ModelKeys.ERROR_MESSAGE, "Invalid User ID, using a random user");
- }
- return getHome(model);
- }
}
\ No newline at end of file
Modified: incubator/rave/trunk/rave-portal/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/resources/META-INF/persistence.xml?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/resources/META-INF/persistence.xml (original)
+++ incubator/rave/trunk/rave-portal/src/main/resources/META-INF/persistence.xml Tue Apr 26 02:40:59 2011
@@ -25,7 +25,7 @@
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>org.apache.rave.portal.model.Page</class>
<class>org.apache.rave.portal.model.Region</class>
- <class>org.apache.rave.portal.model.Person</class>
+ <class>org.apache.rave.portal.model.User</class>
<class>org.apache.rave.portal.model.RegionWidget</class>
<class>org.apache.rave.portal.model.Widget</class>
<class>org.apache.rave.portal.model.PageLayout</class>
Modified: incubator/rave/trunk/rave-portal/src/main/resources/initial_data.sql
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/resources/initial_data.sql?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/resources/initial_data.sql (original)
+++ incubator/rave/trunk/rave-portal/src/main/resources/initial_data.sql Tue Apr 26 02:40:59 2011
@@ -15,14 +15,26 @@
-- specific language governing permissions and limitations
-- under the License.
--- --- start person data ---
-insert into person (user_id) values (set(@user_id_1, 'canonical'));
-insert into person (user_id) values (set(@user_id_2, 'john.doe'));
-insert into person (user_id) values (set(@user_id_3, 'jane.doe'));
-insert into person (user_id) values (set(@user_id_4, 'george.doe'));
-insert into person (user_id) values (set(@user_id_5, 'mario.rossi'));
-insert into person (user_id) values (set(@user_id_6, 'maija.m'));
---- end person data ---
+-- --- start user data ---
+insert into user (user_id, username, password, expired, locked, enabled)
+values (set(@user_id_1, next value for user_id_seq), 'canonical', 'canonical', 'N', 'N', 'Y');
+
+insert into user (user_id, username, password, expired, locked, enabled)
+values (set(@user_id_2, next value for user_id_seq), 'john.doe', 'john.doe', 'N', 'N', 'Y');
+
+insert into user (user_id, username, password, expired, locked, enabled)
+values (set(@user_id_3, next value for user_id_seq), 'jane.doe', 'jane.doe', 'N', 'N', 'Y');
+
+insert into user (user_id, username, password, expired, locked, enabled)
+values (set(@user_id_4, next value for user_id_seq), 'george.doe', 'george.doe', 'N', 'N', 'Y');
+
+insert into user (user_id, username, password, expired, locked, enabled)
+values (set(@user_id_5, next value for user_id_seq),'mario.rossi', 'mario.rossi', 'N', 'N', 'Y');
+
+insert into user (user_id, username, password, expired, locked, enabled)
+values (set(@user_id_6, next value for user_id_seq), 'maija.m', 'maija.m', 'N', 'N', 'Y');
+
+--- end user data ---
--- gadget data ---
-- wikipedia widget
Added: incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext-security.xml
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext-security.xml?rev=1096643&view=auto
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext-security.xml (added)
+++ incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/applicationContext-security.xml Tue Apr 26 02:40:59 2011
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in
+ ~ compliance with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ 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.
+ -->
+
+<!--
+ This security file uses the default spring simple form login
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:security="http://www.springframework.org/schema/security"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
+
+ <security:http auto-config="true" use-expressions="true" disable-url-rewriting="true">
+ <!-- all urls must be authenticated -->
+ <security:intercept-url pattern="/**"
+ access="isAuthenticated()" />
+ </security:http>
+
+ <security:authentication-manager>
+ <security:authentication-provider
+ user-service-ref="userService" />
+ </security:authentication-manager>
+
+</beans>
\ No newline at end of file
Modified: incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/web.xml?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/web.xml (original)
+++ incubator/rave/trunk/rave-portal/src/main/webapp/WEB-INF/web.xml Tue Apr 26 02:40:59 2011
@@ -33,11 +33,20 @@
<param-value>
/WEB-INF/dataContext.xml
/WEB-INF/applicationContext.xml
+ /WEB-INF/applicationContext-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
+ <filter>
+ <filter-name>springSecurityFilterChain</filter-name>
+ <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
+ </filter>
+ <filter-mapping>
+ <filter-name>springSecurityFilterChain</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
<!-- Handles all requests into the application -->
<servlet>
Modified: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/InternationalStringTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/InternationalStringTest.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/InternationalStringTest.java (original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/model/util/InternationalStringTest.java Tue Apr 26 02:40:59 2011
@@ -18,10 +18,10 @@
*/
package org.apache.rave.portal.model.util;
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Test;
import com.ibm.icu.util.GlobalizationPreferences;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
public class InternationalStringTest {
Modified: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaPageRepositoryTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaPageRepositoryTest.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaPageRepositoryTest.java (original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaPageRepositoryTest.java Tue Apr 26 02:40:59 2011
@@ -21,21 +21,21 @@ package org.apache.rave.portal.repositor
import org.apache.rave.portal.model.Page;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
-import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
+
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/dataContext.xml", "file:src/main/webapp/WEB-INF/applicationContext.xml"})
public class JpaPageRepositoryTest {
- private static final String USER_ID = "canonical";
- private static final String INVALID_USER = "BadUserId";
+ private static final Long USER_ID = 1L;
+ private static final Long INVALID_USER = -1L;
private static final String WIDGET_URL = "http://www.google.com/ig/modules/wikipedia.xml";
@Autowired
Copied: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java (from r1096551, incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaPageRepositoryTest.java)
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java?p2=incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java&p1=incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaPageRepositoryTest.java&r1=1096551&r2=1096643&rev=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaPageRepositoryTest.java (original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java Tue Apr 26 02:40:59 2011
@@ -16,48 +16,60 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package org.apache.rave.portal.repository;
import org.apache.rave.portal.model.Page;
+import org.apache.rave.portal.model.User;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
-import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/dataContext.xml", "file:src/main/webapp/WEB-INF/applicationContext.xml"})
-public class JpaPageRepositoryTest {
+public class JpaUserRepositoryTest {
- private static final String USER_ID = "canonical";
- private static final String INVALID_USER = "BadUserId";
- private static final String WIDGET_URL = "http://www.google.com/ig/modules/wikipedia.xml";
+ private static final Long USER_ID = 1L;
+ private static final String USER_NAME = "canonical";
+ private static final Long INVALID_USER = -2L;
@Autowired
- private PageRepository repository;
+ private UserRepository repository;
@Test
- public void getAllPages_validUser_validPageSet() {
- List<Page> pages = repository.getAllPages(USER_ID);
- assertThat(pages, CoreMatchers.notNullValue());
- assertThat(pages.size(), CoreMatchers.equalTo(1));
- assertThat(pages.get(0).getRegions().size(), CoreMatchers.equalTo(2));
- assertThat(pages.get(0).getRegions().get(0).getRegionWidgets().size(), CoreMatchers.equalTo(2));
- assertThat(pages.get(0).getRegions().get(0).getRegionWidgets().get(0).getWidget().getUrl(), CoreMatchers.equalTo(WIDGET_URL));
+ public void getById_validId() {
+ User user = repository.getById(USER_ID);
+ assertThat(user, CoreMatchers.notNullValue());
+ assertThat(user.getUsername(), is(equalTo(USER_NAME)));
+ assertThat(user.getPassword(), is(equalTo(USER_NAME)));
+ assertThat(user.isAccountNonExpired(), is(true));
+ }
+ @Test
+ public void getById_invalidId() {
+ User user = repository.getById(INVALID_USER);
+ assertThat(user, is(nullValue()));
}
@Test
- public void getAllPages_invalidUser_emptySet() {
- List<Page> pages = repository.getAllPages(INVALID_USER);
- assertThat(pages.isEmpty(), CoreMatchers.is(true));
+ public void getByUsername_valid() {
+ User user = repository.getByUsername(USER_NAME);
+ assertThat(user, CoreMatchers.notNullValue());
+ assertThat(user.getUserId(), is(equalTo(USER_ID)));
+ assertThat(user.getPassword(), is(equalTo(USER_NAME)));
+ assertThat(user.isAccountNonExpired(), is(true));
}
@Test
- public void getAllPages_nullUser_emptySet() {
- List<Page> pages = repository.getAllPages(null);
- assertThat(pages.isEmpty(), CoreMatchers.is(true));
+ public void getByUsername_invalid() {
+ User user = repository.getById(INVALID_USER);
+ assertThat(user, is(nullValue()));
}
}
Modified: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/PageServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/PageServiceTest.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/PageServiceTest.java (original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/PageServiceTest.java Tue Apr 26 02:40:59 2011
@@ -44,7 +44,7 @@ public class PageServiceTest {
@Test
public void getAllPages() {
- final String VALID_USER_ID = "jcian";
+ final Long VALID_USER_ID = 1L;
final List<Page> VALID_PAGES = new ArrayList<Page>();
expect(pageRepository.getAllPages(VALID_USER_ID)).andReturn(VALID_PAGES);
Added: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/UserServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/UserServiceTest.java?rev=1096643&view=auto
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/UserServiceTest.java (added)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/service/UserServiceTest.java Tue Apr 26 02:40:59 2011
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.apache.rave.portal.service;
+
+import org.apache.rave.portal.model.User;
+import org.apache.rave.portal.repository.UserRepository;
+import org.apache.rave.portal.service.impl.DefaultUserService;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.dao.IncorrectResultSizeDataAccessException;
+import org.springframework.security.authentication.AbstractAuthenticationToken;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.context.SecurityContextImpl;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertThat;
+
+/**
+ * @author mfranklin
+ * Date: 4/25/11
+ * Time: 4:16 PM
+ */
+public class UserServiceTest {
+
+ private static final Long USER_ID = 1234L;
+ private UserService service;
+ private UserRepository repository;
+ private static final String USER_NAME = "1234";
+
+ @Before
+ public void setup() {
+ repository = createNiceMock(UserRepository.class);
+ service = new DefaultUserService(repository);
+ }
+
+ @After
+ public void tearDown() {
+ SecurityContextHolder.clearContext();
+ }
+
+ @Test
+ public void getAuthenticatedUser_validUser() {
+ final User authUser = new User(USER_ID);
+ AbstractAuthenticationToken auth = createNiceMock(AbstractAuthenticationToken.class);
+ expect(auth.getPrincipal()).andReturn(authUser).anyTimes();
+ replay(auth);
+
+ SecurityContext context = new SecurityContextImpl();
+ context.setAuthentication(auth);
+ SecurityContextHolder.setContext(context);
+
+ User result = service.getAuthenticatedUser();
+
+ assertThat(result, is(sameInstance(authUser)));
+
+ }
+
+ @Test(expected = SecurityException.class)
+ public void getAuthenticatedUser_nullAuth() {
+
+ SecurityContext context = new SecurityContextImpl();
+ SecurityContextHolder.setContext(context);
+ service.getAuthenticatedUser();
+ }
+
+ @Test(expected = SecurityException.class)
+ public void getAuthenticatedUser_wrongPrincipalType() {
+ AbstractAuthenticationToken auth = createNiceMock(AbstractAuthenticationToken.class);
+ expect(auth.getPrincipal()).andReturn(USER_ID).anyTimes();
+ replay(auth);
+
+ SecurityContext context = new SecurityContextImpl();
+ SecurityContextHolder.setContext(context);
+
+ service.getAuthenticatedUser();
+ }
+
+ @Test
+ public void setAuthenticatedUser_valid() {
+ final User authUser = new User(USER_ID);
+ expect(repository.getById(USER_ID)).andReturn(authUser).anyTimes();
+ replay(repository);
+
+ service.setAuthenticatedUser(USER_ID);
+ assertThat((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(),
+ is(sameInstance(authUser)));
+ }
+
+ @Test(expected = UsernameNotFoundException.class)
+ public void setAuthenticatedUser_invalid_null() {
+ final User authUser = new User(USER_ID);
+ expect(repository.getById(USER_ID)).andReturn(null).anyTimes();
+ replay(repository);
+
+ service.setAuthenticatedUser(USER_ID);
+ }
+
+ @Test
+ public void loadByUsername_valid() {
+ final User authUser = new User(USER_ID, USER_NAME);
+ expect(repository.getByUsername(USER_NAME)).andReturn(authUser).anyTimes();
+ replay(repository);
+
+ UserDetails result = service.loadUserByUsername(USER_NAME);
+ assertThat((User)result, is(sameInstance(authUser)));
+ }
+
+ @Test(expected = UsernameNotFoundException.class)
+ public void loadByUsername_invalid_exception() {
+ expect(repository.getByUsername(USER_NAME)).andThrow(new IncorrectResultSizeDataAccessException(1));
+ replay(repository);
+
+ service.setAuthenticatedUser(USER_ID);
+ }
+
+ @Test(expected = UsernameNotFoundException.class)
+ public void loadByUsername_invalid_null() {
+ final User authUser = new User(USER_ID);
+ expect(repository.getById(USER_ID)).andReturn(null).anyTimes();
+ replay(repository);
+
+ service.setAuthenticatedUser(USER_ID);
+ }
+
+ @Test
+ public void clearAuthentication() {
+ SecurityContext context = new SecurityContextImpl();
+ SecurityContextHolder.setContext(context);
+ service.clearAuthenticatedUser();
+ assertThat(SecurityContextHolder.getContext(), not(sameInstance(context)));
+ }
+
+
+}
Modified: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/HomeControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/HomeControllerTest.java?rev=1096643&r1=1096642&r2=1096643&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/HomeControllerTest.java (original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/controller/HomeControllerTest.java Tue Apr 26 02:40:59 2011
@@ -20,7 +20,7 @@
package org.apache.rave.portal.web.controller;
import org.apache.rave.portal.model.Page;
-import org.apache.rave.portal.model.Person;
+import org.apache.rave.portal.model.User;
import org.apache.rave.portal.service.PageService;
import org.apache.rave.portal.service.UserService;
import org.apache.rave.portal.web.util.ModelKeys;
@@ -50,12 +50,12 @@ public class HomeControllerTest {
@Test
public void getHome() {
- final String VALID_USER_ID = "jcian";
+ final Long VALID_USER_ID = 1L;
final String VALID_VIEW = "home";
final Model MODEL = new ExtendedModelMap();
final List<Page> VALID_PAGES = new ArrayList<Page>();
- expect(userService.getAuthenticatedUser()).andReturn(new Person(VALID_USER_ID));
+ expect(userService.getAuthenticatedUser()).andReturn(new User(VALID_USER_ID));
replay(userService);
expect(pageService.getAllPages(VALID_USER_ID)).andReturn(VALID_PAGES);