def has_visible_params_for(self, user): """ Returns true if the given user has access to one or more parameters of this channel. """ if user.super_admin: return True if user.admin: return PluginParamAccessRights.selectBy(plugin=self.plugin, administrator_read=True).count() > 0 if self.has_admin(user): return PluginParamAccessRights.selectBy(plugin=self.plugin, channel_administrator_read=True).count() > 0 if self.has_contrib(user): return PluginParamAccessRights.selectBy(plugin=self.plugin, channel_contributor_read=True).count() > 0 return False
def update_plugins(cls, dirs): """ Takes the list of the plugins directories located in ictv/plugins and updates the database if they're not in db :param dirs: The directory listing of ictv/plugins :return: the list of plugins present in updated database """ s = set() plugins_list = [] for p in Plugin.select(): s.add(p.name) if p.name not in dirs: # Plugin exists in database but was not found in the plugins directory p.activated = 'notfound' else: path = os.path.join(p.package_path, 'config.yaml') if os.path.isfile(path): # Plugin is considered to be found if p.activated == 'notfound': p.activated = 'no' with open(path, 'r') as f: config = yaml.load(f, Loader=yamlordereddictloader.Loader) p.webapp = config['plugin']['webapp'] p.static = config['plugin']['static'] p.description = config['plugin'].get( 'description', None) if 'channels_params' in config: # The plugin has channel specific parameters that can be changed from channel to channel order = 0 for k, v in config['channels_params'].items(): p.channels_params[ k] = v # Sets the parameter to its default value if 'order' not in p.channels_params[k]: p.channels_params[k]['order'] = order order += 1 if PluginParamAccessRights.selectBy( plugin=p, name=k).getOne(None) is None: PluginParamAccessRights(plugin=p, name=k) for k in list(p.channels_params): if k not in config['channels_params'].keys(): p.channels_params.pop(k) PluginParamAccessRights.deleteBy(plugin=p, name=k) p.channels_params = p.channels_params # Force SQLObject update else: p.activated = 'notfound' plugins_list.append(p) for p in dirs: if p not in s: # Plugin was not in database, it should be added but not activated plugins_list.append(Plugin(name=p, activated='no')) return plugins_list
def get_access_rights_for(self, param_name, user): """ Returns a tuple of booleans (read_access, write_access) indicating which type of rights this user has on the given param of this channel depending on they role and the param access configuration. """ if user.super_admin: return True, True rights = PluginParamAccessRights.selectBy(plugin=self.plugin, name=param_name).getOne((False, False)) if user.admin: return rights.administrator_read, rights.administrator_write if self.has_admin(user): return rights.channel_administrator_read, rights.channel_administrator_write if self.has_contrib(user): return rights.channel_contributor_read, rights.channel_contributor_write return False, False
def runTest(self): p = Plugin.byName('fake_plugin') assert 'this_parameter_does_not_exists' not in p.channels_params assert PluginParamAccessRights.selectBy( plugin=p, name='this_parameter_does_not_exists').getOne(None) is None
def runTest(self): """ Tests the PluginChannel SQLObject """ Channel.deleteMany(None) fake_plugin = Plugin.selectBy(name='fake_plugin').getOne() plugin_channel = PluginChannel(name='MyPluginChannel', plugin=fake_plugin, subscription_right='public') user = User(fullname='User', email='test@localhost') user2 = User(fullname='User2', email='test2@localhost') assert plugin_channel.get_type_name() == 'Plugin fake_plugin' # Test user permissions def assert_no_permission(c, u): assert c.get_channel_permissions_of( u) == UserPermissions.no_permission assert u not in c.get_admins() and u not in c.get_contribs() def has_contrib(u, check_inlist=True): return plugin_channel.has_contrib(u) and ( not check_inlist or u in plugin_channel.get_contribs()) def has_admin(u): return plugin_channel.has_admin( u) and u in plugin_channel.get_admins() assert_no_permission(plugin_channel, user) assert_no_permission(plugin_channel, user2) plugin_channel.give_permission_to_user( user, UserPermissions.channel_contributor) role = Role.selectBy(user=user, channel=plugin_channel).getOne() assert has_contrib(user) assert not has_admin(user) assert not has_contrib(user2) assert not has_admin(user2) assert role.permission_level == UserPermissions.channel_contributor == plugin_channel.get_channel_permissions_of( user) assert json.loads(plugin_channel.get_users_as_json()) == { str(user.id): UserPermissions.channel_contributor.value } plugin_channel.give_permission_to_user( user, UserPermissions.channel_administrator) assert has_contrib(user, check_inlist=False) assert has_admin(user) assert not has_contrib(user2) assert not has_admin(user2) assert role.permission_level == UserPermissions.channel_administrator == plugin_channel.get_channel_permissions_of( user) assert json.loads(plugin_channel.get_users_as_json()) == { str(user.id): UserPermissions.channel_administrator.value } assert json.loads(PluginChannel.get_channels_users_as_json([plugin_channel])) == \ {str(plugin_channel.id): {str(user.id): UserPermissions.channel_administrator.value}} plugin_channel.remove_permission_to_user(user) plugin_channel.give_permission_to_user( user2, UserPermissions.channel_administrator) assert not has_contrib(user) assert not has_admin(user) assert has_contrib(user2, check_inlist=False) assert has_admin(user2) plugin_channel.remove_permission_to_user(user2) assert not has_contrib(user2) assert not has_admin(user2) # Test plugin config parameters assert plugin_channel.get_config_param( 'string_param') == 'default string' assert plugin_channel.get_config_param('int_param') == 1 assert plugin_channel.get_config_param('float_param') == float('-inf') assert plugin_channel.get_config_param('boolean_param') is True assert plugin_channel.get_config_param('template_param') is None with pytest.raises(KeyError): assert plugin_channel.get_config_param( 'this_param_does_not_exists') def assert_value_is_set(param, value): plugin_channel.plugin_config[param] = value plugin_channel.plugin_config = plugin_channel.plugin_config # Force SQLObject update assert plugin_channel.get_config_param(param) == value assert_value_is_set('string_param', 'Hello, world!') assert_value_is_set('int_param', 42) assert_value_is_set('float_param', 42.0) assert_value_is_set('boolean_param', False) assert_value_is_set('template_param', 'fake-template') # Test parameters access rights ppar = PluginParamAccessRights.selectBy(plugin=fake_plugin, name='int_param').getOne() ppar.channel_contributor_read = False ppar.channel_contributor_write = False ppar.channel_administrator_read = True ppar.channel_administrator_write = False ppar.administrator_read = True ppar.administrator_write = True user.super_admin = True assert plugin_channel.has_visible_params_for(user) for param in [ 'string_param', 'int_param', 'float_param', 'boolean_param', 'template_param' ]: assert plugin_channel.get_access_rights_for(param, user) == (True, True) user.super_admin = False assert not plugin_channel.has_visible_params_for(user) plugin_channel.give_permission_to_user( user, UserPermissions.channel_contributor) assert not plugin_channel.has_visible_params_for(user) assert plugin_channel.get_access_rights_for('int_param', user) == (False, False) plugin_channel.give_permission_to_user( user, UserPermissions.channel_administrator) assert plugin_channel.has_visible_params_for(user) assert plugin_channel.get_access_rights_for('int_param', user) == (True, False) user.admin = True assert plugin_channel.has_visible_params_for(user) assert plugin_channel.get_access_rights_for('int_param', user) == (True, True) plugin_channel.remove_permission_to_user(user) user.admin = False assert not plugin_channel.has_visible_params_for(user) assert plugin_channel.get_access_rights_for('int_param', user) == (False, False) # Test miscellaneous parameters assert plugin_channel.cache_activated is True plugin_channel.plugin.cache_activated_default = False assert plugin_channel.cache_activated is False plugin_channel.cache_activated = True assert plugin_channel.cache_activated is True assert plugin_channel.cache_validity is 60 plugin_channel.plugin.cache_validity_default = 120 assert plugin_channel.cache_validity is 120 plugin_channel.cache_validity = 42 assert plugin_channel.cache_validity is 42 assert plugin_channel.keep_noncomplying_capsules is False plugin_channel.plugin.keep_noncomplying_capsules_default = True assert plugin_channel.keep_noncomplying_capsules is True plugin_channel.keep_noncomplying_capsules = False assert plugin_channel.keep_noncomplying_capsules is False # Test flatten() plugin_channel.enabled = False assert plugin_channel.flatten() == [] assert plugin_channel.flatten(keep_disabled_channels=True) == [ plugin_channel ] plugin_channel.enabled = True assert plugin_channel.flatten() == [plugin_channel]