1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
# SPDX-License-Identifier: LGPL-2.1-only
from contextlib import suppress
import itertools
import typing
from PyQt6 import QtCore, QtWidgets
import setools
from .. import models
from .criteria import CriteriaWidget, OptionsPlacement
from .list import ListWidget
from .name import NameWidget
# permissive default setting (not checked)
PERMISSIVE_DEFAULT_CHECKED: typing.Final[bool] = False
# Regex for exact matches to types/attrs
VALIDATE_EXACT: typing.Final[str] = r"[A-Za-z0-9._-]*"
# indirect default setting (checked)
INDIRECT_DEFAULT_CHECKED: typing.Final[bool] = True
__all__ = ('PermissiveType', 'TypeList', 'TypeName', 'TypeOrAttrName',)
class PermissiveType(CriteriaWidget):
"""A widget providing a QCheckBox widget for selecting permissive types."""
def __init__(self, title: str, query: setools.PolicyQuery, attrname: str,
parent: QtWidgets.QWidget | None = None) -> None:
super().__init__(title, query, attrname, parent=parent)
self.top_layout = QtWidgets.QHBoxLayout(self)
self.criteria = QtWidgets.QCheckBox(self)
self.criteria.setText("Permissive")
self.criteria.setToolTip("Permissive types will match.")
self.criteria.setWhatsThis("<b>Permissive types will match.</b>")
self.criteria.toggled.connect(self._update_query)
self.top_layout.addWidget(self.criteria)
# set initial state:
self.criteria.setChecked(PERMISSIVE_DEFAULT_CHECKED)
@property
def has_errors(self) -> bool:
"""Get error state of this widget."""
return False
def _update_query(self, state: bool) -> None:
"""Set the permissive boolean value."""
self.log.debug(f"Setting {self.attrname} {state}")
setattr(self.query, self.attrname, state)
#
# Save/Load field
#
def save(self, settings: dict) -> None:
"""Save the widget settings to the settings dictionary."""
settings[self.attrname] = self.criteria.isChecked()
def load(self, settings: dict) -> None:
"""Load the widget settings from the settings dictionary."""
with suppress(KeyError):
self.criteria.setChecked(settings[self.attrname])
class TypeList(ListWidget):
"""A widget providing a QListView widget for selecting the types."""
def __init__(self, title: str, query: setools.PolicyQuery, attrname: str,
enable_equal: bool = False, enable_subset: bool = False,
parent: QtWidgets.QWidget | None = None) -> None:
model = models.TypeTable(data=sorted(query.policy.types()))
super().__init__(title, query, attrname, model, enable_equal=enable_equal,
enable_subset=enable_subset, parent=parent)
self.criteria_any.setToolTip("Any selected type will match.")
self.criteria_any.setWhatsThis("<b>Any selected type will match.</b>")
class TypeName(NameWidget):
"""
Widget providing a QLineEdit that saves the input to the attributes
of the specified query. This supports inputs of types.
"""
indirect_toggled = QtCore.pyqtSignal(bool)
def __init__(self, title: str, query: setools.PolicyQuery, attrname: str, /, *,
parent: QtWidgets.QWidget | None = None,
options_placement: OptionsPlacement = OptionsPlacement.RIGHT,
enable_indirect: bool = False, enable_regex: bool = False,
required: bool = False):
# Create completion list
completion = list[str](t.name for t in query.policy.types())
super().__init__(title, query, attrname, completion, VALIDATE_EXACT,
enable_regex=enable_regex, required=required,
options_placement=options_placement, parent=parent)
if enable_indirect:
self.criteria_indirect = QtWidgets.QCheckBox(self)
# the rstrip("_") is to avoid names like "type__indirect"
self.criteria_indirect.setObjectName(f"{attrname.rstrip('_')}_indirect")
self.criteria_indirect.setText("Indirect")
self.criteria_indirect.setToolTip("Enables indirect matching.")
self.criteria_indirect.setWhatsThis(
"""
<p><b>Indirect matching<b></p>
<p>If the criteria is an attribute, indirect will
match the criteria against the contents of the
attribute, rather than the attribute itself.</p>
""")
self.top_layout.addWidget(self.criteria_indirect, 1, 1, 1, 1)
self.criteria_indirect.toggled.connect(self.set_indirect)
self.criteria_indirect.toggled.connect(self.indirect_toggled)
# set initial state:
self.criteria_indirect.setChecked(INDIRECT_DEFAULT_CHECKED)
self.set_indirect(INDIRECT_DEFAULT_CHECKED)
# place widget.
match options_placement:
case OptionsPlacement.RIGHT | OptionsPlacement.BELOW:
self.top_layout.addWidget(self.criteria_indirect, 1, 1, 1, 1)
case _:
raise AssertionError(
f"Invalid options placement {options_placement}, this is an SETools bug.")
def set_indirect(self, state: bool) -> None:
"""Set the indirect boolean value."""
self.log.debug(f"Setting {self.criteria_indirect.objectName()} {state}")
setattr(self.query, self.criteria_indirect.objectName(), state)
#
# Workspace methods
#
def save(self, settings: dict) -> None:
super().save(settings)
with suppress(AttributeError):
settings[self.criteria_indirect.objectName()] = self.criteria_indirect.isChecked()
def load(self, settings: dict) -> None:
with suppress(AttributeError, KeyError):
self.criteria_indirect.setChecked(settings[self.criteria_indirect.objectName()])
super().load(settings)
class TypeOrAttrName(TypeName):
"""
Widget providing a QLineEdit that saves the input to the attributes
of the specified query. This supports inputs of types or attributes.
"""
def __init__(self, title: str, query: setools.PolicyQuery, attrname: str, /, *,
parent: QtWidgets.QWidget | None = None,
options_placement: OptionsPlacement = OptionsPlacement.RIGHT,
enable_indirect: bool = False, enable_regex: bool = False,
required: bool = False):
super().__init__(title, query, attrname, options_placement=options_placement,
enable_indirect=enable_indirect, enable_regex=enable_regex,
required=required, parent=parent)
# add attributes to completion list
completer = self.criteria.completer()
assert completer, "Completer not set, this is an SETools bug."
model = typing.cast(QtCore.QStringListModel, completer.model())
assert model, "Model not set, this is an SETools bug."
model.setStringList(sorted(itertools.chain(
(a.name for a in query.policy.typeattributes()),
(t.name for t in query.policy.types())
)))
if __name__ == '__main__':
import sys
import warnings
import pprint
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s|%(levelname)s|%(name)s|%(message)s')
warnings.simplefilter("default")
q = setools.TERuleQuery(setools.SELinuxPolicy())
app = QtWidgets.QApplication(sys.argv)
mw = QtWidgets.QMainWindow()
widget = TypeOrAttrName("Test Type/Attribute", q, "source", parent=mw,
enable_regex=True, enable_indirect=True)
widget.setToolTip("test tooltip")
widget.setWhatsThis("test what's this")
mw.setCentralWidget(widget)
mw.resize(widget.size())
whatsthis = QtWidgets.QWhatsThis.createAction(mw)
mw.menuBar().addAction(whatsthis) # type: ignore[union-attr]
mw.setStatusBar(QtWidgets.QStatusBar(mw))
mw.show()
rc = app.exec()
print("source:", q.source)
print("regex:", q.source_regex)
print("indirect:", q.source_indirect)
print("Errors?", widget.has_errors)
# basic test of save/load
saved_settings: dict = {}
widget.save(saved_settings)
pprint.pprint(saved_settings)
saved_settings["source"] = "user_t"
widget.load(saved_settings)
print("Query final state")
print("source:", q.source)
print("regex:", q.source_regex)
print("indirect:", q.source_indirect)
print("Errors?", widget.has_errors)
sys.exit(rc)
|