Alan Davis d257bbc9d0 Merged V3.4-BUG-FIX to HEAD
30744: ALF-9524: WCM: Defining a complex type with 'abstract' attribute to true does not allow other complex types to define elements with the same name 
      - Fix by Pavel
   30804: ALF-9524: Fix to NPE by Pavel
   30812: ALF-718: Rules Fire Emails before Transaction Commit/Rollback
   Added tests and moved email sending into a post commit callback.
   30820: Fix for ALF-10516 - 'My Sites' dashlet should indicate it is loading rather than say 'No sites'
   30824: Fixed ALF-10470: DeclaritiveRegistry is looping continuously and re-initializing
    - Only replicate removals for the cache.  The reset always does a remove first.
       <cache 
           name="org.alfresco.cache.webScriptsRegistryCache"
                                    ...
                                    replicateUpdates = false,
                                    ...
       </cache>
   30827: ALF-10513 60k Site Performance: Unable to delete user + request not acknowledged for 2 minutes
     Now about 3 to 4 seconds
   30828: Andy H recommended change to lucene indexer values
   30831: ALF-718: Fix for InvitationServiceImplTest
   Fixes behaviour broken by original fix for ALF-718: the send-after-commit does NOT work for the invitation service, so it was necessary
   to change the code to only send-after-commit in the context of a rule.  
   30843: Fixed ALF-7698 "Defects in tags picker in SHARE." according to feedback provided in ALF-9953 "Decide order where new items shall appear in the object finder."
   30844: ALF-9544 - Inbound email restricts file name to 86 characters or less.
     used QName.createQNameWithValidLocalName()  as suggested.
     added new EmailServiceImplTest
   30849: Fixed ALF-8776 "Rule details dialog handling of apostrophes"
   30862: Merged DEV/TEMPORARY to V3.4-BUG-FIX (with improvements)
      30856: ALF-10288: Regression of ALF-1997: non domain users cannot bypass SSO in Share using /share/page?pt=login
         In SSOAuthenticationFilter.doFilter() method added check (PAGE_SERVLET_PATH.equals(req.getServletPath()) && LOGIN_PATH_INFORMATION.equals(req.getPathInfo()).
   30864: SMTP Server, To and From address format.
     - Out standards for from and to address were stuck in the 1980s!
   30867: ALF-10517 - 'My Content' slow
    - performance improvements by reworking Lucene queries used to retrieve recently modified and recently created content for a user
    - converted queries to fts-alfresco
    - improvements from 3.5sec per page render down to 1.1 secs
   30874: MERGE DEV to V3.4-BUG-FIX
         30851 : ALF-9558 - Unchecked Return Value
   30882: - ScriptGroup.isRootGroup() now stops if it finds a single parent rather than doing a size on all parent groups.
     Not sure the logic (not changed by me) is correct as it includes parents that are site groups.
   - SiteServiceImpl now only gets the specified number of sites. Sometimes it would have return more if AuthorityDAOImpl
     switched approach used to get containing authorities.
   - Overloaded getContainingAuthorities to take a size parameter, deprecated other and switched calling code
     Done for isRootGroup activity.
   30910: - Fix build error
   - Found a better way to use filter in AuthorityDAOImpl.getContainingAuthoritiesInZone
     so that we don't need to reset the filter, by using the +ve hits from first approach
     if we switch to second approach.
   30925: - ALF-10501 60k Site Performance: Searching for a group to add to a site with a value that matches a few sites takes 2 minutes
     ALF-10502 60k Site Performance: Admin Console | Groups: search with a value that matches a few sites takes 70 seconds
     ALF-10504 60k Site Performance: Admin Console | Groups | Browse Groups page appears corrupt as it never finishes loading
     - All the above were slow as a result of ScriptGroup.isRootGroup()
     - ScriptGroup.isRootGroup() and ScriptGroup.isAdminGroup() removed - Checked with KevinR that these are not needed any more.
     - Removed code added on 30 Sep 11 to support isRootGroup (the size parameter to AuthorityDAO.getContainingAuthorities)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30935 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2011-10-03 16:23:14 +00:00

365 lines
15 KiB
Java

/*
* Copyright (C) 2005-2010 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.repo.preference;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.security.authentication.AuthenticationContext;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.service.cmr.preference.PreferenceService;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.AccessStatus;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.cmr.security.PersonService;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Preference Service Implementation
*
* @author Roy Wetherall
*/
public class PreferenceServiceImpl implements PreferenceService
{
/** Node service */
private NodeService nodeService;
/** Content service */
private ContentService contentService;
/** Person service */
private PersonService personService;
/** Permission Service */
private PermissionService permissionService;
/** Authentication Service */
private AuthenticationContext authenticationContext;
/**
* Set the node service
*
* @param nodeService the node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* Set the content service
*
* @param contentService the content service
*/
public void setContentService(ContentService contentService)
{
this.contentService = contentService;
}
/**
* Set the person service
*
* @param personService the person service
*/
public void setPersonService(PersonService personService)
{
this.personService = personService;
}
/**
* Set the permission service
*
* @param permissionService the permission service
*/
public void setPermissionService(PermissionService permissionService)
{
this.permissionService = permissionService;
}
/**
* Set the authentication component
*
* @param authenticationContext the authentication component
*/
public void setAuthenticationContext(AuthenticationContext authenticationContext)
{
this.authenticationContext = authenticationContext;
}
/**
* @see org.alfresco.service.cmr.preference.PreferenceService#getPreferences(java.lang.String)
*/
public Map<String, Serializable> getPreferences(String userName)
{
return getPreferences(userName, null);
}
/**
* @see org.alfresco.repo.person.PersonService#getPreferences(java.lang.String, java.lang.String)
*/
@SuppressWarnings("unchecked")
public Map<String, Serializable> getPreferences(String userName, String preferenceFilter)
{
Map<String, Serializable> preferences = new HashMap<String, Serializable>(20);
// Get the user node reference
NodeRef personNodeRef = this.personService.getPerson(userName);
if (personNodeRef == null)
{
throw new AlfrescoRuntimeException("Can not get preferences for " + userName + " because he/she does not exist.");
}
try
{
// Check for preferences aspect
if (this.nodeService.hasAspect(personNodeRef, ContentModel.ASPECT_PREFERENCES) == true)
{
// Get the preferences for this user
JSONObject jsonPrefs = new JSONObject();
ContentReader reader = this.contentService.getReader(personNodeRef, ContentModel.PROP_PREFERENCE_VALUES);
if (reader != null)
{
jsonPrefs = new JSONObject(reader.getContentString());
}
// Build hash from preferences stored in the repository
Iterator<String> keys = jsonPrefs.keys();
while (keys.hasNext())
{
String key = (String)keys.next();
if (preferenceFilter == null ||
preferenceFilter.length() == 0 ||
matchPreferenceNames(key, preferenceFilter) == true)
{
preferences.put(key, (Serializable)jsonPrefs.get(key));
}
}
}
}
catch (JSONException exception)
{
throw new AlfrescoRuntimeException("Can not get preferences for " + userName + " because there was an error pasing the JSON data.", exception);
}
return preferences;
}
/**
* Matches the preference name to the partial preference name provided
*
* @param name preference name
* @param matchTo match to the partial preference name provided
* @return boolean true if matches, false otherwise
*/
private boolean matchPreferenceNames(String name, String matchTo)
{
boolean result = true;
// Split strings
name = name.replace(".", "+");
String[] nameArr = name.split("\\+");
matchTo = matchTo.replace(".", "+");
String[] matchToArr = matchTo.split("\\+");
int index = 0;
for (String matchToElement : matchToArr)
{
if (matchToElement.equals(nameArr[index]) == false)
{
result = false;
break;
}
index ++;
}
return result;
}
/**
* @see org.alfresco.repo.person.PersonService#setPreferences(java.lang.String, java.util.HashMap)
*/
public void setPreferences(final String userName, final Map<String, Serializable> preferences)
{
// Get the user node reference
final NodeRef personNodeRef = this.personService.getPerson(userName);
if (personNodeRef == null)
{
throw new AlfrescoRuntimeException("Can not update preferences for " + userName + " because he/she does not exist.");
}
// Can only set preferences if the currently logged in user matches the user name being updated or
// the user already has write permissions on the person node
String currentUserName = AuthenticationUtil.getFullyAuthenticatedUser();
if (authenticationContext.isSystemUserName(currentUserName) == true ||
permissionService.hasPermission(personNodeRef, PermissionService.WRITE) == AccessStatus.ALLOWED ||
userName.equals(currentUserName) == true)
{
AuthenticationUtil.runAs(new RunAsWork<Object>()
{
public Object doWork() throws Exception
{
// Apply the preferences aspect if required
if (PreferenceServiceImpl.this.nodeService.hasAspect(personNodeRef, ContentModel.ASPECT_PREFERENCES) == false)
{
PreferenceServiceImpl.this.nodeService.addAspect(personNodeRef, ContentModel.ASPECT_PREFERENCES, null);
}
try
{
// Get the current preferences
JSONObject jsonPrefs = new JSONObject();
ContentReader reader = PreferenceServiceImpl.this.contentService.getReader(personNodeRef, ContentModel.PROP_PREFERENCE_VALUES);
if (reader != null)
{
jsonPrefs = new JSONObject(reader.getContentString());
}
// Update with the new preference values
for (Map.Entry<String, Serializable> entry : preferences.entrySet())
{
jsonPrefs.put(entry.getKey(), entry.getValue());
}
// Save the updated preferences
ContentWriter contentWriter = PreferenceServiceImpl.this.contentService.getWriter(personNodeRef, ContentModel.PROP_PREFERENCE_VALUES, true);
contentWriter.setEncoding("UTF-8");
contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
contentWriter.putContent(jsonPrefs.toString());
}
catch (JSONException exception)
{
throw new AlfrescoRuntimeException("Can not update preferences for " + userName + " because there was an error pasing the JSON data.", exception);
}
return null;
}
}, AuthenticationUtil.SYSTEM_USER_NAME);
}
else
{
// The current user does not have sufficient permissions to update the preferences for this user
throw new AlfrescoRuntimeException("The current user " + currentUserName + " does not have sufficient permissions to update the preferences of the user " + userName);
}
}
/**
* @see org.alfresco.service.cmr.preference.PreferenceService#clearPreferences(java.lang.String)
*/
public void clearPreferences(String userName)
{
clearPreferences(userName, null);
}
/**
* @see org.alfresco.repo.person.PersonService#clearPreferences(java.lang.String, java.lang.String)
*/
public void clearPreferences(final String userName, final String preferenceFilter)
{
// Get the user node reference
final NodeRef personNodeRef = this.personService.getPerson(userName);
if (personNodeRef == null)
{
throw new AlfrescoRuntimeException("Can not update preferences for " + userName + " because he/she does not exist.");
}
// Can only set preferences if the currently logged in user matches the user name being updated or
// the user already has write permissions on the person node
String currentUserName = AuthenticationUtil.getFullyAuthenticatedUser();
if (authenticationContext.isSystemUserName(currentUserName) == true ||
permissionService.hasPermission(personNodeRef, PermissionService.WRITE) == AccessStatus.ALLOWED ||
userName.equals(currentUserName) == true)
{
AuthenticationUtil.runAs(new RunAsWork<Object>()
{
public Object doWork() throws Exception
{
if (PreferenceServiceImpl.this.nodeService.hasAspect(personNodeRef, ContentModel.ASPECT_PREFERENCES) == true)
{
try
{
JSONObject jsonPrefs = new JSONObject();
if (preferenceFilter != null && preferenceFilter.length() != 0)
{
// Get the current preferences
ContentReader reader = PreferenceServiceImpl.this.contentService.getReader(personNodeRef, ContentModel.PROP_PREFERENCE_VALUES);
if (reader != null)
{
jsonPrefs = new JSONObject(reader.getContentString());
}
// Remove the prefs that match the filter
List<String> removeKeys = new ArrayList<String>(10);
Iterator<String> keys = jsonPrefs.keys();
while (keys.hasNext())
{
String key = (String)keys.next();
if (preferenceFilter == null ||
preferenceFilter.length() == 0 ||
matchPreferenceNames(key, preferenceFilter) == true)
{
removeKeys.add(key);
}
}
for (String removeKey : removeKeys)
{
jsonPrefs.remove(removeKey);
}
}
// Put the updated JSON back into the repo
ContentWriter contentWriter = PreferenceServiceImpl.this.contentService.getWriter(personNodeRef, ContentModel.PROP_PREFERENCE_VALUES, true);
contentWriter.setEncoding("UTF-8");
contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
contentWriter.putContent(jsonPrefs.toString());
}
catch (JSONException exception)
{
throw new AlfrescoRuntimeException("Can not update preferences for " + userName + " because there was an error pasing the JSON data.", exception);
}
}
return null;
}
}, AuthenticationUtil.getAdminUserName());
}
else
{
// The current user does not have sufficient permissions to update the preferences for this user
throw new AlfrescoRuntimeException("The current user " + currentUserName + " does not have sufficient permissions to update the preferences of the user " + userName);
}
}
}