Skip to content

Commit

Permalink
refactor: assertIsAdmin
Browse files Browse the repository at this point in the history
  • Loading branch information
anarcher committed Nov 8, 2022
1 parent ac27436 commit 7042dc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 12 additions & 5 deletions examples/gno.land/r/system/names/names.gno
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type Space struct {
InPause bool
}

func (s *Space) isAdmin(addr std.Address) bool {
if containsAddress(s.Admins, addr) {
return true
}
return false
}

func (s *Space) addAdmin(newAdmin std.Address) {
if !containsAddress(s.Admins, newAdmin) {
s.Admins = append(s.Admins, newAdmin)
Expand Down Expand Up @@ -87,34 +94,34 @@ func Register(namespace string) {
}

func AddAdmin(namespace string, newAdmin std.Address) {
assertIsAdmin(namespace)
space := getSpace(namespace)
assertIsAdmin(space)
space.addAdmin(newAdmin)
}

func RemoveAdmin(namespace string, admin std.Address) {
assertIsAdmin(namespace)
space := getSpace(namespace)
assertIsAdmin(space)
err := space.removeAdmin(admin)
checkErr(err)
}

func AddEditor(namespace string, newEditor std.Address) {
assertIsAdmin(namespace)
space := getSpace(namespace)
assertIsAdmin(space)
space.addEditor(newEditor)
}

func RemoveEditor(namespace string, editor std.Address) {
assertIsAdmin(namespace)
space := getSpace(namespace)
assertIsAdmin(space)
err := space.removeEditor(editor)
checkErr(err)
}

func SetInPause(namespace string, state bool) {
assertIsAdmin(namespace)
space := getSpace(namespace)
assertIsAdmin(space)
space.InPause = state
}

Expand Down
6 changes: 2 additions & 4 deletions examples/gno.land/r/system/names/utils.gno
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ func existsNamespace(name string) bool {
return ok
}

func assertIsAdmin(namespace string) {
func assertIsAdmin(space *Space) {
caller := std.GetOrigCaller()
space := getSpace(namespace)

if !containsAddress(space.Admins, caller) {
if !space.isAdmin(caller) {
panic("Only admins can call this function")
}
}
Expand Down

0 comments on commit 7042dc7

Please sign in to comment.