-// $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.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
-import eu.etaxonomy.cdm.model.common.Group;
-import eu.etaxonomy.cdm.model.common.User;
-import eu.etaxonomy.cdm.persistence.dao.common.IGroupDao;
-import eu.etaxonomy.cdm.persistence.dao.common.IUserDao;
+import eu.etaxonomy.cdm.model.permission.GrantedAuthorityImpl;
+import eu.etaxonomy.cdm.model.permission.Group;
+import eu.etaxonomy.cdm.model.permission.User;
+import eu.etaxonomy.cdm.persistence.dao.permission.IGrantedAuthorityDao;
+import eu.etaxonomy.cdm.persistence.dao.permission.IGroupDao;
+import eu.etaxonomy.cdm.persistence.dao.permission.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
+ * @since Mar 9, 2011
*/
@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#createGroup(java.lang.String, java.util.List)
- */
- @Override
- @Transactional(readOnly=false)
- public void createGroup(String groupName, List<GrantedAuthority> authorities) {
- Assert.hasText(groupName);
- Assert.notNull(authorities);
-
- Group group = Group.NewInstance(groupName);
-
- for(GrantedAuthority authority : authorities) {
- group.getGrantedAuthorities().add(authority);
- }
-
- dao.save(group);
- }
- /* (non-Javadoc)
- * @see org.springframework.security.provisioning.GroupManager#deleteGroup(java.lang.String)
- */
@Override
@Transactional(readOnly=false)
- public void deleteGroup(String groupName) {
- Assert.hasText(groupName);
+ public void deleteGroup(String groupUUID) {
+ Assert.notNull(groupUUID);
+
+ Group group = dao.findByUuid(UUID.fromString(groupUUID));
+ Iterator<User> it = group.getMembers().iterator();
+ group.getMembers().clear();
+// while (it.hasNext()){
+// it.remove();
+// }
+ dao.delete(group);
- Group group = dao.findGroupByName(groupName);
- if(group != null){
- 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;
}
+ @Override
+ @Transactional(readOnly=false)
+ public void createGroup(String groupName, List<GrantedAuthority> authorities) {
+ Assert.hasText(groupName);
+ Assert.notNull(authorities);
+
+ Group newGroup = Group.NewInstance(groupName);
+ for (GrantedAuthority grantedAuthority: authorities){
+ newGroup.addGrantedAuthority(grantedAuthority);
+ }
+ saveGroup(newGroup);
+ }
+
+ @Override
+ @Transactional(readOnly=false)
+ @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
+ public UUID saveGroup(Group group) {
+ return dao.save(group).getUuid();
+ }
+
+ @Override
+ @Transactional(readOnly=false)
+ public DeleteResult delete(UUID groupUUID ){
+
+ String groupUUIDString = groupUUID.toString();
+ Group group = dao.findByUuid(groupUUID);
+ //org.springframework.security.provisioning.GroupManager#deleteGroup needs a string argument
+ this.deleteGroup(groupUUIDString);
+ DeleteResult result = new DeleteResult();
+ result.addDeletedObject(group);
+ //there is no feedback from the deleteGroup method...
+ return result;
+ }
+
+ @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);
+ }
}