001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 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// -------------------------------------------------------------------------------- 016 017package com.echothree.model.control.security.server.control; 018 019import com.echothree.model.control.search.common.SearchOptions; 020import com.echothree.model.control.search.server.control.SearchControl; 021import com.echothree.model.control.security.common.transfer.SecurityRoleResultTransfer; 022import com.echothree.model.data.search.common.CachedExecutedSearchResultConstants; 023import com.echothree.model.data.search.common.SearchResultConstants; 024import com.echothree.model.data.search.server.entity.UserVisitSearch; 025import com.echothree.model.data.search.server.factory.CachedExecutedSearchResultFactory; 026import com.echothree.model.data.search.server.factory.SearchResultFactory; 027import com.echothree.model.data.security.common.pk.SecurityRolePK; 028import com.echothree.model.data.security.server.factory.SecurityRoleFactory; 029import com.echothree.model.data.user.server.entity.UserVisit; 030import static com.echothree.model.jooq.server.keys.search.SearchForeignKeys.CACHED_EXECUTED_SEARCH_RESULTS_ENTITY_INSTANCE_FK; 031import static com.echothree.model.jooq.server.keys.search.SearchForeignKeys.SEARCH_RESULTS_ENTITY_INSTANCE_FK; 032import static com.echothree.model.jooq.server.tables.core.EntityInstances.EntityInstances; 033import static com.echothree.model.jooq.server.tables.search.CachedExecutedSearchResults.CachedExecutedSearchResults; 034import static com.echothree.model.jooq.server.tables.search.SearchResults.SearchResults; 035import com.echothree.util.server.cdi.CommandScope; 036import com.echothree.util.server.control.BaseModelControl; 037import com.echothree.util.server.persistence.EntityPermission; 038import java.util.ArrayList; 039import java.util.List; 040import javax.inject.Inject; 041import org.jooq.Select; 042 043@CommandScope 044public class SecurityRoleControl 045 extends BaseModelControl { 046 047 @Inject 048 private SearchControl searchControl; 049 050 @Inject 051 protected SecurityControl securityControl; 052 053 /** Creates a new instance of SecurityRoleControl */ 054 protected SecurityRoleControl() { 055 super(); 056 } 057 058 // -------------------------------------------------------------------------------- 059 // Security Role Searches 060 // -------------------------------------------------------------------------------- 061 062 @Inject 063 protected SearchResultFactory searchResultFactory; 064 065 @Inject 066 protected SecurityRoleFactory securityRoleFactory; 067 068 private List<Long> getSecurityRoleIdsFromUserVisitSearch(UserVisitSearch userVisitSearch) { 069 var search = userVisitSearch.getSearch(); 070 var cachedSearch = search.getCachedSearch(); 071 Select<?> query; 072 073 if(cachedSearch == null) { 074 query = session.applyLimit(session.getDslContext() 075 .select(EntityInstances.ENTITY_UNIQUE) 076 .from(SearchResults) 077 .join(EntityInstances).onKey(SEARCH_RESULTS_ENTITY_INSTANCE_FK) 078 .where(SearchResults.SEARCH.eq(search.getPrimaryKey())) 079 .orderBy(SearchResults.SORT_ORDER, SearchResults.ENTITY_INSTANCE), SearchResultFactory.class); 080 } else { 081 var cachedExecutedSearch = searchControl.getCachedExecutedSearch(cachedSearch); 082 083 session.copyLimit(SearchResultConstants.ENTITY_TYPE_NAME, CachedExecutedSearchResultConstants.ENTITY_TYPE_NAME); 084 085 query = session.applyLimit(session.getDslContext() 086 .select(EntityInstances.ENTITY_UNIQUE) 087 .from(CachedExecutedSearchResults) 088 .join(EntityInstances).onKey(CACHED_EXECUTED_SEARCH_RESULTS_ENTITY_INSTANCE_FK) 089 .where(CachedExecutedSearchResults.CACHED_EXECUTED_SEARCH.eq(cachedExecutedSearch.getPrimaryKey())) 090 .orderBy(CachedExecutedSearchResults.SORT_ORDER, CachedExecutedSearchResults.ENTITY_INSTANCE), 091 CachedExecutedSearchResultFactory.class); 092 } 093 094 return query.fetch(EntityInstances.ENTITY_UNIQUE); 095 } 096 097 public List<SecurityRoleResultTransfer> getSecurityRoleResultTransfers(UserVisit userVisit, UserVisitSearch userVisitSearch) { 098 var entityIds = getSecurityRoleIdsFromUserVisitSearch(userVisitSearch); 099 var securityRoleResultTransfers = new ArrayList<SecurityRoleResultTransfer>(entityIds.size()); 100 var options = session.getOptions(); 101 var includeSecurityRole = options != null && options.contains(SearchOptions.SecurityRoleResultIncludeSecurityRole); 102 103 for(var entityUniqueId : entityIds) { 104 var securityRole = securityRoleFactory.getEntityFromPK(EntityPermission.READ_ONLY, new SecurityRolePK(entityUniqueId)); 105 var securityRoleDetail = securityRole.getLastDetail(); 106 107 securityRoleResultTransfers.add(new SecurityRoleResultTransfer(securityRoleDetail.getSecurityRoleName(), 108 securityRoleDetail.getSecurityRoleGroup().getLastDetail().getSecurityRoleGroupName(), 109 includeSecurityRole ? securityControl.getSecurityRoleTransfer(userVisit, securityRole) : null)); 110 } 111 112 return securityRoleResultTransfers; 113 } 114 115}