-
Notifications
You must be signed in to change notification settings - Fork 2
/
flags.go
74 lines (69 loc) · 1.76 KB
/
flags.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
// Martini middleware/handler to enable Access Control, such as role-based access control support, Through an flag of integer kind.
package accessflags
import (
"github.com/go-martini/martini"
"net/http"
"reflect"
)
// BitwiseAnd use the following rules:
// if flag equal 0, passed
// if flag bitwise and base is not equal 0, passed
// otherwise WriteHeader(statusCode)
func BitwiseAnd(base interface{}, statusCode int) martini.Handler {
val := reflect.ValueOf(base)
based := val.Int()
t := val.Type()
return func(w http.ResponseWriter, c martini.Context) {
v := c.Get(t)
var flag int64
if v.IsValid() {
flag = v.Int()
}
if flag == 0 || flag&based != 0 {
return
}
w.WriteHeader(statusCode)
}
}
// Less use the following rules:
// if flag Less than or Equal base, passed
// otherwise WriteHeader(statusCode)
func Less(base interface{}, statusCode int) martini.Handler {
val := reflect.ValueOf(base)
based := val.Int()
t := val.Type()
return func(w http.ResponseWriter, c martini.Context) {
v := c.Get(t)
var flag int64
if v.IsValid() {
flag = v.Int()
}
if flag <= based {
return
}
w.WriteHeader(statusCode)
}
}
// Great use the following rules:
// if flag Greater than or Equal base, passed
// otherwise WriteHeader(statusCode)
func Great(base interface{}, statusCode int) martini.Handler {
val := reflect.ValueOf(base)
based := val.Int()
t := val.Type()
return func(w http.ResponseWriter, c martini.Context) {
v := c.Get(t)
var flag int64
if v.IsValid() {
flag = v.Int()
}
if flag >= based {
return
}
w.WriteHeader(statusCode)
}
}
// Forbidden is Quick method the same as BitwiseAnd(base, http.StatusForbidden)
func Forbidden(base interface{}) martini.Handler {
return BitwiseAnd(base, http.StatusForbidden)
}