org.randombits.supplier.confluence.user.UserSupplier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of supplier-confluence Show documentation
Show all versions of supplier-confluence Show documentation
This plugin provides Confluence-specific Suppliers.
/*
* Copyright (c) 2007, CustomWare Asia Pacific
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of "CustomWare Asia Pacific" nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.randombits.supplier.confluence.user;
import com.atlassian.confluence.core.ContentEntityManager;
import com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.core.persistence.ContentEntityObjectDao;
import com.atlassian.confluence.labels.Label;
import com.atlassian.confluence.labels.LabelManager;
import com.atlassian.confluence.labels.LabelParser;
import com.atlassian.confluence.labels.ParsedLabelName;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.security.Permission;
import com.atlassian.confluence.security.PermissionManager;
import com.atlassian.confluence.security.login.LoginInfo;
import com.atlassian.confluence.security.login.LoginManager;
import com.atlassian.confluence.setup.settings.Settings;
import com.atlassian.confluence.setup.settings.SettingsManager;
import com.atlassian.confluence.spaces.Space;
import com.atlassian.confluence.spaces.SpaceDescription;
import com.atlassian.confluence.spaces.SpaceManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.PersonalInformation;
import com.atlassian.confluence.user.PersonalInformationManager;
import com.atlassian.confluence.user.UserPreferencesKeys;
import com.atlassian.confluence.user.actions.ProfilePictureInfo;
import com.atlassian.user.EntityException;
import com.atlassian.user.Group;
import com.atlassian.user.User;
import com.atlassian.user.search.page.Pager;
import com.opensymphony.module.propertyset.PropertyException;
import com.opensymphony.module.propertyset.PropertySet;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.iterators.FilterIterator;
import org.apache.log4j.Logger;
import org.randombits.supplier.confluence.AbstractConfluenceSupplier;
import org.randombits.supplier.core.DisplayableSupplier;
import org.randombits.supplier.core.LinkableSupplier;
import org.randombits.supplier.core.SupplierContext;
import org.randombits.supplier.core.SupplierException;
import org.randombits.supplier.core.annotate.KeyValue;
import org.randombits.supplier.core.annotate.SupplierKey;
import org.randombits.supplier.core.annotate.SupplierPrefix;
import org.randombits.supplier.core.annotate.SupportedTypes;
import org.randombits.utils.lang.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import javax.mail.internet.InternetAddress;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/**
* Provides information about users.
*
* @author David Peterson
*/
@SupplierPrefix("user")
@SupportedTypes(User.class)
public class UserSupplier extends AbstractConfluenceSupplier implements LinkableSupplier, DisplayableSupplier {
private static final Logger LOG = Logger.getLogger( UserSupplier.class );
private ContentEntityManager contentEntityManager;
private ContentEntityObjectDao contentEntityObjectDao;
private PageManager pageManager;
private PersonalInformationManager personalInformationManager;
private SettingsManager settingsManager;
private SpaceManager spaceManager;
private LoginManager loginManager;
@Autowired
public void setContentEntityManager(@Qualifier("contentEntityManager") ContentEntityManager contentEntityManager){
this.contentEntityManager = contentEntityManager ;
}
@Autowired
public void setContentEntityObjectDao(
ContentEntityObjectDao contentEntityObjectDao) {
this.contentEntityObjectDao = contentEntityObjectDao;
}
@Autowired
public void setPageManager(PageManager pageManager) {
this.pageManager = pageManager;
}
@Autowired
public void setPersonalInformationManager(
PersonalInformationManager personalInformationManager) {
this.personalInformationManager = personalInformationManager;
}
@Autowired
public void setSettingsManager(SettingsManager settingsManager) {
this.settingsManager = settingsManager;
}
@Autowired
public void setSpaceManager(SpaceManager spaceManager) {
this.spaceManager = spaceManager;
}
@Autowired
public void setLoginManager(LoginManager loginManager) {
this.loginManager = loginManager;
}
@SupplierKey("personal space")
@API("1.0.0")
public Space getPersonalSpace( @KeyValue User user ) {
return getSpaceManager().getPersonalSpace( user );
}
@SupplierKey("is deactivated")
@API("1.0.0")
public boolean isDeactivated( @KeyValue User user ) {
return getUserAccessor().isDeactivated( user );
}
@SupplierKey({"name", "username"})
@API("1.0.0")
public String getName( @KeyValue User user ) {
return user.getName();
}
@SupplierKey("full name")
@API("1.0.0")
public String getFullName( @KeyValue User user ) {
return user.getFullName();
}
@SupplierKey("authored pages count")
@API("1.0.0")
public Integer getAuthoredPagesCount( @KeyValue User user ) {
if ( getPageManager() != null )
return getPageManager().getAuthoredPagesCountByUser( user.getName() );
return null;
}
@SuppressWarnings("unchecked")
@SupplierKey("recently modified content")
@API("1.0.0")
public Iterator getRecentlyModifiedContent( @KeyValue User user ) {
Iterator content = contentEntityManager.getRecentlyModifiedEntitiesForUser( user
.getName() );
final User currentUser = AuthenticatedUserThreadLocal.getUser();
return new FilterIterator( content, new Predicate() {
public boolean evaluate( Object value ) {
return getPermissionManager().hasPermission( currentUser, Permission.VIEW, value );
}
} );
}
@SupplierKey("signup date")
@API("1.0.0")
public Date getSignupDate( @KeyValue User user ) {
try {
return getDateProperty( user, UserPreferencesKeys.PROPERTY_USER_SIGNUP_DATE );
} catch ( Exception e ) {
LOG.warn( "An error occurred trying to retrieve signup date: " + e, e );
}
return null;
}
@SupplierKey("last login date")
@API("1.0.0")
public Date getLastLoginDate( @KeyValue User user ) {
LoginInfo info = loginManager.getLoginInfo( user.getName() );
if ( info != null ) {
return info.getLastSuccessfulLoginDate();
}
return null;
}
@SupplierKey("previous login date")
@API("1.0.0")
public Date getPreviousLoginDate( @KeyValue User user ) {
LoginInfo info = loginManager.getLoginInfo( user.getName() );
if ( info != null ) {
return info.getPreviousSuccessfulLoginDate();
}
return null;
}
@SupplierKey("last failed login date")
@API("1.0.0")
public Date getLastFailedLoginDate( @KeyValue User user ) {
LoginInfo info = loginManager.getLoginInfo( user.getName() );
if ( info != null ) {
return info.getLastFailedLoginDate();
}
return null;
}
@SupplierKey("current failed login count")
@API("1.0.0")
public Integer getCurrentFailedLoginCount( @KeyValue User user ) {
LoginInfo info = loginManager.getLoginInfo( user.getName() );
if ( info != null ) {
return info.getCurrentFailedLoginCount();
}
return null;
}
@SupplierKey("total failed login count")
@API("1.0.0")
public Integer getTotalFailedLoginCount( @KeyValue User user ) {
LoginInfo info = loginManager.getLoginInfo( user.getName() );
if ( info != null ) {
return info.getTotalFailedLoginCount();
}
return null;
}
/**
* A backward compatibility method 'signup' and 'last login' date
* properties are now actual Date objects but were stored as longs before.
* This method checks the type of the property before fetching the value
* with the appropriate getter method
*
* @param user The user.
* @param propertyKey The property key.
* @return The date property, if set.
*/
private Date getDateProperty( User user, String propertyKey ) {
PropertySet propertySet = getUserAccessor().getPropertySet( user );
if ( propertySet.exists( propertyKey ) ) {
try {
long aLong = propertySet.getLong( propertyKey );
if ( aLong > 0 )
return new Date( aLong );
else
// really ugly, but no way around it.
// getPropertySet().getType(key) is broken, so we can't
// check for types
return propertySet.getDate( propertyKey );
} catch ( PropertyException e ) {
return propertySet.getDate( propertyKey );
}
} else
return null;
}
@SupplierKey({"favorite content", "favourite content"})
@API("1.0.0")
public List getFavouriteContent( @KeyValue User user ) {
if ( isCurrentUser( user ) || isGlobalAdministrator( getCurrentUser() ) ) {
Label favourite = getFavouriteLabel( user );
if ( favourite != null ) {
@SuppressWarnings("unchecked")
List contents = getLabelManager().getCurrentContentForLabel( favourite );
contents = removeSpaceDescriptions( contents );
return getPermissionManager().getPermittedEntities( getCurrentUser(), Permission.VIEW, contents );
}
}
return null;
}
private List removeSpaceDescriptions( List contents ) {
Iterator i = contents.iterator();
while ( i.hasNext() ) {
Object object = i.next();
if ( object instanceof SpaceDescription )
i.remove();
}
return contents;
}
private boolean isCurrentUser( User user ) {
return user != null && user.equals( getCurrentUser() );
}
private Label getFavouriteLabel( User user ) {
return getLabelManager().getLabel(
new ParsedLabelName( LabelManager.FAVOURITE_LABEL, user.getName(),
LabelParser.PERSONAL_LABEL_PREFIX ) );
}
@SupplierKey({"favorite spaces", "favourite spaces"})
@API("1.0.0")
public List getFavouriteSpaces( @KeyValue User user ) {
if ( isCurrentUser( user ) || isGlobalAdministrator( getCurrentUser() ) ) {
return getLabelManager().getFavouriteSpaces( user.getName() );
}
return null;
}
@SupplierKey("authored pages")
@API("1.0.0")
public List getUserAuthoredPages( @KeyValue User user ) {
@SuppressWarnings("unchecked")
List result = contentEntityObjectDao.getContentAuthoredByUser( user.getName() );
if ( result == null || result.isEmpty() )
//noinspection unchecked
return Collections.EMPTY_LIST;
return getPermissionManager().getPermittedEntities( AuthenticatedUserThreadLocal.getUser(),
Permission.VIEW, result );
}
@SupplierKey("personal information")
@API("1.0.0")
public PersonalInformation getUserPersonalInformation( @KeyValue User user ) {
return personalInformationManager.getPersonalInformation( user );
}
@SupplierKey("is removable")
@API("1.0.0")
public boolean isUserRemovable( @KeyValue User user ) throws SupplierException {
try {
return getUserAccessor().isUserRemovable( user );
} catch ( EntityException e ) {
throw new SupplierException( "Error while testing if user is removable.", e );
}
}
@SupplierKey("groups")
@API("1.0.0")
public List getUserGroups( @KeyValue User user ) {
if ( canViewPrivateInfo( user ) ) {
Pager groups = getUserAccessor().getGroups( user );
List groupList = new java.util.ArrayList();
for ( Group group : groups ) {
groupList.add( group );
}
return groupList;
}
return null;
}
private boolean isGlobalAdministrator( User user ) {
return getPermissionManager().hasPermission( user,
Permission.ADMINISTER,
PermissionManager.TARGET_APPLICATION );
}
private boolean canViewPrivateInfo( User user ) {
User currentUser = getCurrentUser();
return currentUser != null && ( currentUser.equals( user ) || isGlobalAdministrator( currentUser ) );
}
@SuppressWarnings("unchecked")
@SupplierKey("labels")
@API("1.0.0")
public List