Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Playground

example_test.gno

package permissions

import (
	"errors"

	"gno.land/p/gnoland/boards/v0"
)

// Example user account
const user address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"

// Define a role
const RoleExample boards.Role = "example"

// Define a permission
const PermissionFoo boards.Permission = 42

func ExamplePermission() {
	// Define a custom foo permission validation function
	validateFoo := func(_ boards.Permissions, args boards.Args) error {
		// Check that the first argument is the string "bob"
		if name, ok := args[0].(string); !ok || name != "bob" {
			return errors.New("unauthorized")
		}
		return nil
	}

	// Create a permissions instance and assign the custom validator to it
	perms := New()
	perms.ValidateFunc(PermissionFoo, validateFoo)

	// Add foo permission to example role
	perms.AddRole(RoleExample, PermissionFoo)

	// Add a guest user
	perms.SetUserRoles(user, RoleExample)

	// Call a permissioned callback
	args := boards.Args{"bob"}
	perms.WithPermission(user, PermissionFoo, args, func() {
		println("Hello Bob!")
	})

	// Output:
	// Hello Bob!
}

gnomod.toml

module = "gno.land/p/gnoland/boards/exts/permissions/v0"
gno = "0.9"

[addpkg]
  creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"

options.gno

package permissions

import (
	"strings"

	"gno.land/p/gnoland/boards/v0"
)

// Option configures permissions.
type Option func(*Permissions)

// UseSingleUserRole configures permissions to only allow one role per user.
func UseSingleUserRole() Option {
	return func(p *Permissions) {
		p.singleUserRole = true
	}
}

// WithSuperRole configures permissions to have a super role.
// A super role is the one that have all permissions.
// This type of role doesn't need to be mapped to any permission.
func WithSuperRole(r boards.Role) Option {
	return func(p *Permissions) {
		if p.superRole != "" {
			panic("permissions super role can be assigned only once")
		}

		name := string(r)
		if strings.TrimSpace(name) == "" {
			panic("permissions super role name is required")
		}

		if _, err := p.group.AddRole(name); err != nil {
			panic(err)
		}
		p.superRole = r
	}
}

permissions.gno

package permissions

import (
	"strings"

	"gno.land/p/gnoland/boards/v0"
	"gno.land/p/nt/bptree/v0"
	"gno.land/p/nt/groups/v0"
)

// ValidatorFunc defines a function type for permissions validators.
//
// SECURITY: validators run inside WithPermission holding the live
// Permissions value, with full mutation access (SetUserRoles, RemoveUser,
// AddRole, ...) under the owning realm's authority. Register only functions
// the owning realm controls, and never expose ValidateFunc or the
// *Permissions value across a realm boundary.
type ValidatorFunc func(boards.Permissions, boards.Args) error

// Permissions manages users, roles and permissions.
//
// This type is a default `gno.land/p/gnoland/boards/v0` package `Permissions`
// implementation that handles boards users, roles and permissions using an
// underlying groups.Group: the base set holds every user (guests included),
// and each boards role is a group Role whose member set is kept a subset of
// the base set, with the role's boards.PermissionSet stored in the role meta
// (a value type, per the groups meta rule). It also supports optionally
// setting validation functions to be triggered within `WithPermission()`
// method before a permissioned callback is called.
//
// No permissions validation is done by default.
//
// Users are allowed to have multiple roles at the same time by default, but
// permissions can be configured to only allow one role per user.
type Permissions struct {
	superRole      boards.Role
	group          *groups.Group
	public         boards.PermissionSet
	validators     *bptree.BPTree // string(boards.Permission) -> ValidatorFunc
	singleUserRole bool
}

// New creates a new permissions type.
func New(options ...Option) *Permissions {
	ps := &Permissions{
		validators: bptree.NewBPTree32(),
		group:      groups.NewGroup(),
	}

	for _, apply := range options {
		apply(ps)
	}
	return ps
}

// ValidateFunc adds a custom permission validator function.
// If an existing permission function exists it's overwritten by the new one.
func (ps *Permissions) ValidateFunc(p boards.Permission, fn ValidatorFunc) {
	ps.validators.Set(p.String(), fn)
}

// SetPublicPermissions assigns permissions that are available to anyone.
// It removes previous public permissions and assigns the new ones.
// By default there are no public permissions.
func (ps *Permissions) SetPublicPermissions(permissions ...boards.Permission) {
	ps.public = boards.NewPermissionSet(permissions...)
}

