Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invite policy based on power level #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions corporal/httpgateway/policycheck/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// CheckRoomCreate is a policy checker for: /_matrix/client/r0/createRoom
func CheckRoomCreate(r *http.Request, ctx context.Context, policy policy.Policy, checker policy.Checker) PolicyCheckResponse {
userId := ctx.Value("userId").(string)
members := ctx.Value("invite").([]string)

if !checker.CanUserCreateRoom(policy, userId) {
return PolicyCheckResponse{
Expand All @@ -23,6 +24,17 @@ func CheckRoomCreate(r *http.Request, ctx context.Context, policy policy.Policy,
}
}

// Check if powerlevel of invited members are same or less than the user powerlevel
for _, memberId := range members {
if !checker.CanSendInvite(policy, userId, memberId) {
return PolicyCheckResponse{
Allow: false,
ErrorCode: matrix.ErrorForbidden,
ErrorMessage: "Denied by policy",
}
}
}

return PolicyCheckResponse{
Allow: true,
}
Expand Down
16 changes: 16 additions & 0 deletions corporal/policy/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ func (me *Checker) CanUserUseCustomDisplayName(policy Policy, userId string) boo
func (me *Checker) CanUserUseCustomAvatar(policy Policy, userId string) bool {
return policy.Flags.AllowCustomUserAvatars
}

//Compares the power level of sender and invited members. Allows invite only within their power level and below.

func (me *Checker) CanSendInvite(policy, userId, memberId) bool {
memberPolicy := policy.GetUserPolicyByUserId(memberId)
userPolicy := policy.GetUserPolicyByUserId(userId)
if memberPolicy == nil {
return true
}

if userPolicy == nil {
return false
}

return memberPolicy.PowerLevel <= userPolicy.PowerLevel
}
4 changes: 4 additions & 0 deletions corporal/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ type UserPolicy struct {

// Tells whether this user is forbidden from creating rooms.
ForbidRoomCreation *bool `json:"forbidRoomCreation"`

//PowerLevel.
PowerLevel int `json:"powerLevel"`

}