From 81d0986afaa255d784ad3d79e81ea6eba65b6d8e Mon Sep 17 00:00:00 2001 From: Neil McErlean Date: Wed, 29 Apr 2015 14:18:36 +0000 Subject: [PATCH] Slight refactor/improvement to SecurityClearanceService as part of reviewing RM-2113. The API had been dealing with Pair objects and I've extracted that out into its own type: SecurityClearance. Should make some of the code more readable. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@103133 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../classification/SecurityClearance.java | 74 +++++++++++++++++++ .../SecurityClearanceService.java | 14 ++-- .../SecurityClearanceServiceImpl.java | 20 +++-- 3 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearance.java diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearance.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearance.java new file mode 100644 index 0000000000..55d7d8779e --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearance.java @@ -0,0 +1,74 @@ +/* + * 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 . + */ +package org.alfresco.module.org_alfresco_module_rm.classification; + +import org.alfresco.service.cmr.security.PersonService.PersonInfo; + +import java.io.Serializable; +import java.util.Objects; + +/** + * A simple data type for a single user's security clearance. + * + * @author Neil Mc Erlean + * @since 3.0 + */ +public final class SecurityClearance implements Serializable +{ + private final PersonInfo personInfo; + private final ClassificationLevel classificationLevel; + + public SecurityClearance(final PersonInfo personInfo, final ClassificationLevel classificationLevel) + { + Objects.requireNonNull(personInfo); + Objects.requireNonNull(classificationLevel); + + this.personInfo = personInfo; + this.classificationLevel = classificationLevel; + } + + /** Returns the {@link PersonInfo} for this security clearance. */ + public PersonInfo getPersonInfo() { return this.personInfo; } + + /** Returns the {@link ClassificationLevel} for this security clearance. */ + public ClassificationLevel getClassificationLevel() { return this.classificationLevel; } + + @Override public String toString() + { + StringBuilder msg = new StringBuilder(); + msg.append(SecurityClearance.class.getSimpleName()) + .append(':').append(personInfo.getUserName()) + .append(" [").append(classificationLevel).append(']'); + + return msg.toString(); + } + + @Override public boolean equals(Object o) + { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + SecurityClearance that = (SecurityClearance) o; + + return this.personInfo.equals(that.personInfo) && + this.classificationLevel.equals(that.classificationLevel); + } + + @Override public int hashCode() { return Objects.hash(personInfo, classificationLevel); } +} diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceService.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceService.java index 0eb97b581d..f1797ed7ec 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceService.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceService.java @@ -21,8 +21,6 @@ package org.alfresco.module.org_alfresco_module_rm.classification; import org.alfresco.query.PagingRequest; import org.alfresco.query.PagingResults; import org.alfresco.service.cmr.security.NoSuchPersonException; -import org.alfresco.service.cmr.security.PersonService.PersonInfo; -import org.alfresco.util.Pair; /** * This service offers access to users' security clearance levels. @@ -35,10 +33,10 @@ public interface SecurityClearanceService /** * Get the currently authenticated user's security clearance. * - * @return a {@link PersonInfo}, {@link ClassificationLevel} pair for the currently authenticated user. + * @return the security clearance for the currently authenticated user. * @throws NoSuchPersonException if the current user's person node cannot be found. */ - Pair getUserSecurityClearance(); + SecurityClearance getUserSecurityClearance(); /** * Get users' security clearances. @@ -46,9 +44,9 @@ public interface SecurityClearanceService * @param userNameFragment A username fragment which will be used to apply a 'starts with' query. * @param sortAscending if @code true} returns data sorted in ascending order by username. * @param req paging request definition. - * @return {@link PersonInfo}, {@link ClassificationLevel} pairs for the specified page of users. + * @return security clearances for the specified page of users. */ - PagingResults> getUsersSecurityClearance(String userNameFragment, - boolean sortAscending, - PagingRequest req); + PagingResults getUsersSecurityClearance(String userNameFragment, + boolean sortAscending, + PagingRequest req); } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceServiceImpl.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceServiceImpl.java index 9a240dfbbe..530c6255d4 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceServiceImpl.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceServiceImpl.java @@ -25,12 +25,10 @@ import static org.alfresco.model.ContentModel.PROP_USERNAME; import static org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel.ASPECT_SECURITY_CLEARANCE; import static org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel.PROP_CLEARANCE_LEVEL; -import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.module.org_alfresco_module_rm.util.ServiceBaseImpl; import org.alfresco.query.PagingRequest; import org.alfresco.query.PagingResults; import org.alfresco.service.cmr.repository.NodeRef; -import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.security.PersonService; import org.alfresco.service.cmr.security.PersonService.PersonInfo; import org.alfresco.service.namespace.QName; @@ -52,7 +50,7 @@ public class SecurityClearanceServiceImpl extends ServiceBaseImpl implements Sec public void setClassificationService(ClassificationService service) { this.classificationService = service; } public void setPersonService (PersonService service) { this.personService = service; } - public Pair getUserSecurityClearance() + public SecurityClearance getUserSecurityClearance() { final String currentUser = authenticationUtil.getFullyAuthenticatedUser(); Objects.requireNonNull(currentUser, "Fully authenticated user is null, which is not allowed."); @@ -60,7 +58,7 @@ public class SecurityClearanceServiceImpl extends ServiceBaseImpl implements Sec return getUserSecurityClearance(currentUser); } - private Pair getUserSecurityClearance(final String userName) + private SecurityClearance getUserSecurityClearance(final String userName) { final NodeRef personNode = personService.getPerson(userName, false); final PersonInfo personInfo = personService.getPerson(personNode); @@ -76,23 +74,23 @@ public class SecurityClearanceServiceImpl extends ServiceBaseImpl implements Sec } else { classificationLevel = classificationService.getDefaultClassificationLevel(); } - return new Pair<>(personInfo, classificationLevel); + return new SecurityClearance(personInfo, classificationLevel); } - public PagingResults> getUsersSecurityClearance(String userNameFragment, - boolean sortAscending, - PagingRequest req) + public PagingResults getUsersSecurityClearance(String userNameFragment, + boolean sortAscending, + PagingRequest req) { final List filterProps = asList(PROP_USERNAME, PROP_FIRSTNAME, PROP_LASTNAME); final List> sortProps = asList(new Pair<>(PROP_USERNAME, sortAscending)); final PagingResults p = personService.getPeople(userNameFragment, filterProps, sortProps, req); - return new PagingResults>() + return new PagingResults() { - @Override public List> getPage() + @Override public List getPage() { - List> pcPage= new ArrayList<>(p.getPage().size()); + List pcPage= new ArrayList<>(p.getPage().size()); for (PersonInfo pi : p.getPage()) { pcPage.add(getUserSecurityClearance(pi.getUserName()));