// AddRole adds a role with one or more assigned permissions.
// If role exists its permissions are overwritten with the new ones.
func (ps *Permissions) AddRole(r boards.Role, p boards.Permission, extra ...boards.Permission) {
	name := string(r)
	if strings.TrimSpace(name) == "" {
		panic("role name is required")
	}

	// If role is the super role it already has all permissions
	if ps.superRole == r {
		return
	}

	// Get the role if it exists or otherwise register a new one
	role, found := ps.group.GetRole(name)
	if !found {
		var err error
		role, err = ps.group.AddRole(name)
		if err != nil {
			panic(err)
		}
	}

	// Save permissions within the role meta overwriting any existing permissions
	permissions := append([]boards.Permission{p}, extra...)
	role.SetMeta(boards.NewPermissionSet(permissions...))
}

// RoleExists checks if a role exists.
func (ps Permissions) RoleExists(r boards.Role) bool {
	return r == ps.superRole || ps.group.HasRole(string(r))
}

// GetUserRoles returns the list of roles assigned to a user.
func (ps Permissions) GetUserRoles(user address) []boards.Role {
	names := ps.group.RolesContaining(user)
	if names == nil {
		return nil
	}

	roles := make([]boards.Role, len(names))
	for i, name := range names {
		roles[i] = boards.Role(name)
	}
	return roles
}

// HasRole checks if a user has a specific role assigned.
func (ps Permissions) HasRole(user address, r boards.Role) bool {
	role, found := ps.group.GetRole(string(r))
	if !found {
		return false
	}
	return role.Members().Has(user)
}

// HasPermission checks if a user has a specific permission.
func (ps Permissions) HasPermission(user address, perm boards.Permission) bool {
	if ps.public.Has(perm) {
		return true
	}

	for _, name := range ps.group.RolesContaining(user) {
		if ps.superRole == boards.Role(name) {
			return true
		}

		role, found := ps.group.GetRole(name)
		if !found {
			continue
		}

		if perms, ok := role.Meta().(boards.PermissionSet); ok && perms.Has(perm) {
			return true
		}
	}
	return false
}

// SetUserRoles adds a new user when it doesn't exist and sets its roles.
// Method can also be called to change the roles of an existing user.
// It removes any existing user roles before assigning new ones.
// All user's roles can be removed by calling this method without roles.
func (ps *Permissions) SetUserRoles(user address, roles ...boards.Role) {
	if len(roles) > 1 && ps.singleUserRole {
		panic("user can only have one role")
	}

	// Resolve every role name upfront so an invalid name panics before any
	// state is mutated.
	newRoles := make([]*groups.Role, len(roles))
	for i, r := range roles {
		role, found := ps.group.GetRole(string(r))
		if !found {
			panic("invalid role: " + string(r))
		}
		newRoles[i] = role
	}

	// Clear current user roles
	for _, name := range ps.group.RolesContaining(user) {
		if role, found := ps.group.GetRole(name); found {
			role.Members().Remove(user)
		}
	}

	// Every user is a base set member, with or without roles; role member
	// sets are kept subsets of the base set.
	ps.group.Add(user)

	// Add user to role member sets
	for _, role := range newRoles {
		role.Members().Add(user)
	}
}

// RemoveUser removes a user from permissions.
func (ps *Permissions) RemoveUser(user address) bool {
	return ps.group.RemoveFromAll(user)
}

// HasUser checks if a user exists.
func (ps Permissions) HasUser(user address) bool {
	return ps.group.Has(user)
}

// UsersCount returns the total number of users the permissioner contains.
func (ps Permissions) UsersCount() int {
	return ps.group.Size()
}

// IterateUsers iterates permissions' users.
func (ps Permissions) IterateUsers(start, count int, fn boards.UsersIterFn) (stopped bool) {
	return ps.group.Iterate(start, count, func(addr address) bool {
		return fn(boards.User{
			Address: addr,
			Roles:   ps.GetUserRoles(addr),
		})
	})
}

