001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2022 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconsole.base;
020
021import java.util.Collections;
022import java.util.HashSet;
023import java.util.Map;
024import java.util.Set;
025import java.util.stream.Collectors;
026import org.jgrapes.core.Channel;
027import org.jgrapes.core.Component;
028import org.jgrapes.core.Event;
029import org.jgrapes.core.Manager;
030import org.jgrapes.core.annotation.Handler;
031import org.jgrapes.webconsole.base.events.AddConletType;
032import org.jgrapes.webconsole.base.events.ConsolePrepared;
033import org.jgrapes.webconsole.base.events.UpdateConletType;
034
035/**
036 * Configures the conlets available based on the user currently logged in.
037 */
038public class UserBasedConletFilter extends Component {
039
040    private final Map<String, Set<String>> acl;
041    private final Set<String> restricted;
042
043    /**
044     * Creates a new component with its channel set to the given 
045     * channel.
046     *
047     * Supported properties are:
048     * 
049     *  * *conletTypesByUsername*: a Map<String, Set<String>> holding
050     *    the conlet types {@link AddConletType#conletType()} to be
051     *    added for given user (see @link {@link ConsoleUser#getName()}.
052     *    Conlets that are not restricted to at least one user are added
053     *    for all users.
054     *
055     * @param componentChannel the channel that the component's
056     * handlers listen on by default and that 
057     * {@link Manager#fire(Event, Channel...)} sends the event to
058     * @param properties the properties used to configure the component
059     */
060    @SuppressWarnings("unchecked")
061    public UserBasedConletFilter(Channel componentChannel,
062            Map<?, ?> properties) {
063        super(componentChannel);
064        acl = (Map<String, Set<String>>) properties
065            .get("conletTypesByUsername");
066        restricted = acl.values().stream().flatMap(Set::stream)
067            .collect(Collectors.toSet());
068    }
069
070    /**
071     * Filter the events.
072     *
073     * @param event the event
074     * @param channel the channel
075     */
076    @Handler(priority = 999)
077    @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
078    public void onConsolePrepared(ConsolePrepared event,
079            ConsoleConnection channel) {
080        Set<String> toRemove = new HashSet<>(restricted);
081        toRemove.removeAll(
082            WebConsoleUtils.userFromSession(channel.session())
083                .map(ConsoleUser::getName).map(user -> acl.get(user))
084                .orElse(Collections.emptySet()));
085        for (var type : toRemove) {
086            channel.respond(new UpdateConletType(type));
087        }
088    }
089}