github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/gnoland/pages/admin.gno (about)

     1  package gnopages
     2  
     3  import (
     4  	"std"
     5  	"strings"
     6  
     7  	"gno.land/p/demo/avl"
     8  )
     9  
    10  var (
    11  	adminAddr     std.Address
    12  	moderatorList avl.Tree
    13  	inPause       bool
    14  )
    15  
    16  func init() {
    17  	// adminAddr = std.GetOrigCaller() // FIXME: find a way to use this from the main's genesis.
    18  	adminAddr = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
    19  }
    20  
    21  func AdminSetAdminAddr(addr std.Address) {
    22  	assertIsAdmin()
    23  	adminAddr = addr
    24  }
    25  
    26  func AdminSetInPause(state bool) {
    27  	assertIsAdmin()
    28  	inPause = state
    29  }
    30  
    31  func AdminAddModerator(addr std.Address) {
    32  	assertIsAdmin()
    33  	moderatorList.Set(addr.String(), true)
    34  }
    35  
    36  func AdminRemoveModerator(addr std.Address) {
    37  	assertIsAdmin()
    38  	moderatorList.Set(addr.String(), false) // XXX: delete instead?
    39  }
    40  
    41  func ModAddPost(slug, title, body, publicationDate, authors, tags string) {
    42  	assertIsModerator()
    43  
    44  	caller := std.GetOrigCaller()
    45  	tagList := strings.Split(tags, ",")
    46  	authorList := strings.Split(authors, ",")
    47  
    48  	err := b.NewPost(caller, slug, title, body, publicationDate, authorList, tagList)
    49  	checkErr(err)
    50  }
    51  
    52  func ModEditPost(slug, title, body, publicationDate, authors, tags string) {
    53  	assertIsModerator()
    54  
    55  	tagList := strings.Split(tags, ",")
    56  	authorList := strings.Split(authors, ",")
    57  
    58  	err := b.GetPost(slug).Update(title, body, publicationDate, authorList, tagList)
    59  	checkErr(err)
    60  }
    61  
    62  func isAdmin(addr std.Address) bool {
    63  	return addr == adminAddr
    64  }
    65  
    66  func isModerator(addr std.Address) bool {
    67  	_, found := moderatorList.Get(addr.String())
    68  	return found
    69  }
    70  
    71  func assertIsAdmin() {
    72  	caller := std.GetOrigCaller()
    73  	if !isAdmin(caller) {
    74  		panic("access restricted.")
    75  	}
    76  }
    77  
    78  func assertIsModerator() {
    79  	caller := std.GetOrigCaller()
    80  	if isAdmin(caller) || isModerator(caller) {
    81  		return
    82  	}
    83  	panic("access restricted")
    84  }
    85  
    86  func assertNotInPause() {
    87  	if inPause {
    88  		panic("access restricted (pause)")
    89  	}
    90  }