// WithPermission calls a callback when a user has a specific permission.
// It panics on error or when a permission validator fails.
// Callbacks are by default called when there is no validator function registered for the permission.
// If a permission validation function exists it's called before calling the callback.
func (ps *Permissions) WithPermission(user address, p boards.Permission, args boards.Args, cb func()) {
	if !ps.HasPermission(user, p) {
		panic("unauthorized, user " + user.String() + " doesn't have the required permission")
	}

	// Execute custom validation before calling the callback
	if v := ps.validators.Get(p.String()); v != nil {
		err := v.(ValidatorFunc)(ps, args)
		if err != nil {
			panic(err)
		}
	}

	cb()
}

permissions_test.gno

package permissions

import (
	"testing"

	"gno.land/p/gnoland/boards/v0"
	"gno.land/p/nt/uassert/v0"
	"gno.land/p/nt/urequire/v0"
)

// Test permission constants
const (
	testPermA boards.Permission = iota
	testPermB
	testPermC
)

var _ boards.Permissions = (*Permissions)(nil)

func TestBasicPermissionsWithPermission(cur realm, t *testing.T) {
	cases := []struct {
		name       string
		user       address
		permission boards.Permission
		args       boards.Args
		setup      func() *Permissions
		err        string
		called     bool
	}{
		{
			name:       "ok",
			user:       "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			permission: testPermA,
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("foo", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "foo")
				return perms
			},
			called: true,
		},
		{
			name:       "ok with arguments",
			user:       "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			permission: testPermA,
			args:       boards.Args{"a", "b"},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("foo", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "foo")
				return perms
			},
			called: true,
		},
		{
			name:       "no permission",
			user:       "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			permission: testPermA,
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("foo", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
				return perms
			},
			err: "unauthorized, user g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 doesn't have the required permission",
		},
		{
			name:       "is not a member",
			user:       "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			permission: testPermA,
			setup: func() *Permissions {
				return New()
			},
			err: "unauthorized, user g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 doesn't have the required permission",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			var called bool

			perms := tc.setup()
			testCaseFn := func() {
				perms.WithPermission(tc.user, tc.permission, tc.args, func() {
					called = true
				})
			}

			if tc.err != "" {
				urequire.PanicsWithMessage(t, cur, tc.err, testCaseFn, "expect panic with message")
				return
			} else {
				urequire.NotPanics(t, cur, testCaseFn, "expect no panic")
			}

			urequire.Equal(t, tc.called, called, "expect callback to be called")
		})
	}
}

func TestBasicPermissionsSetPublicPermissions(t *testing.T) {
	user := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
	perms := New()

	// Add a new role with permissions
	perms.AddRole("adminRole", testPermA, testPermB, testPermC)
	urequire.False(t, perms.HasPermission(user, testPermA))
	urequire.False(t, perms.HasPermission(user, testPermB))
	urequire.False(t, perms.HasPermission(user, testPermC))

	// Assign a couple of public permissions
	perms.SetPublicPermissions(testPermA, testPermC)
	urequire.True(t, perms.HasPermission(user, testPermA))
	urequire.False(t, perms.HasPermission(user, testPermB))
	urequire.True(t, perms.HasPermission(user, testPermC))

	// Clear all public permissions
	perms.SetPublicPermissions()
	urequire.False(t, perms.HasPermission(user, testPermA))
	urequire.False(t, perms.HasPermission(user, testPermB))
	urequire.False(t, perms.HasPermission(user, testPermC))
}

func TestBasicPermissionsGetUserRoles(t *testing.T) {
	cases := []struct {
		name  string
		user  address
		roles []string
		setup func() *Permissions
	}{
		{
			name:  "single role",
			user:  "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			roles: []string{"admin"},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("admin", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "admin")
				return perms
			},
		},
		{
			name:  "multiple roles",
			user:  "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			roles: []string{"admin", "bar", "foo"},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("admin", testPermA)
				perms.AddRole("foo", testPermA)
				perms.AddRole("bar", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "admin", "foo", "bar")
				return perms
			},
		},
		{
			name: "without roles",
			user: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			setup: func() *Permissions {
				perms := New()
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
				return perms
			},
		},
		{
			name: "not a user",
			user: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			setup: func() *Permissions {
				return New()
			},
		},
		{
			name:  "multiple users",
			user:  "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			roles: []string{"admin"},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("admin", testPermA)
				perms.AddRole("bar", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "admin")
				perms.SetUserRoles("g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn", "admin")
				perms.SetUserRoles("g1w4ek2u3jta047h6lta047h6lta047h6l9huexc", "admin", "bar")
				return perms
			},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			perms := tc.setup()
			roles := perms.GetUserRoles(tc.user)

			urequire.Equal(t, len(tc.roles), len(roles), "user role count")
			for i, r := range roles {
				uassert.Equal(t, tc.roles[i], string(r))
			}
		})
	}
}

