Search Apps Documentation Source Content File Folder Download Copy

admin.gno

3.08 Kb ยท 149 lines
  1package gnoblog
  2
  3import (
  4	"std"
  5	"strings"
  6
  7	"gno.land/p/demo/avl"
  8	"gno.land/p/demo/dao"
  9	"gno.land/r/gov/dao/bridge"
 10)
 11
 12var (
 13	adminAddr     std.Address
 14	moderatorList avl.Tree
 15	commenterList avl.Tree
 16	inPause       bool
 17)
 18
 19func init() {
 20	// adminAddr = std.GetOrigCaller() // FIXME: find a way to use this from the main's genesis.
 21	adminAddr = "g1manfred47kzduec920z88wfr64ylksmdcedlf5" // @moul
 22}
 23
 24func AdminSetAdminAddr(addr std.Address) {
 25	assertIsAdmin()
 26	adminAddr = addr
 27}
 28
 29func AdminSetInPause(state bool) {
 30	assertIsAdmin()
 31	inPause = state
 32}
 33
 34func AdminAddModerator(addr std.Address) {
 35	assertIsAdmin()
 36	moderatorList.Set(addr.String(), true)
 37}
 38
 39func AdminRemoveModerator(addr std.Address) {
 40	assertIsAdmin()
 41	moderatorList.Set(addr.String(), false) // FIXME: delete instead?
 42}
 43
 44func NewPostExecutor(slug, title, body, publicationDate, authors, tags string) dao.Executor {
 45	callback := func() error {
 46		addPost(std.PrevRealm().Addr(), slug, title, body, publicationDate, authors, tags)
 47
 48		return nil
 49	}
 50
 51	return bridge.GovDAO().NewGovDAOExecutor(callback)
 52}
 53
 54func ModAddPost(slug, title, body, publicationDate, authors, tags string) {
 55	assertIsModerator()
 56	caller := std.GetOrigCaller()
 57	addPost(caller, slug, title, body, publicationDate, authors, tags)
 58}
 59
 60func addPost(caller std.Address, slug, title, body, publicationDate, authors, tags string) {
 61	var tagList []string
 62	if tags != "" {
 63		tagList = strings.Split(tags, ",")
 64	}
 65	var authorList []string
 66	if authors != "" {
 67		authorList = strings.Split(authors, ",")
 68	}
 69
 70	err := b.NewPost(caller, slug, title, body, publicationDate, authorList, tagList)
 71
 72	checkErr(err)
 73}
 74
 75func ModEditPost(slug, title, body, publicationDate, authors, tags string) {
 76	assertIsModerator()
 77
 78	tagList := strings.Split(tags, ",")
 79	authorList := strings.Split(authors, ",")
 80
 81	err := b.GetPost(slug).Update(title, body, publicationDate, authorList, tagList)
 82	checkErr(err)
 83}
 84
 85func ModRemovePost(slug string) {
 86	assertIsModerator()
 87
 88	b.RemovePost(slug)
 89}
 90
 91func ModAddCommenter(addr std.Address) {
 92	assertIsModerator()
 93	commenterList.Set(addr.String(), true)
 94}
 95
 96func ModDelCommenter(addr std.Address) {
 97	assertIsModerator()
 98	commenterList.Set(addr.String(), false) // FIXME: delete instead?
 99}
100
101func ModDelComment(slug string, index int) {
102	assertIsModerator()
103
104	err := b.GetPost(slug).DeleteComment(index)
105	checkErr(err)
106}
107
108func isAdmin(addr std.Address) bool {
109	return addr == adminAddr
110}
111
112func isModerator(addr std.Address) bool {
113	_, found := moderatorList.Get(addr.String())
114	return found
115}
116
117func isCommenter(addr std.Address) bool {
118	_, found := commenterList.Get(addr.String())
119	return found
120}
121
122func assertIsAdmin() {
123	caller := std.GetOrigCaller()
124	if !isAdmin(caller) {
125		panic("access restricted.")
126	}
127}
128
129func assertIsModerator() {
130	caller := std.GetOrigCaller()
131	if isAdmin(caller) || isModerator(caller) {
132		return
133	}
134	panic("access restricted")
135}
136
137func assertIsCommenter() {
138	caller := std.GetOrigCaller()
139	if isAdmin(caller) || isModerator(caller) || isCommenter(caller) {
140		return
141	}
142	panic("access restricted")
143}
144
145func assertNotInPause() {
146	if inPause {
147		panic("access restricted (pause)")
148	}
149}