Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Aug 29, 2024
1 parent daefb09 commit 5603761
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 100 deletions.
5 changes: 4 additions & 1 deletion examples/gno.land/p/demo/access/gno.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module gno.land/p/demo/access

require gno.land/p/demo/testutils v0.0.0-latest
require (
gno.land/p/demo/ownable v0.0.0-latest
gno.land/p/demo/testutils v0.0.0-latest
)
45 changes: 30 additions & 15 deletions examples/gno.land/p/demo/access/set.gno
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package access

import (
"github.com/gnolang/gno/examples/gno.land/p/demo/ownable"
"github.com/gnolang/gno/gnovm/stdlibs/std"
"std"

"gno.land/p/demo/ownable"
)

const (
AuthorizedAddressAddEvent = "AuthorizedAddressAdd"
AuthorizedAddressDeleteEvent = "AuthorizedAddressDelete"
)

// Set is an object containing the configuration and allowing the application of filters
Expand Down Expand Up @@ -57,32 +63,36 @@ func (s Set) AuthorizedList() []std.Address {
return s.authorized
}

// Add allows any address in the set to add address to the list of authorized addresses
func (s *Set) Add(addr std.Address) error {
if err := s.CallerIsAuthorized(); err != nil {
return ErrUnauthorized
// AddAsOwner adds an address to the list of authorized addresses, only if the caller is the owner of the set
// Expose this function if you want only the set owner to be able to add new authorized addresses
func (s *Set) AddAsOwner(addr std.Address) error {
if err := s.setOwner.CallerIsOwner(); err != nil {
return err
}

if s.IsAuthorized(addr) {
return ErrAlreadyInSet
if err := s.Add(addr); err != nil {
return err
}

s.authorized = append(s.authorized, addr)
return nil
}

// AddAsOwner adds an address to the list of authorized addresses, only if the caller is the owner of the set
// Expose this function if you want only the set owner to be able to add new authorized addresses
func (s *Set) AddAsOwner(addr std.Address) error {
if err := s.setOwner.CallerIsOwner(); err != nil {
return err
// Add allows any address in the set to add address to the list of authorized addresses
func (s *Set) Add(addr std.Address) error {
if err := s.CallerIsAuthorized(); err != nil {
return ErrUnauthorized
}

if s.IsAuthorized(addr) {
return ErrAlreadyInSet
}

s.authorized = append(s.authorized, addr)
std.Emit(
AuthorizedAddressAddEvent,
"address", addr.String(),
)

return nil
}

Expand Down Expand Up @@ -124,12 +134,17 @@ func (s *Set) ForceDel(addr std.Address) error {
}
}

std.Emit(
AuthorizedAddressDeleteEvent,
"address", addr.String(),
)

return ErrAddrNotPresent
}

// Assert functions

// AssertCurrentHasAccess checks whether the PrevRealm is whitelisted as authorized
// AssertCurrentHasAccess checks whgnether the PrevRealm is whitelisted as authorized
// If not, it panics indicating restricted access
func (s *Set) AssertCurrentHasAccess() {
if err := s.CallerIsAuthorized(); err != nil {
Expand Down
44 changes: 0 additions & 44 deletions examples/gno.land/p/demo/access/z0_filetest.gno

This file was deleted.

40 changes: 0 additions & 40 deletions examples/gno.land/p/demo/access/z1_filetest.gno

This file was deleted.

0 comments on commit 5603761

Please sign in to comment.