001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.userdirectory.synchronize; 017 018import org.apache.avalon.framework.component.Component; 019import org.apache.commons.lang.StringUtils; 020import org.slf4j.Logger; 021 022import org.ametys.cms.repository.ModifiableDefaultContent; 023import org.ametys.core.user.UserIdentity; 024import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata; 025import org.ametys.runtime.plugin.component.AbstractLogEnabled; 026 027/** 028 * Helper for user directory. 029 */ 030public class UserSCCHelper extends AbstractLogEnabled implements Component 031{ 032 /** The avalon role. */ 033 public static final String ROLE = UserSCCHelper.class.getName(); 034 035 /** The unique id metadata name for content type user */ 036 public static final String USER_UNIQUE_ID_METADATA_NAME = "uniqueId"; 037 038 /** The user metadata name */ 039 public static final String USERS_METADATA_NAME = "user"; 040 041 /** The login metadata name */ 042 public static final String USERS_LOGIN_METADATA_NAME = "login"; 043 044 /** The populationId metadata name */ 045 public static final String USERS_POPULATION_METADATA_NAME = "population"; 046 047 /** 048 * Synchronize user metadata 049 * @param newLogin the new login 050 * @param newPopulation the new population 051 * @param content the content 052 * @param logger the logger 053 * @return true if changes are made 054 */ 055 @SuppressWarnings("deprecation") 056 public boolean synchronizeUserMetadata(String newLogin, String newPopulation, ModifiableDefaultContent content, Logger logger) 057 { 058 boolean hasChanges = false; 059 060 ModifiableCompositeMetadata metadataHolder = content.getMetadataHolder(); 061 UserIdentity user = metadataHolder.getUser(USERS_METADATA_NAME, null); 062 if (user == null) 063 { 064 if (StringUtils.isNotBlank(newLogin) && StringUtils.isNotBlank(newPopulation)) 065 { 066 UserIdentity newUser = new UserIdentity(newLogin, newPopulation); 067 metadataHolder.setMetadata(USERS_METADATA_NAME, newUser); 068 hasChanges = true; 069 } 070 } 071 else if (!StringUtils.equals(user.getLogin(), newLogin) || !StringUtils.equals(user.getPopulationId(), newPopulation)) 072 { 073 UserIdentity newUser = new UserIdentity(newLogin, newPopulation); 074 metadataHolder.setMetadata(USERS_LOGIN_METADATA_NAME, newUser); 075 hasChanges = true; 076 } 077 078 return hasChanges; 079 } 080}