Skip to content

Commit

Permalink
Handle seting role for nonexistent nick error
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Apr 23, 2022
1 parent 8f88a85 commit e1817cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
10 changes: 10 additions & 0 deletions big_tests/tests/graphql_muc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ admin_muc_handler() ->
admin_try_set_nonexistent_room_user_affiliation,
admin_set_user_role,
admin_try_set_nonexistent_room_user_role,
admin_try_set_nonexistent_nick_role,
admin_try_set_user_role_in_room_without_moderators,
admin_make_user_enter_room,
admin_make_user_enter_room_with_password,
Expand Down Expand Up @@ -459,6 +460,15 @@ admin_set_user_role(Config, Alice, Bob) ->
assert_success(?SET_ROLE_PATH, Res2),
assert_user_role(RoomJID, Bob, moderator).

admin_try_set_nonexistent_nick_role(Config) ->
muc_helper:story_with_room(Config, [], [{alice, 1}], fun admin_try_set_nonexistent_nick_role/2).

admin_try_set_nonexistent_nick_role(Config, Alice) ->
RoomJID = jid:from_binary(?config(room_jid, Config)),
enter_room(RoomJID, Alice, escalus_client:username(Alice)),
Res = execute_auth(set_user_role_body(RoomJID, <<"kik">>, visitor), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res), <<"does not exist">>)).

admin_try_set_user_role_in_room_without_moderators(Config) ->
muc_helper:story_with_room(Config, [], [{alice, 1}, {bob, 1}],
fun admin_try_set_user_role_in_room_without_moderators/3).
Expand Down
43 changes: 29 additions & 14 deletions src/mod_muc_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
-define(MODERATOR_NOT_FOUND_RESULT, {moderator_not_found, "Moderator user not found"}).
-define(MODERATOR_RES_NOT_FOUND_RESULT,
{moderator_not_found, "Resource with moderator role not found"}).
-define(SET_ROLE_SUCC_RESULT, {ok, "Role set successfully"}).

-spec get_rooms(jid:lserver(), jid:jid(), non_neg_integer() | undefined,
non_neg_integer()) -> get_rooms_result().
Expand Down Expand Up @@ -325,22 +326,22 @@ set_affiliation(RoomJID, FromJID, UserJID, Affiliation) ->
case mod_muc_room:set_admin_items(RoomJID, FromJID, [AffItem]) of
ok ->
{ok, "Affiliation set successfully"};
{error, Error} ->
format_xml_error(Error, Affiliation, <<"affiliation">>);
{error, not_found} ->
?ROOM_NOT_FOUND_RESULT
?ROOM_NOT_FOUND_RESULT;
{error, #xmlel{} = E} ->
format_xml_error(E, Affiliation)
end.

-spec set_role(jid:jid(), binary(), mod_muc:role()) ->
{ok | room_not_found | not_allowed, iolist()}.
{ok | moderator_not_found | room_not_found | not_allowed | cannot_modify, iolist()}.
set_role(RoomJID, Nick, Role) ->
case room_moderator(RoomJID) of
{ok, ModJID} ->
case mod_muc_room:set_admin_items(RoomJID, ModJID, [role_item(Nick, Role)]) of
ok ->
{ok, "Role set successfully"};
{error, Error} ->
format_xml_error(Error, Role, <<"role">>)
?SET_ROLE_SUCC_RESULT;
{error, #xmlel{} = E} ->
format_xml_error(E, Role)
end;
{error, moderator_not_found} ->
?MODERATOR_NOT_FOUND_RESULT;
Expand All @@ -349,16 +350,16 @@ set_role(RoomJID, Nick, Role) ->
end.

-spec set_role(jid:jid(), jid:jid(), binary(), mod_muc:role()) ->
{ok | room_not_found | not_allowed, iolist()}.
{ok | moderator_not_found | room_not_found | not_allowed | cannot_modify, iolist()}.
set_role(RoomJID, UserJID, Nick, Role) ->
RoleItem = role_item(Nick, Role),
case try_add_role_resource(RoomJID, UserJID, moderator) of
{ok, ModJID} ->
case mod_muc_room:set_admin_items(RoomJID, ModJID, [RoleItem]) of
ok ->
{ok, "Role set successfully"};
{error, Error} ->
format_xml_error(Error, Role, <<"role">>)
?SET_ROLE_SUCC_RESULT;
{error, #xmlel{} = E} ->
format_xml_error(E, Role)
end;
{error, not_found} ->
?ROOM_NOT_FOUND_RESULT
Expand Down Expand Up @@ -411,9 +412,23 @@ try_add_role_resource(RoomJID, InUserJID, Role) ->
filter_affs_by_type(undefined, Affs) -> Affs;
filter_affs_by_type(Type, Affs) -> [Aff || Aff = {_, T} <- Affs, T =:= Type].

format_xml_error(#xmlel{name = <<"error">>}, Aff, Op) ->
Msg = io_lib:format("Given user does not have permission to set the ~p ~s", [Aff, Op]),
{not_allowed, Msg}.
format_xml_error(#xmlel{name = <<"error">>} = E, Value) ->
case unwrap_xml_error(E) of
{<<"406">>, <<"modify">>, <<"not-acceptable">>} ->
Msg = xml:get_path_s(E, [{elem, <<"text">>}, cdata]),
{cannot_modify, Msg};
{<<"403">>, <<"auth">>, <<"forbidden">>} ->
{not_allowed, no_permission_msg("affiliation", Value)};
{<<"405">>, <<"cancel">>, <<"not-allowed">>} ->
{not_allowed, no_permission_msg("role", Value)}
end.

no_permission_msg(Op, Value) ->
io_lib:format("Given user does not have permission to set the ~p ~s", [Value, Op]).

unwrap_xml_error(#xmlel{attrs = [{<<"code">>, Code}, {<<"type">>, Type}],
children = [#xmlel{name = Condition} | _]}) ->
{Code, Type, Condition}.

-spec modify_room_config_raw(jid:jid(), room_conf_mod_fun()) -> mod_muc_room:config().
modify_room_config_raw(RoomJID, Fun) ->
Expand Down

0 comments on commit e1817cf

Please sign in to comment.