Commit 74eb82e2 authored by Matija Obreza's avatar Matija Obreza

Catalog code refactored

- Moved classes to right packages
parent bd4cc563
......@@ -23,7 +23,7 @@ import org.genesys.catalog.server.model.dataset.DatasetCreator;
import org.genesys.catalog.server.persistence.dataset.DatasetCreatorRepository;
import org.genesys.catalog.server.service.DatasetCreatorService;
import org.genesys.catalog.server.service.DatasetService;
import org.genesys.catalog.server.util.Utils;
import org.genesys2.server.security.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -60,9 +60,9 @@ public class DatasetCreatorServiceImpl implements DatasetCreatorService {
@Autowired
private DatasetCreatorRepository datasetCreatorRepository;
/** The utils. */
/** The securityUtils. */
@Autowired
private Utils utils;
private SecurityUtils securityUtils;
/**
* {@inheritDoc}
......@@ -191,7 +191,7 @@ public class DatasetCreatorServiceImpl implements DatasetCreatorService {
*/
@Override
public List<DatasetCreator> autocompleteCreators(final String text) {
final HashSet<Long> ids = new HashSet<>(utils.listObjectIdentityIdsForCurrentUser(Dataset.class, BasePermission.WRITE));
final HashSet<Long> ids = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Dataset.class, BasePermission.WRITE));
final Predicate predicate = datasetCreator.dataset.id.in(ids).and(datasetCreator.fullName.startsWithIgnoreCase(text).or(datasetCreator.institutionalAffiliation.startsWithIgnoreCase(text)));
return datasetCreatorRepository.findAll(predicate, new PageRequest(0, 20, new Sort("fullName"))).getContent();
}
......
......@@ -31,13 +31,13 @@ import org.genesys.catalog.server.persistence.dataset.DatasetRepository;
import org.genesys.catalog.server.persistence.dataset.DatasetVersionsRepository;
import org.genesys.catalog.server.service.DatasetService;
import org.genesys.catalog.server.service.filters.DatasetFilter;
import org.genesys.catalog.server.util.Utils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.ConcurrencyFailureException;
......@@ -78,9 +78,9 @@ public class DatasetServiceImpl implements DatasetService {
@Autowired
private CustomAclService aclService;
/** The utils. */
/** The securityUtils. */
@Autowired
private Utils utils;
private SecurityUtils securityUtils;
/** The file repository path. */
@Value("${file.repository.datasets.folder}")
......@@ -252,10 +252,10 @@ public class DatasetServiceImpl implements DatasetService {
*/
@Override
public Page<Dataset> listDatasetsForCurrentUser(final DatasetFilter filter, final Pageable page) {
if (utils.hasRole(UserRole.ADMINISTRATOR)) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
return datasetRepository.findAll(filter.buildQuery(), page);
} else {
final HashSet<Long> partners = new HashSet<>(utils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
final HashSet<Long> partners = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
return datasetRepository.findAll(dataset.owner.id.in(partners).and(filter.buildQuery()), page);
}
}
......@@ -451,7 +451,7 @@ public class DatasetServiceImpl implements DatasetService {
throw new NotFoundElement("No dataset with specified uuid and version");
}
if (!utils.hasRole(UserRole.ADMINISTRATOR)) {
if (!securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
long oneDay = 24 * 60 * 60 * 1000;
if (loaded.getLastModifiedDate() != null && loaded.getLastModifiedDate().getTime() <= (System.currentTimeMillis() - oneDay)) {
throw new InvalidApiUsageException("Cannot be un-published. More than 24 hours have passed since the publication.");
......
......@@ -28,8 +28,8 @@ import org.genesys.catalog.server.persistence.traits.DescriptorListRepository;
import org.genesys.catalog.server.service.DescriptorListService;
import org.genesys.catalog.server.service.DescriptorService;
import org.genesys.catalog.server.service.filters.DescriptorListFilter;
import org.genesys.catalog.server.util.Utils;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.security.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -66,9 +66,9 @@ public class DescriptorListServiceImpl implements DescriptorListService {
@Autowired
private DescriptorListRepository descriptorListRepository;
/** The utils. */
/** The securityUtils. */
@Autowired
private Utils utils;
private SecurityUtils securityUtils;
@Autowired
private DescriptorService descriptorService;
......@@ -300,10 +300,10 @@ public class DescriptorListServiceImpl implements DescriptorListService {
@Override
public Page<DescriptorList> listDescriptorListsForCurrentUser(final DescriptorListFilter filter, final Pageable page) {
if (utils.hasRole(UserRole.ADMINISTRATOR)) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
return descriptorListRepository.findAll(filter.buildQuery(), page);
} else {
final HashSet<Long> partners = new HashSet<>(utils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
final HashSet<Long> partners = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
return descriptorListRepository.findAll(descriptorList.owner.id.in(partners).and(filter.buildQuery()), page);
}
}
......@@ -352,7 +352,7 @@ public class DescriptorListServiceImpl implements DescriptorListService {
throw new NotFoundElement("No DescriptorList with specified uuid and version");
}
if (!utils.hasRole(UserRole.ADMINISTRATOR)) {
if (!securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
long oneDay = 24 * 60 * 60 * 1000;
if (loaded.getLastModifiedDate() != null && loaded.getLastModifiedDate().getTime() <= (System.currentTimeMillis() - oneDay)) {
throw new InvalidApiUsageException("Cannot be un-published. More than 24 hours have passed since the publication.");
......
......@@ -31,8 +31,8 @@ import org.genesys.catalog.server.persistence.vocab.VocabularyTermRepository;
import org.genesys.catalog.server.service.DescriptorService;
import org.genesys.catalog.server.service.VersionManager;
import org.genesys.catalog.server.service.filters.DescriptorFilter;
import org.genesys.catalog.server.util.Utils;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.security.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -72,9 +72,9 @@ public class DescriptorServiceImpl implements DescriptorService {
@Autowired
private VocabularyTermRepository termRepository;
/** The utils. */
/** The securityUtils. */
@Autowired
private Utils utils;
private SecurityUtils securityUtils;
@Autowired
private EntityManager entityManager;
......@@ -199,10 +199,10 @@ public class DescriptorServiceImpl implements DescriptorService {
@Override
@PreAuthorize("isAuthenticated()")
public Page<Descriptor> listDescriptorsForCurrentUser(final DescriptorFilter filter, final Pageable page) {
if (utils.hasRole(UserRole.ADMINISTRATOR)) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
return descriptorRepository.findAll(filter.buildQuery(), page);
} else {
final HashSet<Long> partners = new HashSet<>(utils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
final HashSet<Long> partners = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
return descriptorRepository.findAll(descriptor.owner.id.in(partners).and(filter.buildQuery()), page);
}
}
......@@ -333,7 +333,7 @@ public class DescriptorServiceImpl implements DescriptorService {
throw new NotFoundElement("No Descriptor with specified uuid and version");
}
if (!utils.hasRole(UserRole.ADMINISTRATOR)) {
if (!securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
long oneDay = 24 * 60 * 60 * 1000;
if (loaded.getLastModifiedDate() != null && loaded.getLastModifiedDate().getTime() <= (System.currentTimeMillis() - oneDay)) {
throw new InvalidApiUsageException("Cannot be un-published. More than 24 hours have passed since the publication.");
......
......@@ -21,8 +21,8 @@ import org.genesys.catalog.server.model.Partner;
import org.genesys.catalog.server.persistence.PartnerRepository;
import org.genesys.catalog.server.service.PartnerService;
import org.genesys.catalog.server.service.filters.PartnerFilter;
import org.genesys.catalog.server.util.Utils;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.security.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
......@@ -54,7 +54,7 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
private PartnerRepository partnerRepository;
@Autowired
private Utils utils;
private SecurityUtils securityUtils;
@Value("${partner.primary.uuid}")
private UUID primaryPartnerUuid;
......@@ -139,10 +139,10 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
*/
@Override
public Page<Partner> listPartnersForCurrentUser(final PartnerFilter filter, final Pageable page) {
if (utils.hasRole(UserRole.ADMINISTRATOR)) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
return partnerRepository.findAll(filter.buildQuery(), page);
} else {
final HashSet<Long> ids = new HashSet<>(utils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
final HashSet<Long> ids = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
return partnerRepository.findAll(partner.id.in(ids).and(filter.buildQuery()), page);
}
}
......
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.catalog.server.util;
import java.util.Random;
/**
* The Class RandomPasswordUtil.
*
* @author Maxym Borodenko
*/
public class RandomPasswordUtil {
private static final String symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$&@?~!%#";
/**
* This method generates a random password that consists of at least one special
* character, one number, one lowercase letter and one uppercase letter.
*
* @param random the random
* @param length the password length that should be generated
* @return string with generated password
* @throws IllegalArgumentException if the 'length' parameter is lower than 4
* characters
*/
public static String generatePassword(final Random random, final int length) {
if (length < 4) {
throw new IllegalArgumentException("Password must be at least 4 characters");
}
while (true) {
final char[] password = new char[length];
boolean hasUpper = false, hasLower = false, hasDigit = false, hasSpecial = false;
for (int i = 0; i < password.length; i++) {
final char ch = symbols.charAt(random.nextInt(symbols.length()));
if (Character.isUpperCase(ch)) {
hasUpper = true;
} else if (Character.isLowerCase(ch)) {
hasLower = true;
} else if (Character.isDigit(ch)) {
hasDigit = true;
} else {
hasSpecial = true;
}
password[i] = ch;
}
if (hasUpper && hasLower && hasDigit && hasSpecial) {
return new String(password);
}
}
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.catalog.server.util;
package org.genesys2.server.security;
import org.genesys.blocks.security.SecurityContextUtil;
import org.genesys.blocks.security.model.AclAwareModel;
......@@ -30,12 +30,12 @@ import org.springframework.stereotype.Component;
import java.util.List;
/**
* The Class Utils.
* The Class SecurityUtils.
*
* @author Andrey Lugovskoy.
*/
@Component
public class Utils {
public class SecurityUtils {
@Autowired
private CustomAclService aclService;
......
/**
* Copyright 2017 Global Crop Diversity Trust
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,49 +12,52 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.util;
import java.util.Random;
/**
* The Class RandomPasswordUtil.
*
* @author Maxym Borodenko
*/
public class RandomPasswordUtil {
private static final String symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$&@?~!%#";
private static final String symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$&@?~!%#";
/**
* This method generates a random password that consists of at least one special character, one number,
* one lowercase letter and one uppercase letter
*
* @param random
* @param length the password length that should be generated
* @return string with generated password
* @throws IllegalArgumentException if the 'length' parameter is lower than 4 characters
*/
public static String generatePassword(final Random random, final int length) {
if (length < 4) {
throw new IllegalArgumentException("Password must be at least 4 characters");
}
while (true) {
char[] password = new char[length];
boolean hasUpper = false, hasLower = false, hasDigit = false, hasSpecial = false;
for (int i = 0; i < password.length; i++) {
char ch = symbols.charAt(random.nextInt(symbols.length()));
if (Character.isUpperCase(ch))
hasUpper = true;
else if (Character.isLowerCase(ch))
hasLower = true;
else if (Character.isDigit(ch))
hasDigit = true;
else
hasSpecial = true;
password[i] = ch;
}
if (hasUpper && hasLower && hasDigit && hasSpecial) {
return new String(password);
}
}
}
/**
* This method generates a random password that consists of at least one special
* character, one number, one lowercase letter and one uppercase letter.
*
* @param random the random
* @param length the password length that should be generated
* @return string with generated password
* @throws IllegalArgumentException if the 'length' parameter is lower than 4
* characters
*/
public static String generatePassword(final Random random, final int length) {
if (length < 4) {
throw new IllegalArgumentException("Password must be at least 4 characters");
}
while (true) {
final char[] password = new char[length];
boolean hasUpper = false, hasLower = false, hasDigit = false, hasSpecial = false;
for (int i = 0; i < password.length; i++) {
final char ch = symbols.charAt(random.nextInt(symbols.length()));
if (Character.isUpperCase(ch)) {
hasUpper = true;
} else if (Character.isLowerCase(ch)) {
hasLower = true;
} else if (Character.isDigit(ch)) {
hasDigit = true;
} else {
hasSpecial = true;
}
password[i] = ch;
}
if (hasUpper && hasLower && hasDigit && hasSpecial) {
return new String(password);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment