RM-1947 New constraint to avoid invalid classification levels.

Create a service locator class to allow the non-Spring constraint to access
the Spring service.

+review RM-21

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@101788 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-04-14 09:33:51 +00:00
parent 99576a26a3
commit 7d52c49b2b
10 changed files with 514 additions and 246 deletions

View File

@@ -0,0 +1,30 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.caveat;
/**
* A class to hold I18N keys for messages related to constraint validation.
*
* @author tpage
*/
public class RMConstraintMessageKeys
{
public static final String ERR_NON_STRING = "d_dictionary.constraint.string_length.non_string";
public static final String ERR_INVALID_VALUE = "d_dictionary.constraint.list_of_values.invalid_value";
}

View File

@@ -1,4 +1,4 @@
/*
/*
* Copyright (C) 2005-2014 Alfresco Software Limited.
*
* This file is part of Alfresco
@@ -14,239 +14,237 @@
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.caveat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.dictionary.ConstraintException;
import org.alfresco.service.cmr.i18n.MessageLookup;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
import org.apache.commons.lang.StringUtils;
import org.springframework.extensions.surf.util.I18NUtil;
/**
* RM Constraint implementation that ensures the value is one of a constrained
* <i>list of values</i>. By default, this constraint is case-sensitive.
*
* @see #setAllowedValues(List)
* @see #setCaseSensitive(boolean)
*
* @author janv
*/
public class RMListOfValuesConstraint extends ListOfValuesConstraint
{
private static final String ERR_NON_STRING = "d_dictionary.constraint.string_length.non_string";
private static final String ERR_INVALID_VALUE = "d_dictionary.constraint.list_of_values.invalid_value";
private static final String LOV_CONSTRAINT_VALUE = "listconstraint";
private List<String> allowedValues;
private List<String> allowedValuesUpper;
// defined match logic used by caveat matching (default = "AND")
private MatchLogic matchLogic = MatchLogic.AND;
public enum MatchLogic
{
// closed marking - all values must match
AND,
// open marking - at least one value must match
OR;
}
// note: alternative to static init could be to use 'registered' constraint
private static RMCaveatConfigService caveatConfigService;
public void setCaveatConfigService(RMCaveatConfigService caveatConfigService)
{
RMListOfValuesConstraint.caveatConfigService = caveatConfigService;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder(80);
sb.append("RMListOfValuesConstraint")
.append("[allowedValues=").append(getAllowedValues())
.append(", caseSensitive=").append(isCaseSensitive())
.append(", sorted=").append(isCaseSensitive())
.append(", matchLogic=").append(getMatchLogic())
.append("]");
return sb.toString();
}
public RMListOfValuesConstraint()
{
super();
// Set RM list of value constraints to be sorted by default
sorted = true;
}
/**
* Get the allowed values. Note that these are <tt>String</tt> instances, but may
* represent non-<tt>String</tt> values. It is up to the caller to distinguish.
*
* @return Returns the values allowed
*/
@Override
public List<String> getRawAllowedValues()
{
String runAsUser = AuthenticationUtil.getRunAsUser();
if ((runAsUser != null) && (! runAsUser.equals(AuthenticationUtil.getSystemUserName())) && (caveatConfigService != null))
{
// get allowed values for current user
List<String> allowedForUser = caveatConfigService.getRMAllowedValues(getShortName());
List<String> filteredList = new ArrayList<String>(allowedForUser.size());
for (String allowed : allowedForUser)
{
if (this.allowedValues.contains(allowed))
{
filteredList.add(allowed);
}
}
return filteredList;
}
else
{
return this.allowedValues;
}
}
public String getDisplayLabel(String constraintAllowableValue, MessageLookup messageLookup)
{
if (!this.allowedValues.contains(constraintAllowableValue))
{
return null;
}
String key = LOV_CONSTRAINT_VALUE;
key += "." + this.getShortName();
key += "." + constraintAllowableValue;
key = StringUtils.replace(key, ":", "_");
String message = messageLookup.getMessage(key, I18NUtil.getLocale());
return message == null ? constraintAllowableValue : message;
}
private List<String> getAllowedValuesUpper()
{
String runAsUser = AuthenticationUtil.getRunAsUser();
if ((runAsUser != null) && (! runAsUser.equals(AuthenticationUtil.getSystemUserName())) && (caveatConfigService != null))
{
// get allowed values for current user
List<String> allowedForUser = caveatConfigService.getRMAllowedValues(getType());
List<String> filteredList = new ArrayList<String>(allowedForUser.size());
for (String allowed : allowedForUser)
{
if (this.allowedValuesUpper.contains(allowed.toUpperCase()))
{
filteredList.add(allowed);
}
}
return filteredList;
}
else
{
return this.allowedValuesUpper;
}
}
/**
* Set the values that are allowed by the constraint.
*
* @param values a list of allowed values
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void setAllowedValues(List allowedValues)
{
if (allowedValues == null)
{
allowedValues = new ArrayList<String>(0);
}
int valueCount = allowedValues.size();
this.allowedValues = Collections.unmodifiableList(allowedValues);
// make the upper case versions
this.allowedValuesUpper = new ArrayList<String>(valueCount);
for (String allowedValue : this.allowedValues)
{
allowedValuesUpper.add(allowedValue.toUpperCase());
}
}
@Override
public void initialize()
{
checkPropertyNotNull("allowedValues", allowedValues);
}
@Override
public Map<String, Object> getParameters()
{
Map<String, Object> params = new HashMap<String, Object>(2);
params.put("caseSensitive", isCaseSensitive());
params.put("allowedValues", getAllowedValues());
params.put("sorted", isSorted());
params.put("matchLogic", getMatchLogic());
return params;
}
public MatchLogic getMatchLogicEnum()
{
return matchLogic;
}
public String getMatchLogic()
{
return matchLogic.toString();
}
public void setMatchLogic(String matchLogicStr)
{
this.matchLogic = MatchLogic.valueOf(matchLogicStr);
}
@Override
protected void evaluateSingleValue(Object value)
{
// convert the value to a String
String valueStr = null;
try
{
valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
}
catch (TypeConversionException e)
{
throw new ConstraintException(ERR_NON_STRING, value, e);
}
// check that the value is in the set of allowed values
if (isCaseSensitive())
{
if (!getAllowedValues().contains(valueStr))
{
throw new ConstraintException(ERR_INVALID_VALUE, value);
}
}
else
{
if (!getAllowedValuesUpper().contains(valueStr.toUpperCase()))
{
throw new ConstraintException(ERR_INVALID_VALUE, value);
}
}
}
}
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.caveat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.dictionary.ConstraintException;
import org.alfresco.service.cmr.i18n.MessageLookup;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
import org.apache.commons.lang.StringUtils;
import org.springframework.extensions.surf.util.I18NUtil;
/**
* RM Constraint implementation that ensures the value is one of a constrained
* <i>list of values</i>. By default, this constraint is case-sensitive.
*
* @see #setAllowedValues(List)
* @see #setCaseSensitive(boolean)
*
* @author janv
*/
public class RMListOfValuesConstraint extends ListOfValuesConstraint
{
private static final String LOV_CONSTRAINT_VALUE = "listconstraint";
private List<String> allowedValues;
private List<String> allowedValuesUpper;
// defined match logic used by caveat matching (default = "AND")
private MatchLogic matchLogic = MatchLogic.AND;
public enum MatchLogic
{
// closed marking - all values must match
AND,
// open marking - at least one value must match
OR;
}
// note: alternative to static init could be to use 'registered' constraint
private static RMCaveatConfigService caveatConfigService;
public void setCaveatConfigService(RMCaveatConfigService caveatConfigService)
{
RMListOfValuesConstraint.caveatConfigService = caveatConfigService;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder(80);
sb.append("RMListOfValuesConstraint")
.append("[allowedValues=").append(getAllowedValues())
.append(", caseSensitive=").append(isCaseSensitive())
.append(", sorted=").append(isSorted())
.append(", matchLogic=").append(getMatchLogic())
.append("]");
return sb.toString();
}
public RMListOfValuesConstraint()
{
super();
// Set RM list of value constraints to be sorted by default
sorted = true;
}
/**
* Get the allowed values. Note that these are <tt>String</tt> instances, but may
* represent non-<tt>String</tt> values. It is up to the caller to distinguish.
*
* @return Returns the values allowed
*/
@Override
public List<String> getRawAllowedValues()
{
String runAsUser = AuthenticationUtil.getRunAsUser();
if ((runAsUser != null) && (! runAsUser.equals(AuthenticationUtil.getSystemUserName())) && (caveatConfigService != null))
{
// get allowed values for current user
List<String> allowedForUser = caveatConfigService.getRMAllowedValues(getShortName());
List<String> filteredList = new ArrayList<String>(allowedForUser.size());
for (String allowed : allowedForUser)
{
if (this.allowedValues.contains(allowed))
{
filteredList.add(allowed);
}
}
return filteredList;
}
else
{
return this.allowedValues;
}
}
public String getDisplayLabel(String constraintAllowableValue, MessageLookup messageLookup)
{
if (!this.allowedValues.contains(constraintAllowableValue))
{
return null;
}
String key = LOV_CONSTRAINT_VALUE;
key += "." + this.getShortName();
key += "." + constraintAllowableValue;
key = StringUtils.replace(key, ":", "_");
String message = messageLookup.getMessage(key, I18NUtil.getLocale());
return message == null ? constraintAllowableValue : message;
}
private List<String> getAllowedValuesUpper()
{
String runAsUser = AuthenticationUtil.getRunAsUser();
if ((runAsUser != null) && (! runAsUser.equals(AuthenticationUtil.getSystemUserName())) && (caveatConfigService != null))
{
// get allowed values for current user
List<String> allowedForUser = caveatConfigService.getRMAllowedValues(getType());
List<String> filteredList = new ArrayList<String>(allowedForUser.size());
for (String allowed : allowedForUser)
{
if (this.allowedValuesUpper.contains(allowed.toUpperCase()))
{
filteredList.add(allowed);
}
}
return filteredList;
}
else
{
return this.allowedValuesUpper;
}
}
/**
* Set the values that are allowed by the constraint.
*
* @param values a list of allowed values
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void setAllowedValues(List allowedValues)
{
if (allowedValues == null)
{
allowedValues = new ArrayList<String>(0);
}
int valueCount = allowedValues.size();
this.allowedValues = Collections.unmodifiableList(allowedValues);
// make the upper case versions
this.allowedValuesUpper = new ArrayList<String>(valueCount);
for (String allowedValue : this.allowedValues)
{
allowedValuesUpper.add(allowedValue.toUpperCase());
}
}
@Override
public void initialize()
{
checkPropertyNotNull("allowedValues", allowedValues);
}
@Override
public Map<String, Object> getParameters()
{
Map<String, Object> params = new HashMap<String, Object>(2);
params.put("caseSensitive", isCaseSensitive());
params.put("allowedValues", getAllowedValues());
params.put("sorted", isSorted());
params.put("matchLogic", getMatchLogic());
return params;
}
public MatchLogic getMatchLogicEnum()
{
return matchLogic;
}
public String getMatchLogic()
{
return matchLogic.toString();
}
public void setMatchLogic(String matchLogicStr)
{
this.matchLogic = MatchLogic.valueOf(matchLogicStr);
}
@Override
protected void evaluateSingleValue(Object value)
{
// convert the value to a String
String valueStr = null;
try
{
valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
}
catch (TypeConversionException e)
{
throw new ConstraintException(RMConstraintMessageKeys.ERR_NON_STRING, value, e);
}
// check that the value is in the set of allowed values
if (isCaseSensitive())
{
if (!getAllowedValues().contains(valueStr))
{
throw new ConstraintException(RMConstraintMessageKeys.ERR_INVALID_VALUE, value);
}
}
else
{
if (!getAllowedValuesUpper().contains(valueStr.toUpperCase()))
{
throw new ConstraintException(RMConstraintMessageKeys.ERR_INVALID_VALUE, value);
}
}
}
}

View File

@@ -35,7 +35,7 @@ public final class ClassificationLevel implements Serializable
{
/** serial version uid */
private static final long serialVersionUID = -3375064867090476422L;
private final String id;
private final String displayLabelKey;
@@ -50,7 +50,10 @@ public final class ClassificationLevel implements Serializable
/** Returns the unique identifier for this classification level. */
public String getId() { return this.id; }
/** Returns the localised (current locale) display label for this classification level. */
/**
* Returns the localised (current locale) display label for this classification level. If no translation is found
* then return the key instead.
*/
public String getDisplayLabel()
{
String message = I18NUtil.getMessage(displayLabelKey);

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.classification;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.module.org_alfresco_module_rm.caveat.RMConstraintMessageKeys;
import org.alfresco.repo.dictionary.constraint.AbstractConstraint;
import org.alfresco.service.cmr.dictionary.ConstraintException;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
/**
* Check that a value is a valid {@link ClassificationLevel} by checking the {@link ClassificationService}.
*
* @author tpage
*/
public class ClassificationLevelConstraint extends AbstractConstraint
{
/** The classification service provides access to the valid classification levels. */
private ClassificationService classificationService;
/** Constraints must use a default constructor. */
public ClassificationLevelConstraint()
{
super();
this.classificationService = ClassificationServiceProvider.getClassificationService();
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
// TODO Decide whether calling getAllowedValues() is a good idea.
sb.append("ClassificationLevelConstraint")
.append("[allowedValues=").append(getAllowedValues())
.append("]");
return sb.toString();
}
/**
* Get the allowed values. Note that these are <tt>String</tt> instances, but may
* represent non-<tt>String</tt> values. It is up to the caller to distinguish.
*
* @return Returns the values allowed
*/
private List<String> getAllowedValues()
{
List<ClassificationLevel> classificationLevels = classificationService.getClassificationLevels();
List<String> values = new ArrayList<String>();
for (ClassificationLevel classificationLevel : classificationLevels)
{
values.add(classificationLevel.getId());
}
return values;
}
/** {@inheritDoc} */
@Override
protected void evaluateSingleValue(Object value)
{
// convert the value to a String
String valueStr = null;
try
{
valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
}
catch (TypeConversionException e)
{
throw new ConstraintException(RMConstraintMessageKeys.ERR_NON_STRING, value);
}
// Check that the classification level is one of the configured levels.
if (!getAllowedValues().contains(valueStr))
{
throw new ConstraintException(RMConstraintMessageKeys.ERR_INVALID_VALUE, value);
}
}
}

View File

@@ -42,7 +42,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
private static final Serializable[] REASONS_KEY = new String[] { "org.alfresco",
"module.org_alfresco_module_rm",
"classification.reasons" };
private static final Logger LOGGER = LoggerFactory.getLogger(ClassificationServiceImpl.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ClassificationServiceImpl.class);
private AttributeService attributeService; // TODO What about other code (e.g. REST API) accessing the AttrService?
private ClassificationServiceDAO classificationServiceDao;
@@ -80,7 +80,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
this.configuredLevels = allPersistedLevels;
}
}
void initConfiguredClassificationReasons()
{
final List<ClassificationReason> persistedReasons = getPersistedReasons();
@@ -143,7 +143,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
{
return classificationServiceDao.getConfiguredLevels();
}
/**
* Gets the list of classification reasons as persisted in the system.
* @return the list of classification reasons if they have been persisted, else {@code null}.
@@ -186,7 +186,8 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
@Override
public List<ClassificationLevel> getClassificationLevels()
{
if (configuredLevels == null) {
if (configuredLevels == null)
{
return Collections.emptyList();
}
// FIXME Currently assume user has highest security clearance, this should be fixed as part of RM-2112.

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.classification;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A Spring class used to provide the {@link ClassificationService} to non-Spring classes.
*
* @author tpage
*/
public class ClassificationServiceProvider
{
/** Logging utility for the class. */
private static final Logger LOGGER = LoggerFactory.getLogger(ClassificationServiceProvider.class);
/** The Spring application context. */
private static final AtomicReference<ClassificationService> CLASSIFICATION_SERVICE_REF = new AtomicReference<>();
/** Constructor that takes the classification service and makes it available statically. */
public ClassificationServiceProvider(ClassificationService classificationService)
{
ClassificationService oldClassificationService = CLASSIFICATION_SERVICE_REF.getAndSet(classificationService);
if (oldClassificationService != null)
{
LOGGER.debug("Unexpected instantiation of ClassificationServiceProvider has updated reference to classification service.");
}
}
/**
* Get the <code>ClassificationService</code> as defined in the Spring context.
*
* @return The service bean.
*/
public static ClassificationService getClassificationService()
{
return CLASSIFICATION_SERVICE_REF.get();
}
}