admin.gno

1.77 Kb ยท 90 lines
 1package gnopages
 2
 3import (
 4	"std"
 5	"strings"
 6
 7	"gno.land/p/demo/avl"
 8)
 9
10var (
11	adminAddr     std.Address
12	moderatorList avl.Tree
13	inPause       bool
14)
15
16func init() {
17	// adminAddr = std.GetOrigCaller() // FIXME: find a way to use this from the main's genesis.
18	adminAddr = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
19}
20
21func AdminSetAdminAddr(addr std.Address) {
22	assertIsAdmin()
23	adminAddr = addr
24}
25
26func AdminSetInPause(state bool) {
27	assertIsAdmin()
28	inPause = state
29}
30
31func AdminAddModerator(addr std.Address) {
32	assertIsAdmin()
33	moderatorList.Set(addr.String(), true)
34}
35
36func AdminRemoveModerator(addr std.Address) {
37	assertIsAdmin()
38	moderatorList.Set(addr.String(), false) // XXX: delete instead?
39}
40
41func 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
52func 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
62func isAdmin(addr std.Address) bool {
63	return addr == adminAddr
64}
65
66func isModerator(addr std.Address) bool {
67	_, found := moderatorList.Get(addr.String())
68	return found
69}
70
71func assertIsAdmin() {
72	caller := std.GetOrigCaller()
73	if !isAdmin(caller) {
74		panic("access restricted.")
75	}
76}
77
78func assertIsModerator() {
79	caller := std.GetOrigCaller()
80	if isAdmin(caller) || isModerator(caller) {
81		return
82	}
83	panic("access restricted")
84}
85
86func assertNotInPause() {
87	if inPause {
88		panic("access restricted (pause)")
89	}
90}