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 jakarta.inject.Inject;
022
023import org.apache.causeway.applib.annotation.Action;
024import org.apache.causeway.applib.annotation.ActionLayout;
025import org.apache.causeway.applib.annotation.MemberSupport;
026import org.apache.causeway.applib.annotation.Publishing;
027import org.apache.causeway.applib.annotation.Redirect;
028import org.apache.causeway.applib.annotation.RestrictTo;
029import org.apache.causeway.applib.annotation.SemanticsOf;
030import org.apache.causeway.applib.layout.LayoutConstants;
031import org.apache.causeway.applib.mixins.security.HasUsername;
032import org.apache.causeway.applib.services.factory.FactoryService;
033import org.apache.causeway.applib.services.user.ImpersonateMenu;
034
035import lombok.RequiredArgsConstructor;
036
037/**
038 * Same as {@link org.apache.causeway.applib.services.user.ImpersonateMenu.impersonate#act(String)},
039 * but implemented as a mixin so that can be invoked while accessing an object.
040 *
041 * @since 2.0 {@index}
042 */
043@Action(
044        commandPublishing = Publishing.DISABLED,
045        domainEvent = Object_impersonate.ActionDomainEvent.class,
046        executionPublishing = Publishing.DISABLED,
047        restrictTo = RestrictTo.PROTOTYPING,
048        semantics = SemanticsOf.IDEMPOTENT
049)
050@ActionLayout(
051        cssClassFa = "fa-mask",
052        describedAs = "Switch to another user account (for prototype/testing only)",
053        fieldSetId = LayoutConstants.FieldSetId.METADATA,
054        position = ActionLayout.Position.PANEL_DROPDOWN,
055        redirectPolicy = Redirect.EVEN_IF_SAME,
056        sequence = "850.1"
057)
058@RequiredArgsConstructor
059public class Object_impersonate {
060
061    public static class ActionDomainEvent
062    extends org.apache.causeway.applib.events.domain.ActionDomainEvent<Object> {}
063
064    private final Object holder;
065
066    @MemberSupport public Object act(final String userName) {
067        impersonate().act(userName);
068        return holder;
069    }
070
071    @MemberSupport public boolean hideAct() { return impersonate().hideAct(); }
072    @MemberSupport public String disableAct() { return impersonate().disableAct();
073    }
074    @MemberSupport public String default0Act() {
075        return holder instanceof HasUsername
076                ? ((HasUsername)holder).getUsername()
077                : null;
078    }
079
080    private ImpersonateMenu.impersonate impersonate() {
081        return factoryService.mixin(ImpersonateMenu.impersonate.class, impersonateMenu);
082    }
083
084    @Inject ImpersonateMenu impersonateMenu;
085    @Inject FactoryService factoryService;
086
087}