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.rbac; 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.ConsoleConnection; 032import org.jgrapes.webconsole.base.ConsoleUser; 033import org.jgrapes.webconsole.base.WebConsoleUtils; 034import org.jgrapes.webconsole.base.events.AddConletType; 035import org.jgrapes.webconsole.base.events.ConsolePrepared; 036import org.jgrapes.webconsole.base.events.UpdateConletType; 037 038/** 039 * Configures the conlets available based on the user currently logged in. 040 */ 041public class UserBasedConletFilter extends Component { 042 043 private final Map<String, Set<String>> acl; 044 private final Set<String> restricted; 045 046 /** 047 * Creates a new component with its channel set to the given 048 * channel. 049 * 050 * Supported properties are: 051 * 052 * * *conletTypesByUsername*: a Map<String, Set<String>> holding 053 * the conlet types {@link AddConletType#conletType()} to be 054 * added for given user (see @link {@link ConsoleUser#getName()}. 055 * Conlets that are not restricted to at least one user are added 056 * for all users. 057 * 058 * @param componentChannel the channel that the component's 059 * handlers listen on by default and that 060 * {@link Manager#fire(Event, Channel...)} sends the event to 061 * @param properties the properties used to configure the component 062 */ 063 @SuppressWarnings("unchecked") 064 public UserBasedConletFilter(Channel componentChannel, 065 Map<?, ?> properties) { 066 super(componentChannel); 067 acl = (Map<String, Set<String>>) properties 068 .get("conletTypesByUsername"); 069 restricted = acl.values().stream().flatMap(Set::stream) 070 .collect(Collectors.toSet()); 071 } 072 073 /** 074 * Filter the events. 075 * 076 * @param event the event 077 * @param channel the channel 078 */ 079 @Handler(priority = 999) 080 @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") 081 public void onConsolePrepared(ConsolePrepared event, 082 ConsoleConnection channel) { 083 Set<String> toRemove = new HashSet<>(restricted); 084 toRemove.removeAll( 085 WebConsoleUtils.userFromSession(channel.session()) 086 .map(ConsoleUser::getName).map(user -> acl.get(user)) 087 .orElse(Collections.emptySet())); 088 for (var type : toRemove) { 089 channel.respond(new UpdateConletType(type)); 090 } 091 } 092}