Return Restricted to determine if the room was restricted or not

This commit is contained in:
Neil Alexander 2022-05-23 13:41:30 +01:00
parent ab5f992567
commit 5e801bb5b1
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
3 changed files with 23 additions and 3 deletions

View file

@ -386,18 +386,36 @@ func checkRestrictedJoin(
if err := rsAPI.QueryRestrictedJoinAllowed(httpReq.Context(), req, res); err != nil { if err := rsAPI.QueryRestrictedJoinAllowed(httpReq.Context(), req, res); err != nil {
return nil, err return nil, err
} }
switch { switch {
case !res.Restricted:
// The join rules for the room don't restrict membership.
return nil, nil
case !res.Resident: case !res.Resident:
// The join rules restrict membership but our server isn't currently
// joined to all of the allowed rooms, so we can't actually decide
// whether or not to allow the user to join. This error code should
// tell the joining server to try joining via another resident server
// instead.
return &util.JSONResponse{ return &util.JSONResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
JSON: jsonerror.UnableToAuthoriseJoin("This server cannot authorise the join."), JSON: jsonerror.UnableToAuthoriseJoin("This server cannot authorise the join."),
}, nil }, nil
case !res.Allowed: case !res.Allowed:
// The join rules restrict membership, our server is in the relevant
// rooms and the user wasn't joined to join any of the allowed rooms
// and therefore can't join this room.
return &util.JSONResponse{ return &util.JSONResponse{
Code: http.StatusForbidden, Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("You are not joined to any matching rooms."), JSON: jsonerror.Forbidden("You are not joined to any matching rooms."),
}, nil }, nil
default: default:
// The join rules restrict membership, our server is in the relevant
// rooms and the user was allowed to join because they belong to one
// of the allowed rooms.
return nil, nil return nil, nil
} }
} }

View file

@ -354,8 +354,9 @@ type QueryRestrictedJoinAllowedRequest struct {
} }
type QueryRestrictedJoinAllowedResponse struct { type QueryRestrictedJoinAllowedResponse struct {
Resident bool `json:"resident"` // Is our homeserver in the relevant rooms? Restricted bool `json:"restricted"` // Is the room membership restricted?
Allowed bool `json:"allowed"` // Is the join allowed by the rules? Resident bool `json:"resident"` // Is our homeserver in the relevant rooms?
Allowed bool `json:"allowed"` // Is the join allowed by the rules?
} }
// MarshalJSON stringifies the room ID and StateKeyTuple keys so they can be sent over the wire in HTTP API mode. // MarshalJSON stringifies the room ID and StateKeyTuple keys so they can be sent over the wire in HTTP API mode.

View file

@ -787,7 +787,8 @@ func (r *Queryer) QueryRestrictedJoinAllowed(ctx context.Context, req *api.Query
return fmt.Errorf("json.Unmarshal: %w", err) return fmt.Errorf("json.Unmarshal: %w", err)
} }
// If the join rule isn't "restricted" then there's nothing more to do. // If the join rule isn't "restricted" then there's nothing more to do.
if joinRules.JoinRule != gomatrixserverlib.Restricted { res.Restricted = joinRules.JoinRule == gomatrixserverlib.Restricted
if !res.Restricted {
return nil return nil
} }
// Start off by populating the "resident" flag in the response. If we // Start off by populating the "resident" flag in the response. If we