1 # Copyright 2015, Tresys Technology, LLC 2 # 3 # This file is part of SETools. 4 # 5 # SETools is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Lesser General Public License as 7 # published by the Free Software Foundation, either version 2.1 of 8 # the License, or (at your option) any later version. 9 # 10 # SETools is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Lesser General Public License for more details. 14 # 15 # You should have received a copy of the GNU Lesser General Public 16 # License along with SETools. If not, see 17 # <http://www.gnu.org/licenses/>. 18 # 19 20 from PyQt5.QtCore import Qt, QAbstractTableModel, QModelIndex 21 from setools.policyrep.exception import RuleNotConditional, RuleUseError 22 23 24 class RuleResultModel(QAbstractTableModel): 25 def __init__(self, parent): 26 super(RuleResultModel, self).__init__(parent) 27 self.resultlist = None 28 29 def rowCount(self, parent=QModelIndex()): 30 if self.resultlist: 31 return len(self.resultlist) 32 else: 33 return 0 34 35 def columnCount(self, parent=QModelIndex()): 36 return 5 37 38 def headerData(self, section, orientation, role): 39 raise NotImplementedError 40 41 def data(self, index, role): 42 if role == Qt.DisplayRole: 43 if not self.resultlist: 44 return None 45 46 row = index.row() 47 col = index.column() 48 49 if col == 0: 50 return self.resultlist[row].ruletype 51 elif col == 1: 52 return str(self.resultlist[row].source) 53 elif col == 2: 54 return str(self.resultlist[row].target) 55 elif col == 3: 56 try: 57 return str(self.resultlist[row].tclass) 58 except RuleUseError: 59 # role allow 60 return None 61 elif col == 4: 62 # most common: permissions 63 try: 64 return ", ".join(sorted(self.resultlist[row].perms)) 65 except RuleUseError: 66 pass 67 68 # next most common: default 69 # TODO: figure out filename trans 70 try: 71 return str(self.resultlist[row].default) 72 except RuleUseError: 73 pass 74 75 # least common: nothing (role allow) 76 return None 77 elif col == 5: 78 try: 79 return str(self.resultlist[row].conditional) 80 except RuleNotConditional: 81 return None 82 else: 83 raise ValueError("Invalid column number") 84 elif role == Qt.UserRole: 85 # get the whole rule for user role 86 return self.resultlist[row].statement() 87 88 def set_rules(self, result_list): 89 self.beginResetModel() 90 self.resultlist = result_list 91 self.endResetModel() 92 93 94 class TERuleListModel(RuleResultModel): 95 96 """Type Enforcement rule model. Represents rules as a column.""" 97 98 def columnCount(self, parent=QModelIndex()): 99 return 6 100 101 def headerData(self, section, orientation, role): 102 if role == Qt.DisplayRole and orientation == Qt.Horizontal: 103 if section == 0: 104 return "Rule Type" 105 elif section == 1: 106 return "Source" 107 elif section == 2: 108 return "Target" 109 elif section == 3: 110 return "Object Class" 111 elif section == 4: 112 return "Permissons/Default Type" 113 elif section == 5: 114 return "Conditional Expression" 115 else: 116 raise ValueError("Invalid column number") 117