def on_login(self, activity: Activity) -> (bool, int, str): 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') logger.info('user %s is banned from chatting for: %ss' % (user_id, duration)) return False, ECodes.USER_IS_BANNED, 'user %s is banned from chatting for: %ss' % ( user_id, duration) if hasattr(activity.actor, 'attachments') and activity.actor.attachments is not None: for attachment in activity.actor.attachments: environ.env.session[ attachment.object_type] = attachment.content if SessionKeys.token.value not in environ.env.session: logger.warning( 'no token in session when logging in for user id %s' % str(user_id)) return False, ECodes.NO_USER_IN_SESSION, 'no token in session' token = environ.env.session.get(SessionKeys.token.value) is_valid, error_msg, session = self.validate_login(user_id, token) if not is_valid: logger.warning('login is not valid for user id %s: %s' % (str(user_id), str(error_msg))) environ.env.stats.incr('on_login.failed') return False, ECodes.NOT_ALLOWED, error_msg for session_key, session_value in session.items(): environ.env.session[session_key] = session_value 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
def on_join(self, activity: Activity) -> (bool, int, str): room_id = activity.target.id room_name = activity.target.display_name user_id = environ.env.session.get(SessionKeys.user_id.value, None) if user_id is None or len(user_id.strip()) == 0: user_id = activity.actor.id if room_id is not None and len(room_id.strip()) > 0: try: room_name = utils.get_room_name(room_id) except NoSuchRoomException: return False, ECodes.NO_SUCH_ROOM, 'room does not exist' else: if room_name is None or len(room_name.strip()) == 0: return False, ECodes.MISSING_TARGET_DISPLAY_NAME, 'neither room id nor name supplied' try: room_id = utils.get_room_id(room_name) except NoSuchRoomException: return False, ECodes.NO_SUCH_ROOM, 'room does not exists with given name' except MultipleRoomsFoundForNameException: return False, ECodes.MULTIPLE_ROOMS_WITH_NAME, 'found multiple rooms with name "%s"' % room_name if not hasattr(activity, 'object'): activity.object = DefObject(dict()) if not utils.user_is_online(user_id): user_name = '<unknown>' try: user_name = utils.get_user_name_for(user_id) except NoSuchUserException: logger.error('could not get username for user id %s' % user_id) logger.warning( 'user "%s" (%s) is not online, not joining room "%s" (%s)!' % (user_name, user_id, room_name, room_id)) return False, ECodes.NOT_ONLINE, 'user is not online' if utils.is_super_user(user_id) or utils.is_global_moderator(user_id): return True, None, None if utils.is_owner(room_id, user_id): return True, None, None channel_id = utils.get_channel_for_room(room_id) if utils.is_owner_channel(channel_id, user_id): return True, None, None activity.object.url = channel_id activity.object.display_name = utils.get_channel_name(channel_id) activity.target.object_type = 'room' try: acls = utils.get_acls_in_room_for_action(room_id, ApiActions.JOIN) except NoSuchRoomException: return False, ECodes.NO_SUCH_ROOM, 'no such room' is_valid, error_msg = validation.acl.validate_acl_for_action( activity, ApiTargets.ROOM, ApiActions.JOIN, acls) if not is_valid: return False, ECodes.NOT_ALLOWED, error_msg is_banned, info_dict = utils.is_banned(user_id, room_id) if is_banned: scope = info_dict['scope'] seconds_left = info_dict['seconds'] target_id = info_dict['id'] target_name = '' if scope == 'room': target_name = utils.get_room_name(target_id) elif scope == 'channel': target_name = utils.get_channel_name(target_id) reason = utils.reason_for_ban(user_id, scope, target_id) json_act = utils.activity_for_already_banned( seconds_left, reason, scope, target_id, target_name) return False, ECodes.USER_IS_BANNED, json_act return True, None, None