def has_perm_mask(self, user_mask, perm, notice=True): """ :type user_mask: str :type perm: str :rtype: bool """ if backdoor: if match_mask(user_mask.lower(), backdoor.lower()): return True if not perm.lower() in self.perm_users: # no one has access return False allowed_users = self.perm_users[perm.lower()] for allowed_mask in allowed_users: if match_mask(user_mask.lower(), allowed_mask): if notice: logger.info( "[%s|permissions] Allowed user %s access to %s", self.name, user_mask, perm) return True return False
def test_mask_match(data): pattern = data['mask'] for hostmask in data['matches']: assert match_mask(hostmask, pattern) for hostmask in data['fails']: assert not match_mask(hostmask, pattern)
def test_mask_match(data): """Test the mask matching logic""" pattern = data["mask"] for hostmask in data["matches"]: assert match_mask(hostmask, pattern) for hostmask in data["fails"]: assert not match_mask(hostmask, pattern)
def is_ignored(conn, chan, mask): mask_cf = mask.casefold() for _conn, _chan, _mask in ignore_cache: _mask_cf = _mask.casefold() if _chan == "*": # this is a global ignore if match_mask(mask_cf, _mask_cf): return True else: # this is a channel-specific ignore if (conn, chan) != (_conn, _chan): continue if match_mask(mask_cf, _mask_cf): return True
def remove_group_user(self, group, user_mask): """ Removes all users that match user_mask from group. Returns a list of user masks removed from the group. Use permission_manager.reload() to make this change take affect. Use bot.config.save_config() to save this change to file. :type group: str :type user_mask: str :rtype: list[str] """ masks_removed = [] config_groups = self.config.get("permissions", {}) for mask_to_check in list(self.group_users[group.lower()]): if match_mask(user_mask.lower(), mask_to_check): masks_removed.append(mask_to_check) # We're going to act like the group keys are all lowercase. # The user has been warned (above) if they aren't. # Okay, maybe a warning, but no support. if group not in config_groups: logger.warning( "[%s|permissions] Can't remove user from group due to" " upper-case group names!", self.name) continue config_group = config_groups.get(group) config_users = config_group.get("users") config_users.remove(mask_to_check) return masks_removed
def get_user_permissions(self, user_mask): """ :type user_mask: str :rtype: list[str] """ permissions = set() for permission, users in self.perm_users.items(): for mask_to_check in users: if match_mask(user_mask.lower(), mask_to_check): permissions.add(permission) return permissions
def get_user_groups(self, user_mask): """ :type user_mask: str :rtype: list[str] """ groups = [] for group, users in self.group_users.items(): for mask_to_check in users: if match_mask(user_mask.lower(), mask_to_check): groups.append(group) continue return groups
def user_in_group(self, user_mask, group): """ Checks whether a user is matched by any masks in a given group :type group: str :type user_mask: str :rtype: bool """ users = self.group_users.get(group.lower()) if not users: return False for mask_to_check in users: if match_mask(user_mask.lower(), mask_to_check): return True return False
def match_chan(self, channel): return match_mask(channel.casefold(), self.channel)
def match(self, channel, hook_name): return self.match_chan(channel) and match_mask(hook_name.casefold(), self.hook)