001/* 002 * PermissionsEx 003 * Copyright (C) zml and PermissionsEx contributors 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package ca.stellardrift.permissionsex.impl.logging; 018 019import ca.stellardrift.permissionsex.context.ContextValue; 020import ca.stellardrift.permissionsex.logging.FormattedLogger; 021import ca.stellardrift.permissionsex.logging.PermissionCheckNotifier; 022import ca.stellardrift.permissionsex.subject.SubjectRef; 023import org.checkerframework.checker.nullness.qual.Nullable; 024 025import java.util.List; 026import java.util.Map; 027import java.util.Set; 028import java.util.function.Predicate; 029 030import static ca.stellardrift.permissionsex.impl.logging.Messages.CHECK_OPTION; 031import static ca.stellardrift.permissionsex.impl.logging.Messages.CHECK_PERMISSION; 032 033/** 034 * Log debug messages 035 */ 036public class DebugPermissionCheckNotifier implements PermissionCheckNotifier { 037 private final FormattedLogger logger; 038 private final PermissionCheckNotifier delegate; 039 private final Predicate<String> filterPredicate; 040 041 public DebugPermissionCheckNotifier(FormattedLogger logger, PermissionCheckNotifier delegate, final @Nullable Predicate<String> filterPredicate) { 042 this.logger = logger; 043 this.delegate = delegate; 044 this.filterPredicate = filterPredicate == null ? x -> true : filterPredicate; 045 } 046 047 private <I> String stringIdentifier(SubjectRef<I> identifier) { 048 return identifier.type().name() + " " + identifier.type().serializeIdentifier(identifier.identifier()); 049 } 050 051 public PermissionCheckNotifier getDelegate() { 052 return this.delegate; 053 } 054 055 @Override 056 public void onPermissionCheck(SubjectRef<?> subject, Set<ContextValue<?>> contexts, String permission, int value) { 057 if (this.filterPredicate.test(permission)) { 058 logger.info(CHECK_PERMISSION.tr(permission, contexts, stringIdentifier(subject), value)); 059 } 060 delegate.onPermissionCheck(subject, contexts, permission, value); 061 } 062 063 @Override 064 public void onOptionCheck(SubjectRef<?> subject, Set<ContextValue<?>> contexts, String option, String value) { 065 if (this.filterPredicate.test(option)) { 066 logger.info(CHECK_OPTION.tr(option, contexts, stringIdentifier(subject), value)); 067 } 068 delegate.onOptionCheck(subject, contexts, option, value); 069 } 070 071 @Override 072 public void onParentCheck(SubjectRef<?> subject, Set<ContextValue<?>> contexts, List<SubjectRef<?>> parents) { 073 logger.info(Messages.CHECK_PARENT.tr(contexts, stringIdentifier(subject), parents)); 074 delegate.onParentCheck(subject, contexts, parents); 075 } 076}