You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rave.apache.org by ja...@apache.org on 2011/10/05 13:30:48 UTC

svn commit: r1179170 - in /incubator/rave/trunk: rave-components/rave-core/src/main/java/org/apache/rave/portal/model/ rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/ rave-components/rave-core/src/main/java/org/apache/rave/po...

Author: jasha
Date: Wed Oct  5 11:30:47 2011
New Revision: 1179170

URL: http://svn.apache.org/viewvc?rev=1179170&view=rev
Log:
RAVE-232 first setup of storing GrantedAuthorities for Users

Added:
    incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Authority.java
    incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/AuthorityRepository.java
    incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaAuthorityRepository.java
    incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/model/DefaultGrantedAuthorityTest.java
    incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaAuthorityRepositoryTest.java
Modified:
    incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/User.java
    incubator/rave/trunk/rave-components/rave-core/src/main/resources/META-INF/persistence.xml
    incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java
    incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/UserServiceTest.java
    incubator/rave/trunk/rave-components/rave-core/src/test/resources/test_data.sql
    incubator/rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/db/initial_data.sql

Added: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Authority.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Authority.java?rev=1179170&view=auto
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Authority.java (added)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Authority.java Wed Oct  5 11:30:47 2011
@@ -0,0 +1,116 @@
+package org.apache.rave.portal.model;
+
+import org.apache.rave.persistence.BasicEntity;
+import org.springframework.security.core.GrantedAuthority;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.PreRemove;
+import javax.persistence.Table;
+import javax.persistence.TableGenerator;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * The {@link GrantedAuthority} a {@link User} can have
+ */
+@Entity
+@Table(name = "granted_authority")
+@NamedQueries({
+        @NamedQuery(name = Authority.GET_BY_AUTHORITY_NAME, query = "SELECT a FROM Authority a WHERE a.authority = :authority")
+})
+public class Authority implements GrantedAuthority, BasicEntity, Serializable {
+
+    private static final long serialVersionUID = -3946689281908099905L;
+
+    public static final String PARAM_AUTHORITY_NAME = "authority";
+    public static final String GET_BY_AUTHORITY_NAME = "Authority.GetByAuthorityName";
+
+    @Id
+    @Column(name = "entity_id")
+    @GeneratedValue(strategy = GenerationType.TABLE, generator = "grantedAuthorityIdGenerator")
+    @TableGenerator(name = "grantedAuthorityIdGenerator", table = "RAVE_PORTAL_SEQUENCES", pkColumnName = "SEQ_NAME",
+            valueColumnName = "SEQ_COUNT", pkColumnValue = "granted_authority", allocationSize = 1, initialValue = 1)
+    private Long entityId;
+
+    @Basic
+    @Column(name = "authority", unique = true)
+    private String authority;
+
+
+    @ManyToMany(mappedBy = "authorities", fetch = FetchType.LAZY)
+    private Collection<User> users;
+
+    /**
+     * Default constructor, needed for JPA
+     */
+    public Authority() {
+        this(null);
+    }
+
+    /**
+     * Utility constructor
+     *
+     * @param authority (unique) name of the authority (role)
+     */
+    public Authority(String authority) {
+        super();
+        this.authority = authority;
+        this.users = new ArrayList<User>();
+    }
+
+    @Override
+    public Long getEntityId() {
+        return entityId;
+    }
+
+    @Override
+    public void setEntityId(Long entityId) {
+        this.entityId = entityId;
+    }
+
+    @Override
+    public String getAuthority() {
+        return authority;
+    }
+
+    public void setAuthority(String authority) {
+        this.authority = authority;
+    }
+
+    public Collection<User> getUsers() {
+        return users;
+    }
+
+    public void addUser(User user) {
+        if (!users.contains(user)) {
+            users.add(user);
+        }
+        if (!user.getAuthorities().contains(this)) {
+            user.addAuthority(this);
+        }
+    }
+
+    public void removeUser(User user) {
+        if (users.contains(user)) {
+            users.remove(user);
+        }
+    }
+
+    @PreRemove
+    public void preRemove() {
+        for (User user : users) {
+            user.removeAuthority(this);
+        }
+        this.users = null;
+    }
+}

