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/ComponentRegistryFactoryDbImpl.java

    r4098 r5549  
    33import clarin.cmdi.componentregistry.ComponentRegistry;
    44import clarin.cmdi.componentregistry.ComponentRegistryFactory;
    5 import clarin.cmdi.componentregistry.ComponentStatus;
    65import clarin.cmdi.componentregistry.Configuration;
    76import clarin.cmdi.componentregistry.Owner;
    87import clarin.cmdi.componentregistry.OwnerUser;
     8import clarin.cmdi.componentregistry.RegistrySpace;
    99import clarin.cmdi.componentregistry.UserCredentials;
    1010import clarin.cmdi.componentregistry.UserUnauthorizedException;
     
    2525 * ComponentRegistryDbImpl implementation of ComponentRegistry for accessing the
    2626 * registry
    27  * 
     27 *
    2828 * @author Twan Goosen <twan.goosen@mpi.nl>
    2929 * @author George.Georgovassilis@mpi.nl
     
    3232
    3333    private final static Logger LOG = LoggerFactory
    34             .getLogger(ComponentRegistryFactoryDbImpl.class);
     34            .getLogger(ComponentRegistryFactoryDbImpl.class);
    3535    @Autowired
    3636    private Configuration configuration;
     
    4343    @Override
    4444    public List<ComponentRegistry> getAllUserRegistries() {
    45         // TODO: this probably could use some caching
    46         try {
    47             List<RegistryUser> users = userDao.getAllUsers();
    48             List<ComponentRegistry> registries = new ArrayList<ComponentRegistry>();
    49             for (RegistryUser user : users) {
    50                 registries.add(getNewComponentRegistryForUser(user.getId()));
    51             }
    52             return registries;
    53         } catch (DataAccessException ex) {
    54             LOG.error("Could not retrieve users", ex);
    55             throw ex;
    56         }
    57     }
    58 
    59     @Override
    60     public ComponentRegistry getComponentRegistry(ComponentStatus status,
    61             Owner owner, UserCredentials credentials)
    62             throws UserUnauthorizedException {
    63         switch (status) {
    64         case PRIVATE:
    65             return getPrivateRegistry(owner, credentials);
    66         case PUBLISHED:
    67             return getPublicRegistry();
    68         default:
    69             // TODO: Add support for other status types
    70             throw new UnsupportedOperationException(
    71                     "Unsupported component status" + status);
    72         }
     45        // TODO: this probably could use some caching
     46        try {
     47            List<RegistryUser> users = userDao.getAllUsers();
     48            List<ComponentRegistry> registries = new ArrayList<ComponentRegistry>();
     49            for (RegistryUser user : users) {
     50                registries.add(getNewComponentRegistryForUser(user.getId()));
     51            }
     52            return registries;
     53        } catch (DataAccessException ex) {
     54            LOG.error("Could not retrieve users", ex);
     55            throw ex;
     56        }
     57    }
     58
     59    @Override
     60    public ComponentRegistry getComponentRegistry(RegistrySpace space,
     61            Owner owner, UserCredentials credentials, Number groupId)
     62            throws UserUnauthorizedException {
     63
     64        switch (space) {
     65            case PRIVATE:
     66                return this.getPrivateRegistry(owner, credentials);
     67            case PUBLISHED:
     68                return this.getPublicRegistry();
     69            case GROUP:
     70                return this.getGroupRegistry(groupId, owner, credentials);
     71            default:
     72                // TODO: Add support for "review" types
     73                throw new UnsupportedOperationException(
     74                        "Unsupported component space" + space);
     75        }
     76    }
     77
     78    private ComponentRegistry getGroupRegistry(Number groupId, Owner owner, UserCredentials credentials) {
     79        if (owner == null || owner instanceof OwnerUser) {
     80            RegistryUser user = this.getOrCreateUser(credentials);
     81            owner = new OwnerUser(user.getId());
     82        }
     83        ComponentRegistryDbImpl cr = componentRegistryBeanFactory.getNewComponentRegistry();// default public registry
     84        cr.setRegistrySpace(RegistrySpace.PRIVATE);
     85        cr.setRegistryOwner(owner);
     86        cr.setGroupId(groupId);
     87        return cr;
    7388    }
    7489
    7590    private ComponentRegistry getPrivateRegistry(Owner owner,
    76             UserCredentials credentials) throws IllegalArgumentException,
    77             DataAccessException, UserUnauthorizedException {
    78         if (owner == null || owner instanceof OwnerUser) {
    79             RegistryUser user = getOrCreateUser(credentials);
    80             if (user != null) {
    81                 if (owner != null && !user.getId().equals(owner.getId())) {
    82                     throw new UserUnauthorizedException(
    83                             "User cannot access other user's private registry");
    84                 }
    85 
    86                 try {
    87                     return getNewComponentRegistryForUser(user.getId());
    88                 } catch (DataAccessException ex) {
    89                     LOG.error("Could not retrieve or create user", ex);
    90                     throw ex;
    91                 }
    92             } else {
    93                 throw new UserUnauthorizedException(
    94                         "No user credentials available cannot load userspace.");
    95             }
    96         } else {
    97             // TODO: Support group owners
    98             throw new UnsupportedOperationException(
    99                     "Group owners not supported");
    100         }
     91            UserCredentials credentials) throws IllegalArgumentException,
     92            DataAccessException, UserUnauthorizedException {
     93        if (owner == null || owner instanceof OwnerUser) {
     94            RegistryUser user = getOrCreateUser(credentials);
     95            if (user != null) {
     96                if (owner != null && !user.getId().equals(owner.getId())) {
     97                    throw new UserUnauthorizedException(
     98                            "User cannot access other user's private registry");
     99                }
     100
     101                try {
     102                    return this.getNewComponentRegistryForUser(user.getId());
     103                } catch (DataAccessException ex) {
     104                    LOG.error("Could not retrieve or create user", ex);
     105                    throw ex;
     106                }
     107            } else {
     108                throw new UserUnauthorizedException(
     109                        "No user credentials available cannot load userspace.");
     110            }
     111        } else {
     112            // TODO: Support group owners
     113            throw new UnsupportedOperationException(
     114                    "Group owners not supported");
     115        }
    101116    }
    102117
    103118    @Override
    104119    public ComponentRegistry getOtherUserComponentRegistry(
    105             Principal adminPrincipal, ComponentStatus status, Owner owner) {
    106         try {
    107             RegistryUser user;
    108             if (owner instanceof OwnerUser) {
    109                 user = userDao.findOne(owner.getId().longValue());
    110             } else {
    111                 // TODO: Implement for groups
    112                 throw new UnsupportedOperationException(
    113                         "Groups not implemented yet");
    114             }
    115             ComponentRegistry result = null;
    116             if (user != null) {
    117                 if (configuration.isAdminUser(adminPrincipal)) {
    118                     result = getNewComponentRegistryForUser(user.getId());
    119                 } else {
    120                     LOG.info("{} not found in list of {}",
    121                             adminPrincipal.getName(),
    122                             configuration.getAdminUsersArray().length);
    123                     throw new IllegalArgumentException("User "
    124                             + adminPrincipal.getName()
    125                             + " is not admin user cannot load userspace.");
    126                 }
    127             }
    128             return result;
    129         } catch (DataAccessException ex) {
    130             LOG.error("Could not retrieve user by id", ex);
    131             throw ex;
    132         }
     120            Principal adminPrincipal, Owner owner) {
     121        try {
     122            RegistryUser user;
     123            if (owner instanceof OwnerUser) {
     124                user = userDao.findOne(owner.getId().longValue());
     125            } else {
     126                // TODO: Implement for groups ??? Twan
     127                throw new UnsupportedOperationException(
     128                        "Groups not implemented yet");
     129            }
     130            ComponentRegistry result = null;
     131            if (user != null) {
     132                if (configuration.isAdminUser(adminPrincipal)) {
     133                    result = this.getNewComponentRegistryForUser(user.getId());
     134                } else {
     135                    LOG.info("{} not found in list of {}",
     136                            adminPrincipal.getName(),
     137                            configuration.getAdminUsersArray().length);
     138                    throw new IllegalArgumentException("User "
     139                            + adminPrincipal.getName()
     140                            + " is not admin user cannot load userspace.");
     141                }
     142            }
     143            return result;
     144        } catch (DataAccessException ex) {
     145            LOG.error("Could not retrieve user by id", ex);
     146            throw ex;
     147        }
    133148    }
    134149
    135150    @Override
    136151    public ComponentRegistry getPublicRegistry() {
    137         if (publicComponentRegistry == null) {
    138             publicComponentRegistry = getNewComponentRegistryForUser(null);
    139         }
    140         return publicComponentRegistry;
     152        if (publicComponentRegistry == null) {
     153            publicComponentRegistry = this.getNewComponentRegistryForUser(null);
     154        }
     155        return publicComponentRegistry;
    141156    }
    142157
    143158    private ComponentRegistry getNewComponentRegistryForUser(Number userId) {
    144         ComponentRegistryDbImpl componentRegistry = componentRegistryBeanFactory
    145                 .getNewComponentRegistry();
    146         if (userId != null) {
    147             // Null means public registry
    148             // TODO: Make this more explicit
    149             componentRegistry.setStatus(ComponentStatus.PRIVATE, new OwnerUser(
    150                     userId));
    151         }
    152         return componentRegistry;
     159        ComponentRegistryDbImpl componentRegistry = componentRegistryBeanFactory.getNewComponentRegistry();
     160        if (userId != null) {
     161            OwnerUser ou = new OwnerUser(userId);
     162            componentRegistry.setRegistrySpace(RegistrySpace.PRIVATE);
     163            componentRegistry.setRegistryOwner(ou);
     164            componentRegistry.setGroupId(null);
     165        }
     166        return componentRegistry;
     167    }
     168
     169    @Override
     170    public ComponentRegistryDbImpl getBaseRegistry(UserCredentials credentials) {
     171        RegistryUser user = this.getOrCreateUser(credentials);
     172        ComponentRegistryDbImpl componentRegistryDbImpl = componentRegistryBeanFactory.getNewComponentRegistry();// default public registry
     173        if (user != null) {
     174            Number userId = user.getId();
     175            Owner owner = new OwnerUser(userId);
     176            componentRegistryDbImpl.setRegistrySpace(null);
     177            componentRegistryDbImpl.setRegistryOwner(owner);
     178            componentRegistryDbImpl.setGroupId(null);
     179        }
     180        return componentRegistryDbImpl;
    153181    }
    154182
    155183    @Override
    156184    public RegistryUser getOrCreateUser(UserCredentials credentials) {
    157         if (credentials != null
    158                 && !ANONYMOUS_USER.equals(credentials.getPrincipalName())) {
    159             String principalName = credentials.getPrincipalName();
    160             return getOrCreateUser(principalName, credentials.getDisplayName());
    161         }
    162         return null;
     185        if (credentials != null
     186                && !ANONYMOUS_USER.equals(credentials.getPrincipalName())) {
     187            String principalName = credentials.getPrincipalName();
     188            return this.getOrCreateUser(principalName, credentials.getDisplayName());
     189        }
     190        return null;
    163191    }
    164192
    165193    private synchronized RegistryUser getOrCreateUser(String principalName,
    166             String displayName) {
    167         // Try getting it from db
    168         RegistryUser user = userDao.getByPrincipalName(principalName);
    169         if (user == null) {
    170             LOG.info(
    171                     "Request to create user with principal name {} and display name {}",
    172                     new Object[] { principalName, displayName });
    173             // Create the new user
    174             user = new RegistryUser();
    175             user.setPrincipalName(principalName);
    176             user.setName(displayName);
    177             userDao.saveAndFlush(user);
    178             // Retrieve from db
    179             user = userDao.getByPrincipalName(principalName);
    180         }
    181         return user;
     194            String displayName) {
     195        // Try getting it from db
     196        RegistryUser user = userDao.getByPrincipalName(principalName);
     197        if (user == null) {
     198            LOG.info(
     199                    "Request to create user with principal name {} and display name {}",
     200                    new Object[]{principalName, displayName});
     201            // Create the new user
     202            user = new RegistryUser();
     203            user.setPrincipalName(principalName);
     204            user.setName(displayName);
     205            userDao.saveAndFlush(user);
     206            // Retrieve from db
     207            user = userDao.getByPrincipalName(principalName);
     208        }
     209        return user;
    182210    }
    183211}
Note: See TracChangeset for help on using the changeset viewer.