func TestBasicPermissionsHasRole(t *testing.T) {
	cases := []struct {
		name  string
		user  address
		role  boards.Role
		setup func() *Permissions
		want  bool
	}{
		{
			name: "ok",
			user: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			role: "admin",
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("admin", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "admin")
				return perms
			},
			want: true,
		},
		{
			name: "ok with multiple roles",
			user: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			role: "foo",
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("admin", testPermA)
				perms.AddRole("foo", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "admin", "foo")
				return perms
			},
			want: true,
		},
		{
			name: "user without roles",
			user: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			setup: func() *Permissions {
				perms := New()
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
				return perms
			},
		},
		{
			name: "has no role",
			user: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			role: "bar",
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("foo", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "foo")
				return perms
			},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			perms := tc.setup()
			got := perms.HasRole(tc.user, tc.role)
			uassert.Equal(t, tc.want, got)
		})
	}
}

func TestBasicPermissionsHasPermission(t *testing.T) {
	cases := []struct {
		name       string
		user       address
		permission boards.Permission
		setup      func() *Permissions
		want       bool
	}{
		{
			name:       "ok",
			user:       "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			permission: testPermA,
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("foo", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "foo")
				return perms
			},
			want: true,
		},
		{
			name:       "ok with multiple users",
			user:       "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			permission: testPermA,
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("foo", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "foo")
				perms.SetUserRoles("g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn", "foo")
				return perms
			},
			want: true,
		},
		{
			name:       "ok with multiple roles",
			user:       "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			permission: testPermB,
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("foo", testPermA)
				perms.AddRole("baz", testPermB)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "foo", "baz")
				return perms
			},
			want: true,
		},
		{
			name:       "no permission",
			user:       "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			permission: testPermB,
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("foo", testPermA)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "foo")
				return perms
			},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			perms := tc.setup()
			got := perms.HasPermission(tc.user, tc.permission)
			uassert.Equal(t, tc.want, got)
		})
	}
}

func TestBasicPermissionsSetUserRoles(cur realm, t *testing.T) {
	cases := []struct {
		name          string
		user          address
		expectedRoles []boards.Role
		setup         func() *Permissions
		err           string
	}{
		{
			name:          "add user",
			user:          address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			expectedRoles: []boards.Role{"a"},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("a", testPermA)
				return perms
			},
		},
		{
			name:          "add user with multiple roles",
			user:          address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			expectedRoles: []boards.Role{"a", "b"},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("a", testPermA)
				perms.AddRole("b", testPermB)
				return perms
			},
		},
		{
			name:          "add when other users exists",
			user:          address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			expectedRoles: []boards.Role{"a"},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("a", testPermA)
				perms.SetUserRoles("g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn", "a")
				perms.SetUserRoles("g1w4ek2u3jta047h6lta047h6lta047h6l9huexc")
				return perms
			},
		},
		{
			name:          "add user using single role",
			user:          address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			expectedRoles: []boards.Role{"a"},
			setup: func() *Permissions {
				perms := New(UseSingleUserRole())
				perms.AddRole("a", testPermA)
				return perms
			},
		},
		{
			name:          "update user roles",
			user:          address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			expectedRoles: []boards.Role{"a", "b"},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("a", testPermA)
				perms.AddRole("b", testPermB)
				perms.AddRole("c", testPermB)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "c")
				return perms
			},
		},
		{
			name:          "update user roles using single role",
			user:          address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			expectedRoles: []boards.Role{"b"},
			setup: func() *Permissions {
				perms := New(UseSingleUserRole())
				perms.AddRole("a", testPermA)
				perms.AddRole("b", testPermB)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "a")
				return perms
			},
		},
		{
			name:          "clear user roles",
			user:          address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			expectedRoles: []boards.Role{},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("a", testPermA)
				perms.AddRole("b", testPermB)
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "a", "b")
				return perms
			},
		},
		{
			name:          "set invalid role",
			user:          address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			expectedRoles: []boards.Role{"a", "foo"},
			setup: func() *Permissions {
				perms := New()
				perms.AddRole("a", testPermA)
				return perms
			},
			err: "invalid role: foo",
		},
		{
			name:          "use single role error",
			user:          address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			expectedRoles: []boards.Role{"a", "b"},
			setup: func() *Permissions {
				perms := New(UseSingleUserRole())
				perms.AddRole("a", testPermA)
				perms.AddRole("b", testPermB)
				return perms
			},
			err: "user can only have one role",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			perms := tc.setup()

			setUserRoles := func() {
				perms.SetUserRoles(tc.user, tc.expectedRoles...)
			}

			if tc.err != "" {
				urequire.PanicsWithMessage(t, cur, tc.err, setUserRoles, "expected an error")
				return
			} else {
				urequire.NotPanics(t, cur, setUserRoles, "expected no error")
			}

			roles := perms.GetUserRoles(tc.user)
			uassert.Equal(t, len(tc.expectedRoles), len(roles))
			for i, r := range roles {
				urequire.Equal(t, string(tc.expectedRoles[i]), string(r))
			}
		})
	}
}

