- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
118 lines (98 loc) · 3.72 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"log"
"strings"
"github.com/kataras/iris/v12"
permissions "github.com/xyproto/permissionbolt"
// * PostgreSQL support:
// permissions "github.com/xyproto/pstore" and
// perm, err := permissions.New(...)
//
// * MariaDB/MySQL support:
// permissions "github.com/xyproto/permissionsql" and
// perm, err := permissions.New/NewWithDSN(...)
// * Redis support:
// permissions "github.com/xyproto/permissions2"
// perm, err := permissions.New2()
// * Bolt support (this one):
// permissions "github.com/xyproto/permissionbolt" and
// perm, err := permissions.New(...)
)
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
// New permissions middleware.
perm, err := permissions.New()
if err != nil {
log.Fatalln(err)
}
// Blank slate, no default permissions
// perm.Clear()
// Set up a middleware handler for Iris, with a custom "permission denied" message.
permissionHandler := func(ctx iris.Context) {
// Check if the user has the right admin/user rights
if perm.Rejected(ctx.ResponseWriter(), ctx.Request()) {
// Deny the request, don't call other middleware handlers
ctx.StopWithText(iris.StatusForbidden, "Permission denied!")
return
}
// Call the next middleware handler
ctx.Next()
}
// Register the permissions middleware
app.Use(permissionHandler)
// Get the userstate, used in the handlers below
userstate := perm.UserState()
app.Get("/", func(ctx iris.Context) {
msg := ""
msg += fmt.Sprintf("Has user bob: %v\n", userstate.HasUser("bob"))
msg += fmt.Sprintf("Logged in on server: %v\n", userstate.IsLoggedIn("bob"))
msg += fmt.Sprintf("Is confirmed: %v\n", userstate.IsConfirmed("bob"))
msg += fmt.Sprintf("Username stored in cookies (or blank): %v\n", userstate.Username(ctx.Request()))
msg += fmt.Sprintf("Current user is logged in, has a valid cookie and *user rights*: %v\n", userstate.UserRights(ctx.Request()))
msg += fmt.Sprintf("Current user is logged in, has a valid cookie and *admin rights*: %v\n", userstate.AdminRights(ctx.Request()))
msg += fmt.Sprintln("\nTry: /register, /confirm, /remove, /login, /logout, /makeadmin, /clear, /data and /admin")
ctx.WriteString(msg)
})
app.Get("/register", func(ctx iris.Context) {
userstate.AddUser("bob", "hunter1", "bob@zombo.com")
ctx.Writef("User bob was created: %v\n", userstate.HasUser("bob"))
})
app.Get("/confirm", func(ctx iris.Context) {
userstate.MarkConfirmed("bob")
ctx.Writef("User bob was confirmed: %v\n", userstate.IsConfirmed("bob"))
})
app.Get("/remove", func(ctx iris.Context) {
userstate.RemoveUser("bob")
ctx.Writef("User bob was removed: %v\n", !userstate.HasUser("bob"))
})
app.Get("/login", func(ctx iris.Context) {
// Headers will be written, for storing a cookie
userstate.Login(ctx.ResponseWriter(), "bob")
ctx.Writef("bob is now logged in: %v\n", userstate.IsLoggedIn("bob"))
})
app.Get("/logout", func(ctx iris.Context) {
userstate.Logout("bob")
ctx.Writef("bob is now logged out: %v\n", !userstate.IsLoggedIn("bob"))
})
app.Get("/makeadmin", func(ctx iris.Context) {
userstate.SetAdminStatus("bob")
ctx.Writef("bob is now administrator: %v\n", userstate.IsAdmin("bob"))
})
app.Get("/clear", func(ctx iris.Context) {
userstate.ClearCookie(ctx.ResponseWriter())
ctx.WriteString("Clearing cookie")
})
app.Get("/data", func(ctx iris.Context) {
ctx.WriteString("user page that only logged in users must see!")
})
app.Get("/admin", func(ctx iris.Context) {
ctx.WriteString("super secret information that only logged in administrators must see!\n\n")
if usernames, err := userstate.AllUsernames(); err == nil {
ctx.Writef("list of all users: %s" + strings.Join(usernames, ", "))
}
})
// Serve
app.Listen(":8080")
}