-// $Id$
/**
* Copyright (C) 2007 EDIT
* European Distributed Institute of Taxonomy
package eu.etaxonomy.cdm.api.service;
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import java.util.UUID;
import org.hibernate.criterion.Criterion;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
+import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
import eu.etaxonomy.cdm.model.common.Group;
import eu.etaxonomy.cdm.model.common.User;
+import eu.etaxonomy.cdm.persistence.dao.common.IGrantedAuthorityDao;
import eu.etaxonomy.cdm.persistence.dao.common.IGroupDao;
import eu.etaxonomy.cdm.persistence.dao.common.IUserDao;
+import eu.etaxonomy.cdm.persistence.dto.MergeResult;
import eu.etaxonomy.cdm.persistence.query.MatchMode;
import eu.etaxonomy.cdm.persistence.query.OrderHint;
/**
* @author n.hoffmann
* @created Mar 9, 2011
- * @version 1.0
*/
@Service
@Transactional(readOnly = true)
protected IUserDao userDao;
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#findAllGroups()
- */
+ protected IGrantedAuthorityDao grantedAuthorityDao;
+
@Override
public List<String> findAllGroups() {
return dao.listNames(null,null);
}
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#findUsersInGroup(java.lang.String)
- */
@Override
public List<String> findUsersInGroup(String groupName) {
Assert.hasText(groupName);
return users;
}
-
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#deleteGroup(java.lang.String)
- */
@Override
@Transactional(readOnly=false)
public void deleteGroup(String groupUUID) {
Assert.notNull(groupUUID);
Group group = dao.findByUuid(UUID.fromString(groupUUID));
- for (User user : group.getMembers()){
- group.removeMember(user);
- }
- if(group != null){
- dao.delete(group);
+ Iterator<User> it = group.getMembers().iterator();
+ while (it.hasNext()){
+ it.remove();
}
+ dao.delete(group);
}
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#renameGroup(java.lang.String, java.lang.String)
- */
@Override
@Transactional(readOnly=false)
public void renameGroup(String oldName, String newName) {
dao.update(group);
}
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#addUserToGroup(java.lang.String, java.lang.String)
- */
@Override
@Transactional(readOnly=false)
public void addUserToGroup(String username, String groupName) {
Group group = dao.findGroupByName(groupName);
User user = userDao.findUserByUsername(username);
- if(group != null || user != null){
+ if(group != null && user != null){
if(group.addMember(user)) {
dao.update(group);
}
}
}
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#removeUserFromGroup(java.lang.String, java.lang.String)
- */
@Override
@Transactional(readOnly=false)
public void removeUserFromGroup(String username, String groupName) {
Group group = dao.findGroupByName(groupName);
User user = userDao.findUserByUsername(username);
- if(group != null || user != null){
+ if(group != null && user != null){
if(group.removeMember(user)){
dao.update(group);
}
}
}
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#findGroupAuthorities(java.lang.String)
- */
@Override
public List<GrantedAuthority> findGroupAuthorities(String groupName) {
Assert.hasText(groupName);
Group group = dao.findGroupByName(groupName);
if (group != null){
- return new ArrayList<GrantedAuthority>(group.getGrantedAuthorities());
+ return new ArrayList<>(group.getGrantedAuthorities());
}
- return new ArrayList<GrantedAuthority>();
+ return new ArrayList<>();
}
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#addGroupAuthority(java.lang.String, org.springframework.security.core.GrantedAuthority)
- */
@Override
@Transactional(readOnly=false)
public void addGroupAuthority(String groupName, GrantedAuthority authority) {
}
}
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#removeGroupAuthority(java.lang.String, org.springframework.security.core.GrantedAuthority)
- */
@Override
@Transactional(readOnly=false)
public void removeGroupAuthority(String groupName,
}
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.api.service.ServiceBase#setDao(eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao)
- */
@Override
@Autowired
protected void setDao(IGroupDao dao) {
this.userDao = userDao;
}
+ @Autowired
+ public void setGrantedAuthorityDao(IGrantedAuthorityDao grantedAuthorityDao){
+ this.grantedAuthorityDao = grantedAuthorityDao;
+ }
+
+
@Override
@Transactional(readOnly = true)
public List<Group> listByName(String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
- Integer numberOfResults = dao.countByName(queryString, matchmode, criteria);
+ long numberOfResults = dao.countByName(queryString, matchmode, criteria);
- List<Group> results = new ArrayList<Group>();
+ List<Group> results = new ArrayList<>();
if(numberOfResults > 0) {
results = dao.findByName(queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
}
return results;
}
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#createGroup(java.lang.String, java.util.List)
- */
@Override
@Transactional(readOnly=false)
public void createGroup(String groupName, List<GrantedAuthority> authorities) {
saveGroup(newGroup);
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.api.service.IUserService#saveGroup(eu.etaxonomy.cdm.model.common.Group)
- */
@Override
@Transactional(readOnly=false)
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
public UUID saveGroup(Group group) {
- return dao.save(group);
+ return dao.save(group).getUuid();
}
@Override
return new DeleteResult();
}
+ @Override
+ @Transactional(readOnly = false)
+ public MergeResult<Group> merge(Group newInstance, boolean returnTransientEntity) {
+
+ Set<GrantedAuthority> newAuthorities = newInstance.getGrantedAuthorities();
+ Map<GrantedAuthority, GrantedAuthority> mapOfAlreadyExistingAuthorities = new HashMap<GrantedAuthority, GrantedAuthority>();
+ GrantedAuthorityImpl alreadyInDB;
+ for (GrantedAuthority authority: newAuthorities){
+ if (authority instanceof GrantedAuthorityImpl){
+ alreadyInDB = grantedAuthorityDao.findAuthorityString(authority.getAuthority());
+ if (alreadyInDB != null){
+ if (alreadyInDB.getId() != ((GrantedAuthorityImpl)authority).getId()){
+ mapOfAlreadyExistingAuthorities.put(authority,alreadyInDB);
+ }
+ }
+ }
+ }
+ for (GrantedAuthority authority : mapOfAlreadyExistingAuthorities.keySet()){
+ newInstance.removeGrantedAuthority(authority);
+ newInstance.addGrantedAuthority(mapOfAlreadyExistingAuthorities.get(authority));
+ }
+
+ return dao.merge(newInstance, returnTransientEntity);
+ }
}