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# pylint: disable=attribute-defined-outside-init,no-member 20import re 21 22from .descriptors import CriteriaDescriptor, CriteriaSetDescriptor 23 24 25class MatchAlias(object): 26 27 """Mixin for matching an object's aliases.""" 28 29 alias = CriteriaDescriptor("alias_regex") 30 alias_regex = False 31 32 def _match_alias(self, obj): 33 """ 34 Match the alias criteria 35 36 Parameter: 37 obj An object with an alias generator method named "aliases" 38 """ 39 40 if not self.alias: 41 # if there is no criteria, everything matches. 42 return True 43 44 return self._match_in_set(obj.aliases(), self.alias, self.alias_regex) 45 46 47class MatchObjClass(object): 48 49 """Mixin for matching an object's class.""" 50 51 tclass = CriteriaSetDescriptor("tclass_regex", "lookup_class") 52 tclass_regex = False 53 54 def _match_object_class(self, obj): 55 """ 56 Match the object class criteria 57 58 Parameter: 59 obj An object with an object class attribute named "tclass" 60 """ 61 62 if not self.tclass: 63 # if there is no criteria, everything matches. 64 return True 65 elif self.tclass_regex: 66 return bool(self.tclass.search(str(obj.tclass))) 67 else: 68 return obj.tclass in self.tclass 69 70 71class MatchPermission(object): 72 73 """Mixin for matching an object's permissions.""" 74 75 perms = CriteriaSetDescriptor("perms_regex") 76 perms_equal = False 77 perms_regex = False 78 79 def _match_perms(self, obj): 80 """ 81 Match the permission criteria 82 83 Parameter: 84 obj An object with a permission set class attribute named "perms" 85 """ 86 87 if not self.perms: 88 # if there is no criteria, everything matches. 89 return True 90 91 return self._match_regex_or_set(obj.perms, self.perms, self.perms_equal, self.perms_regex) 92