001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.causeway.viewer.commons.applib.mixins; 020 021import java.util.List; 022 023import jakarta.inject.Inject; 024 025import org.apache.causeway.applib.annotation.Action; 026import org.apache.causeway.applib.annotation.ActionLayout; 027import org.apache.causeway.applib.annotation.MemberSupport; 028import org.apache.causeway.applib.annotation.Publishing; 029import org.apache.causeway.applib.annotation.Redirect; 030import org.apache.causeway.applib.annotation.RestrictTo; 031import org.apache.causeway.applib.annotation.SemanticsOf; 032import org.apache.causeway.applib.layout.LayoutConstants; 033import org.apache.causeway.applib.mixins.security.HasUsername; 034import org.apache.causeway.applib.services.factory.FactoryService; 035import org.apache.causeway.applib.services.user.ImpersonateMenu; 036 037import lombok.RequiredArgsConstructor; 038import lombok.val; 039 040/** 041 * Same as {@link org.apache.causeway.applib.services.user.ImpersonateMenu.impersonateWithRoles#act(String, List, String)}, 042 * but implemented as a mixin so that can be invoked while accessing an object. 043 * 044 * @since 2.0 {@index} 045 */ 046@Action( 047 commandPublishing = Publishing.DISABLED, 048 domainEvent = Object_impersonateWithRoles.ActionDomainEvent.class, 049 executionPublishing = Publishing.DISABLED, 050 restrictTo = RestrictTo.PROTOTYPING, 051 semantics = SemanticsOf.IDEMPOTENT 052) 053@ActionLayout( 054 cssClassFa = "fa-mask", 055 describedAs = "Switch to another user account with specified roles (for prototype/testing only)", 056 fieldSetId = LayoutConstants.FieldSetId.METADATA, 057 position = ActionLayout.Position.PANEL_DROPDOWN, 058 redirectPolicy = Redirect.EVEN_IF_SAME, 059 sequence = "850.2" 060) 061//mixin's don't need a logicalTypeName 062@RequiredArgsConstructor 063public class Object_impersonateWithRoles { 064 065 public static class ActionDomainEvent 066 extends org.apache.causeway.applib.events.domain.ActionDomainEvent<Object> {} 067 068 private final Object holder; 069 070 @MemberSupport public Object act( 071 final String userName, 072 final List<String> roleNames, 073 final String multiTenancyToken) { 074 impersonateWithRoles().act(userName, roleNames, multiTenancyToken); 075 return holder; 076 } 077 078 @MemberSupport public boolean hideAct() { 079 return impersonateWithRoles().hideAct(); 080 } 081 082 @MemberSupport public String disableAct() { 083 return impersonateWithRoles().disableAct(); 084 } 085 086 @MemberSupport public List<String> choices0Act() { 087 return impersonateWithRoles().choices0Act(); 088 } 089 090 @MemberSupport public String default0Act() { 091 if (holder instanceof HasUsername) { 092 val username = ((HasUsername) holder).getUsername(); 093 if (choices0Act().contains(username)) { 094 return username; 095 } 096 } 097 return null; 098 } 099 100 @MemberSupport public List<String> choices1Act(final String userName) { 101 return impersonateWithRoles().choices1Act(userName); 102 } 103 @MemberSupport public List<String> default1Act(final String userName) { 104 return impersonateWithRoles().default1Act(userName); 105 } 106 @MemberSupport public String default2Act(final String userName, final List<String> roleNames) { 107 return impersonateWithRoles().default2Act(userName, roleNames); 108 } 109 110 private ImpersonateMenu.impersonateWithRoles impersonateWithRoles() { 111 return factoryService.mixin(ImpersonateMenu.impersonateWithRoles.class, impersonateMenu); 112 } 113 114 @Inject ImpersonateMenu impersonateMenu; 115 @Inject FactoryService factoryService; 116 117}