Ignore:
Timestamp:
08/11/14 16:07:55 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/GroupServiceImpl.java

    r4169 r5549  
    11package clarin.cmdi.componentregistry.impl.database;
    22
     3import clarin.cmdi.componentregistry.UserUnauthorizedException;
    34import java.util.ArrayList;
    45import java.util.Collections;
     
    235236                ownerships.addAll(ownershipDao.findOwnershipByComponentId(itemId));
    236237                Set<Group> groups = new HashSet<Group>();
    237                 for (Ownership ownership : ownerships)
     238                for (Ownership ownership : ownerships) {
    238239                        groups.add(groupDao.findOne(ownership.getGroupId()));
     240                }
    239241                List<Group> groupList = new ArrayList<Group>(groups);
    240242                Collections.sort(groupList, new Comparator<Group>() {
     
    247249                return groupList;
    248250        }
     251       
     252       
    249253
    250254        @ManagedOperation(description = "Make a component owned by a group instead of a user")
     
    254258                        @ManagedOperationParameter(name = "componentId", description = "Id of component") })
    255259        @Override
    256         public void transferItemOwnershipFromUserToGroup(String principal, String groupName, String itemId) {
    257 
     260        public void transferItemOwnershipFromUserToGroup(String principal, String groupName, String itemId) throws UserUnauthorizedException{
     261               
     262                           
    258263                BaseDescription item = null;
    259264                item = componentDao.getByCmdId(itemId);
    260                 if (item == null)
     265                if (item == null) {
    261266                        throw new ValidationException("No profile or component found with ID " + itemId);
     267                }
    262268                Group group = groupDao.findGroupByName(groupName);
    263                 if (group == null)
     269                if (group == null) {
    264270                        throw new ValidationException("No group found with name " + groupName);
     271                }
     272               
     273                if (!this.userGroupMember(principal, String.valueOf(group.getId()))) {
     274                   throw new UserUnauthorizedException("User " + principal+ " is not a member of group "+groupName);
     275                }
     276               
     277                String creatorName = item.getCreatorName();
     278                if (!creatorName.equals(principal)) {
     279                   throw new UserUnauthorizedException("User " + principal+ " is not creator of the item  "+item.getName());
     280                }
     281               
     282               
    265283                Ownership ownership = null;
    266284                List<Ownership> oldOwnerships = ownershipDao.findOwnershipByComponentId(itemId);
     
    273291
    274292        @Override
    275         public void transferItemOwnershipFromUserToGroupId(String principal, long groupId, String componentId) {
     293        public void transferItemOwnershipFromUserToGroupId(String principal, long groupId, String componentId) throws UserUnauthorizedException{
    276294                Group group = groupDao.findOne(groupId);
    277                 if (group == null)
     295                if (group == null) {
    278296                        throw new ValidationException("No group found with id " + groupId);
    279                 transferItemOwnershipFromUserToGroup(principal, group.getName(), componentId);
    280         }
    281 
     297                }
     298                this.transferItemOwnershipFromUserToGroup(principal, group.getName(), componentId);
     299        }
     300
     301        @Override
     302        public boolean userGroupMember(String principalName, String groupId) {
     303            RegistryUser user = userDao.getByPrincipalName(principalName);
     304            GroupMembership gm = groupMembershipDao.findMembership(user.getId(), Long.parseLong(groupId));
     305            return gm != null;
     306        }
    282307}
Note: See TracChangeset for help on using the changeset viewer.