def on_create(self, activity: Activity) -> (bool, int, str): if not hasattr(activity, 'object') or not hasattr( activity.object, 'url'): return False, ECodes.MISSING_OBJECT_URL, 'no channel id set' if not hasattr(activity.target, 'display_name'): return False, ECodes.MISSING_TARGET_DISPLAY_NAME, 'no room name set' room_name = activity.target.display_name channel_id = activity.object.url if not hasattr(activity, 'actor') or not hasattr(activity.actor, 'id'): return False, ECodes.MISSING_ACTOR_ID, 'need actor.id (user uuid)' try: activity.object.display_name = utils.get_channel_name(channel_id) except NoSuchChannelException: return False, ECodes.NO_SUCH_CHANNEL, 'channel does not exist' if room_name is None or room_name.strip() == '': return False, ECodes.MISSING_TARGET_DISPLAY_NAME, 'got blank room name, can not create' if not utils.is_base64(room_name): return False, ECodes.NOT_BASE64, 'invalid room name, not base64 encoded' room_name = utils.b64d(room_name) if not environ.env.db.channel_exists(channel_id): return False, ECodes.NO_SUCH_CHANNEL, 'channel does not exist' if utils.room_name_restricted(room_name): return False, ECodes.ROOM_NAME_RESTRICTED, 'restricted room name' if environ.env.db.room_name_exists(channel_id, room_name): return False, ECodes.ROOM_ALREADY_EXISTS, 'a room with that name already exists' if not hasattr(activity.target, 'object_type') or \ activity.target.object_type is None or \ len(str(activity.target.object_type).strip()) == 0: # for acl validation to know we're trying to create a room activity.target.object_type = 'room' channel_acls = utils.get_acls_in_channel_for_action( channel_id, ApiActions.CREATE) is_valid, msg = validation.acl.validate_acl_for_action( activity, ApiTargets.CHANNEL, ApiActions.CREATE, channel_acls) if not is_valid: return False, ECodes.NOT_ALLOWED, msg return True, None, None
def on_kick(self, activity: Activity) -> (bool, int, str): room_id = activity.target.id user_id_to_kick = activity.object.id if room_id is None or room_id.strip() == '': return False, ECodes.MISSING_TARGET_ID, 'got blank room id, can not kick' try: utils.get_room_name(room_id) except NoSuchRoomException: return False, ECodes.NO_SUCH_ROOM, 'no room with id "%s" exists' % room_id if user_id_to_kick is None or user_id_to_kick.strip() == '': return False, ECodes.MISSING_TARGET_DISPLAY_NAME, 'got blank user id, can not kick' if utils.is_super_user(user_id_to_kick) or utils.is_global_moderator( user_id_to_kick): return False, ECodes.NOT_ALLOWED, "not allowed to kick operators" channel_id = utils.get_channel_for_room(room_id) channel_acls = utils.get_acls_in_channel_for_action( channel_id, ApiActions.KICK) is_valid, msg = validation.acl.validate_acl_for_action( activity, ApiTargets.CHANNEL, ApiActions.KICK, channel_acls) if not is_valid: return False, ECodes.NOT_ALLOWED, msg try: room_acls = utils.get_acls_in_room_for_action( room_id, ApiActions.KICK) except NoSuchRoomException: return False, ECodes.NO_SUCH_ROOM, 'no such room' is_valid, msg = validation.acl.validate_acl_for_action( activity, ApiTargets.ROOM, ApiActions.KICK, room_acls) if not is_valid: return False, ECodes.NOT_ALLOWED, msg return True, None, None
def on_whisper(self, activity: Activity) -> (bool, int, str): if not hasattr(activity, 'target') or not hasattr( activity.target, 'id'): return False, ECodes.MISSING_TARGET_ID, 'no target.id (user uuid to whisper to)' if not hasattr(activity, 'actor') or not hasattr(activity.actor, 'id'): return False, ECodes.MISSING_ACTOR_ID, 'no actor.id (id of user who is whispering)' if not hasattr(activity, 'actor') or not hasattr( activity.actor, 'url'): return False, ECodes.MISSING_ACTOR_URL, 'no actor.url (room uuid to whisper in)' if not hasattr(activity, 'object') or not hasattr( activity.object, 'content'): return False, ECodes.MISSING_OBJECT_CONTENT, 'no object.content (message to whisper)' if not utils.is_base64(activity.object.content): return False, ECodes.NOT_BASE64, 'object.content needs to be base64 encoded' try: activity.object.url = utils.get_channel_for_room( activity.actor.url) except (NoSuchChannelException, NoChannelFoundException): return False, ECodes.NO_SUCH_ROOM, 'no room found for actor.url (room uuid to whisper in)' try: activity.object.display_name = utils.get_channel_name( activity.object.url) except (NoSuchChannelException, NoChannelFoundException): return False, ECodes.NO_SUCH_CHANNEL, 'no channel found for actor.url (room uuid to whisper in)' channel_acls = utils.get_acls_in_channel_for_action( activity.object.url, ApiActions.WHISPER) is_valid, msg = validation.acl.validate_acl_for_action( activity, ApiTargets.CHANNEL, ApiActions.WHISPER, channel_acls) if not is_valid: return False, ECodes.NOT_ALLOWED, msg return True, None, None
def on_list_rooms(self, activity: Activity) -> (bool, int, str): if not hasattr(activity, 'object') or not hasattr( activity.object, 'url'): return False, ECodes.MISSING_OBJECT_URL, 'need channel ID to list rooms' channel_id = activity.object.url if channel_id is None or channel_id == '': return False, ECodes.MISSING_OBJECT_URL, 'need channel ID to list rooms' user_id = activity.actor.id is_banned, duration = utils.is_banned_globally(user_id) if is_banned: environ.env.join_room(user_id) reason = utils.reason_for_ban(user_id) json_act = utils.activity_for_already_banned(duration, reason) environ.env.emit('gn_banned', json_act, json=True, room=user_id, broadcast=False, include_self=True, namespace='/ws') environ.env.disconnect() logger.info('user %s is banned from chatting for: %ss' % (user_id, duration)) return False, ECodes.USER_IS_BANNED, json_act activity.target = Target({'objectType': 'channel'}) acls = utils.get_acls_in_channel_for_action(channel_id, ApiActions.LIST) is_valid, msg = validation.acl.validate_acl_for_action( activity, ApiTargets.CHANNEL, ApiActions.LIST, acls) if not is_valid: return False, ECodes.NOT_ALLOWED, msg return True, None, None