#NEWLINE# @ log_function #NEWLINE# def is_membership_change_allowed( self , event , auth_events ) : #NEWLINE# #INDENT# membership = event . content [ 'membership' ] #NEWLINE# if ( ( len ( event . prev_events ) == 1 ) and ( Membership . JOIN == membership ) ) : #NEWLINE# #INDENT# key = ( EventTypes . Create , '' ) #NEWLINE# create = auth_events . get ( key ) #NEWLINE# if ( create and ( event . prev_events [ 0 ] [ 0 ] == create . event_id ) ) : #NEWLINE# #INDENT# if ( create . content [ 'creator' ] == event . state_key ) : #NEWLINE# #INDENT# return True #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# target_user_id = event . state_key #NEWLINE# creating_domain = RoomID . from_string ( event . room_id ) . domain #NEWLINE# target_domain = UserID . from_string ( target_user_id ) . domain #NEWLINE# if ( creating_domain != target_domain ) : #NEWLINE# #INDENT# if ( not self . can_federate ( event , auth_events ) ) : #NEWLINE# #INDENT# raise AuthError ( 403 , 'This room has been marked as unfederatable.' ) #NEWLINE# #UNINDENT# #UNINDENT# key = ( EventTypes . Member , event . user_id ) #NEWLINE# caller = auth_events . get ( key ) #NEWLINE# caller_in_room = ( caller and ( caller . membership == Membership . JOIN ) ) #NEWLINE# caller_invited = ( caller and ( caller . membership == Membership . INVITE ) ) #NEWLINE# key = ( EventTypes . Member , target_user_id ) #NEWLINE# target = auth_events . get ( key ) #NEWLINE# target_in_room = ( target and ( target . membership == Membership . JOIN ) ) #NEWLINE# target_banned = ( target and ( target . membership == Membership . BAN ) ) #NEWLINE# key = ( EventTypes . JoinRules , '' ) #NEWLINE# join_rule_event = auth_events . get ( key ) #NEWLINE# if join_rule_event : #NEWLINE# #INDENT# join_rule = join_rule_event . content . get ( 'join_rule' , JoinRules . INVITE ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# join_rule = JoinRules . INVITE #NEWLINE# #UNINDENT# user_level = self . _get_user_power_level ( event . user_id , auth_events ) #NEWLINE# target_level = self . _get_user_power_level ( target_user_id , auth_events ) #NEWLINE# ban_level = self . _get_named_level ( auth_events , 'ban' , 50 ) #NEWLINE# logger . debug ( 'is_membership_change_allowed: %s' , { #NEWLINE# #INDENT# 'caller_in_room' : caller_in_room , #NEWLINE# 'caller_invited' : caller_invited , #NEWLINE# 'target_banned' : target_banned , #NEWLINE# 'target_in_room' : target_in_room , #NEWLINE# 'membership' : membership , #NEWLINE# 'join_rule' : join_rule , #NEWLINE# 'target_user_id' : target_user_id , #NEWLINE# 'event.user_id' : event . user_id , #NEWLINE# #UNINDENT# } ) #NEWLINE# if ( ( Membership . INVITE == membership ) and ( 'third_party_invite' in event . content ) ) : #NEWLINE# #INDENT# if ( not self . _verify_third_party_invite ( event , auth_events ) ) : #NEWLINE# #INDENT# raise AuthError ( 403 , 'You are not invited to this room.' ) #NEWLINE# #UNINDENT# return True #NEWLINE# #UNINDENT# if ( Membership . JOIN != membership ) : #NEWLINE# #INDENT# if ( caller_invited and ( Membership . LEAVE == membership ) and ( target_user_id == event . user_id ) ) : #NEWLINE# #INDENT# return True #NEWLINE# #UNINDENT# if ( not caller_in_room ) : #NEWLINE# #INDENT# raise AuthError ( 403 , ( '%s not in room %s.' % ( event . user_id , event . room_id ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( Membership . INVITE == membership ) : #NEWLINE# #INDENT# if target_banned : #NEWLINE# #INDENT# raise AuthError ( 403 , ( '%s is banned from the room' % ( target_user_id , ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if target_in_room : #NEWLINE# #INDENT# raise AuthError ( 403 , ( '%s is already in the room.' % target_user_id ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# invite_level = self . _get_named_level ( auth_events , 'invite' , 0 ) #NEWLINE# if ( user_level < invite_level ) : #NEWLINE# #INDENT# raise AuthError ( 403 , ( 'You cannot invite user %s.' % target_user_id ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( Membership . JOIN == membership ) : #NEWLINE# #INDENT# if ( event . user_id != target_user_id ) : #NEWLINE# #INDENT# raise AuthError ( 403 , 'Cannot force another user to join.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if target_banned : #NEWLINE# #INDENT# raise AuthError ( 403 , 'You are banned from this room' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( join_rule == JoinRules . PUBLIC ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( join_rule == JoinRules . INVITE ) : #NEWLINE# #INDENT# if ( ( not caller_in_room ) and ( not caller_invited ) ) : #NEWLINE# #INDENT# raise AuthError ( 403 , 'You are not invited to this room.' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise AuthError ( 403 , 'You are not allowed to join this room' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( Membership . LEAVE == membership ) : #NEWLINE# #INDENT# if ( target_banned and ( user_level < ban_level ) ) : #NEWLINE# #INDENT# raise AuthError ( 403 , ( 'You cannot unban user &s.' % ( target_user_id , ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( target_user_id != event . user_id ) : #NEWLINE# #INDENT# kick_level = self . _get_named_level ( auth_events , 'kick' , 50 ) #NEWLINE# if ( ( user_level < kick_level ) or ( user_level <= target_level ) ) : #NEWLINE# #INDENT# raise AuthError ( 403 , ( 'You cannot kick user %s.' % target_user_id ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( Membership . BAN == membership ) : #NEWLINE# #INDENT# if ( ( caller_invited < ban_level ) or ( user_level <= target_level ) ) : #NEWLINE# #INDENT# raise AuthError ( 403 , "You don't have permission to ban" ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise AuthError ( 500 , ( 'Unknown membership %s' % membership ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return True