2022-04-29 09:10:08 +01:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2022-08-12 12:00:07 +01:00
|
|
|
"encoding/json"
|
2022-04-29 09:10:08 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2022-08-12 12:00:07 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-04-29 09:10:08 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2022-08-12 12:00:07 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2022-04-29 09:10:08 +01:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2022-08-12 12:00:07 +01:00
|
|
|
func AdminEvacuateRoom(req *http.Request, cfg *config.ClientAPI, device *userapi.Device, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
2022-04-29 09:10:08 +01:00
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
roomID, ok := vars["roomID"]
|
|
|
|
if !ok {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting room ID."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res := &roomserverAPI.PerformAdminEvacuateRoomResponse{}
|
2022-08-11 15:29:33 +01:00
|
|
|
if err := rsAPI.PerformAdminEvacuateRoom(
|
2022-04-29 09:10:08 +01:00
|
|
|
req.Context(),
|
|
|
|
&roomserverAPI.PerformAdminEvacuateRoomRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
},
|
|
|
|
res,
|
2022-08-11 15:29:33 +01:00
|
|
|
); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2022-04-29 09:10:08 +01:00
|
|
|
if err := res.Error; err != nil {
|
|
|
|
return err.JSONResponse()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: map[string]interface{}{
|
|
|
|
"affected": res.Affected,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-06-29 15:29:39 +01:00
|
|
|
|
2022-08-12 12:00:07 +01:00
|
|
|
func AdminEvacuateUser(req *http.Request, cfg *config.ClientAPI, device *userapi.Device, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
2022-06-29 15:29:39 +01:00
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
userID, ok := vars["userID"]
|
|
|
|
if !ok {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting user ID."),
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 12:00:07 +01:00
|
|
|
_, domain, err := gomatrixserverlib.SplitID('@', userID)
|
|
|
|
if err != nil {
|
|
|
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
if domain != cfg.Matrix.ServerName {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("User ID must belong to this server."),
|
|
|
|
}
|
|
|
|
}
|
2022-06-29 15:29:39 +01:00
|
|
|
res := &roomserverAPI.PerformAdminEvacuateUserResponse{}
|
2022-08-11 15:29:33 +01:00
|
|
|
if err := rsAPI.PerformAdminEvacuateUser(
|
2022-06-29 15:29:39 +01:00
|
|
|
req.Context(),
|
|
|
|
&roomserverAPI.PerformAdminEvacuateUserRequest{
|
|
|
|
UserID: userID,
|
|
|
|
},
|
|
|
|
res,
|
2022-08-11 15:29:33 +01:00
|
|
|
); err != nil {
|
|
|
|
return jsonerror.InternalAPIError(req.Context(), err)
|
|
|
|
}
|
2022-06-29 15:29:39 +01:00
|
|
|
if err := res.Error; err != nil {
|
|
|
|
return err.JSONResponse()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: map[string]interface{}{
|
|
|
|
"affected": res.Affected,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 12:00:07 +01:00
|
|
|
|
|
|
|
func AdminResetPassword(req *http.Request, cfg *config.ClientAPI, device *userapi.Device, userAPI userapi.ClientUserAPI) util.JSONResponse {
|
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
localpart, ok := vars["localpart"]
|
|
|
|
if !ok {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting user localpart."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request := struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.Unknown("Failed to decode request body: " + err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if request.Password == "" {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting non-empty password."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateReq := &userapi.PerformPasswordUpdateRequest{
|
|
|
|
Localpart: localpart,
|
|
|
|
Password: request.Password,
|
|
|
|
LogoutDevices: true,
|
|
|
|
}
|
|
|
|
updateRes := &userapi.PerformPasswordUpdateResponse{}
|
|
|
|
if err := userAPI.PerformPasswordUpdate(req.Context(), updateReq, updateRes); err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.Unknown("Failed to perform password update: " + err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct {
|
|
|
|
Updated bool `json:"password_updated"`
|
|
|
|
}{
|
|
|
|
Updated: updateRes.PasswordUpdated,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|