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

     1  package gnoblog
     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  	commenterList avl.Tree
    14  	inPause       bool
    15  )
    16  
    17  func init() {
    18  	// adminAddr = std.GetOrigCaller() // FIXME: find a way to use this from the main's genesis.
    19  	adminAddr = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
    20  }
    21  
    22  func AdminSetAdminAddr(addr std.Address) {
    23  	assertIsAdmin()
    24  	adminAddr = addr
    25  }
    26  
    27  func AdminSetInPause(state bool) {
    28  	assertIsAdmin()
    29  	inPause = state
    30  }
    31  
    32  func AdminAddModerator(addr std.Address) {
    33  	assertIsAdmin()
    34  	moderatorList.Set(addr.String(), true)
    35  }
    36  
    37  func AdminRemoveModerator(addr std.Address) {
    38  	assertIsAdmin()
    39  	moderatorList.Set(addr.String(), false) // FIXME: delete instead?
    40  }
    41  
    42  func ModAddPost(slug, title, body, publicationDate, authors, tags string) {
    43  	assertIsModerator()
    44  
    45  	caller := std.GetOrigCaller()
    46  
    47  	var tagList []string
    48  	if tags != "" {
    49  		tagList = strings.Split(tags, ",")
    50  	}
    51  	var authorList []string
    52  	if authors != "" {
    53  		authorList = strings.Split(authors, ",")
    54  	}
    55  
    56  	err := b.NewPost(caller, slug, title, body, publicationDate, authorList, tagList)
    57  
    58  	checkErr(err)
    59  }
    60  
    61  func ModEditPost(slug, title, body, publicationDate, authors, tags string) {
    62  	assertIsModerator()
    63  
    64  	tagList := strings.Split(tags, ",")
    65  	authorList := strings.Split(authors, ",")
    66  
    67  	err := b.GetPost(slug).Update(title, body, publicationDate, authorList, tagList)
    68  	checkErr(err)
    69  }
    70  
    71  func ModRemovePost(slug string) {
    72  	assertIsModerator()
    73  
    74  	b.RemovePost(slug)
    75  }
    76  
    77  func ModAddCommenter(addr std.Address) {
    78  	assertIsModerator()
    79  	commenterList.Set(addr.String(), true)
    80  }
    81  
    82  func ModDelCommenter(addr std.Address) {
    83  	assertIsModerator()
    84  	commenterList.Set(addr.String(), false) // FIXME: delete instead?
    85  }
    86  
    87  func ModDelComment(slug string, index int) {
    88  	assertIsModerator()
    89  
    90  	err := b.GetPost(slug).DeleteComment(index)
    91  	checkErr(err)
    92  }
    93  
    94  func isAdmin(addr std.Address) bool {
    95  	return addr == adminAddr
    96  }
    97  
    98  func isModerator(addr std.Address) bool {
    99  	_, found := moderatorList.Get(addr.String())
   100  	return found
   101  }
   102  
   103  func isCommenter(addr std.Address) bool {
   104  	_, found := commenterList.Get(addr.String())
   105  	return found
   106  }
   107  
   108  func assertIsAdmin() {
   109  	caller := std.GetOrigCaller()
   110  	if !isAdmin(caller) {
   111  		panic("access restricted.")
   112  	}
   113  }
   114  
   115  func assertIsModerator() {
   116  	caller := std.GetOrigCaller()
   117  	if isAdmin(caller) || isModerator(caller) {
   118  		return
   119  	}
   120  	panic("access restricted")
   121  }
   122  
   123  func assertIsCommenter() {
   124  	caller := std.GetOrigCaller()
   125  	if isAdmin(caller) || isModerator(caller) || isCommenter(caller) {
   126  		return
   127  	}
   128  	panic("access restricted")
   129  }
   130  
   131  func assertNotInPause() {
   132  	if inPause {
   133  		panic("access restricted (pause)")
   134  	}
   135  }