Skip to content

Commit

Permalink
geoserver#128: jdk11: ldap refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
etj committed Jan 17, 2020
1 parent c00fbe4 commit 718d470
Show file tree
Hide file tree
Showing 27 changed files with 431 additions and 375 deletions.
2 changes: 1 addition & 1 deletion src/services/core/persistence/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.26</version>
<version>1.7.30</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* @author Emanuele Tajariol (etj at geo-solutions.it)
*/

public interface AdminRuleDAO extends PrioritizableDAO<AdminRule> {
public interface AdminRuleDAO //
extends PrioritizableDAO<AdminRule>, SearchableDAO<AdminRule> {

long persist(AdminRule entity, InsertPosition position);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
* @author Emanuele Tajariol (etj at geo-solutions.it)
*/

public interface GFUserDAO extends RestrictedGenericDAO<GFUser> {
public interface GFUserDAO //
extends RestrictedGenericDAO<GFUser>, SearchableDAO<GFUser> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
* @author Emanuele Tajariol (etj at geo-solutions.it)
*/

public interface GSInstanceDAO extends RestrictedGenericDAO<GSInstance> {
public interface GSInstanceDAO //
extends RestrictedGenericDAO<GSInstance>, SearchableDAO<GSInstance> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.geoserver.geofence.core.dao;

import java.util.List;
import org.geoserver.geofence.core.model.GSUser;

/**
Expand All @@ -17,4 +18,7 @@ public interface GSUserDAO extends RestrictedGenericDAO<GSUser>, RegistrableDAO

/** Fetch a GSUser with all of its related groups */
GSUser getFull(String name);

List<GSUser> search(String nameLike, Integer page, Integer entries, boolean fetchGroups) throws IllegalArgumentException;
long countByNameLike(String nameLike);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@
*/

public interface RestrictedGenericDAO<ENTITY> /* extends GenericDAO<ENTITY, Long> */{

public Search createSearch();
public Search createCountSearch();

public List<ENTITY> findAll();
public ENTITY find(Long id);
public void persist(ENTITY... entities);
public ENTITY merge(ENTITY entity);
public boolean remove(ENTITY entity);
public boolean removeById(Long id);
public List<ENTITY> search(Search search);
public long count(Search search);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* @author Emanuele Tajariol (etj at geo-solutions.it)
*/

public interface RuleDAO extends PrioritizableDAO<Rule> {
public interface RuleDAO //
extends PrioritizableDAO<Rule>, SearchableDAO<Rule> {

long persist(Rule entity, InsertPosition position);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* (c) 2020 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.geofence.core.dao;

import java.util.List;
import org.geoserver.geofence.core.dao.search.Search;

/**
* @author Emanuele Tajariol (etj at geo-solutions.it)
*/
public interface SearchableDAO<ENTITY> {

public Search createSearch();
public Search createCountSearch();

public List<ENTITY> search(Search search);
public long count(Search search);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.geoserver.geofence.core.dao;

import java.util.List;
import org.geoserver.geofence.core.model.UserGroup;

/**
Expand All @@ -13,5 +14,11 @@
* @author Emanuele Tajariol (etj at geo-solutions.it)
*/

public interface UserGroupDAO extends RestrictedGenericDAO<UserGroup>, RegistrableDAO {
public interface UserGroupDAO //
extends RestrictedGenericDAO<UserGroup>, RegistrableDAO {

UserGroup get(String name);

List<UserGroup> search(String nameLike, Integer page, Integer entries) throws IllegalArgumentException;
long countByNameLike(String nameLike);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,38 +66,17 @@ public GSUser getFull(String name) {
*/
protected GSUser searchFull(Search search) {
search.addFetch("userGroups");
// When fetching users with multiple groups, the gsusers list id multiplied for the number of groups found
search.setDistinct(true);
List<GSUser> users = super.search(search);

// When fetching users with multiple groups, the gsusers list id multiplied for the number of groups found.
// Next there is a workaround to this problem; maybe this:
// search.setDistinct(true);
// Dunno if some annotations in the GSUser definition are wrong, some deeper checks have to be performed.


switch(users.size()) {
case 0:
return null;
case 1:
return users.get(0);
default:
// if(users.size() == users.get(0).getGroups().size()) { // normal hibernate behaviour
// if(LOGGER.isDebugEnabled()) { // perform some more consistency tests only when debugging
// for (GSUser user : users) {
// if(user.getId() != users.get(0).getId() ||
// user.getGroups().size() != users.get(0).getGroups().size()) {
// LOGGER.error("Inconsistent userlist " + user);
// }
// }
// }
//
// return users.get(0);
// } else {
// LOGGER.error("Too many users in unique search " + search);
// for (GSUser user : users) {
// LOGGER.error(" " + user + " grp:"+user.getGroups().size());
// }
throw new IllegalStateException("Found more than one user (search:"+search+")");
// }
throw new IllegalStateException("Found more than one user (search:"+search+")");
}
}

Expand All @@ -120,4 +99,44 @@ public boolean removeById(Long id)
return super.removeById(id);
}

@Override
public List<GSUser> search(String nameLike, Integer page, Integer entries, boolean fetchGroups) throws IllegalArgumentException {

if( (page != null && entries == null) || (page ==null && entries != null)) {
throw new IllegalArgumentException("Page and entries params should be declared together.");
}

Search search = createSearch();

if(page != null) {
search.setMaxResults(entries);
search.setPage(page);
}

if(fetchGroups) {
search.addFetch("userGroups");
search.setDistinct(true);
}

search.addSortAsc("name");

if (nameLike != null) {
search.addFilterILike("name", nameLike);
}

return search(search);
}


@Override
public long countByNameLike(String nameLike) {
Search searchCriteria = createCountSearch();

if (nameLike != null) {
searchCriteria.addFilterILike("name", nameLike);
}

return count(searchCriteria);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/

package org.geoserver.geofence.core.dao.impl;

import java.util.Date;
Expand All @@ -14,68 +13,97 @@
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.geoserver.geofence.core.dao.search.Search;
import org.geoserver.geofence.core.model.GSUser;

import org.springframework.transaction.annotation.Transactional;


/**
* Public implementation of the UserGroupDAO interface
*
* @author Emanuele Tajariol (etj at geo-solutions.it)
*/
@Transactional(value = "geofenceTransactionManager")
public class UserGroupDAOImpl extends BaseDAO<UserGroup, Long>
// extends GenericDAOImpl<User, Long>
implements UserGroupDAO
{
// extends GenericDAOImpl<User, Long>
implements UserGroupDAO {

private static final Logger LOGGER = LogManager.getLogger(UserGroupDAOImpl.class);

public UserGroupDAOImpl() {
super(UserGroup.class);
}

@Override
public void persist(UserGroup... entities)
{
public void persist(UserGroup... entities) {
Date now = new Date();
for (UserGroup e : entities)
{
for (UserGroup e : entities) {
e.setDateCreation(now);
}

super.persist(entities);
}

@Override
public List<UserGroup> findAll()
{
public List<UserGroup> findAll() {
return super.findAll();
}

@Override
public List<UserGroup> search(Search search)
{
return super.search(search);
}

@Override
public UserGroup merge(UserGroup entity)
{
public UserGroup merge(UserGroup entity) {
return super.merge(entity);
}

@Override
public boolean remove(UserGroup entity)
{
public boolean remove(UserGroup entity) {
return super.remove(entity);
}

@Override
public boolean removeById(Long id)
{
public boolean removeById(Long id) {
return super.removeById(id);
}

// ==========================================================================
@Override
public UserGroup get(String name) {
Search search = createSearch();
search.addFilterEqual("name", name);
return (UserGroup)searchUnique(search);
}

@Override
public List<UserGroup> search(String nameLike, Integer page, Integer entries) throws IllegalArgumentException {

if( (page != null && entries == null) || (page ==null && entries != null)) {
throw new IllegalArgumentException("Page and entries params should be declared together.");
}

Search search = createSearch();

if(page != null) {
search.setMaxResults(entries);
search.setPage(page);
}

search.addSortAsc("name");

if (nameLike != null) {
search.addFilterILike("name", nameLike);
}

return search(search);
}


@Override
public long countByNameLike(String nameLike) {
Search searchCriteria = createCountSearch();

if (nameLike != null) {
searchCriteria.addFilterILike("name", nameLike);
}

return count(searchCriteria);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected void removeAllUsers() {
assertNull("User not removed", userDAO.find(item.getId()));
}

assertEquals("Users have not been properly deleted", 0, userDAO.count(null));
assertEquals("Users have not been properly deleted", 0, userDAO.countByNameLike(null));
}

protected void removeAllGRUsers() {
Expand Down Expand Up @@ -132,7 +132,7 @@ protected void removeAllUserGroups() {
assertNull("UserGroup not removed", userGroupDAO.find(item.getId()));
}

assertEquals("UserGroups have not been properly deleted", 0, userGroupDAO.count(null));
assertEquals("UserGroups have not been properly deleted", 0, userGroupDAO.countByNameLike(null));
}

protected GSUser createUser(String base, UserGroup userGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public interface UserAdminService extends GetProviderService<GSUser>
*/
@Override
GSUser get(long id) throws NotFoundServiceEx;
GSUser get(String name) throws NotFoundServiceEx;
GSUser getFull(String name) throws NotFoundServiceEx;

long getCount(String nameLike);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import org.geoserver.geofence.core.model.GSUser;
import org.geoserver.geofence.core.dao.GSUserDAO;
import org.geoserver.geofence.core.dao.search.Search;
import org.geoserver.geofence.services.dto.AuthUser;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
Expand All @@ -27,7 +25,7 @@ public class AuthorizationServiceImpl implements AuthorizationService {

@Override
public AuthUser authorize(String username, String password) {
GSUser user = getUserByName(username);
GSUser user = userDAO.getFull(username);
if(user == null) {
LOGGER.debug("User not found " + username);
return null;
Expand All @@ -40,16 +38,6 @@ public AuthUser authorize(String username, String password) {
return new AuthUser(username, user.isAdmin() ? AuthUser.Role.ADMIN : AuthUser.Role.USER);
}

private GSUser getUserByName(String userName) {
Search search = userDAO.createSearch();
search.addFilterEqual("name", userName);
List<GSUser> users = userDAO.search(search);
if(users.size() > 1)
throw new IllegalStateException("Found more than one user with name '"+userName+"'");

return users.isEmpty() ? null : users.get(0);
}

public void setGsUserDAO(GSUserDAO userDAO) {
this.userDAO = userDAO;
}
Expand Down
Loading

0 comments on commit 718d470

Please sign in to comment.