Modified: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/User.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/User.java?rev=1179170&r1=1179169&r2=1179170&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/User.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/User.java Wed Oct  5 11:30:47 2011
@@ -25,72 +25,97 @@ import org.springframework.security.core
 import javax.persistence.Basic;
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
+import javax.persistence.ManyToMany;
 import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
+import javax.persistence.PreRemove;
 import javax.persistence.Table;
 import javax.persistence.TableGenerator;
 import javax.persistence.Transient;
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.Collection;
 
 /**
  * {@inheritDoc}
- *
+ * <p/>
  * A user of the system
  */
 @Entity
 // user can be a restricted table name
 @Table(name = "raveuser")
 @NamedQueries({
-    @NamedQuery(name="User.getByUsername", query = "select u from User u where u.username = :username"),
-    @NamedQuery(name="User.getByUserEmail", query = "select u from User u where u.email = :email")
+        @NamedQuery(name = "User.getByUsername", query = "select u from User u where u.username = :username"),
+        @NamedQuery(name = "User.getByUserEmail", query = "select u from User u where u.email = :email")
 })
 public class User implements UserDetails, BasicEntity, Serializable {
     private static final long serialVersionUID = 1L;
-    
-    @Id @Column(name = "entity_id")
+
+    @Id
+    @Column(name = "entity_id")
     @GeneratedValue(strategy = GenerationType.TABLE, generator = "raveuserIdGenerator")
     @TableGenerator(name = "raveuserIdGenerator", table = "RAVE_PORTAL_SEQUENCES", pkColumnName = "SEQ_NAME",
             valueColumnName = "SEQ_COUNT", pkColumnValue = "raveuser", allocationSize = 1, initialValue = 1)
     private Long entityId;
 
-    @Basic @Column(name = "username", unique = true)
+    @Basic
+    @Column(name = "username", unique = true)
     private String username;
 
-    @Basic @Column(name = "password")
+    @Basic
+    @Column(name = "password")
     private String password;
 
-    @Basic @Column(name = "expired")
+    @Basic
+    @Column(name = "expired")
     private boolean expired;
 
-    @Basic @Column(name = "locked")
+    @Basic
+    @Column(name = "locked")
     private boolean locked;
 
-    @Basic @Column(name = "enabled")
+    @Basic
+    @Column(name = "enabled")
     private boolean enabled;
 
-    @Basic @Column(name="email", unique = true)
+    @Basic
+    @Column(name = "email", unique = true)
     private String email;
 
-    @Basic @Column(name="openid")
+    @Basic
+    @Column(name = "openid")
     private String openId;
 
     @Transient
     private String confirmPassword;
 
+    @ManyToMany(fetch = FetchType.EAGER)
+    @JoinTable(name = "user_authorities",
+            joinColumns =
+            @JoinColumn(name = "user_id", referencedColumnName = "entity_id"),
+            inverseJoinColumns =
+            @JoinColumn(name = "authority_id", referencedColumnName = "entity_id"))
+    private Collection<Authority> authorities;
+
     public User() {
+        this(null, null);
     }
 
     public User(Long entityId) {
-        this.entityId = entityId;
+        this(entityId, null);
     }
 
     public User(Long entityId, String username) {
+        super();
         this.entityId = entityId;
         this.username = username;
+        this.authorities = new ArrayList<Authority>();
     }
 
     /**
@@ -108,12 +133,29 @@ public class User implements UserDetails
         this.entityId = entityId;
     }
 
-    //TODO RAVE-232: Add GrantedAuthorities to user
     @Override
     public Collection<GrantedAuthority> getAuthorities() {
-        return null;
+        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
+        grantedAuthorities.addAll(authorities);
+        return grantedAuthorities;
+    }
+
+    public void addAuthority(Authority authority) {
+        if (!authorities.contains(authority)) {
+            authorities.add(authority);
+        }
+        if (!authority.getUsers().contains(this)) {
+            authority.addUser(this);
+        }
     }
 
+    public void removeAuthority(Authority authority) {
+        if (authorities.contains(authority)) {
+            authorities.remove(authority);
+        }
+    }
+
+
     //TODO RAVE-233:Setup Hashing and Salting of Passwords
 
     /**
@@ -179,35 +221,42 @@ public class User implements UserDetails
 
     //The following properties are specific to the user profile.
     public String getEmail() {
-          return email;
+        return email;
     }
 
     public void setEmail(String email) {
-          this.email=email;
+        this.email = email;
     }
 
     public String getOpenId() {
-          return openId;
+        return openId;
     }
 
-    public void setOpenId(String openId){
-          this.openId=openId;
+    public void setOpenId(String openId) {
+        this.openId = openId;
     }
 
     public String getConfirmPassword() {
-          //confirmPassword is not stored persistently, so if the value is not set,
-          //return the password instead. This will need to be as secure as the password
-          //field itself. 
-          if(confirmPassword!=null && confirmPassword.length()>0) {
-                        return confirmPassword;
-          }
-          else {
-                        return password;
-          }
+        //confirmPassword is not stored persistently, so if the value is not set,
+        //return the password instead. This will need to be as secure as the password
+        //field itself.
+        if (confirmPassword != null && confirmPassword.length() > 0) {
+            return confirmPassword;
+        } else {
+            return password;
+        }
     }
 
     public void setConfirmPassword(String confirmPassword) {
-          this.confirmPassword=confirmPassword;
+        this.confirmPassword = confirmPassword;
+    }
+
+    @PreRemove
+    public void preRemove() {
+        for(Authority authority : authorities) {
+            authority.removeUser(this);
+        }
+        this.authorities = null;
     }
 
     @Override

Added: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/AuthorityRepository.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/AuthorityRepository.java?rev=1179170&view=auto
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/AuthorityRepository.java (added)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/AuthorityRepository.java Wed Oct  5 11:30:47 2011
@@ -0,0 +1,18 @@
+package org.apache.rave.portal.repository;
+
+import org.apache.rave.persistence.Repository;
+import org.apache.rave.portal.model.Authority;
+
+/**
+ * Repository interface for {@link org.apache.rave.portal.model.Authority}
+ */
+public interface AuthorityRepository extends Repository<Authority> {
+
+    /**
+     * Finds the {@link Authority} by its name
+     *
+     * @param authorityName (unique) name of the Authority
+     * @return Authority if it can be found, otherwise {@literal null}
+     */
+    Authority getByAuthority(String authorityName);
+}

Added: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaAuthorityRepository.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaAuthorityRepository.java?rev=1179170&view=auto
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaAuthorityRepository.java (added)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaAuthorityRepository.java Wed Oct  5 11:30:47 2011
@@ -0,0 +1,29 @@
+package org.apache.rave.portal.repository.impl;
+
+import org.apache.rave.persistence.jpa.AbstractJpaRepository;
+import org.apache.rave.portal.model.Authority;
+import org.apache.rave.portal.repository.AuthorityRepository;
+import org.springframework.stereotype.Repository;
+
+import javax.persistence.TypedQuery;
+
+import static org.apache.rave.persistence.jpa.util.JpaUtil.getSingleResult;
+
+/**
+ * JPA implementation for {@link org.apache.rave.portal.repository.AuthorityRepository}
+ */
+@Repository
+public class JpaAuthorityRepository extends AbstractJpaRepository<Authority>
+        implements AuthorityRepository {
+
+    public JpaAuthorityRepository() {
+        super(Authority.class);
+    }
+
+    @Override
+    public Authority getByAuthority(String authorityName) {
+        TypedQuery<Authority> query = manager.createNamedQuery(Authority.GET_BY_AUTHORITY_NAME, Authority.class);
+        query.setParameter(Authority.PARAM_AUTHORITY_NAME, authorityName);
+        return getSingleResult(query.getResultList());
+    }
+}

Modified: incubator/rave/trunk/rave-components/rave-core/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/resources/META-INF/persistence.xml?rev=1179170&r1=1179169&r2=1179170&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/resources/META-INF/persistence.xml (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/resources/META-INF/persistence.xml Wed Oct  5 11:30:47 2011
@@ -30,5 +30,6 @@
             <class>org.apache.rave.portal.model.RegionWidgetPreference</class>
             <class>org.apache.rave.portal.model.Widget</class>
             <class>org.apache.rave.portal.model.PageLayout</class>
+            <class>org.apache.rave.portal.model.Authority</class>
         </persistence-unit>
 </persistence>

Added: incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/model/DefaultGrantedAuthorityTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/model/DefaultGrantedAuthorityTest.java?rev=1179170&view=auto
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/model/DefaultGrantedAuthorityTest.java (added)
+++ incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/model/DefaultGrantedAuthorityTest.java Wed Oct  5 11:30:47 2011
@@ -0,0 +1,22 @@
+package org.apache.rave.portal.model;
+
+import org.junit.Test;
+import org.springframework.security.core.GrantedAuthority;
+
+import static junit.framework.Assert.assertEquals;
+
+/**
+ * Test for {@link Authority}
+ */
+public class DefaultGrantedAuthorityTest {
+
+    @Test
+    public void testAuthority() throws Exception {
+        GrantedAuthority grantedAuthority = new Authority();
+        ((Authority) grantedAuthority).setAuthority("user");
+        assertEquals("user", grantedAuthority.getAuthority());
+
+        GrantedAuthority grantedAuthority2 = new Authority("admin");
+        assertEquals("admin", grantedAuthority2.getAuthority());
+    }
+}

Added: incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaAuthorityRepositoryTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaAuthorityRepositoryTest.java?rev=1179170&view=auto
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaAuthorityRepositoryTest.java (added)
+++ incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaAuthorityRepositoryTest.java Wed Oct  5 11:30:47 2011
@@ -0,0 +1,100 @@
+package org.apache.rave.portal.repository;
+
+import org.apache.rave.portal.model.Authority;
+import org.apache.rave.portal.model.User;
+import org.junit.Assert;
+import org.junit.Test;
+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 org.springframework.transaction.annotation.Transactional;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNull;
+import static junit.framework.Assert.assertTrue;
+
+/**
+ *
+ */
+@Transactional
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = {"classpath:test-dataContext.xml", "classpath:test-applicationContext.xml"})
+public class JpaAuthorityRepositoryTest {
+
+    @PersistenceContext
+    private EntityManager manager;
+
+    @Autowired
+    private AuthorityRepository repository;
+
+    @Autowired
+    private UserRepository userRepository;
+
+    private static final Long VALID_ID = 1L;
+
+    @Test
+    public void getById_validId() {
+        final Authority authority = repository.get(VALID_ID);
+        assertNotNull(authority);
+        assertEquals(VALID_ID, authority.getEntityId());
+    }
+
+    @Test
+    public void getByAuthorityName() {
+        String authorityName = "administrator";
+        Authority authority = repository.getByAuthority(authorityName);
+        assertNotNull(authority);
+        assertEquals(authorityName, authority.getAuthority());
+        assertTrue(authority.getUsers().isEmpty());
+    }
+
+    @Test
+    public void getUsersByAuthorityName() {
+        String authorityName = "administrator";
+        Authority authority = repository.getByAuthority(authorityName);
+        assertNotNull(authority);
+        assertEquals(authorityName, authority.getAuthority());
+        assertTrue(authority.getUsers().isEmpty());
+
+        User newUser = new User();
+        newUser.setUsername("adminuser");
+        newUser.addAuthority(authority);
+        newUser = userRepository.save(newUser);
+        assertEquals(authority, newUser.getAuthorities().iterator().next());
+
+        authority = repository.getByAuthority(authorityName);
+        assertEquals(1, authority.getUsers().size());
+    }
+
+    @Test
+    public void addOrDeleteAuthorityDoesNotAffectUser() {
+        final String authorityName = "guest";
+        Authority authority = new Authority(authorityName);
+        User user = userRepository.get(1L);
+
+        Assert.assertNotNull("User is not null", user);
+        Assert.assertTrue("User has no authorities", user.getAuthorities().isEmpty());
+        assertNull("No authority guest", repository.getByAuthority(authorityName));
+
+        user.addAuthority(authority);
+        user = userRepository.save(user);
+
+        assertNull("Persisting a user does not persist an unknown Authority", repository.getByAuthority(authorityName));
+        repository.save(authority);
+
+        Assert.assertEquals("Found authority", authorityName, user.getAuthorities().iterator().next().getAuthority());
+        Assert.assertNotNull("New authority: guest", authority);
+
+        repository.delete(authority);
+        assertNull("No authority guest", repository.getByAuthority(authorityName));
+
+        user = userRepository.get(1L);
+        Assert.assertNotNull("User should not be deleted after removing an authority", user);
+        Assert.assertTrue("User should have no authorities", user.getAuthorities().isEmpty());
+    }
+}

Modified: incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java?rev=1179170&r1=1179169&r2=1179170&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaUserRepositoryTest.java Wed Oct  5 11:30:47 2011
@@ -19,6 +19,8 @@
 
 package org.apache.rave.portal.repository;
 
+import junit.framework.Assert;
+import org.apache.rave.portal.model.Authority;
 import org.apache.rave.portal.model.User;
 import org.hamcrest.CoreMatchers;
 import org.junit.Test;
@@ -31,6 +33,7 @@ import org.springframework.transaction.a
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 
+import static junit.framework.Assert.assertNull;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
@@ -48,13 +51,16 @@ public class JpaUserRepositoryTest {
     private static final String USER_NAME = "canonical";
     //The password value depends on the hash algorithm and salt used, so this
     //may need updating in the future.
-    private static final String HASHED_SALTED_PASSWORD="b97fd0fa25ba8a504309be2b6651ac6dee167ded";
+    private static final String HASHED_SALTED_PASSWORD = "b97fd0fa25ba8a504309be2b6651ac6dee167ded";
     private static final Long INVALID_USER = -2L;
-	 private static final String USER_EMAIL = "canonical@example.com";
+    private static final String USER_EMAIL = "canonical@example.com";
 
     @Autowired
     private UserRepository repository;
 
+    @Autowired
+    private AuthorityRepository authorityRepository;
+
     @Test
     public void getById_validId() {
         User user = repository.get(USER_ID);
@@ -62,7 +68,7 @@ public class JpaUserRepositoryTest {
         assertThat(user.getUsername(), is(equalTo(USER_NAME)));
         assertThat(user.getPassword(), is(equalTo(HASHED_SALTED_PASSWORD)));
         assertThat(user.isAccountNonExpired(), is(true));
-		  assertThat(user.getEmail(), is(equalTo(USER_EMAIL)));
+        assertThat(user.getEmail(), is(equalTo(USER_EMAIL)));
     }
 
     @Test
@@ -78,7 +84,7 @@ public class JpaUserRepositoryTest {
         assertThat(user.getEntityId(), is(equalTo(USER_ID)));
         assertThat(user.getPassword(), is(equalTo(HASHED_SALTED_PASSWORD)));
         assertThat(user.isAccountNonExpired(), is(true));
-		  assertThat(user.getEmail(), is(equalTo(USER_EMAIL)));
+        assertThat(user.getEmail(), is(equalTo(USER_EMAIL)));
     }
 
     @Test
@@ -94,6 +100,30 @@ public class JpaUserRepositoryTest {
         assertThat(user.getEntityId(), is(equalTo(USER_ID)));
         assertThat(user.getPassword(), is(equalTo(HASHED_SALTED_PASSWORD)));
         assertThat(user.isAccountNonExpired(), is(true));
-		  assertThat(user.getEmail(), is(equalTo(USER_EMAIL)));
+        assertThat(user.getEmail(), is(equalTo(USER_EMAIL)));
+    }
+
+    @Test
+    public void addOrDeleteUserDoesNotAffectAuthority() {
+        Authority authority = authorityRepository.get(1L);
+        Assert.assertNotNull("Existing authority", authority);
+
+        int usercount = authority.getUsers().size();
+        User user = new User();
+        user.setUsername("dummy");
+        authority.addUser(user);
+        authorityRepository.save(authority);
+        assertNull("Persisting an Authority does not persist an unknown user", repository.getByUsername("dummy"));
+        Assert.assertEquals("Authority has 1 more user", usercount + 1, authority.getUsers().size());
+
+        repository.save(user);
+        user = repository.getByUsername("dummy");
+        Assert.assertNotNull(user);
+        Assert.assertEquals("Authority has 1 more user", usercount + 1, authority.getUsers().size());
+
+        repository.delete(user);
+        authority = authorityRepository.get(1L);
+        Assert.assertNotNull("Authority has not been removed after deleting user", authority);
+        Assert.assertEquals("Authority has original amount of users", usercount, authority.getUsers().size());
     }
 }

Modified: incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/UserServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/UserServiceTest.java?rev=1179170&r1=1179169&r2=1179170&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/UserServiceTest.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/UserServiceTest.java Wed Oct  5 11:30:47 2011
@@ -19,6 +19,7 @@
 
 package org.apache.rave.portal.service;
 
+import org.apache.rave.portal.model.Authority;
 import org.apache.rave.portal.model.User;
 import org.apache.rave.portal.repository.UserRepository;
 import org.apache.rave.portal.service.impl.DefaultUserService;
@@ -27,15 +28,20 @@ import org.junit.Before;
 import org.junit.Test;
 import org.springframework.dao.IncorrectResultSizeDataAccessException;
 import org.springframework.security.authentication.AbstractAuthenticationToken;
+import org.springframework.security.core.GrantedAuthority;
 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.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
 
 public class UserServiceTest {
@@ -104,6 +110,23 @@ public class UserServiceTest {
                 is(sameInstance(authUser)));
     }
 
+    @Test
+    public void setAuthenticatedUser_validRole() {
+        final User authUser = new User(USER_ID);
+        final Authority userRole = new Authority("admin");
+        authUser.addAuthority(userRole);
+        expect(repository.get(USER_ID)).andReturn(authUser).anyTimes();
+        replay(repository);
+
+        service.setAuthenticatedUser(USER_ID);
+        assertThat((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(),
+                is(sameInstance(authUser)));
+        final GrantedAuthority grantedAuthority =
+                SecurityContextHolder.getContext().getAuthentication().getAuthorities().iterator().next();
+        assertEquals("has authority admin", "admin", grantedAuthority.getAuthority());
+    }
+
+
     @Test(expected = UsernameNotFoundException.class)
     public void setAuthenticatedUser_invalid_null() {
         final User authUser = new User(USER_ID);

Modified: incubator/rave/trunk/rave-components/rave-core/src/test/resources/test_data.sql
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/test/resources/test_data.sql?rev=1179170&r1=1179169&r2=1179170&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/test/resources/test_data.sql (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/test/resources/test_data.sql Wed Oct  5 11:30:47 2011
@@ -30,6 +30,7 @@ set @region_seq = 'region';
 set @region_widget_seq = 'region_widget';
 set @user_seq = 'raveuser';
 set @widget_seq = 'widget';
+set @granted_authority_seq = 'granted_authority';
 
 CREATE TABLE IF NOT EXISTS RAVE_PORTAL_SEQUENCES (seq_name VARCHAR(255) PRIMARY KEY NOT NULL, seq_count BIGINT(19));
 INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values (@page_seq, 1);
@@ -39,6 +40,7 @@ INSERT INTO RAVE_PORTAL_SEQUENCES(seq_na
 INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values ('region_widget_preference', 1);
 INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values (@user_seq, 1);
 INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values (@widget_seq, 1);
+INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values (@granted_authority_seq, 1);
 
   -- ***********************************************************************************
   -- start page layout data, required to make the portal work ---
@@ -723,4 +725,22 @@ set @widget_id = (SELECT seq_count FROM 
 insert into widget (entity_id,title, url, type, widget_status)
 values(@widget_id,'Useless Knowledge', 'http://www.great-goofy-gadgets.com/humor/uselessknowledge/uselessknowledge.xml', 'OpenSocial', 'PREVIEW');
 UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @widget_seq;
--- end widget data ----
\ No newline at end of file
+-- end widget data ----
+
+-- authorities
+set @next_authority_id = (SELECT seq_count FROM RAVE_PORTAL_SEQUENCES WHERE seq_name = @granted_authority_seq);
+insert into granted_authority (entity_id, authority)
+values (@next_authority_id, 'user');
+UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @granted_authority_seq;
+
+set @next_authority_id = (SELECT seq_count FROM RAVE_PORTAL_SEQUENCES WHERE seq_name = @granted_authority_seq);
+insert into granted_authority (entity_id, authority)
+values (@next_authority_id, 'manager');
+UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @granted_authority_seq;
+
+set @next_authority_id = (SELECT seq_count FROM RAVE_PORTAL_SEQUENCES WHERE seq_name = @granted_authority_seq);
+insert into granted_authority (entity_id, authority)
+values (@next_authority_id, 'administrator');
+UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @granted_authority_seq;
+
+-- end authorities
\ No newline at end of file

Modified: incubator/rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/db/initial_data.sql
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/db/initial_data.sql?rev=1179170&r1=1179169&r2=1179170&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/db/initial_data.sql (original)
+++ incubator/rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/db/initial_data.sql Wed Oct  5 11:30:47 2011
@@ -30,6 +30,7 @@ set @region_seq = 'region';
 set @region_widget_seq = 'region_widget';
 set @user_seq = 'raveuser';
 set @widget_seq = 'widget';
+set @granted_authority_seq = 'granted_authority';
 
 CREATE TABLE IF NOT EXISTS RAVE_PORTAL_SEQUENCES (seq_name VARCHAR(255) PRIMARY KEY NOT NULL, seq_count BIGINT(19));
 INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values (@page_seq, 1);
@@ -39,7 +40,7 @@ INSERT INTO RAVE_PORTAL_SEQUENCES(seq_na
 INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values ('region_widget_preference', 1);
 INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values (@user_seq, 1);
 INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values (@widget_seq, 1);
-
+INSERT INTO RAVE_PORTAL_SEQUENCES(seq_name, seq_count) values (@granted_authority_seq, 1);
   -- ***********************************************************************************
   -- start page layout data, required to make the portal work ---
 set @one_col_id = (SELECT seq_count FROM RAVE_PORTAL_SEQUENCES WHERE seq_name = @page_layout_seq);
@@ -714,4 +715,22 @@ set @next_region_widget = (SELECT seq_co
 INSERT INTO region_widget(entity_id, widget_id, region_id, render_order, collapsed)
 values (@next_region_widget, @translate_widget_id, @page_13_region_1, 1, FALSE);
 UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @region_widget_seq;
---- End openid user_id_13 layout ---
\ No newline at end of file
+--- End openid user_id_13 layout ---
+
+-- authorities
+set @next_authority_id = (SELECT seq_count FROM RAVE_PORTAL_SEQUENCES WHERE seq_name = @granted_authority_seq);
+insert into granted_authority (entity_id, authority)
+values (@next_authority_id, 'user');
+UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @granted_authority_seq;
+
+set @next_authority_id = (SELECT seq_count FROM RAVE_PORTAL_SEQUENCES WHERE seq_name = @granted_authority_seq);
+insert into granted_authority (entity_id, authority)
+values (@next_authority_id, 'manager');
+UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @granted_authority_seq;
+
+set @next_authority_id = (SELECT seq_count FROM RAVE_PORTAL_SEQUENCES WHERE seq_name = @granted_authority_seq);
+insert into granted_authority (entity_id, authority)
+values (@next_authority_id, 'administrator');
+UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @granted_authority_seq;
+
+-- end authorities
\ No newline at end of file