func TestBasicPermissionsAddRoleOverwrite(t *testing.T) {
	user := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")

	perms := New()
	perms.AddRole("foo", testPermA)
	perms.SetUserRoles(user, "foo")

	// Re-adding an existing role replaces its permissions and keeps members
	perms.AddRole("foo", testPermB)
	uassert.True(t, perms.HasRole(user, "foo"), "expect role members to be kept")
	uassert.False(t, perms.HasPermission(user, testPermA), "expect old permissions to be revoked")
	uassert.True(t, perms.HasPermission(user, testPermB), "expect new permissions to be granted")
}

func TestBasicPermissionsAddRoleEmptyName(cur realm, t *testing.T) {
	urequire.PanicsWithMessage(t, cur, "role name is required", func() {
		New().AddRole("  ", testPermA)
	}, "expect whitespace only role names to be rejected")

	urequire.PanicsWithMessage(t, cur, "permissions super role name is required", func() {
		New(WithSuperRole("  "))
	}, "expect whitespace only super role names to be rejected")

	// The empty string is rejected before the superRole sentinel check, so
	// the outcome doesn't depend on whether a super role is configured.
	urequire.PanicsWithMessage(t, cur, "role name is required", func() {
		New().AddRole("", testPermA)
	}, "expect empty role name to be rejected without a super role")
	urequire.PanicsWithMessage(t, cur, "role name is required", func() {
		New(WithSuperRole("owner")).AddRole("", testPermA)
	}, "expect empty role name to be rejected with a super role")
}

func TestBasicPermissionsSuperRole(t *testing.T) {
	user := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
	super := boards.Role("owner")

	perms := New(WithSuperRole(super))
	urequire.True(t, perms.RoleExists(super), "expect super role to exist")
	urequire.False(t, perms.RoleExists("unknown"))

	// Super role members get every permission without explicit mapping
	perms.SetUserRoles(user, super)
	urequire.True(t, perms.HasRole(user, super))
	uassert.True(t, perms.HasUser(user))
	uassert.True(t, perms.HasPermission(user, testPermA))
	uassert.True(t, perms.HasPermission(user, testPermC))

	// AddRole on the super role is a no-op: the super role branch grants
	// every permission regardless of any meta a mapping would set
	perms.AddRole(super, testPermB)
	uassert.True(t, perms.HasPermission(user, testPermA), "expect super role to keep all permissions")
}

func TestBasicPermissionsHasUserUsersCount(t *testing.T) {
	member := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
	guest := address("g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn")

	perms := New()
	perms.AddRole("foo", testPermA)
	urequire.Equal(t, 0, perms.UsersCount())
	urequire.False(t, perms.HasUser(member))

	// A user added only through a role counts as a user
	perms.SetUserRoles(member, "foo")
	urequire.True(t, perms.HasUser(member), "expect role holder to be a user")
	urequire.Equal(t, 1, perms.UsersCount())

	// A guest without roles counts as a user
	perms.SetUserRoles(guest)
	urequire.True(t, perms.HasUser(guest))
	urequire.Equal(t, 2, perms.UsersCount())

	// Clearing roles keeps the user and doesn't change the count
	perms.SetUserRoles(member)
	urequire.True(t, perms.HasUser(member), "expect user to remain after roles are cleared")
	urequire.Equal(t, 2, perms.UsersCount())

	// Re-assigning roles must not double count
	perms.SetUserRoles(member, "foo")
	urequire.Equal(t, 2, perms.UsersCount())
}

