Home | History | Annotate | Download | only in apol
      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 import QtCore
     21 from PyQt5.QtCore import QAbstractListModel, QModelIndex, QStringListModel, Qt
     22 from setools.policyrep.exception import NoCommon
     23 
     24 
     25 class SEToolsListModel(QAbstractListModel):
     26 
     27     """
     28     The purpose of this model is to have the
     29     objects return their string representations
     30     for Qt.DisplayRole and return the object
     31     for Qt.UserRole.
     32     """
     33 
     34     def __init__(self, parent):
     35         super(SEToolsListModel, self).__init__(parent)
     36         self._item_list = None
     37 
     38     @property
     39     def item_list(self):
     40         return self._item_list
     41 
     42     @item_list.setter
     43     def item_list(self, item_list):
     44         self.beginResetModel()
     45         self._item_list = item_list
     46         self.endResetModel()
     47 
     48     def rowCount(self, parent=QModelIndex()):
     49         if self.item_list:
     50             return len(self.item_list)
     51         else:
     52             return 0
     53 
     54     def columnCount(self, parent=QModelIndex()):
     55         return 1
     56 
     57     def data(self, index, role):
     58         if self.item_list:
     59             row = index.row()
     60 
     61             if role == Qt.DisplayRole:
     62                 return str(self.item_list[row])
     63             elif role == Qt.UserRole:
     64                 return self.item_list[row]
     65 
     66 
     67 class PermListModel(SEToolsListModel):
     68 
     69     """
     70     A model that will return the intersection of permissions
     71     for the selected classes.  If no classes are
     72     set, all permissions in the policy will be returned.
     73     """
     74 
     75     def __init__(self, parent, policy):
     76         super(PermListModel, self).__init__(parent)
     77         self.policy = policy
     78         self.set_classes()
     79 
     80     def set_classes(self, classes=[]):
     81         permlist = set()
     82 
     83         # start will all permissions.
     84         for cls in self.policy.classes():
     85             permlist.update(cls.perms)
     86 
     87             try:
     88                 permlist.update(cls.common.perms)
     89             except NoCommon:
     90                 pass
     91 
     92         # create intersection
     93         for cls in classes:
     94             cls_perms = cls.perms
     95 
     96             try:
     97                 cls_perms.update(cls.common.perms)
     98             except NoCommon:
     99                 pass
    100 
    101             permlist.intersection_update(cls_perms)
    102 
    103         self.item_list = sorted(permlist)
    104