You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rave.apache.org by ca...@apache.org on 2011/10/28 00:01:54 UTC
svn commit: r1190055 - 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: carlucci
Date: Thu Oct 27 22:01:53 2011
New Revision: 1190055
URL: http://svn.apache.org/viewvc?rev=1190055&view=rev
Log:
RAVE-321: add ROLE_USER authority to new users when they register
Modified:
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/main/java/org/apache/rave/portal/service/AuthorityService.java
incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultAuthorityService.java
incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultNewAccountService.java
incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaAuthorityRepositoryTest.java
incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultAuthorityServiceTest.java
incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultNewAccountServiceTest.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
Modified: 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=1190055&r1=1190054&r2=1190055&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Authority.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Authority.java Thu Oct 27 22:01:53 2011
@@ -47,6 +47,7 @@ import java.util.Collection;
@NamedQueries({
@NamedQuery(name = Authority.GET_BY_AUTHORITY_NAME, query = "SELECT a FROM Authority a WHERE a.authority = :authority"),
@NamedQuery(name = Authority.GET_ALL, query = "SELECT a FROM Authority a"),
+ @NamedQuery(name = Authority.GET_ALL_DEFAULT, query = "SELECT a FROM Authority a WHERE a.defaultForNewUser = true"),
@NamedQuery(name = Authority.COUNT_ALL, query = "SELECT COUNT(a) FROM Authority a")
})
public class Authority implements GrantedAuthority, BasicEntity, Serializable {
@@ -56,6 +57,7 @@ public class Authority implements Grante
public static final String PARAM_AUTHORITY_NAME = "authority";
public static final String GET_BY_AUTHORITY_NAME = "Authority.GetByAuthorityName";
public static final String GET_ALL = "Authority.GetAll";
+ public static final String GET_ALL_DEFAULT = "Authority.GetAllDefault";
public static final String COUNT_ALL = "Authority.CountAll";
@Id
@@ -69,9 +71,12 @@ public class Authority implements Grante
@Column(name = "authority", unique = true)
private String authority;
-
@ManyToMany(mappedBy = "authorities", fetch = FetchType.LAZY)
private Collection<User> users;
+
+ @Basic
+ @Column(name = "default_for_new_user")
+ private boolean defaultForNewUser;
/**
* Default constructor, needed for JPA
@@ -98,6 +103,14 @@ public class Authority implements Grante
public void setAuthority(String authority) {
this.authority = authority;
}
+
+ public boolean isDefaultForNewUser() {
+ return defaultForNewUser;
+ }
+
+ public void setDefaultForNewUser(boolean defaultForNewUser) {
+ this.defaultForNewUser = defaultForNewUser;
+ }
public Collection<User> getUsers() {
return users;
@@ -149,5 +162,4 @@ public class Authority implements Grante
public int hashCode() {
return entityId != null ? entityId.hashCode() : 0;
}
-
}
Modified: 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=1190055&r1=1190054&r2=1190055&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/AuthorityRepository.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/AuthorityRepository.java Thu Oct 27 22:01:53 2011
@@ -41,6 +41,11 @@ public interface AuthorityRepository ext
* @return a List of all {@link Authority}'s.
*/
List<Authority> getAll();
+
+ /**
+ * @return a List of all default {@link Authority}'s.
+ */
+ List<Authority> getAllDefault();
/**
* @return the total number of authorities in the repository
Modified: 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=1190055&r1=1190054&r2=1190055&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaAuthorityRepository.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaAuthorityRepository.java Thu Oct 27 22:01:53 2011
@@ -53,6 +53,12 @@ public class JpaAuthorityRepository exte
TypedQuery<Authority> query = manager.createNamedQuery(Authority.GET_ALL, Authority.class);
return query.getResultList();
}
+
+ @Override
+ public List<Authority> getAllDefault() {
+ TypedQuery<Authority> query = manager.createNamedQuery(Authority.GET_ALL_DEFAULT, Authority.class);
+ return query.getResultList();
+ }
@Override
public int getCountAll() {
Modified: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/AuthorityService.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/AuthorityService.java?rev=1190055&r1=1190054&r2=1190055&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/AuthorityService.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/AuthorityService.java Thu Oct 27 22:01:53 2011
@@ -40,4 +40,10 @@ public interface AuthorityService {
* @return a {@link SearchResult} with all {@link Authority}'s
*/
SearchResult<Authority> getAllAuthorities();
+
+ /**
+ * @return a {@link SearchResult} with the list of all default
+ * {@link Authority}'s
+ */
+ SearchResult<Authority> getDefaultAuthorities();
}
Modified: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultAuthorityService.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultAuthorityService.java?rev=1190055&r1=1190054&r2=1190055&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultAuthorityService.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultAuthorityService.java Thu Oct 27 22:01:53 2011
@@ -55,4 +55,10 @@ public class DefaultAuthorityService imp
final List<Authority> authorities = repository.getAll();
return new SearchResult<Authority>(authorities, count);
}
+
+ @Override
+ public SearchResult<Authority> getDefaultAuthorities() {
+ final List<Authority> authorities = repository.getAllDefault();
+ return new SearchResult<Authority>(authorities, authorities.size());
+ }
}
Modified: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultNewAccountService.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultNewAccountService.java?rev=1190055&r1=1190054&r2=1190055&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultNewAccountService.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultNewAccountService.java Thu Oct 27 22:01:53 2011
@@ -21,6 +21,7 @@ package org.apache.rave.portal.service.i
import org.apache.rave.portal.model.NewUser;
import org.apache.rave.portal.model.User;
+import org.apache.rave.portal.service.AuthorityService;
import org.apache.rave.portal.service.NewAccountService;
import org.apache.rave.portal.service.PageLayoutService;
import org.apache.rave.portal.service.PageService;
@@ -43,6 +44,7 @@ public class DefaultNewAccountService im
// TODO RAVE-236 why are these unused PageLayoutService and RegionService declared?
private final PageLayoutService pageLayoutService;
private final RegionService regionService;
+ private final AuthorityService authorityService;
@Autowired
private SaltSource saltSource;
@@ -51,11 +53,16 @@ public class DefaultNewAccountService im
private PasswordEncoder passwordEncoder;
@Autowired
- public DefaultNewAccountService(UserService userService, PageService pageService, PageLayoutService pageLayoutService, RegionService regionService) {
+ public DefaultNewAccountService(UserService userService,
+ PageService pageService,
+ PageLayoutService pageLayoutService,
+ RegionService regionService,
+ AuthorityService authorityService) {
this.userService = userService;
this.pageService = pageService;
this.pageLayoutService = pageLayoutService;
this.regionService = regionService;
+ this.authorityService = authorityService;
}
@Override
@@ -80,7 +87,8 @@ public class DefaultNewAccountService im
user.setExpired(false);
user.setLocked(false);
user.setEnabled(true);
- user.setDefaultPageLayout(pageLayoutService.getPageLayoutByCode(defaultPageLayoutCode));
+ user.setDefaultPageLayout(pageLayoutService.getPageLayoutByCode(defaultPageLayoutCode));
+ user.setAuthorities(authorityService.getDefaultAuthorities().getResultSet());
userService.registerNewUser(user);
}
Modified: 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=1190055&r1=1190054&r2=1190055&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaAuthorityRepositoryTest.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/repository/JpaAuthorityRepositoryTest.java Thu Oct 27 22:01:53 2011
@@ -33,11 +33,8 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertFalse;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertNull;
-import static junit.framework.Assert.assertTrue;
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
/**
*
@@ -125,6 +122,15 @@ public class JpaAuthorityRepositoryTest
List<Authority> allAuthorities = repository.getAll();
assertFalse("Found authorities", allAuthorities.isEmpty());
}
+
+ @Test
+ public void getAllDefault() {
+ List<Authority> allAuthorities = repository.getAllDefault();
+ assertThat(allAuthorities.isEmpty(), is(false));
+ for (Authority authority : allAuthorities) {
+ assertThat(authority.isDefaultForNewUser(), is(true));
+ }
+ }
@Test
public void countAll() {
Modified: incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultAuthorityServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultAuthorityServiceTest.java?rev=1190055&r1=1190054&r2=1190055&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultAuthorityServiceTest.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultAuthorityServiceTest.java Thu Oct 27 22:01:53 2011
@@ -30,12 +30,10 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.rave.portal.service.AuthorityService;
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNull;
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
+import static org.easymock.EasyMock.*;
+
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
/**
Test for {@link org.apache.rave.portal.service.impl.DefaultAuthorityService}
@@ -118,4 +116,26 @@ public class DefaultAuthorityServiceTest
assertEquals(2, allAuthorities.getTotalResults());
assertEquals(2, allAuthorities.getResultSet().size());
}
+
+ @Test
+ public void test_getAllDefault() {
+ List<Authority> authorities = new ArrayList<Authority>();
+ Authority foo = new Authority();
+ foo.setAuthority("FOO");
+ foo.setDefaultForNewUser(true);
+ Authority bar = new Authority();
+ bar.setAuthority("BAR");
+ bar.setDefaultForNewUser(true);
+
+ authorities.add(foo);
+ authorities.add(bar);
+
+ expect(repository.getAllDefault()).andReturn(authorities);
+ replay(repository);
+ SearchResult<Authority> defaultAuthorities = service.getDefaultAuthorities();
+ verify(repository);
+
+ assertThat(defaultAuthorities.getTotalResults(), is(2));
+ assertThat(defaultAuthorities.getResultSet().size(), is(2));
+ }
}
Modified: incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultNewAccountServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultNewAccountServiceTest.java?rev=1190055&r1=1190054&r2=1190055&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultNewAccountServiceTest.java (original)
+++ incubator/rave/trunk/rave-components/rave-core/src/test/java/org/apache/rave/portal/service/impl/DefaultNewAccountServiceTest.java Thu Oct 27 22:01:53 2011
@@ -19,6 +19,12 @@
package org.apache.rave.portal.service.impl;
+import org.springframework.security.core.GrantedAuthority;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.rave.portal.model.util.SearchResult;
+import org.apache.rave.portal.model.Authority;
+import org.apache.rave.portal.service.AuthorityService;
import org.apache.rave.portal.model.NewUser;
import org.apache.rave.portal.model.PageLayout;
import org.apache.rave.portal.model.User;
@@ -48,6 +54,7 @@ public class DefaultNewAccountServiceTes
private PageService pageService;
private RegionService regionService;
private NewAccountService newAccountService;
+ private AuthorityService authorityService;
private SaltSource saltSource;
private UserDetails userDetails;
private PasswordEncoder passwordEncoder;
@@ -57,6 +64,8 @@ public class DefaultNewAccountServiceTes
private final String VALID_LAYOUT_CODE = "valid.layout";
private final String VALID_EMAIL = "valid.email";
private PageLayout validPageLayout;
+ private SearchResult<Authority> validAuthoritySearchResult;
+ private List<Authority> validAuthorityList;
private final Logger logger = LoggerFactory.getLogger(DefaultNewAccountServiceTest.class);
@@ -69,13 +78,24 @@ public class DefaultNewAccountServiceTes
saltSource = createMock(SaltSource.class);
userDetails = createMock(UserDetails.class);
passwordEncoder = createMock(PasswordEncoder.class);
+ authorityService = createMock(AuthorityService.class);
- newAccountService = new DefaultNewAccountService(userService, pageService, pageLayoutService, regionService);
+ newAccountService = new DefaultNewAccountService(userService, pageService, pageLayoutService, regionService, authorityService);
validPageLayout = new PageLayout();
validPageLayout.setEntityId(99L);
validPageLayout.setNumberOfRegions(4L);
validPageLayout.setCode(VALID_LAYOUT_CODE);
+
+ Authority role1 = new Authority();
+ role1.setAuthority("DEFAULT_ROLE1");
+ Authority role2 = new Authority();
+ role2.setAuthority("DEFAULT_ROLE2");
+
+ validAuthorityList = new ArrayList<Authority>();
+ validAuthorityList.add(role1);
+ validAuthorityList.add(role2);
+ validAuthoritySearchResult = new SearchResult<Authority>(validAuthorityList, validAuthorityList.size());
}
@Test
@@ -104,9 +124,10 @@ public class DefaultNewAccountServiceTes
expect(userService.getUserByUsername(VALID_USER)).andReturn(null);
expect(userService.getUserByEmail(VALID_EMAIL)).andReturn(null);
expect(pageLayoutService.getPageLayoutByCode(VALID_LAYOUT_CODE)).andReturn(validPageLayout);
+ expect(authorityService.getDefaultAuthorities()).andReturn(validAuthoritySearchResult);
userService.registerNewUser(expectedUser);
expectLastCall();
- replay(saltSource, userDetails, passwordEncoder, userService, pageLayoutService);
+ replay(saltSource, userDetails, passwordEncoder, userService, pageLayoutService, authorityService);
newAccountService.createNewAccount(newUser);
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=1190055&r1=1190054&r2=1190055&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 Thu Oct 27 22:01:53 2011
@@ -739,18 +739,18 @@ UPDATE RAVE_PORTAL_SEQUENCES SET seq_cou
-- 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');
+insert into granted_authority (entity_id, authority, default_for_new_user)
+values (@next_authority_id, 'user', true);
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');
+insert into granted_authority (entity_id, authority, default_for_new_user)
+values (@next_authority_id, 'manager', false);
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');
+insert into granted_authority (entity_id, authority, default_for_new_user)
+values (@next_authority_id, 'administrator', false);
UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @granted_authority_seq;
-- end authorities
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=1190055&r1=1190054&r2=1190055&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 Thu Oct 27 22:01:53 2011
@@ -729,13 +729,13 @@ UPDATE RAVE_PORTAL_SEQUENCES SET seq_cou
-- authorities
set @user_authority_id = (SELECT seq_count FROM RAVE_PORTAL_SEQUENCES WHERE seq_name = @granted_authority_seq);
-insert into granted_authority (entity_id, authority)
-values (@user_authority_id, 'ROLE_USER');
+insert into granted_authority (entity_id, authority, default_for_new_user)
+values (@user_authority_id, 'ROLE_USER', true);
UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @granted_authority_seq;
set @admin_authority_id = (SELECT seq_count FROM RAVE_PORTAL_SEQUENCES WHERE seq_name = @granted_authority_seq);
-insert into granted_authority (entity_id, authority)
-values (@admin_authority_id, 'ROLE_ADMIN');
+insert into granted_authority (entity_id, authority, default_for_new_user)
+values (@admin_authority_id, 'ROLE_ADMIN', false);
UPDATE RAVE_PORTAL_SEQUENCES SET seq_count = (seq_count + 1) WHERE seq_name = @granted_authority_seq;
-- end authorities