def give_permission_to_user(self, user: User, permission_level: UserPermissions = UserPermissions.channel_contributor) -> None: """ Give permission to the user or modify existing permission previously given. :param user: The user receiving the permission level on this channel. :param permission_level: The level of permission to give on this channel. :return: None """ role = Role.selectBy(user=user, channel=self).getOne(None) if role is None: Role(user=user, channel=self, permission_level=permission_level) else: role.permission_level = permission_level
def send_email_alert(self, channel, filtered_out_content): message = 'the content of channel %s contained a total of %d slide(s) with non-complying field(s):\n' % ( channel.name, len(filtered_out_content)) for slide in filtered_out_content: for f in slide: message += 'The field %s from template %s allows up to %d characters but it was provided with "%s" for a total of %d characters.\n' % ( f[0], f[1], f[3], f[4], f[2]) message += '\n' message_hash = hash(message) message = ('At %s, ' % datetime.now()) + message if Role.selectBy(channel=channel, permission_level='channel_administrator').count() == 0: for super_admin in User.selectBy(super_admin=True): self.template_limits_email_digester.add_email(super_admin, message, message_hash) else: for administrator in Role.selectBy(channel=channel, permission_level='channel_administrator').throughTo.user: self.template_limits_email_digester.add_email(administrator, message, message_hash)
def runTest(self): """ Tests the Role SQLObject """ Channel.deleteMany(None) fake_plugin = Plugin(name='fake_plugin', activated='notfound') plugin_channel = PluginChannel(name='Plugin Channel', plugin=fake_plugin, subscription_right='public') user = User(fullname='User', email='test@localhost') role = Role(user=user, channel=plugin_channel, permission_level=UserPermissions.channel_administrator) assert role._SO_get_permission_level() == 'channel_administrator' assert role.permission_level == UserPermissions.channel_administrator role.permission_level = UserPermissions.channel_contributor assert role._SO_get_permission_level() == 'channel_contributor' assert role.permission_level == UserPermissions.channel_contributor
def _get_users_as_dict(self): """ Return a dictionary in the form { user.id: UserPermissions integer value, ... } """ return {role.user.id: role.permission_level.value for role in Role.selectBy(channel=self)}
def get_channel_permissions_of(self, user): """ Return the permission level of this user on this channel. :return: UserPermissions """ role = Role.selectBy(user=user, channel=self).getOne(None) if role is not None: return role.permission_level return UserPermissions.no_permission
def remove_permission_to_user(self, user: User) -> None: """ Remove previously given permission to the user :param user: The user to withdraw permission on this channel. :return: """ role = Role.selectBy(user=user, channel=self).getOne(None) if role is not None: role.destroySelf()
def create_database(): Building.createTable() Channel.createTable() Plugin.createTable() User.createTable() PluginChannel.createTable() ChannelBundle.createTable() Role.createTable() Screen.createTable() ScreenMac.createTable() Subscription.createTable() Template.createTable() Asset.createTable() PluginParamAccessRights.createTable() LogStat.createTable() DBVersion.createTable() DBVersion(version=database_version) User(username="******", fullname="ICTV Admin", email="admin@ictv", super_admin=True, disabled=False)
def runTest(self): """ Tests the Role object. """ try: u = User(username='******', fullname='test test', email='*****@*****.**', super_admin=True, disabled=False) PluginChannel(name='test', plugin=Plugin(name='role_plugin', activated='no'), subscription_right='restricted') c = PluginChannel.selectBy(name="test").getOne() r = Role(user=u, channel=c, permission_level=UserPermissions.channel_contributor) assert_true(r != None) assert_true( r.permission_level == UserPermissions.channel_contributor) r.permission_level = UserPermissions.channel_administrator assert_true( r.permission_level == UserPermissions.channel_administrator) Role.delete(r.id) role = Role.selectBy(user=u, channel=c).getOne(None) assert_true(role == None) PluginChannel.delete(c.id) User.delete(u.id) except DuplicateEntryError: assert_true(False)
def get_visible_channels_of(cls, user): """ Returns the channels that are accessible for the user (public channels or channels with the user specified in authorized_subscribers, or all channels if the user is superadmin) :param user: The user to retrieve the accessible channels. :return: A iterable with the accessible channels (iterable of sqlobjects) """ if UserPermissions.administrator in user.highest_permission_level: return set(Channel.select()) public_channels = set(Channel.selectBy(subscription_right='public')) if UserPermissions.screen_administrator in \ user.highest_permission_level else set() return public_channels | set(Role.selectBy(user=user).throughTo.channel) | set( User.selectBy(id=user.id).throughTo.authorized_channels)
def runTest(self): """ Tests the Channel object. """ try: PluginChannel(name='test', plugin=Plugin(name='channel_plugin', activated='no'), subscription_right='restricted') PluginChannel( name='test2', plugin=Plugin.selectBy(name='channel_plugin').getOne(), subscription_right='restricted') c = PluginChannel.selectBy(name="test").getOne() assert_not_equal(None, c) c.set(name="testNew") assert_equal(c.name, "testNew") u = User(username='******', fullname='test test', email='*****@*****.**', super_admin=True, disabled=False) up = UserPermissions.channel_contributor c.give_permission_to_user(u, up) role = Role.selectBy(user=u, channel=c).getOne(None) assert_true(role != None) c.give_permission_to_user(u, up) role = Role.selectBy(user=u, channel=c).getOne(None) assert_true(role != None) getup = c.get_channel_permissions_of(u) assert_equal(getup, up) getupnoperm = c.get_channel_permissions_of(User.get(1)) assert_equal(getupnoperm, UserPermissions.no_permission) c.remove_permission_to_user(u) role = Role.selectBy(user=u, channel=c).getOne(None) assert_true(role == None) assert_false(c.has_admin(u)) up = UserPermissions.channel_administrator c.give_permission_to_user(u, up) assert_true(c.has_admin(u)) assert_true(c.has_contrib(u)) assert_is_not_none(c.get_admins()) assert_is_not_none(c.get_contribs()) assert_in(str(u.id), c.get_users_as_json()) c.remove_permission_to_user(u) role = Role.selectBy(user=u, channel=c).getOne(None) assert_true(role == None) c.give_permission_to_user(None, up) role = Role.selectBy(user=u).getOne(None) assert_true(role == None) tru = c.has_visible_params_for(u) assert_true(tru) u3 = User(username='******', fullname='test3 test2', email='*****@*****.**', super_admin=False, disabled=False) tru = c.has_visible_params_for(u3) assert_false(tru) t = PluginChannel.delete(c.id) assert_equal(t, None) # try to delete a channel used by a screen - Seems to work... sc = Screen(name='A', building=Building(name='A')) sc.subscribe_to(u, PluginChannel.get(2)) # t2 = PluginChannel.delete(2) c4 = PluginChannel.get(2) nbSub = c4.subscriptions.count() c4.set(enabled=False) assert_equal(nbSub, c4.subscriptions.count()) c4.set(enabled=True) c4.set(subscription_right="private") assert_equal(nbSub, c4.subscriptions.count()) c4.set(subscription_right="public") # todo seems working by bypassing the webinterface c4.set(cache_validity=-10) assert_true(c4.cache_validity < 0) sc.unsubscribe_from(u, PluginChannel.get(2)) u2 = User(username='******', fullname='test2 test2', email='*****@*****.**', super_admin=False, disabled=False) l = PluginChannel.get_screens_channels_from(u2) assert_is_not_none(l) temp = PluginChannel.get_visible_channels_of(u2) assert_is_not_none(temp) User.delete(u.id) User.delete(u2.id) User.delete(u3.id) except DuplicateEntryError: assert_true(False) return
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]
def _get_users_with_permissions(self, permission_level): """ Return a list of users with sufficient permission on this channel. """ return Role.selectBy(channel=self, permission_level=UserPermissions.get_permission_string( permission_level)).throughTo.user
def get_channels_with_permission_level(self, permission_level): """ Returns the channels which precisely grants this permission level to the user. """ return Role.selectBy( user=self, permission_level=UserPermissions.get_permission_string( permission_level)).throughTo.channel