func TestBasicPermissionsSetUserRolesPanicKeepsState(cur realm, t *testing.T) {
	user := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")

	perms := New()
	perms.AddRole("a", testPermA)
	perms.SetUserRoles(user, "a")

	urequire.PanicsWithMessage(t, cur, "invalid role: nope", func() {
		perms.SetUserRoles(user, "nope")
	}, "expect invalid role to panic")

	// The failed call must not have mutated state
	uassert.True(t, perms.HasUser(user))
	uassert.True(t, perms.HasRole(user, "a"))
	uassert.Equal(t, 1, perms.UsersCount())
}

func TestBasicPermissionsRemoveUser(t *testing.T) {
	cases := []struct {
		name  string
		user  address
		setup func() *Permissions
		want  bool
	}{
		{
			name: "ok",
			user: address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			setup: func() *Permissions {
				perms := New()
				perms.SetUserRoles("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
				return perms
			},
			want: true,
		},
		{
			name: "user not found",
			user: address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
			setup: func() *Permissions {
				return New()
			},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			perms := tc.setup()
			got := perms.RemoveUser(tc.user)
			uassert.Equal(t, tc.want, got)
		})
	}
}

func TestBasicPermissionsRemoveUserWithRoles(t *testing.T) {
	user := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")

	perms := New()
	perms.AddRole("foo", testPermA)
	perms.AddRole("bar", testPermB)
	perms.SetUserRoles(user, "foo", "bar")

	urequire.True(t, perms.RemoveUser(user), "expect role holder to be removed")

	uassert.False(t, perms.HasUser(user), "expect removed user to not exist")
	uassert.False(t, perms.HasRole(user, "foo"), "expect removed user to keep no roles")
	uassert.False(t, perms.HasPermission(user, testPermA), "expect removed user to keep no permissions")
	uassert.Equal(t, 0, len(perms.GetUserRoles(user)))
	uassert.Equal(t, 0, perms.UsersCount())
	uassert.False(t, perms.RemoveUser(user), "expect second removal to report user not found")
}

func TestBasicPermissionsIterateUsers(t *testing.T) {
	// Users are listed sorted by address, including a roleless guest.
	users := []boards.User{
		{
			Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
			Roles:   []boards.Role{"foo"},
		},
		{
			Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
			Roles:   []boards.Role{"bar", "foo"},
		},
		{
			Address: "g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5",
			Roles:   []boards.Role{"bar"},
		},
		{
			Address: "g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn",
		},
	}

	perms := New()
	perms.AddRole("foo", testPermA)
	perms.AddRole("bar", testPermB)
	// Add users in reverse order to pin that iteration is address-sorted,
	// not insertion-ordered.
	for i := len(users) - 1; i >= 0; i-- {
		perms.SetUserRoles(users[i].Address, users[i].Roles...)
	}

	cases := []struct {
		name               string
		start, count, want int
	}{
		{
			name:  "exceed users count",
			count: 50,
			want:  4,
		},
		{
			name:  "exact users count",
			count: 4,
			want:  4,
		},
		{
			name:  "two users",
			start: 1,
			count: 2,
			want:  2,
		},
		{
			name:  "one user",
			start: 1,
			count: 1,
			want:  1,
		},
		{
			name:  "no iteration",
			start: 50,
			count: 1,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			var visited int
			stopped := perms.IterateUsers(tc.start, tc.count, func(u boards.User) bool {
				i := tc.start + visited
				urequire.True(t, i < len(users), "expect iterator to respect number of users")
				uassert.Equal(t, users[i].Address, u.Address)

				urequire.Equal(t, len(users[i].Roles), len(u.Roles), "expect number of roles to match")
				for j := range u.Roles {
					uassert.Equal(t, string(users[i].Roles[j]), string(u.Roles[j]))
				}

				visited++
				return false
			})

			uassert.False(t, stopped, "expect full iteration not to report an early stop")
			uassert.Equal(t, tc.want, visited, "expect iterator to visit the windowed users")
		})
	}

	// Early stop is reported, and iteration actually halts.
	var visited int
	stopped := perms.IterateUsers(0, 10, func(boards.User) bool {
		visited++
		return true
	})
	uassert.True(t, stopped, "expect early stop to be reported")
	uassert.Equal(t, 1, visited, "expect iteration to halt on early stop")
}