001/* 002 * Copyright 2020 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.workspaces.project; 017 018import java.time.ZonedDateTime; 019import java.util.ArrayList; 020import java.util.Arrays; 021import java.util.Collection; 022import java.util.Collections; 023import java.util.HashMap; 024import java.util.HashSet; 025import java.util.Iterator; 026import java.util.LinkedHashSet; 027import java.util.List; 028import java.util.Map; 029import java.util.Objects; 030import java.util.Optional; 031import java.util.Set; 032import java.util.function.Predicate; 033import java.util.regex.Pattern; 034import java.util.stream.Collectors; 035import java.util.stream.Stream; 036import java.util.stream.StreamSupport; 037 038import javax.jcr.Node; 039import javax.jcr.PathNotFoundException; 040import javax.jcr.Property; 041import javax.jcr.RepositoryException; 042import javax.jcr.Session; 043import javax.jcr.Value; 044 045import org.apache.avalon.framework.activity.Initializable; 046import org.apache.avalon.framework.component.Component; 047import org.apache.avalon.framework.context.Context; 048import org.apache.avalon.framework.context.ContextException; 049import org.apache.avalon.framework.context.Contextualizable; 050import org.apache.avalon.framework.logger.AbstractLogEnabled; 051import org.apache.avalon.framework.service.ServiceException; 052import org.apache.avalon.framework.service.ServiceManager; 053import org.apache.avalon.framework.service.Serviceable; 054import org.apache.cocoon.components.ContextHelper; 055import org.apache.cocoon.environment.Request; 056import org.apache.commons.collections.CollectionUtils; 057import org.apache.commons.lang3.ArrayUtils; 058import org.apache.commons.lang3.StringUtils; 059import org.apache.commons.lang3.tuple.Pair; 060 061import org.ametys.cms.tag.Tag; 062import org.ametys.core.cache.AbstractCacheManager; 063import org.ametys.core.cache.AbstractCacheManager.CacheType; 064import org.ametys.core.cache.Cache; 065import org.ametys.core.cache.CacheException; 066import org.ametys.core.group.GroupDirectoryContextHelper; 067import org.ametys.core.group.GroupIdentity; 068import org.ametys.core.observation.Event; 069import org.ametys.core.observation.ObservationManager; 070import org.ametys.core.observation.Observer; 071import org.ametys.core.right.RightManager; 072import org.ametys.core.right.RightManager.RightResult; 073import org.ametys.core.ui.Callable; 074import org.ametys.core.user.CurrentUserProvider; 075import org.ametys.core.user.UserIdentity; 076import org.ametys.core.user.population.PopulationContextHelper; 077import org.ametys.core.util.I18nUtils; 078import org.ametys.core.util.LambdaUtils; 079import org.ametys.plugins.core.impl.cache.AbstractCacheKey; 080import org.ametys.plugins.core.search.UserAndGroupSearchManager; 081import org.ametys.plugins.core.user.UserHelper; 082import org.ametys.plugins.explorer.ExplorerNode; 083import org.ametys.plugins.explorer.resources.ModifiableResourceCollection; 084import org.ametys.plugins.repository.AmetysObject; 085import org.ametys.plugins.repository.AmetysObjectIterable; 086import org.ametys.plugins.repository.AmetysObjectResolver; 087import org.ametys.plugins.repository.AmetysRepositoryException; 088import org.ametys.plugins.repository.CollectionIterable; 089import org.ametys.plugins.repository.ModifiableAmetysObject; 090import org.ametys.plugins.repository.ModifiableTraversableAmetysObject; 091import org.ametys.plugins.repository.RepositoryConstants; 092import org.ametys.plugins.repository.UnknownAmetysObjectException; 093import org.ametys.plugins.repository.jcr.JCRAmetysObject; 094import org.ametys.plugins.repository.jcr.NodeTypeHelper; 095import org.ametys.plugins.repository.provider.AbstractRepository; 096import org.ametys.plugins.repository.provider.JackrabbitRepository; 097import org.ametys.plugins.repository.provider.WorkspaceSelector; 098import org.ametys.plugins.repository.query.expression.Expression; 099import org.ametys.plugins.repository.query.expression.Expression.Operator; 100import org.ametys.plugins.repository.query.expression.StringExpression; 101import org.ametys.plugins.workspaces.ObservationConstants; 102import org.ametys.plugins.workspaces.catalog.CatalogSiteType; 103import org.ametys.plugins.workspaces.categories.Category; 104import org.ametys.plugins.workspaces.categories.CategoryProviderExtensionPoint; 105import org.ametys.plugins.workspaces.members.JCRProjectMember; 106import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType; 107import org.ametys.plugins.workspaces.members.ProjectMemberManager; 108import org.ametys.plugins.workspaces.members.ProjectMemberManager.ProjectMember; 109import org.ametys.plugins.workspaces.project.modules.WorkspaceModule; 110import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint; 111import org.ametys.plugins.workspaces.project.objects.Project; 112import org.ametys.plugins.workspaces.project.objects.Project.InscriptionStatus; 113import org.ametys.plugins.workspaces.project.rights.ProjectRightHelper; 114import org.ametys.plugins.workspaces.tags.ProjectTagProviderExtensionPoint; 115import org.ametys.plugins.workspaces.util.StatisticColumn; 116import org.ametys.plugins.workspaces.util.StatisticsColumnType; 117import org.ametys.runtime.authentication.AccessDeniedException; 118import org.ametys.runtime.config.Config; 119import org.ametys.runtime.i18n.I18nizableText; 120import org.ametys.runtime.plugin.component.PluginAware; 121import org.ametys.web.repository.page.ModifiablePage; 122import org.ametys.web.repository.page.Page; 123import org.ametys.web.repository.page.PageQueryHelper; 124import org.ametys.web.repository.page.SitemapElement; 125import org.ametys.web.repository.page.ZoneItem; 126import org.ametys.web.repository.site.Site; 127import org.ametys.web.repository.site.SiteDAO; 128import org.ametys.web.repository.site.SiteManager; 129import org.ametys.web.repository.sitemap.Sitemap; 130import org.ametys.web.site.SiteConfigurationManager; 131 132/** 133 * Helper component for managing project workspaces 134 */ 135public class ProjectManager extends AbstractLogEnabled implements Serviceable, Component, Contextualizable, PluginAware, Initializable, Observer 136{ 137 /** Avalon Role */ 138 public static final String ROLE = ProjectManager.class.getName(); 139 140 /** Constant for the {@link Cache} id (the {@link Cache} is in {@link CacheType#REQUEST REQUEST} attribute) for the {@link Project}s objects 141 * in cache by {@link RequestProjectCacheKey} (composition of project name and workspace name). */ 142 public static final String REQUEST_PROJECTBYID_CACHE = ProjectManager.class.getName() + "$ProjectById"; 143 144 /** Constant for the {@link Cache} id for the {@link Project} ids (as {@link String}s) in cache by project name (for whole application). */ 145 public static final String MEMORY_PROJECTIDBYNAMECACHE = ProjectManager.class.getName() + "$UUID"; 146 147 /** Constant for the {@link Cache} id (the {@link Cache} is in {@link CacheType#REQUEST REQUEST} attribute) for the {@link Page}s objects 148 * in cache by {@link RequestModuleCacheKey} (composition of project name and module name). */ 149 public static final String REQUEST_PAGESBYPROJECTANDMODULE_CACHE = ProjectManager.class.getName() + "$PagesByModule"; 150 151 /** Constant for the {@link Cache} id for the {@link Project} ids (as {@link String}s) in cache by project name (for whole application). */ 152 public static final String MEMORY_PAGESBYIDCACHE = ProjectManager.class.getName() + "$PageUUID"; 153 154 /** Constant for the {@link Cache} id for the {@link Project} ids (as {@link String}s) in cache by site name (for whole application). */ 155 public static final String MEMORY_SITEASSOCIATION_CACHE = ProjectManager.class.getName() + "$SiteAssociation"; 156 157 /** Workspaces plugin node name */ 158 private static final String __WORKSPACES_PLUGIN_NODE_NAME = "workspaces"; 159 160 /** Workspaces plugin node name */ 161 private static final String __WORKSPACES_PLUGIN_NODE_TYPE = RepositoryConstants.NAMESPACE_PREFIX + ":unstructured"; 162 163 /** The name of the projects root node */ 164 private static final String __PROJECTS_ROOT_NODE_NAME = "projects"; 165 166 /** The type of the projects root node */ 167 private static final String __PROJECTS_ROOT_NODE_TYPE = RepositoryConstants.NAMESPACE_PREFIX + ":unstructured"; 168 169 /** Constants for tags metadata */ 170 private static final String __PROJECTS_TAGS_PROPERTY = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":tags"; 171 172 private static final String __PAGE_MODULES_VALUE = "workspaces-modules"; 173 174 private static final String __IS_CACHE_FILLED = "###iscachefilled###"; 175 176 /** Ametys object resolver */ 177 protected AmetysObjectResolver _resolver; 178 179 /** The i18n utils. */ 180 protected I18nUtils _i18nUtils; 181 182 /** Site manager */ 183 protected SiteManager _siteManager; 184 185 /** Site DAO */ 186 protected SiteDAO _siteDao; 187 188 /** Site configuration manager */ 189 protected SiteConfigurationManager _siteConfigurationManager; 190 191 /** Module Managers EP */ 192 protected WorkspaceModuleExtensionPoint _moduleManagerEP; 193 194 /** Helper for user population */ 195 protected PopulationContextHelper _populationContextHelper; 196 197 /** Helper for group directory's context */ 198 protected GroupDirectoryContextHelper _groupDirectoryContextHelper; 199 200 /** The project members' manager */ 201 protected ProjectMemberManager _projectMemberManager; 202 203 /** Avalon context */ 204 protected Context _context; 205 206 private ObservationManager _observationManager; 207 208 private CurrentUserProvider _currentUserProvider; 209 210 private ProjectMemberManager _projectMembers; 211 212 private String _pluginName; 213 214 private ProjectRightHelper _projectRightHelper; 215 216 private ProjectTagProviderExtensionPoint _projectTagProviderEP; 217 218 private CategoryProviderExtensionPoint _categoryProviderEP; 219 220 private UserHelper _userHelper; 221 222 private AbstractCacheManager _cacheManager; 223 224 private UserAndGroupSearchManager _userAndGroupSearchManager; 225 226 private JackrabbitRepository _repository; 227 228 private WorkspaceSelector _workspaceSelector; 229 230 private RightManager _rightManager; 231 232 private GroupDirectoryContextHelper _directoryContextHelper; 233 234 @Override 235 public void contextualize(Context context) throws ContextException 236 { 237 _context = context; 238 } 239 240 @Override 241 public void service(ServiceManager manager) throws ServiceException 242 { 243 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 244 _repository = (JackrabbitRepository) manager.lookup(AbstractRepository.ROLE); 245 _workspaceSelector = (WorkspaceSelector) manager.lookup(WorkspaceSelector.ROLE); 246 _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE); 247 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 248 _siteDao = (SiteDAO) manager.lookup(SiteDAO.ROLE); 249 _siteConfigurationManager = (SiteConfigurationManager) manager.lookup(SiteConfigurationManager.ROLE); 250 _projectMembers = (ProjectMemberManager) manager.lookup(ProjectMemberManager.ROLE); 251 _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE); 252 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 253 _moduleManagerEP = (WorkspaceModuleExtensionPoint) manager.lookup(WorkspaceModuleExtensionPoint.ROLE); 254 _projectRightHelper = (ProjectRightHelper) manager.lookup(ProjectRightHelper.ROLE); 255 _projectTagProviderEP = (ProjectTagProviderExtensionPoint) manager.lookup(ProjectTagProviderExtensionPoint.ROLE); 256 _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE); 257 _categoryProviderEP = (CategoryProviderExtensionPoint) manager.lookup(CategoryProviderExtensionPoint.ROLE); 258 _cacheManager = (AbstractCacheManager) manager.lookup(AbstractCacheManager.ROLE); 259 _populationContextHelper = (PopulationContextHelper) manager.lookup(PopulationContextHelper.ROLE); 260 _groupDirectoryContextHelper = (GroupDirectoryContextHelper) manager.lookup(GroupDirectoryContextHelper.ROLE); 261 _projectMemberManager = (ProjectMemberManager) manager.lookup(ProjectMemberManager.ROLE); 262 _userAndGroupSearchManager = (UserAndGroupSearchManager) manager.lookup(UserAndGroupSearchManager.ROLE); 263 _rightManager = (RightManager) manager.lookup(RightManager.ROLE); 264 _directoryContextHelper = (GroupDirectoryContextHelper) manager.lookup(GroupDirectoryContextHelper.ROLE); 265 } 266 267 public void initialize() throws Exception 268 { 269 _createCaches(); 270 _observationManager.registerObserver(this); 271 } 272 273 @Override 274 public void setPluginInfo(String pluginName, String featureName, String id) 275 { 276 _pluginName = pluginName; 277 } 278 279 /** 280 * Enumeration for the profile to assign for new module 281 */ 282 public enum ProfileForNewModule 283 { 284 /** No profile assigned for new modules */ 285 NONE, 286 287 /** Default profile assigned for new modules */ 288 DEFAULT_MEMBER_PROFILE 289 290 } 291 292 /** 293 * Retrieves all projects 294 * @return the projects 295 */ 296 public AmetysObjectIterable<Project> getProjects() 297 { 298 return getProjects(true); 299 } 300 301 /** 302 * Retrieves all projects 303 * @param onlyWorking true to retrieve only working projects with non null sites 304 * @return the projects 305 */ 306 public AmetysObjectIterable<Project> getProjects(boolean onlyWorking) 307 { 308 // As cache is computed from default JCR workspace, we need to filter on sites that exist into the current JCR workspace 309 Set<Project> projects = _getUUIDCache().values().stream() 310 .filter(_resolver::hasAmetysObjectForId) 311 .map(_resolver::<Project>resolveById) 312 // If needed, check if the site is not null, as it can happen if site was deleted from admin side 313 .filter(project -> !onlyWorking || Objects.nonNull(project.getSite())) 314 .collect(Collectors.toSet()); 315 316 return new CollectionIterable<>(projects); 317 } 318 319 /** 320 * Retrieves projects filtered by categories 321 * @param filteredCategories the filtered categories. Can be empty to no filter by categories. 322 * @return the projects 323 */ 324 public List<Project> getProjects(Set<String> filteredCategories) 325 { 326 return getProjects(filteredCategories, null, null, true); 327 } 328 329 /** 330 * Retrieves projects filtered by categories and/or keywords 331 * @param filteredCategories the filtered categories. Can be empty to no filter by categories. 332 * @param filteredKeywords the filtered keywords. Can be empty to no filter by keywords. 333 * @param anyMatch true to get projects matching categories OR keywords OR pattern 334 * @return the projects 335 */ 336 public List<Project> getProjects(Set<String> filteredCategories, Set<String> filteredKeywords, boolean anyMatch) 337 { 338 return getProjects(filteredCategories, filteredKeywords, null, anyMatch); 339 } 340 341 /** 342 * Retrieves projects filtered by categories and/or keywords and/or pattern 343 * @param filteredCategories the filtered categories. Can be empty to no filter by categories. 344 * @param filteredKeywords the filtered keywords. Can be empty to no filter by keywords. 345 * @param pattern to filter on pattern. Can be null or empty to no filter on pattern 346 * @param anyMatch true to get projects matching categories OR keywords OR pattern 347 * @return the projects 348 */ 349 public List<Project> getProjects(Set<String> filteredCategories, Set<String> filteredKeywords, String pattern, boolean anyMatch) 350 { 351 return getProjects(filteredCategories, filteredKeywords, null, anyMatch, false); 352 } 353 354 /** 355 * Retrieves projects filtered by categories and/or keywords and/or pattern 356 * @param filteredCategories the filtered categories. Can be null or empty to no filter by categories. 357 * @param filteredKeywords the filtered keywords. Can be empty to no filter by keywords. 358 * @param pattern to filter on pattern. Can be null or empty to no filter on pattern 359 * @param anyMatch true to get projects matching categories OR keywords OR pattern 360 * @param excludePrivate true to exclude private projects 361 * @return the projects 362 */ 363 public List<Project> getProjects(Set<String> filteredCategories, Set<String> filteredKeywords, String pattern, boolean anyMatch, boolean excludePrivate) 364 { 365 List<Predicate<Project>> filters = new ArrayList<>(); 366 if (filteredCategories != null && !filteredCategories.isEmpty()) 367 { 368 filters.add(p -> !Collections.disjoint(p.getCategories(), filteredCategories)); 369 } 370 371 if (filteredKeywords != null && !filteredKeywords.isEmpty()) 372 { 373 filters.add(p -> !Collections.disjoint(Arrays.asList(p.getKeywords()), filteredKeywords)); 374 } 375 376 Pattern patternFilter = StringUtils.isNotEmpty(pattern) ? Pattern.compile(pattern, Pattern.CASE_INSENSITIVE) : null; 377 if (patternFilter != null) 378 { 379 filters.add(p -> p.getTitle() != null && patternFilter.matcher(p.getTitle()).find() || p.getDescription() != null && patternFilter.matcher(p.getDescription()).find()); 380 } 381 382 Predicate<Project> fullMatch = filters.stream().reduce(anyMatch ? Predicate::or : Predicate::and).orElse(p -> Boolean.TRUE); 383 Predicate<Project> matchStatus = p -> !excludePrivate || p.getInscriptionStatus() != InscriptionStatus.PRIVATE; 384 385 return getProjects() 386 .stream() 387 .filter(matchStatus) 388 .filter(fullMatch) 389 .collect(Collectors.toList()); 390 } 391 392 /** 393 * Get the projects categories 394 * @return the projects categories 395 */ 396 public Set<Category> getProjectsCategories() 397 { 398 return getProjects().stream() 399 .map(Project::getCategories) 400 .flatMap(Collection::stream) 401 .map(id -> _categoryProviderEP.getTag(id, null)) 402 .filter(Objects::nonNull) 403 .collect(Collectors.toSet()); 404 } 405 406 /** 407 * Get the user's projects categories 408 * @param user the user 409 * @return the user's projects categories 410 */ 411 public Set<Category> getUserProjectsCategories(UserIdentity user) 412 { 413 return getUserProjects(user).keySet() 414 .stream() 415 .map(Project::getCategories) 416 .flatMap(Collection::stream) 417 .map(id -> _categoryProviderEP.getTag(id, null)) 418 .filter(Objects::nonNull) 419 .collect(Collectors.toSet()); 420 } 421 422 /** 423 * Get the projects modules 424 * @return the projects modules 425 */ 426 public Set<WorkspaceModule> getProjectsModules() 427 { 428 return getProjects().stream() 429 .map(Project::getModules) 430 .flatMap(Arrays::stream) 431 .map(id -> _moduleManagerEP.<WorkspaceModule>getModule(id)) 432 .filter(Objects::nonNull) 433 .collect(Collectors.toSet()); 434 } 435 436 /** 437 * Get the projects modules for new members (depending on the project configuration) 438 * @param project the project 439 * @return the project modules 440 */ 441 public Set<WorkspaceModule> getProjectModulesForNewMembers(Project project) 442 { 443 Site projectSite = project.getSite(); 444 445 // Check if project site overrides catalog configuration 446 String membersModulesPolicy = projectSite.getValue(ProjectWorkspaceSiteType.PROJECT_OVERRIDE_MEMBERS_MODULES_POLICY_SITE_PARAM, false, ProjectWorkspaceSiteType.MembersModulesPolicy.GLOBAL.name()); 447 448 String membersModulesAsString = null; 449 if (membersModulesPolicy.equals(ProjectWorkspaceSiteType.MembersModulesPolicy.MEMBERS_MODULES_BY_PROJECT.name())) 450 { 451 membersModulesAsString = projectSite.getValue(ProjectWorkspaceSiteType.PROJECT_MEMBERS_MODULES_PROJECT_SITE_PARAM); 452 } 453 else 454 { 455 Site catalogSite = _siteManager.getSite(getCatalogSiteName()); 456 // Use #hasValueOrEmpty because we can't use #getValue with a default value. Indeed, the #getValue use the default value for null value or empty value. 457 membersModulesAsString = catalogSite.hasValueOrEmpty(CatalogSiteType.PROJECT_MEMBERS_MODULES_SITE_PARAM) 458 ? catalogSite.getValue(CatalogSiteType.PROJECT_MEMBERS_MODULES_SITE_PARAM) 459 : String.join(",", _moduleManagerEP.getExtensionsIds()); 460 } 461 462 String[] membersModuleIds = StringUtils.split(StringUtils.defaultString(membersModulesAsString), ","); 463 464 Set<WorkspaceModule> modules = new HashSet<>(); 465 for (String moduleId : membersModuleIds) 466 { 467 if (StringUtils.isNotBlank(moduleId)) 468 { 469 WorkspaceModule module = _moduleManagerEP.getModule(moduleId.trim()); 470 modules.add(module); 471 } 472 } 473 return modules; 474 } 475 476 /** 477 * Get the projects modules 478 * @param user the user 479 * @return the projects modules 480 */ 481 public Set<WorkspaceModule> getUserProjectsModules(UserIdentity user) 482 { 483 return getUserProjects(user).keySet() 484 .stream() 485 .map(Project::getModules) 486 .flatMap(Arrays::stream) 487 .map(id -> _moduleManagerEP.<WorkspaceModule>getModule(id)) 488 .filter(Objects::nonNull) 489 .collect(Collectors.toSet()); 490 } 491 492 /** 493 * Retrieves all projects for client side 494 * @return the projects 495 */ 496 @Callable(rights = "Runtime_Rights_Admin_Access", context = "/admin") 497 public List<Map<String, Object>> getProjectsForClientSide() 498 { 499 return getProjects(false) 500 .stream() 501 .map(p -> getProjectProperties(p)) 502 .collect(Collectors.toList()); 503 } 504 505 506 /** 507 * Retrieves a project by its name 508 * @param projectName The project name 509 * @return the project or <code>null</code> if not found 510 */ 511 public Project getProject(String projectName) 512 { 513 if (StringUtils.isBlank(projectName)) 514 { 515 return null; 516 } 517 518 Request request = _getRequest(); 519 if (request == null) 520 { 521 // There is no request to store cache 522 return _computeProject(projectName); 523 } 524 525 Cache<RequestProjectCacheKey, Project> projectsCache = _getRequestProjectCache(); 526 527 // The site key in the cache is of the form {site + workspace}. 528 String currentWorkspace = _workspaceSelector.getWorkspace(); 529 RequestProjectCacheKey projectKey = RequestProjectCacheKey.of(projectName, currentWorkspace); 530 531 try 532 { 533 Project project = projectsCache.get(projectKey, __ -> _computeProject(projectName)); 534 return project; 535 } 536 catch (CacheException e) 537 { 538 if (e.getCause() instanceof UnknownAmetysObjectException) 539 { 540 throw new UnknownAmetysObjectException(e.getMessage()); 541 } 542 else 543 { 544 throw e; 545 } 546 } 547 } 548 549 /** 550 * Get the user's projects 551 * @param user the user 552 * @return the user's projects 553 */ 554 public Map<Project, MemberType> getUserProjects(UserIdentity user) 555 { 556 return getUserProjects(user, Set.of()); 557 } 558 559 /** 560 * Get the user's projects filtered by categories 561 * @param user the user 562 * @param filteredCategories the filtered categories. Can be empty to no filter by categories 563 * @return the user's projects 564 */ 565 public Map<Project, MemberType> getUserProjects(UserIdentity user, Set<String> filteredCategories) 566 { 567 return getUserProjects(user, filteredCategories, null, null, true); 568 } 569 570 /** 571 * Get the user's projects filtered by categories OR keywords 572 * @param user the user 573 * @param filteredCategories the filtered categories. Can be empty to no filter by categories 574 * @param filteredKeywords the filtered keywords. Can be empty to no filter by keywords 575 * @return the user's projects 576 */ 577 public Map<Project, MemberType> getUserProjects(UserIdentity user, Set<String> filteredCategories, Set<String> filteredKeywords) 578 { 579 return getUserProjects(user, filteredCategories, filteredKeywords, null, true); 580 } 581 582 /** 583 * Get the user's projects filtered by categories, keywords and/or pattern 584 * @param user the user 585 * @param filteredCategories the filtered categories. Can be empty to no filter by categories. 586 * @param filteredKeywords the filtered keywords. Can be empty to no filter by keywords. 587 * @param pattern to filter on pattern. Can be null or empty to no filter on pattern 588 * @param anyMatch true to get projects matching categories OR keywords OR pattern 589 * @return the user's projects 590 */ 591 public Map<Project, MemberType> getUserProjects(UserIdentity user, Set<String> filteredCategories, Set<String> filteredKeywords, String pattern, boolean anyMatch) 592 { 593 return getUserProjects(user, filteredCategories, filteredKeywords, pattern, true, false); 594 } 595 596 /** 597 * Get the user's projects filtered by categories, keywords and/or pattern 598 * @param user the user 599 * @param filteredCategories the filtered categories. Can be empty to no filter by categories. 600 * @param filteredKeywords the filtered keywords. Can be empty to no filter by keywords. 601 * @param pattern to filter on pattern. Can be null or empty to no filter on pattern 602 * @param anyMatch true to get projects matching categories OR keywords OR pattern 603 * @param excludePrivate true to exclude private projects 604 * @return the user's projects 605 */ 606 public Map<Project, MemberType> getUserProjects(UserIdentity user, Set<String> filteredCategories, Set<String> filteredKeywords, String pattern, boolean anyMatch, boolean excludePrivate) 607 { 608 Map<Project, MemberType> userProjects = new HashMap<>(); 609 610 List<Project> projects = getProjects(filteredCategories, filteredKeywords, pattern, anyMatch, excludePrivate); 611 for (Project project : projects) 612 { 613 ProjectMember member = _projectMembers.getProjectMember(project, user); 614 if (member != null) 615 { 616 userProjects.put(project, member.getType()); 617 } 618 } 619 620 return userProjects; 621 } 622 623 /** 624 * Get the projects managed by the user 625 * @param user the user 626 * @return the projects for which the user is a manager 627 */ 628 public List<Project> getManagedProjects(UserIdentity user) 629 { 630 return getManagedProjects(user, Set.of()); 631 } 632 633 /** 634 * Get the projects managed by the user 635 * @param user the user 636 * @param filteredCategories the filtered categories. Can be empty to no filter by categories. 637 * @return the projects for which the user is a manager 638 */ 639 public List<Project> getManagedProjects(UserIdentity user, Set<String> filteredCategories) 640 { 641 return getProjects(filteredCategories) 642 .stream() 643 .filter(p -> ArrayUtils.contains(p.getManagers(), user)) 644 .collect(Collectors.toList()); 645 } 646 647 /** 648 * Returns true if the given project exists. 649 * @param projectName the project name. 650 * @return true if the given project exists. 651 */ 652 public boolean hasProject(String projectName) 653 { 654 Map<String, String> uuidCache = _getUUIDCache(); 655 if (uuidCache.containsKey(projectName)) 656 { 657 // As cache is computed from default JCR workspace, we need to check if the project exists into the current JCR workspace 658 return _resolver.hasAmetysObjectForId(uuidCache.get(projectName)); 659 } 660 return false; 661 } 662 663 /** 664 * Get all managers 665 * @return the managers 666 */ 667 public Set<UserIdentity> getManagers() 668 { 669 return getProjects() 670 .stream() 671 .map(Project::getManagers) 672 .flatMap(Arrays::stream) 673 .collect(Collectors.toSet()); 674 } 675 676 /** 677 * Determines if the current user is a manager of at least one project 678 * @return true if the user is a manager 679 */ 680 public boolean isManager() 681 { 682 return isManager(_currentUserProvider.getUser()); 683 } 684 685 /** 686 * Determines if the user is a manager of at least one project 687 * @param user the user 688 * @return true if the user is a manager 689 */ 690 public boolean isManager(UserIdentity user) 691 { 692 AmetysObjectIterable<Project> projects = getProjects(); 693 for (Project project : projects) 694 { 695 if (isManager(project, user)) 696 { 697 return true; 698 } 699 } 700 return false; 701 } 702 703 /** 704 * Determines if the user is a manager of the project 705 * @param projectName the project name 706 * @param user the user 707 * @return true if the user is a manager 708 */ 709 public boolean isManager(String projectName, UserIdentity user) 710 { 711 Project project = getProject(projectName); 712 if (project != null) 713 { 714 return isManager(project, user); 715 } 716 return false; 717 } 718 719 /** 720 * Determines if the user is a manager of the project 721 * @param project the project 722 * @param user the user 723 * @return true if the user is a manager 724 */ 725 public boolean isManager(Project project, UserIdentity user) 726 { 727 return ArrayUtils.contains(project.getManagers(), user); 728 } 729 730 /** 731 * Can the current user access backoffice on the site of the current project 732 * @param project The non null project to analyse 733 * @return true if the user can access to the backoffice 734 */ 735 public boolean canAccessBO(Project project) 736 { 737 Site site = project.getSite(); 738 if (site == null) 739 { 740 return false; 741 } 742 743 Request request = ContextHelper.getRequest(_context); 744 String currentSiteName = (String) request.getAttribute("siteName"); 745 try 746 { 747 request.setAttribute("siteName", site.getName()); // Setting temporarily this attribute to check user rights on any object on this site 748 return !_rightManager.getUserRights(_currentUserProvider.getUser(), "/cms").isEmpty(); 749 } 750 finally 751 { 752 request.setAttribute("siteName", currentSiteName); 753 } 754 } 755 756 /** 757 * Can the current user leave the project 758 * @param project The non null project to analyse 759 * @return true if the user can leave the project 760 */ 761 public boolean canLeaveProject(Project project) 762 { 763 if (project == null) 764 { 765 return false; 766 } 767 768 Site site = project.getSite(); 769 if (site == null) 770 { 771 return false; 772 } 773 774 UserIdentity userIdentity = _currentUserProvider.getUser(); 775 776 // As a user in a group can't leave the project, check if he is registered as User member 777 Set<ProjectMember> members = _projectMemberManager.getProjectMembers(project, false); 778 ProjectMember projectMember = members.stream() 779 .filter(member -> MemberType.USER == member.getType()) 780 .filter(member -> userIdentity.equals(member.getUser().getIdentity())) 781 .findFirst() 782 .orElse(null); 783 784 // The user is either on a group, or has not been found 785 if (projectMember == null) 786 { 787 return false; 788 } 789 790 // The user is the only remaining manager, and therefore can not leave the project 791 if (_projectMemberManager.isOnlyManager(project, userIdentity)) 792 { 793 return false; 794 } 795 796 return true; 797 } 798 799 /** 800 * Retrieves the mapping of all the projects name with their title on which the current user has access 801 * @return the map (projectName, projectTitle) for all projects 802 */ 803 @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION) 804 public List<Map<String, Object>> getUserProjectsData() 805 { 806 return getUserProjects(_currentUserProvider.getUser()) 807 .keySet() 808 .stream() 809 .map(p -> _project2json(p)) 810 .collect(Collectors.toList()); 811 } 812 813 /** 814 * Retrieves the users that have not been yet added to a project with a given criteria 815 * @param projectName the project name 816 * @param limit limit of request 817 * @param criteria the criteria of the search 818 * @param previousSearchData the previous search data to compute offset. Null if first search 819 * @return list of users 820 */ 821 @SuppressWarnings("unchecked") 822 @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION) 823 public Map<String, Object> searchUserByProject(String projectName, int limit, String criteria, Map<String, Object> previousSearchData) 824 { 825 826 Project project = this.getProject(projectName); 827 828 if (!_projectRightHelper.canAddMember(project)) 829 { 830 throw new AccessDeniedException("User '" + _currentUserProvider.getUser() + "' tried to do read operation without convenient right"); 831 } 832 833 Map<String, Object> results = new HashMap<>(); 834 Site site = project.getSite(); 835 836 Set<String> projectMemberList = _projectMemberManager.getProjectMembers(project, false) 837 .stream() 838 .map(member -> 839 { 840 if (member.getType() == MemberType.USER) 841 { 842 return UserIdentity.userIdentityToString(member.getUser().getIdentity()); 843 } 844 else 845 { 846 GroupIdentity groupIdentityAsString = member.getGroup().getIdentity(); 847 return GroupIdentity.groupIdentityToString(groupIdentityAsString); 848 } 849 }) 850 .collect(Collectors.toSet()); 851 852 Set<String> contexts = new HashSet<>(Arrays.asList("/sites/" + site.getName(), "/sites-fo/" + site.getName())); 853 854 Map<String, Object> params = new HashMap<>(); 855 params.put("pattern", criteria); 856 857 Map<String, Object> result = new HashMap<>(); 858 Map<String, Object> searchData = previousSearchData; 859 List<Map<String, Object>> memberList = new ArrayList<>(); 860 861 Set<String> groupDirectories = _directoryContextHelper.getGroupDirectoriesOnContexts(contexts); 862 Set<String> userPopulations = getPopulation(site, true); 863 864 do 865 { 866 result = _userAndGroupSearchManager.searchUsersAndGroup(userPopulations, groupDirectories, limit - memberList.size(), searchData, params, true); 867 List<Map<String, Object>> filteredMembers = ((List<Map<String, Object>>) result.get("results")) 868 .stream() 869 .filter(member -> 870 { 871 return !projectMemberList.contains(member.get("login") + "#" + member.get("populationId")) && !projectMemberList.contains(member.get("id") + "#" + member.get("groupDirectory")); 872 }).collect(Collectors.toList()); 873 searchData = (Map<String, Object>) result.get("searchData"); 874 memberList.addAll(filteredMembers); 875 } 876 while (!result.containsKey("finished") && memberList.size() < limit); 877 878 results.put("searchData", searchData); 879 results.put("memberList", memberList); 880 return results; 881 } 882 883 /** 884 * Get the populations of the project 885 * @param site the project site 886 * @param excludeConfigurationPopulations true to exclude populations configured in the catalog site or in the project site 887 * @return the populations of the project 888 */ 889 public Set<String> getPopulation(Site site, boolean excludeConfigurationPopulations) 890 { 891 Set<String> contexts = new HashSet<>(Arrays.asList("/sites/" + site.getName(), "/sites-fo/" + site.getName())); 892 893 Set<String> userPopulations = _populationContextHelper.getUserPopulationsOnContexts(contexts, false, true); 894 895 if (excludeConfigurationPopulations) 896 { 897 Site catalogSite = _siteManager.getSite(getCatalogSiteName()); 898 899 // Get the excluded population configuration 900 String excludedPopulationsString = catalogSite.getValue(CatalogSiteType.PROJECT_EXCLUDED_POPULATIONS_SITE_PARAM); 901 902 // Check if project site overrides catalog configuration 903 if (site.getValue(ProjectWorkspaceSiteType.PROJECT_OVERRIDE_EXCLUDED_POPULATIONS_SITE_PARAM, false, false)) 904 { 905 excludedPopulationsString = site.getValue(ProjectWorkspaceSiteType.PROJECT_EXCLUDED_POPULATIONS_PROJECT_SITE_PARAM); 906 } 907 908 List<String> excludedPopulationsList = Arrays.asList(StringUtils.split(StringUtils.defaultString(excludedPopulationsString), ",")); 909 910 if (!excludedPopulationsList.isEmpty()) 911 { 912 userPopulations.removeAll(excludedPopulationsList); 913 } 914 } 915 916 return userPopulations; 917 } 918 919 /** 920 * Retrieves the mapping of all the projects name with their title (regarless user rights) 921 * @return the map (projectName, projectTitle) for all projects 922 */ 923 @Callable(rights = "Runtime_Rights_Admin_Access", context = "/admin") 924 public List<Map<String, Object>> getProjectsData() 925 { 926 return getProjects() 927 .stream() 928 .map(p -> _project2json(p)) 929 .collect(Collectors.toList()); 930 } 931 932 /** 933 * Get the project's main properties as json object 934 * @param project the project 935 * @return the json representation of project 936 */ 937 protected Map<String, Object> _project2json(Project project) 938 { 939 Map<String, Object> json = new HashMap<>(); 940 941 json.put("id", project.getId()); 942 json.put("name", project.getName()); 943 json.put("title", project.getTitle()); 944 json.put("url", getProjectUrl(project, StringUtils.EMPTY)); 945 946 return json; 947 } 948 /** 949 * Retrieves the project names 950 * @return the project names 951 */ 952 @Callable(rights = "Runtime_Rights_Admin_Access", context = "/admin") 953 public Collection<String> getProjectNames() 954 { 955 // As cache is computed from default JCR workspace, we need to filter on sites that exist into the current JCR workspace 956 return _getUUIDCache().entrySet().stream() 957 .filter(e -> _resolver.hasAmetysObjectForId(e.getValue())) 958 .map(Map.Entry::getKey) 959 .collect(Collectors.toList()); 960 } 961 962 /** 963 * Return the root for projects 964 * The root node will be created if necessary 965 * @return The root for projects 966 */ 967 public ModifiableTraversableAmetysObject getProjectsRoot() 968 { 969 try 970 { 971 ModifiableTraversableAmetysObject pluginsNode = _resolver.resolveByPath("/ametys:plugins"); 972 ModifiableTraversableAmetysObject workspacesPluginNode = _getOrCreateObject(pluginsNode, __WORKSPACES_PLUGIN_NODE_NAME, __WORKSPACES_PLUGIN_NODE_TYPE); 973 return _getOrCreateObject(workspacesPluginNode, __PROJECTS_ROOT_NODE_NAME, __PROJECTS_ROOT_NODE_TYPE); 974 } 975 catch (AmetysRepositoryException e) 976 { 977 throw new AmetysRepositoryException("Error getting the projects root node.", e); 978 } 979 } 980 981 /** 982 * Retrieves the standard information of a project 983 * @param projectId Identifier of the project 984 * @return The map of information 985 */ 986 @Callable(rights = "Runtime_Rights_Admin_Access", context = "/admin") 987 public Map<String, Object> getProjectProperties(String projectId) 988 { 989 return getProjectProperties((Project) _resolver.resolveById(projectId)); 990 } 991 992 /** 993 * Retrieves the standard information of a project 994 * @param project The project 995 * @return The map of information 996 */ 997 public Map<String, Object> getProjectProperties(Project project) 998 { 999 Map<String, Object> info = new HashMap<>(); 1000 1001 info.put("id", project.getId()); 1002 info.put("name", project.getName()); 1003 info.put("type", "project"); 1004 1005 info.put("title", project.getTitle()); 1006 info.put("description", project.getDescription()); 1007 info.put("inscriptionStatus", project.getInscriptionStatus().toString()); 1008 info.put("defaultProfile", project.getDefaultProfile()); 1009 1010 info.put("creationDate", project.getCreationDate()); 1011 1012 // check if the project workspace configuration is valid 1013 Site site = project.getSite(); 1014 boolean valid = site != null && _siteConfigurationManager.isSiteConfigurationValid(site); 1015 1016 Set<String> categories = project.getCategories(); 1017 info.put("categories", categories.stream() 1018 .map(c -> _categoryProviderEP.getTag(c, new HashMap<>())) 1019 .filter(Objects::nonNull) 1020 .map(t -> _tag2json(t)) 1021 .collect(Collectors.toList())); 1022 1023 Set<String> tags = project.getTags(); 1024 info.put("tags", tags.stream() 1025 .map(c -> _projectTagProviderEP.getTag(c, new HashMap<>())) 1026 .filter(Objects::nonNull) 1027 .map(t -> _tag2json(t)) 1028 .collect(Collectors.toList())); 1029 1030 info.put("valid", valid); 1031 1032 UserIdentity[] managers = project.getManagers(); 1033 info.put("managers", Arrays.stream(managers) 1034 .map(u -> _userHelper.user2json(u)) 1035 .collect(Collectors.toList())); 1036 1037 Map<String, String> siteProps = new HashMap<>(); 1038 // site map with id ,name, title and url property 1039 // { id: site id, name: site name, title: site title, url: site url } 1040 if (site != null) 1041 { 1042 siteProps.put("id", site.getId()); 1043 siteProps.put("name", site.getName()); 1044 siteProps.put("title", site.getTitle()); 1045 siteProps.put("url", site.getUrl()); 1046 } 1047 info.put("site", siteProps); 1048 1049 return info; 1050 } 1051 1052 private Map<String, Object> _tag2json(Tag tag) 1053 { 1054 Map<String, Object> json = new HashMap<>(); 1055 json.put("id", tag.getId()); 1056 json.put("name", tag.getName()); 1057 json.put("title", tag.getTitle()); 1058 return json; 1059 } 1060 1061 /** 1062 * Get the project URL. 1063 * @param project The project 1064 * @param defaultValue The default value to use if there is no site 1065 * @return The project URL if a site is configured, otherwise return the default value. 1066 */ 1067 public String getProjectUrl(Project project, String defaultValue) 1068 { 1069 Site site = project.getSite(); 1070 if (site == null) 1071 { 1072 return defaultValue; 1073 } 1074 else 1075 { 1076 return site.getUrl(); 1077 } 1078 } 1079 1080 /** 1081 * Create a project 1082 * @param name The project name 1083 * @param title The project title 1084 * @param description The project description 1085 * @param emailList Project mailing list 1086 * @param inscriptionStatus The inscription status of the project 1087 * @param defaultProfile The default profile for new members 1088 * @return A map containing the id of the new project or an error key. 1089 */ 1090 @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION) 1091 public Map<String, Object> createProject(String name, String title, String description, String emailList, String inscriptionStatus, String defaultProfile) 1092 { 1093 checkRightsForProjectCreation(InscriptionStatus.valueOf(inscriptionStatus.toUpperCase()), null); 1094 1095 Map<String, Object> result = new HashMap<>(); 1096 List<String> errors = new ArrayList<>(); 1097 1098 Map<String, Object> additionalValues = new HashMap<>(); 1099 additionalValues.put("description", description); 1100 additionalValues.put("emailList", emailList); 1101 additionalValues.put("inscriptionStatus", inscriptionStatus); 1102 additionalValues.put("defaultProfile", defaultProfile); 1103 1104 Project project = createProject(name, title, additionalValues, null, errors); 1105 1106 if (CollectionUtils.isEmpty(errors)) 1107 { 1108 result.put("id", project.getId()); 1109 } 1110 else 1111 { 1112 result.put("error", errors.get(0)); 1113 } 1114 1115 return result; 1116 } 1117 1118 /** 1119 * Create a project 1120 * @param name The project name 1121 * @param title The project title 1122 * @param additionalValues A list of optional additional values. Accepted values are : description, mailingList, inscriptionStatus, defaultProfile, tags, categoryTags, keywords and language 1123 * @param modulesIds The list of modules to activate. Can be null to activate all modules 1124 * @param errors A list that will be populated with the encountered errors. If null, errors will not be tracked. 1125 * @return The id of the new project 1126 */ 1127 public Project createProject(String name, String title, Map<String, Object> additionalValues, Set<String> modulesIds, List<String> errors) 1128 { 1129 if (StringUtils.isEmpty(title)) 1130 { 1131 throw new IllegalArgumentException(String.format("Cannot create project. Title is mandatory")); 1132 } 1133 1134 ModifiableTraversableAmetysObject projectsRoot = getProjectsRoot(); 1135 1136 // Project name should be unique 1137 if (hasProject(name)) 1138 { 1139 if (getLogger().isWarnEnabled()) 1140 { 1141 getLogger().warn(String.format("A project with the name '%s' already exists", name)); 1142 } 1143 1144 if (errors != null) 1145 { 1146 errors.add("project-exists"); 1147 } 1148 1149 return null; 1150 } 1151 1152 Project project = projectsRoot.createChild(name, Project.NODE_TYPE); 1153 project.setTitle(title); 1154 String description = (String) additionalValues.getOrDefault("description", null); 1155 if (StringUtils.isNotEmpty(description)) 1156 { 1157 project.setDescription(description); 1158 } 1159 String mailingList = (String) additionalValues.getOrDefault("emailList", null); 1160 if (StringUtils.isNotEmpty(mailingList)) 1161 { 1162 project.setMailingList(mailingList); 1163 } 1164 1165 String inscriptionStatus = (String) additionalValues.getOrDefault("inscriptionStatus", null); 1166 if (StringUtils.isNotEmpty(inscriptionStatus)) 1167 { 1168 project.setInscriptionStatus(inscriptionStatus); 1169 } 1170 1171 String defaultProfile = (String) additionalValues.getOrDefault("defaultProfile", null); 1172 if (StringUtils.isNotEmpty(defaultProfile)) 1173 { 1174 project.setDefaultProfile(defaultProfile); 1175 } 1176 1177 @SuppressWarnings("unchecked") 1178 List<String> tags = (List<String>) additionalValues.getOrDefault("tags", null); 1179 if (tags != null) 1180 { 1181 project.setTags(tags); 1182 } 1183 @SuppressWarnings("unchecked") 1184 List<String> categoryTags = (List<String>) additionalValues.getOrDefault("categoryTags", null); 1185 if (categoryTags != null) 1186 { 1187 project.setCategoryTags(categoryTags); 1188 } 1189 1190 @SuppressWarnings("unchecked") 1191 List<String> keywords = (List<String>) additionalValues.getOrDefault("keywords", null); 1192 if (keywords != null) 1193 { 1194 project.setKeywords(keywords.toArray(new String[keywords.size()])); 1195 } 1196 1197 project.setCreationDate(ZonedDateTime.now()); 1198 1199 // Create the project workspace = a site + a set of pages 1200 _createProjectWorkspace(project, errors); 1201 1202 activateModules(project, modulesIds, additionalValues); 1203 1204 if (CollectionUtils.isEmpty(errors)) 1205 { 1206 project.saveChanges(); 1207 1208 // Notify observers 1209 Map<String, Object> eventParams = new HashMap<>(); 1210 eventParams.put(ObservationConstants.ARGS_PROJECT, project); 1211 _observationManager.notify(new Event(ObservationConstants.EVENT_PROJECT_ADDED, _currentUserProvider.getUser(), eventParams)); 1212 1213 } 1214 else 1215 { 1216 deleteProject(project); 1217 } 1218 1219 clearCaches(); 1220 1221 return project; 1222 } 1223 1224 /** 1225 * Edit a project 1226 * @param id The project identifier 1227 * @param title The title to set 1228 * @param description The description to set 1229 * @param mailingList Project mailing list 1230 * @param inscriptionStatus The inscription status of the project 1231 * @param defaultProfile The default profile for new members 1232 */ 1233 @Callable(rights = ProjectConstants.RIGHT_PROJECT_EDIT, context = "/admin") 1234 public void editProject(String id, String title, String description, String mailingList, String inscriptionStatus, String defaultProfile) 1235 { 1236 Project project = _resolver.resolveById(id); 1237 editProject(project, title, description, mailingList, inscriptionStatus, defaultProfile); 1238 } 1239 1240 /** 1241 * Edit a project 1242 * @param project The project 1243 * @param title The title to set 1244 * @param description The description to set 1245 * @param mailingList Project mailing list 1246 * @param inscriptionStatus The inscription status of the project 1247 * @param defaultProfile The default profile for new members 1248 */ 1249 public void editProject(Project project, String title, String description, String mailingList, String inscriptionStatus, String defaultProfile) 1250 { 1251 checkRightsForProjectEdition(project, InscriptionStatus.valueOf(inscriptionStatus.toUpperCase()), null); 1252 1253 project.setTitle(title); 1254 1255 if (StringUtils.isNotEmpty(description)) 1256 { 1257 project.setDescription(description); 1258 } 1259 else 1260 { 1261 project.removeDescription(); 1262 } 1263 1264 if (StringUtils.isNotEmpty(mailingList)) 1265 { 1266 project.setMailingList(mailingList); 1267 } 1268 else 1269 { 1270 project.removeMailingList(); 1271 } 1272 1273 project.setInscriptionStatus(inscriptionStatus); 1274 project.setDefaultProfile(defaultProfile); 1275 1276 project.saveChanges(); 1277 1278 // Notify observers 1279 Map<String, Object> eventParams = new HashMap<>(); 1280 eventParams.put(ObservationConstants.ARGS_PROJECT, project); 1281 eventParams.put(org.ametys.plugins.workspaces.ObservationConstants.ARGS_PROJECT_ID, project.getId()); 1282 _observationManager.notify(new Event(ObservationConstants.EVENT_PROJECT_UPDATED, _currentUserProvider.getUser(), eventParams)); 1283 } 1284 1285 /** 1286 * Delete a list of project. 1287 * @param ids The ids of projects to delete 1288 * @return The ids of the deleted projects, unknowns projects and the deleted sites 1289 */ 1290 @Callable(rights = ProjectConstants.RIGHT_PROJECT_DELETE, context = "/admin") 1291 public Map<String, Object> deleteProjectsByIds(List<String> ids) 1292 { 1293 Map<String, Object> result = new HashMap<>(); 1294 List<Map<String, Object>> deleted = new ArrayList<>(); 1295 List<String> unknowns = new ArrayList<>(); 1296 1297 for (String id : ids) 1298 { 1299 try 1300 { 1301 Project project = _resolver.resolveById(id); 1302 1303 Map<String, Object> projectInfo = new HashMap<>(); 1304 projectInfo.put("id", id); 1305 projectInfo.put("title", project.getTitle()); 1306 projectInfo.put("sites", deleteProject(project)); 1307 1308 deleted.add(projectInfo); 1309 } 1310 catch (UnknownAmetysObjectException e) 1311 { 1312 getLogger().warn(String.format("Unable to delete the definition of id '%s', because it does not exist.", id), e); 1313 unknowns.add(id); 1314 } 1315 } 1316 1317 result.put("deleted", deleted); 1318 result.put("unknowns", unknowns); 1319 1320 return result; 1321 } 1322 1323 /** 1324 * Delete a project. 1325 * @param projects The list of projects to delete 1326 * @return list of deleted sites (each list entry contains a data map with 1327 * the id and the name of the delete site). 1328 */ 1329 public List<Map<String, String>> deleteProject(List<Project> projects) 1330 { 1331 List<Map<String, String>> deletedSitesInfo = new ArrayList<>(); 1332 1333 for (Project project : projects) 1334 { 1335 deletedSitesInfo.addAll(deleteProject(project)); 1336 } 1337 1338 return deletedSitesInfo; 1339 } 1340 1341 /** 1342 * Delete a project and its sites 1343 * @param project The project to delete 1344 * @return list of deleted sites (each list entry contains a data map with 1345 * the id and the name of the delete site). 1346 */ 1347 public List<Map<String, String>> deleteProject(Project project) 1348 { 1349 ModifiableAmetysObject parent = project.getParent(); 1350 1351 1352 // list of map entry with id, name and title property 1353 // { id: site id, name: site name } 1354 List<Map<String, String>> deletedSitesInfo = new ArrayList<>(); 1355 1356 Site site = project.getSite(); 1357 if (site != null) 1358 { 1359 try 1360 { 1361 Map<String, String> siteProps = new HashMap<>(); 1362 siteProps.put("id", site.getId()); 1363 siteProps.put("name", site.getName()); 1364 1365 _siteDao.deleteSite(site.getId()); 1366 deletedSitesInfo.add(siteProps); 1367 } 1368 catch (RepositoryException e) 1369 { 1370 String errorMsg = String.format("Error while trying to delete the site '%s' for the project '%s'.", site.getName(), project.getName()); 1371 getLogger().error(errorMsg, e); 1372 } 1373 } 1374 1375 String projectId = project.getId(); 1376 Collection<ProjectMember> projectMembers = _projectMembers.getProjectMembers(project, true); 1377 project.remove(); 1378 parent.saveChanges(); 1379 1380 // Notify observers 1381 Map<String, Object> eventParams = new HashMap<>(); 1382 eventParams.put(ObservationConstants.ARGS_PROJECT_ID, projectId); 1383 eventParams.put(ObservationConstants.ARGS_PROJECT_NAME, project.getName()); 1384 eventParams.put(ObservationConstants.ARGS_PROJECT_MEMBERS, projectMembers); 1385 _observationManager.notify(new Event(ObservationConstants.EVENT_PROJECT_DELETED, _currentUserProvider.getUser(), eventParams)); 1386 1387 clearCaches(); 1388 1389 return deletedSitesInfo; 1390 } 1391 1392 /** 1393 * Utility method to get or create an ametys object 1394 * @param <A> A sub class of AmetysObject 1395 * @param parent The parent object 1396 * @param name The ametys object name 1397 * @param type The ametys object type 1398 * @return ametys object 1399 * @throws AmetysRepositoryException if an repository error occurs 1400 */ 1401 private <A extends AmetysObject> A _getOrCreateObject(ModifiableTraversableAmetysObject parent, String name, String type) throws AmetysRepositoryException 1402 { 1403 A object; 1404 1405 if (parent.hasChild(name)) 1406 { 1407 object = parent.getChild(name); 1408 } 1409 else 1410 { 1411 object = parent.createChild(name, type); 1412 parent.saveChanges(); 1413 } 1414 1415 return object; 1416 } 1417 1418 /** 1419 * Get the project of an ametys object inside a project. 1420 * It can be an explorer node, or any type of resource in a module. 1421 * @param id The identifier of the ametys object 1422 * @return the project or null if not found 1423 */ 1424 public Project getParentProject(String id) 1425 { 1426 return getParentProject(_resolver.<AmetysObject>resolveById(id)); 1427 } 1428 1429 /** 1430 * Get the project of an ametys object inside a project. 1431 * It can be an explorer node, or any type of resource in a module. 1432 * @param object The ametys object 1433 * @return the project or null if not found 1434 */ 1435 public Project getParentProject(AmetysObject object) 1436 { 1437 AmetysObject ametysObject = object; 1438 // Go back to the local explorer root. 1439 do 1440 { 1441 ametysObject = ametysObject.getParent(); 1442 } 1443 while (ametysObject instanceof ExplorerNode); 1444 1445 if (!(ametysObject instanceof Project)) 1446 { 1447 getLogger().warn(String.format("No project found for ametys object with id '%s'", ametysObject.getId())); 1448 return null; 1449 } 1450 1451 return (Project) ametysObject; 1452 } 1453 1454 /** 1455 * Get the list of project names for a given site 1456 * @param siteName The site name 1457 * @return the list of project names 1458 */ 1459 @Callable(rights = "Runtime_Rights_Admin_Access", context = "/admin") 1460 public List<String> getProjectsForSite(String siteName) 1461 { 1462 Cache<String, List<Pair<String, String>>> cache = _getMemorySiteAssociationCache(); 1463 if (cache.hasKey(siteName)) 1464 { 1465 return cache.get(siteName).stream() 1466 .map(p -> p.getRight()) 1467 .collect(Collectors.toList()); 1468 } 1469 else 1470 { 1471 List<String> projectNames = new ArrayList<>(); 1472 1473 if (_siteManager.hasSite(siteName)) 1474 { 1475 Site site = _siteManager.getSite(siteName); 1476 getProjectsForSite(site) 1477 .stream() 1478 .map(Project::getName) 1479 .forEach(projectNames::add); 1480 } 1481 1482 return projectNames; 1483 } 1484 } 1485 1486 /** 1487 * Get the list of project for a given site 1488 * @param site The site 1489 * @return the list of project 1490 */ 1491 public List<Project> getProjectsForSite(Site site) 1492 { 1493 Cache<String, List<Pair<String, String>>> cache = _getMemorySiteAssociationCache(); 1494 if (cache.hasKey(site.getName())) 1495 { 1496 return cache.get(site.getName()).stream() 1497 .map(p -> _resolver.<Project>resolveById(p.getLeft())) 1498 .collect(Collectors.toList()); 1499 } 1500 1501 try 1502 { 1503 // Stream over the weak reference properties pointing to this 1504 // node to find referencing projects 1505 Iterator<Property> propertyIterator = site.getNode().getWeakReferences(); 1506 Iterable<Property> propertyIterable = () -> propertyIterator; 1507 1508 List<Project> projects = StreamSupport.stream(propertyIterable.spliterator(), false) 1509 .map(p -> 1510 { 1511 try 1512 { 1513 Node parent = p.getParent(); 1514 1515 // Check if the parent is a project" 1516 if (NodeTypeHelper.isNodeType(parent, "ametys:project")) 1517 { 1518 Project project = _resolver.resolve(parent, false); 1519 return project; 1520 } 1521 } 1522 catch (Exception e) 1523 { 1524 if (getLogger().isWarnEnabled()) 1525 { 1526 // this weak reference is not from a project 1527 String propertyPath = null; 1528 try 1529 { 1530 propertyPath = p.getPath(); 1531 } 1532 catch (Exception e2) 1533 { 1534 // ignore 1535 } 1536 1537 String warnMsg = String.format("Site '%s' is pointed by a weak reference '%s' which is not representing a relation with project. This reference is ignored.", site.getName(), propertyPath); 1538 getLogger().warn(warnMsg); 1539 } 1540 } 1541 1542 return null; 1543 }) 1544 .filter(Objects::nonNull) 1545 .collect(Collectors.toList()); 1546 1547 List<Pair<String, String>> projectsPairs = projects.stream().map(p -> Pair.of(p.getId(), p.getName())).collect(Collectors.toList()); 1548 cache.put(site.getName(), projectsPairs); 1549 return projects; 1550 } 1551 catch (RepositoryException e) 1552 { 1553 getLogger().error(String.format("Unable to find projects for site '%s'", site.getName()), e); 1554 } 1555 1556 return new ArrayList<>(); 1557 } 1558 1559 /** 1560 * Create the project workspace for a given project. 1561 * @param project The project for which the workspace must be created 1562 * @param errors A list of possible errors to populate. Can be null if the caller is not interested in error tracking. 1563 * @return The site created for this workspace 1564 */ 1565 protected Site _createProjectWorkspace(Project project, List<String> errors) 1566 { 1567 String initialSiteName = project.getName(); 1568 Site site = null; 1569 1570 Site catalogSite = _siteManager.getSite(getCatalogSiteName()); 1571 String rootId = catalogSite != null ? catalogSite.getId() : null; 1572 1573 Map<String, Object> result = _siteDao.createSite(rootId, initialSiteName, ProjectWorkspaceSiteType.TYPE_ID, true); 1574 1575 String siteId = (String) result.get("id"); 1576 String siteName = (String) result.get("name"); 1577 if (StringUtils.isNotEmpty(siteId)) 1578 { 1579 // Creation success 1580 site = _siteManager.getSite(siteName); 1581 1582 setProjectSiteTitle(site, project.getTitle()); 1583 1584 // Add site to project 1585 project.setSite(site); 1586 1587 site.saveChanges(); 1588 } 1589 1590 return site; 1591 } 1592 1593 /** 1594 * Get the project's tags 1595 * @return The project's tags 1596 */ 1597 public List<String> getTags() 1598 { 1599 AmetysObject projectsRootNode = getProjectsRoot(); 1600 if (projectsRootNode instanceof JCRAmetysObject) 1601 { 1602 Node node = ((JCRAmetysObject) projectsRootNode).getNode(); 1603 1604 try 1605 { 1606 return Arrays.stream(node.getProperty(__PROJECTS_TAGS_PROPERTY).getValues()) 1607 .map(LambdaUtils.wrap(Value::getString)) 1608 .collect(Collectors.toList()); 1609 } 1610 catch (PathNotFoundException e) 1611 { 1612 // property is not set, empty list will be returned. 1613 } 1614 catch (RepositoryException e) 1615 { 1616 throw new AmetysRepositoryException(e); 1617 } 1618 } 1619 1620 return new ArrayList<>(); 1621 } 1622 1623 /** 1624 * Add project's tags 1625 * @param newTags The new tags to add 1626 */ 1627 public synchronized void addTags(Collection<String> newTags) 1628 { 1629 if (CollectionUtils.isNotEmpty(newTags)) 1630 { 1631 AmetysObject projectsRootNode = getProjectsRoot(); 1632 if (projectsRootNode instanceof JCRAmetysObject) 1633 { 1634 // Concat existing tags with new lowercased tags 1635 String[] tags = Stream.concat(getTags().stream(), newTags.stream().map(String::trim).map(String::toLowerCase).filter(StringUtils::isNotEmpty)) 1636 .distinct() 1637 .toArray(String[]::new); 1638 1639 try 1640 { 1641 ((JCRAmetysObject) projectsRootNode).getNode().setProperty(__PROJECTS_TAGS_PROPERTY, tags); 1642 } 1643 catch (RepositoryException e) 1644 { 1645 throw new AmetysRepositoryException(e); 1646 } 1647 } 1648 } 1649 } 1650 1651 /** 1652 * Get the list of activated modules for a project 1653 * @param project The project 1654 * @return The list of activated modules 1655 */ 1656 public List<WorkspaceModule> getModules(Project project) 1657 { 1658 return _moduleManagerEP.getModules().stream() 1659 .filter(module -> isModuleActivated(project, module.getId())) 1660 .collect(Collectors.toList()); 1661 } 1662 1663 /** 1664 * Retrieves the page of the module for all available languages 1665 * @param project The project 1666 * @param moduleId The project module id 1667 * @return the page or null if not found 1668 */ 1669 public Set<Page> getModulePages(Project project, String moduleId) 1670 { 1671 if (_moduleManagerEP.hasExtension(moduleId)) 1672 { 1673 WorkspaceModule module = _moduleManagerEP.getExtension(moduleId); 1674 return getModulePages(project, module); 1675 } 1676 return null; 1677 } 1678 1679 /** 1680 * Return the possible module roots associated to a page 1681 * @param page The given page 1682 * @return A non null set of the data of the linked modules 1683 */ 1684 public Set<ModifiableResourceCollection> pageToModuleRoot(Page page) 1685 { 1686 Set<ModifiableResourceCollection> data = new LinkedHashSet<>(); 1687 1688 Page rootPage = page; 1689 SitemapElement parent = page.getParent(); 1690 while (!(parent instanceof Sitemap) && !page.hasValue(__PAGE_MODULES_VALUE)) 1691 { 1692 rootPage = (Page) parent; 1693 parent = parent.getParent(); 1694 } 1695 1696 String[] modulesRootsIds = rootPage.getValue(__PAGE_MODULES_VALUE, new String[0]); 1697 if (modulesRootsIds.length > 0) 1698 { 1699 for (String moduleRootId : modulesRootsIds) 1700 { 1701 try 1702 { 1703 ModifiableResourceCollection moduleRoot = _resolver.resolveById(moduleRootId); 1704 data.add(moduleRoot); 1705 } 1706 catch (UnknownAmetysObjectException e) 1707 { 1708 // Ignore obsolete data 1709 } 1710 } 1711 } 1712 1713 return data; 1714 } 1715 1716 /** 1717 * Mark the given page as this module page. The modified page will not be saved. 1718 * @param page The page to change 1719 * @param moduleRoot The workspace module that use this page 1720 */ 1721 public void tagProjectPage(ModifiablePage page, ModifiableResourceCollection moduleRoot) 1722 { 1723 String[] currentModules = page.getValue(__PAGE_MODULES_VALUE, new String[0]); 1724 1725 Set<String> modules = new LinkedHashSet<>(Arrays.asList(currentModules)); 1726 modules.add(moduleRoot.getId()); 1727 1728 String[] newModules = new String[modules.size()]; 1729 modules.toArray(newModules); 1730 1731 page.setValue(__PAGE_MODULES_VALUE, newModules); 1732 } 1733 1734 /** 1735 * Remove the mark on the given page of this module. The modified page will not be saved. 1736 * @param page The page to change 1737 * @param moduleRoot The workspace module that use this page 1738 */ 1739 public void untagProjectPage(ModifiablePage page, ModifiableResourceCollection moduleRoot) 1740 { 1741 if (moduleRoot != null) 1742 { 1743 String[] currentModules = page.getValue(__PAGE_MODULES_VALUE, new String[0]); 1744 1745 Set<String> modules = new LinkedHashSet<>(Arrays.asList(currentModules)); 1746 modules.remove(moduleRoot.getId()); 1747 1748 String[] newModules = new String[modules.size()]; 1749 modules.toArray(newModules); 1750 1751 page.setValue(__PAGE_MODULES_VALUE, newModules); 1752 } 1753 } 1754 1755 /** 1756 * Get a page in the site of a given project with a specific tag 1757 * @param project The project 1758 * @param workspaceModule the module 1759 * @return The module's pages 1760 */ 1761 public Set<Page> getModulePages(Project project, WorkspaceModule workspaceModule) 1762 { 1763 Request request = _getRequest(); 1764 if (request == null) 1765 { 1766 // There is no request to store cache 1767 return _computePages(project, workspaceModule); 1768 } 1769 1770 Cache<RequestModuleCacheKey, Set<Page>> pagesCache = _getRequestPageCache(); 1771 1772 // The site key in the cache is of the form {site + workspace}. 1773 String currentWorkspace = _workspaceSelector.getWorkspace(); 1774 RequestModuleCacheKey pagesKey = RequestModuleCacheKey.of(project.getName(), workspaceModule.getId(), currentWorkspace); 1775 1776 try 1777 { 1778 return pagesCache.get(pagesKey, __ -> _computePages(project, workspaceModule)); 1779 } 1780 catch (CacheException e) 1781 { 1782 if (e.getCause() instanceof UnknownAmetysObjectException) 1783 { 1784 throw (UnknownAmetysObjectException) e.getCause(); 1785 } 1786 else 1787 { 1788 throw new RuntimeException("An error occurred while computing page of module " + workspaceModule.getModuleName() + " in project " + project.getName(), e); 1789 } 1790 } 1791 } 1792 1793 private Set<Page> _computePages(Project project, WorkspaceModule workspaceModule) 1794 { 1795 Set<String> pagesUUids = _getMemoryPageCache().get(ModuleCacheKey.of(project.getName(), workspaceModule.getId()), __ -> _computePagesIds(project, workspaceModule)); 1796 if (pagesUUids != null) 1797 { 1798 return pagesUUids.stream().map(uuid -> _resolver.<Page>resolveById(uuid)).collect(Collectors.toSet()); 1799 } 1800 else 1801 { 1802 // Project may be present in cache for 'default' workspace but does not exist in current JCR workspace 1803 throw new UnknownAmetysObjectException("There is no pages for '" + project.getName() + "', module '" + workspaceModule.getModuleName() + "'"); 1804 } 1805 } 1806 1807 private Set<String> _computePagesIds(Project project, WorkspaceModule workspaceModule) 1808 { 1809 Site site = project.getSite(); 1810 String siteName = site != null ? site.getName() : null; 1811 if (StringUtils.isEmpty(siteName)) 1812 { 1813 return null; 1814 } 1815 1816 ModifiableResourceCollection moduleRoot = workspaceModule.getModuleRoot(project, false); 1817 if (moduleRoot != null) 1818 { 1819 Expression expression = new StringExpression(__PAGE_MODULES_VALUE, Operator.EQ, moduleRoot.getId()); 1820 String query = PageQueryHelper.getPageXPathQuery(siteName, null, null, expression, null); 1821 1822 return StreamSupport.stream(_resolver.query(query).spliterator(), false) 1823 .map(page -> page.getId()) 1824 .collect(Collectors.toSet()); 1825 } 1826 else 1827 { 1828 return Set.of(); 1829 } 1830 } 1831 1832 /** 1833 * Activate the list of module of the project 1834 * @param project The project 1835 * @param moduleIds The list of modules. Can be null to activate all modules 1836 * @param additionalValues A list of optional additional values. Accepted values are : description, mailingList, inscriptionStatus, defaultProfile, tags, categoryTags, keywords and language 1837 */ 1838 public void activateModules(Project project, Set<String> moduleIds, Map<String, Object> additionalValues) 1839 { 1840 Set<String> modules = moduleIds == null ? _moduleManagerEP.getExtensionsIds() : moduleIds; 1841 1842 for (String moduleId : modules) 1843 { 1844 WorkspaceModule module = _moduleManagerEP.getModule(moduleId); 1845 if (module != null && !isModuleActivated(project, moduleId)) 1846 { 1847 module.activateModule(project, additionalValues); 1848 project.addModule(moduleId); 1849 } 1850 } 1851 1852 _setDefaultProfileForMembers(project, modules); 1853 1854 project.saveChanges(); 1855 } 1856 1857 private void _setDefaultProfileForMembers(Project project, Set<String> modules) 1858 { 1859 String profileForNewModule = StringUtils.defaultString(Config.getInstance().getValue("workspaces.profile.new.module")); 1860 1861 if (profileForNewModule.equals(ProfileForNewModule.DEFAULT_MEMBER_PROFILE.name())) 1862 { 1863 Set<String> defaultProfiles = Set.of(StringUtils.defaultString(Config.getInstance().getValue("workspaces.profile.default"))); 1864 Map<JCRProjectMember, Object> projectMembers = _projectMembers.getJCRProjectMembers(project); 1865 1866 Set<WorkspaceModule> modulesForMembers = getProjectModulesForNewMembers(project); 1867 for (String moduleId : modules) 1868 { 1869 WorkspaceModule module = _moduleManagerEP.getModule(moduleId); 1870 Set<String> profiles = modulesForMembers.contains(module) ? defaultProfiles : Set.of(); 1871 for (JCRProjectMember member : projectMembers.keySet()) 1872 { 1873 _projectMembers.setProfileOnModule(member, project, module, profiles); 1874 } 1875 } 1876 } 1877 } 1878 1879 /** 1880 * Initialize the sitemap with the active module of the project 1881 * @param project The project 1882 * @param sitemap The sitemap 1883 */ 1884 public void initializeModulesSitemap(Project project, Sitemap sitemap) 1885 { 1886 Set<String> modules = _moduleManagerEP.getExtensionsIds(); 1887 1888 for (String moduleId : modules) 1889 { 1890 if (_moduleManagerEP.hasExtension(moduleId)) 1891 { 1892 WorkspaceModule module = _moduleManagerEP.getExtension(moduleId); 1893 1894 if (isModuleActivated(project, moduleId)) 1895 { 1896 module.initializeSitemap(project, sitemap); 1897 } 1898 } 1899 } 1900 } 1901 1902 /** 1903 * Determines if a module is activated 1904 * @param project The project 1905 * @param moduleId The id of module 1906 * @return true if the module the currently activated 1907 */ 1908 public boolean isModuleActivated(Project project, String moduleId) 1909 { 1910 return ArrayUtils.contains(project.getModules(), moduleId); 1911 } 1912 1913 /** 1914 * Remove the explorer root node of the project module, remove all events 1915 * related to that module and set it to deactivated 1916 * @param project The project 1917 * @param moduleIds The id of module to activate 1918 */ 1919 public void deactivateModules(Project project, Set<String> moduleIds) 1920 { 1921 for (String moduleId : moduleIds) 1922 { 1923 WorkspaceModule module = _moduleManagerEP.getModule(moduleId); 1924 if (module != null && isModuleActivated(project, moduleId)) 1925 { 1926 module.deactivateModule(project); 1927 project.removeModule(moduleId); 1928 } 1929 } 1930 1931 project.saveChanges(); 1932 } 1933 1934 1935 /** 1936 * Get the list of profiles configured for the workspaces' projects 1937 * @return The list of profiles as JSON 1938 */ 1939 @Callable(rights = {ProjectConstants.RIGHT_PROJECT_CREATE_PRIVATE, ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_MODERATED, ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_OPENED, ProjectConstants.RIGHT_PROJECT_EDIT}, context = "/admin") 1940 public Map<String, Object> getProjectProfiles() 1941 { 1942 Map<String, Object> result = new HashMap<>(); 1943 List<Map<String, Object>> profiles = _projectRightHelper.getProfiles().stream().map(p -> p.toJSON()).collect(Collectors.toList()); 1944 result.put("profiles", profiles); 1945 return result; 1946 } 1947 1948 /** 1949 * Get the site name holding the catalog of projects 1950 * @return the catalog's site name 1951 * @throws UnknownCatalogSiteException when the config is invalid 1952 */ 1953 public String getCatalogSiteName() throws UnknownCatalogSiteException 1954 { 1955 String catalogSiteName = Config.getInstance().getValue("workspaces.catalog.site.name"); 1956 if (!_siteManager.hasSite(catalogSiteName)) 1957 { 1958 throw new UnknownCatalogSiteException("Unknown site '" + catalogSiteName + "'. The global Ametys configuration is invalid for the parameter 'workspaces.catalog.site.name'"); 1959 } 1960 return catalogSiteName; 1961 } 1962 1963 /** 1964 * Get the site name holding the users directory 1965 * @return the users directory's site name 1966 * @throws UnknownUserDirectorySiteException when the config is invalid 1967 */ 1968 public String getUsersDirectorySiteName() throws UnknownUserDirectorySiteException 1969 { 1970 String udSiteName = Config.getInstance().getValue("workspaces.member.userdirectory.site.name"); 1971 if (!_siteManager.hasSite(udSiteName)) 1972 { 1973 throw new UnknownUserDirectorySiteException("Unknown site '" + udSiteName + "'. The global Ametys configuration is invalid for the parameter 'workspaces.member.userdirectory.site.name'"); 1974 } 1975 return udSiteName; 1976 } 1977 1978 @Override 1979 public boolean supports(Event event) 1980 { 1981 return event.getId().equals(ObservationConstants.EVENT_PROJECT_DELETED) 1982 || event.getId().equals(ObservationConstants.EVENT_PROJECT_UPDATED) 1983 || event.getId().equals(ObservationConstants.EVENT_PROJECT_ADDED) 1984 || event.getId().equals(org.ametys.web.ObservationConstants.EVENT_PAGE_ADDED) 1985 || event.getId().equals(org.ametys.web.ObservationConstants.EVENT_PAGE_DELETED); 1986 } 1987 1988 public int getPriority() 1989 { 1990 return 0; 1991 } 1992 1993 public void observe(Event event, Map<String, Object> transientVars) throws Exception 1994 { 1995 clearCaches(); 1996 } 1997 1998 /** 1999 * Prefix project title 2000 * @param site the site 2001 * @param title the title 2002 */ 2003 public void setProjectSiteTitle(Site site, String title) 2004 { 2005 I18nizableText i18nSiteTitle = new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_DEFAULT_PROJECT_WORKSPACE_TITLE", Arrays.asList(title)); 2006 site.setTitle(_i18nUtils.translate(i18nSiteTitle)); 2007 } 2008 2009 private Project _computeProject(String projectName) 2010 { 2011 if (hasProject(projectName)) 2012 { 2013 String uuid = _getUUIDCache().get(projectName); 2014 return _resolver.<Project>resolveById(uuid); 2015 } 2016 else 2017 { 2018 // Project may be present in cache for 'default' workspace but does not exist in current JCR workspace 2019 throw new UnknownAmetysObjectException("There is no site named '" + projectName + "'"); 2020 } 2021 } 2022 2023 /** 2024 * Check rights to create project 2025 * @param inscriptionStatus the inscription status 2026 * @param zoneItem the zoneItem containing catalog service. Must be a catalog service if not null 2027 */ 2028 public void checkRightsForProjectCreation(InscriptionStatus inscriptionStatus, ZoneItem zoneItem) 2029 { 2030 SitemapElement catalogPage = zoneItem != null ? zoneItem.getZone().getSitemapElement() : null; 2031 2032 if (catalogPage != null && !_projectRightHelper.hasCatalogReadAccess(zoneItem)) 2033 { 2034 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to create project from page '" + catalogPage.getId() + "' without sufficient rights"); 2035 } 2036 2037 switch (inscriptionStatus) 2038 { 2039 case PRIVATE: 2040 boolean hasRightToCreatePrivateProjetOnPages = catalogPage != null ? _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PRIVATE, catalogPage) == RightResult.RIGHT_ALLOW : false; 2041 if (!hasRightToCreatePrivateProjetOnPages && _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PRIVATE, "/${WorkspaceName}") != RightResult.RIGHT_ALLOW) 2042 { 2043 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to create private project without sufficient rights"); 2044 } 2045 break; 2046 case MODERATED: 2047 boolean hasRightToCreateModeratedProjetOnPages = catalogPage != null ? _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_MODERATED, catalogPage) == RightResult.RIGHT_ALLOW : false; 2048 if (!hasRightToCreateModeratedProjetOnPages && _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_MODERATED, "/${WorkspaceName}") != RightResult.RIGHT_ALLOW) 2049 { 2050 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to create public project with moderation without sufficient rights"); 2051 } 2052 break; 2053 case OPEN: 2054 boolean hasRightToCreateOpenProjetOnPages = catalogPage != null ? _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_OPENED, catalogPage) == RightResult.RIGHT_ALLOW : false; 2055 if (!hasRightToCreateOpenProjetOnPages && _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_OPENED, "/${WorkspaceName}") != RightResult.RIGHT_ALLOW) 2056 { 2057 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to create public project without sufficient rights"); 2058 } 2059 break; 2060 default: 2061 throw new IllegalArgumentException("Inscription status '" + inscriptionStatus.toString() + "' is unknown"); 2062 } 2063 } 2064 2065 /** 2066 * Check rights to edit project 2067 * @param project the project 2068 * @param inscriptionStatus the inscription status 2069 * @param zoneItem the zoneItem containing catalog service. Must be a catalog service if not null 2070 */ 2071 public void checkRightsForProjectEdition(Project project, InscriptionStatus inscriptionStatus, ZoneItem zoneItem) 2072 { 2073 InscriptionStatus oldInscriptionStatus = project.getInscriptionStatus(); 2074 SitemapElement catalogPage = zoneItem != null ? zoneItem.getZone().getSitemapElement() : null; 2075 2076 if (catalogPage != null && !_projectRightHelper.hasCatalogReadAccess(zoneItem)) 2077 { 2078 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to edit project from page '" + catalogPage.getId() + "' without sufficient rights"); 2079 } 2080 2081 if (oldInscriptionStatus != inscriptionStatus) 2082 { 2083 boolean canCreatePrivateProjet = _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PRIVATE, "/${WorkspaceName}") == RightResult.RIGHT_ALLOW 2084 || (catalogPage != null ? _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PRIVATE, catalogPage) == RightResult.RIGHT_ALLOW : false); 2085 boolean canCreatePublicProjetWithModeration = _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_MODERATED, "/${WorkspaceName}") == RightResult.RIGHT_ALLOW 2086 || (catalogPage != null ? _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_MODERATED, catalogPage) == RightResult.RIGHT_ALLOW : false); 2087 boolean canCreatePublicProjet = _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_OPENED, "/${WorkspaceName}") == RightResult.RIGHT_ALLOW 2088 || (catalogPage != null ? _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_CREATE_PUBLIC_OPENED, catalogPage) == RightResult.RIGHT_ALLOW : false); 2089 2090 switch (oldInscriptionStatus) 2091 { 2092 case PRIVATE: 2093 if (!canCreatePrivateProjet) 2094 { 2095 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to edit private project without sufficient rights"); 2096 } 2097 break; 2098 case MODERATED: 2099 if (!canCreatePublicProjetWithModeration) 2100 { 2101 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to edit public project with moderation without sufficient rights"); 2102 } 2103 break; 2104 case OPEN: 2105 if (!canCreatePublicProjet) 2106 { 2107 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to edit public project without sufficient rights"); 2108 } 2109 break; 2110 default: 2111 throw new IllegalArgumentException("Inscription status '" + oldInscriptionStatus.toString() + "' is unknown"); 2112 } 2113 2114 switch (inscriptionStatus) 2115 { 2116 case PRIVATE: 2117 if (!canCreatePrivateProjet) 2118 { 2119 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to edit project to private project without sufficient rights"); 2120 } 2121 break; 2122 case MODERATED: 2123 if (!canCreatePublicProjetWithModeration) 2124 { 2125 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to edit project to public project with moderation without sufficient rights"); 2126 } 2127 break; 2128 case OPEN: 2129 if (!canCreatePublicProjet) 2130 { 2131 throw new AccessDeniedException("User " + _currentUserProvider.getUser() + " tried to edit project to public project without sufficient rights"); 2132 } 2133 break; 2134 default: 2135 throw new IllegalArgumentException("Inscription status '" + inscriptionStatus.toString() + "' is unknown"); 2136 } 2137 } 2138 } 2139 2140 /** 2141 * Clear the site cache 2142 */ 2143 public void clearCaches () 2144 { 2145 _getMemorySiteAssociationCache().invalidateAll(); 2146 _getMemoryProjectCache().invalidateAll(); 2147 _getMemoryPageCache().invalidateAll(); 2148 _getRequestProjectCache().invalidateAll(); 2149 _getRequestPageCache().invalidateAll(); 2150 } 2151 2152 private Cache<String, List<Pair<String, String>>> _getMemorySiteAssociationCache() 2153 { 2154 return _cacheManager.get(MEMORY_SITEASSOCIATION_CACHE); 2155 } 2156 2157 private Cache<String, String> _getMemoryProjectCache() 2158 { 2159 return _cacheManager.get(MEMORY_PROJECTIDBYNAMECACHE); 2160 } 2161 2162 private Cache<ModuleCacheKey, Set<String>> _getMemoryPageCache() 2163 { 2164 return _cacheManager.get(MEMORY_PAGESBYIDCACHE); 2165 } 2166 2167 private Cache<RequestProjectCacheKey, Project> _getRequestProjectCache() 2168 { 2169 return _cacheManager.get(REQUEST_PROJECTBYID_CACHE); 2170 } 2171 2172 private Cache<RequestModuleCacheKey, Set<Page>> _getRequestPageCache() 2173 { 2174 return _cacheManager.get(REQUEST_PAGESBYPROJECTANDMODULE_CACHE); 2175 } 2176 2177 2178 /** 2179 * Creates the caches 2180 */ 2181 protected void _createCaches() 2182 { 2183 _cacheManager.createMemoryCache(MEMORY_SITEASSOCIATION_CACHE, 2184 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_CACHE_PROJECT_MANAGER_LABEL"), 2185 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_CACHE_PROJECT_MANAGER_DESCRIPTION"), 2186 true, 2187 null); 2188 _cacheManager.createMemoryCache(MEMORY_PROJECTIDBYNAMECACHE, 2189 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_MANAGER_UUID_CACHE_LABEL"), 2190 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_MANAGER_UUID_CACHE_DESCRIPTION"), 2191 true, 2192 null); 2193 _cacheManager.createMemoryCache(MEMORY_PAGESBYIDCACHE, 2194 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_MANAGER_PAGEUUID_CACHE_LABEL"), 2195 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_MANAGER_PAGEUUID_CACHE_DESCRIPTION"), 2196 true, 2197 null); 2198 _cacheManager.createRequestCache(REQUEST_PROJECTBYID_CACHE, 2199 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_MANAGER_REQUEST_CACHE_LABEL"), 2200 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_MANAGER_REQUEST_CACHE_DESCRIPTION"), 2201 false); 2202 _cacheManager.createRequestCache(REQUEST_PAGESBYPROJECTANDMODULE_CACHE, 2203 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_MANAGER_PAGEREQUEST_CACHE_LABEL"), 2204 new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_MANAGER_PAGEREQUEST_CACHE_DESCRIPTION"), 2205 false); 2206 } 2207 2208 private synchronized Map<String, String> _getUUIDCache() 2209 { 2210 if (!_getMemoryProjectCache().hasKey(__IS_CACHE_FILLED)) 2211 { 2212 Session defaultSession = null; 2213 try 2214 { 2215 // Force default workspace to execute query 2216 defaultSession = _repository.login(RepositoryConstants.DEFAULT_WORKSPACE); 2217 2218 String jcrQuery = "//element(*, ametys:project)"; 2219 2220 AmetysObjectIterable<Project> projects = _resolver.query(jcrQuery, defaultSession); 2221 2222 for (Project project : projects) 2223 { 2224 _getMemoryProjectCache().put(project.getName(), project.getId()); 2225 } 2226 2227 _getMemoryProjectCache().put(__IS_CACHE_FILLED, null); 2228 } 2229 catch (RepositoryException e) 2230 { 2231 throw new AmetysRepositoryException(e); 2232 } 2233 finally 2234 { 2235 if (defaultSession != null) 2236 { 2237 defaultSession.logout(); 2238 } 2239 } 2240 } 2241 2242 Map<String, String> cacheAsMap = _getMemoryProjectCache().asMap(); 2243 cacheAsMap.remove(__IS_CACHE_FILLED); 2244 return cacheAsMap; 2245 } 2246 2247 private static final class RequestProjectCacheKey extends AbstractCacheKey 2248 { 2249 private RequestProjectCacheKey(String projectName, String workspaceName) 2250 { 2251 super(projectName, workspaceName); 2252 } 2253 2254 static RequestProjectCacheKey of(String projectName, String workspaceName) 2255 { 2256 return new RequestProjectCacheKey(projectName, workspaceName); 2257 } 2258 } 2259 2260 private static final class ModuleCacheKey extends AbstractCacheKey 2261 { 2262 private ModuleCacheKey(String projectName, String moduleId) 2263 { 2264 super(projectName, moduleId); 2265 } 2266 2267 static ModuleCacheKey of(String projectName, String moduleId) 2268 { 2269 return new ModuleCacheKey(projectName, moduleId); 2270 } 2271 } 2272 2273 private static final class RequestModuleCacheKey extends AbstractCacheKey 2274 { 2275 private RequestModuleCacheKey(String projectName, String moduleId, String workspaceName) 2276 { 2277 super(projectName, moduleId, workspaceName); 2278 } 2279 2280 static RequestModuleCacheKey of(String projectName, String moduleId, String workspaceName) 2281 { 2282 return new RequestModuleCacheKey(projectName, moduleId, workspaceName); 2283 } 2284 } 2285 2286 private Request _getRequest () 2287 { 2288 try 2289 { 2290 return (Request) _context.get(ContextHelper.CONTEXT_REQUEST_OBJECT); 2291 } 2292 catch (ContextException ce) 2293 { 2294 getLogger().info("Unable to get the request", ce); 2295 return null; 2296 } 2297 } 2298 2299 /** 2300 * Retrieves all projects for client side 2301 * @return the projects 2302 */ 2303 @Callable(rights = "Runtime_Rights_Admin_Access", context = "/admin") 2304 public List<Map<String, Object>> getProjectsStatisticsForClientSide() 2305 { 2306 return getProjects() 2307 .stream() 2308 .map(p -> getProjectStatistics(p)) 2309 .collect(Collectors.toList()); 2310 } 2311 2312 /** 2313 * Retrieves the standard information of a project 2314 * @param project The project 2315 * @return The map of information 2316 */ 2317 public Map<String, Object> getProjectStatistics(Project project) 2318 { 2319 Map<String, Object> statistics = new HashMap<>(); 2320 2321 statistics.put("title", project.getTitle()); 2322 2323 long totalSize = 0; 2324 for (WorkspaceModule moduleManager : _moduleManagerEP.getModules()) 2325 { 2326 Map<String, Object> moduleStatistics = moduleManager.getStatistics(project); 2327 statistics.putAll(moduleStatistics); 2328 Long size = (Long) moduleStatistics.get(moduleManager.getModuleSizeKey()); 2329 totalSize += (size != null && size >= 0) ? (Long) moduleStatistics.get(moduleManager.getModuleSizeKey()) : 0; 2330 } 2331 2332 statistics.put("totalSize", totalSize); 2333 2334 ZonedDateTime creationDate = project.getCreationDate(); 2335 2336 statistics.put("creationDate", creationDate); 2337 statistics.put("managers", Arrays.stream(project.getManagers()) 2338 .map(u -> _userHelper.user2json(u)) 2339 .collect(Collectors.toList())); 2340 2341 return statistics; 2342 } 2343 2344 /** 2345 * Retrieves all projects for client side 2346 * @return the projects 2347 */ 2348 @Callable(rights = "Runtime_Rights_Admin_Access", context = "/admin") 2349 public List<Map<String, Object>> getProjectsStatisticsColumnsModel() 2350 { 2351 return getStatisticHeaders() 2352 .stream() 2353 .map(p -> p.convertToJSON()) 2354 .collect(Collectors.toList()); 2355 } 2356 2357 private List<StatisticColumn> getStatisticHeaders() 2358 { 2359 2360 List<StatisticColumn> flatStatisticHeaders = getFlatStatisticHeaders(); 2361 List<StatisticColumn> headers = new ArrayList<>(); 2362 for (StatisticColumn statisticColumn : flatStatisticHeaders) 2363 { 2364 // this column have a parent, we have to find it and attach it 2365 if (statisticColumn.getGroup() != null) 2366 { 2367 Optional<StatisticColumn> parent = flatStatisticHeaders.stream() 2368 .filter(column -> column.getId().equals(statisticColumn.getGroup())) 2369 .findAny(); 2370 if (parent.isPresent()) 2371 { 2372 parent.get().addSubColumn(statisticColumn); 2373 } 2374 } 2375 else 2376 { 2377 headers.add(statisticColumn); 2378 } 2379 } 2380 2381 return headers; 2382 } 2383 2384 private List<StatisticColumn> getFlatStatisticHeaders() 2385 { 2386 List<StatisticColumn> headers = new ArrayList<>(); 2387 headers.add(new StatisticColumn("title", new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_STATISTICS_TOOL_COLUMN_TITLE")) 2388 .withType(StatisticsColumnType.STRING) 2389 .withWidth(200) 2390 .withRenderer("Ametys.plugins.workspaces.project.tool.ProjectsGridHelper.renderTitle")); 2391 headers.add(new StatisticColumn("creationDate", new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_STATISTICS_TOOL_COLUMN_CREATION")) 2392 .withType(StatisticsColumnType.DATE) 2393 .withWidth(150)); 2394 headers.add(new StatisticColumn("managers", new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_STATISTICS_TOOL_COLUMN_MANAGERS")) 2395 .withRenderer("Ametys.grid.GridColumnHelper.renderUser") 2396 .withFilter(false)); 2397 for (WorkspaceModule moduleManager : _moduleManagerEP.getModules()) 2398 { 2399 headers.addAll(moduleManager.getStatisticModel()); 2400 } 2401 2402 StatisticColumn elements = new StatisticColumn(WorkspaceModule.GROUP_HEADER_ELEMENTS_ID, new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_STATISTICS_TOOL_COLUMN_ELEMENTS")) 2403 .withFilter(false); 2404 headers.add(elements); 2405 2406 StatisticColumn activatedModules = new StatisticColumn(WorkspaceModule.GROUP_HEADER_ACTIVATED_ID, new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_STATISTICS_TOOL_COLUMN_ACTIVE_MODULES")) 2407 .isHidden(true) 2408 .withFilter(false); 2409 headers.add(activatedModules); 2410 2411 StatisticColumn lastActivity = new StatisticColumn(WorkspaceModule.GROUP_HEADER_LAST_ACTIVITY_ID, new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_STATISTICS_TOOL_COLUMN_LAST_ACTIVITY")).isHidden(true); 2412 headers.add(lastActivity); 2413 2414 StatisticColumn modulesSize = new StatisticColumn(WorkspaceModule.GROUP_HEADER_SIZE_ID, new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_STATISTICS_TOOL_COLUMN_MODULES_SIZE")) 2415 .withFilter(false); 2416 modulesSize.addSubColumn(new StatisticColumn("totalSize", new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_STATISTICS_TOOL_COLUMN_MODULES_SIZE_TOTAL")) 2417 .withRenderer("Ametys.plugins.workspaces.project.tool.ProjectsGridHelper.renderSize") 2418 .withType(StatisticsColumnType.LONG)); 2419 2420 headers.add(modulesSize); 2421 2422 return headers; 2423 } 2424 2425 /** 2426 * Check if the user is in one of the populations of project 2427 * @param project the project 2428 * @param user the user 2429 * @return true if the user is in one of the populations of project 2430 */ 2431 public boolean isUserInProjectPopulations(Project project, UserIdentity user) 2432 { 2433 Site site = project.getSite(); 2434 2435 if (site == null) 2436 { 2437 throw new IllegalArgumentException("Cannot determine if user " + UserIdentity.userIdentityToString(user) + " can connect to the project " + project.getName() + " since the project has no associated site to determine the populations"); 2438 } 2439 String siteName = site.getName(); 2440 2441 Set<String> populations = _populationContextHelper.getUserPopulationsOnContext("/sites/" + siteName, false); 2442 Set<String> frontPopulations = _populationContextHelper.getUserPopulationsOnContext("/sites-fo/" + siteName, false); 2443 2444 return populations.contains(user.getPopulationId()) || frontPopulations.contains(user.getPopulationId()); 2445 } 2446 2447 /** 2448 * Thrown to indicate that the catalog site is unknown 2449 */ 2450 public class UnknownCatalogSiteException extends IllegalArgumentException 2451 { 2452 /** 2453 * Construct a {@code UnknownCatalogSiteException} with the specified message 2454 * @param message the message 2455 */ 2456 public UnknownCatalogSiteException(String message) 2457 { 2458 super(message); 2459 } 2460 } 2461 2462 /** 2463 * Thrown to indicate that the user directory site is unknown 2464 */ 2465 public class UnknownUserDirectorySiteException extends IllegalArgumentException 2466 { 2467 /** 2468 * Construct a {@code UnknownUserDirectorySiteException} with the specified message 2469 * @param message the message 2470 */ 2471 public UnknownUserDirectorySiteException(String message) 2472 { 2473 super(message); 2474 } 2475 } 2476}