001/* 002 * Copyright 2016 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.core.user.population; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Optional; 021 022import org.apache.cocoon.components.LifecycleHelper; 023import org.apache.commons.lang3.StringUtils; 024 025import org.ametys.core.authentication.CredentialProvider; 026import org.ametys.core.user.directory.UserDirectory; 027import org.ametys.runtime.i18n.I18nizableText; 028 029/** 030 * This class represents a population of users. 031 */ 032public class UserPopulation 033{ 034 /** The id */ 035 protected String _id; 036 037 /** The label */ 038 protected I18nizableText _label; 039 040 /** The enabled nature of the population. True if enabled, false otherwise. */ 041 protected boolean _enabled; 042 043 /** The list of the user directories */ 044 protected List<UserDirectory> _userDirectories; 045 046 /** The list of the credential providers */ 047 protected List<CredentialProvider> _credentialProviders; 048 049 /** 050 * Default constructor 051 */ 052 public UserPopulation() 053 { 054 super(); 055 _enabled = true; 056 _userDirectories = new ArrayList<>(); 057 _credentialProviders = new ArrayList<>(); 058 } 059 060 /** 061 * Get the label of the population. 062 * @return the label of the population 063 */ 064 public I18nizableText getLabel() 065 { 066 return _label; 067 } 068 069 /** 070 * Set the label of the population. 071 * @param label the label 072 */ 073 public void setLabel(I18nizableText label) 074 { 075 _label = label; 076 } 077 078 /** 079 * Tells if the population is enabled. 080 * @return True if the population is enabled, false otherwise 081 */ 082 public boolean isEnabled() 083 { 084 return _enabled; 085 } 086 087 /** 088 * Enables/disables a population. 089 * @param enabled True to enable the population, false to disable it. 090 */ 091 public void enable(boolean enabled) 092 { 093 this._enabled = enabled; 094 } 095 096 /** 097 * Get the id of the population. 098 * @return the id of the population 099 */ 100 public String getId() 101 { 102 return _id; 103 } 104 105 /** 106 * Set the id of the population. 107 * @param id the id 108 */ 109 public void setId(String id) 110 { 111 _id = id; 112 } 113 114 /** 115 * Get the associated {@link UserDirectory}s 116 * @return The associated user directories 117 */ 118 public List<UserDirectory> getUserDirectories() 119 { 120 return _userDirectories; 121 } 122 123 124 /** 125 * Get the selected UserDirectory 126 * @param id The id of the directory to get 127 * @return The associated user directory or null 128 */ 129 public UserDirectory getUserDirectory(String id) 130 { 131 Optional<UserDirectory> findAny = _userDirectories.stream().filter(ud -> StringUtils.equals(ud.getId(), id)).findAny(); 132 if (findAny.isPresent()) 133 { 134 return findAny.get(); 135 } 136 return null; 137 } 138 139 /** 140 * Set the user directories 141 * @param userDirectories The list of {@link UserDirectory}s to set. 142 */ 143 public void setUserDirectories(List<UserDirectory> userDirectories) 144 { 145 resetUserDirectories(); 146 _userDirectories = userDirectories; 147 } 148 149 /** 150 * Reset the user directories, i.e. remove all the user directories linked to this population. 151 */ 152 public void resetUserDirectories() 153 { 154 for (UserDirectory ud : _userDirectories) 155 { 156 LifecycleHelper.dispose(ud); 157 } 158 159 _userDirectories.clear(); 160 } 161 162 /** 163 * Get the associated {@link CredentialProvider}s 164 * @return The associated credential providers 165 */ 166 public List<CredentialProvider> getCredentialProviders() 167 { 168 return _credentialProviders; 169 } 170 171 /** 172 * Get the selected CredentialProvider 173 * @param id The id of the credential provider to get 174 * @return The associated credential provider or null 175 */ 176 public CredentialProvider getCredentialProvider(String id) 177 { 178 Optional<CredentialProvider> findAny = _credentialProviders.stream().filter(cp -> StringUtils.equals(cp.getId(), id)).findAny(); 179 if (findAny.isPresent()) 180 { 181 return findAny.get(); 182 } 183 return null; 184 } 185 186 /** 187 * Set the credential providers 188 * @param credentialProviders The list of {@link CredentialProvider}s to set 189 */ 190 public void setCredentialProviders(List<CredentialProvider> credentialProviders) 191 { 192 resetCredentialProviders(); 193 _credentialProviders = credentialProviders; 194 } 195 196 /** 197 * Reset the credential providers, i.e. remove all the credential providers linked to this population. 198 */ 199 public void resetCredentialProviders() 200 { 201 for (CredentialProvider cp : _credentialProviders) 202 { 203 LifecycleHelper.dispose(cp); 204 } 205 206 _credentialProviders.clear(); 207 } 208 209 /** 210 * Dispose the user directories and credential providers of this population. 211 */ 212 public void dispose() 213 { 214 resetUserDirectories(); 215 resetCredentialProviders(); 216 } 217 218 @Override 219 public String toString() 220 { 221 return super.toString() + "[" + _id + "]"; 222 } 223}