This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
forked from seven5/seven5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
105 lines (95 loc) · 3.8 KB
/
base.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
package seven5
//NewBaseDispatcher returns a raw dispatcher that has several defaults set.
//* The Allow() interfaces are used for authorization checks
//* The application will keep a single cookie on the browser (that's why the cookie mapper is passed in)
//* The application will keep a session associated with the cookie for each "logged in" user (via the SessionManager)
//* Json is used to encode and decode the wire types
//* Rest resources dispatched by this object are mapped to /rest in the URL space.
//You must pass an already created session manager into this method
//(see NewSimpleSessionManager(...))
func NewBaseDispatcher(sm SessionManager, cm CookieMapper) *BaseDispatcher {
prefix := "/rest"
result := &BaseDispatcher{}
io := &RawIOHook{&JsonDecoder{}, &JsonEncoder{}, cm}
result.RawDispatcher = NewRawDispatcher(io, sm, result, prefix)
return result
}
//BaseDispatcher is a slight "specialization" of the RawDispatcher for REST resources. BaseDispatcher
//understands how to dispatch to REST resources (like Raw) but can also handle the Allower protocol for
//primitive, coarse-grained authorization. Additionally, it allows easy creation of a BaseDispatcher
//with a custom SessionManager, as this is often used with user roles (and Allow protocol).
type BaseDispatcher struct {
*RawDispatcher
}
//Index checks with AllowReader.AllowRead to allow/refuse access to this method on _any_ resource
//associated with this BaseDispatcher.
func (self *BaseDispatcher) Index(d *restShared, bundle PBundle) bool {
allowReader, ok := d.index.(AllowReader)
if !ok {
return true
}
return allowReader.AllowRead(bundle)
}
//Post checks with AllowWriter.AllowWrite to allow/refuse access to this method on _any_ resource
//associated with this BaseDispatcher.
func (self *BaseDispatcher) Post(d *restShared, bundle PBundle) bool {
allowWriter, ok := d.post.(AllowWriter)
if !ok {
return true
}
return allowWriter.AllowWrite(bundle)
}
//Find checks with Allower.Allow(FIND) to allow/refuse access to this method on _any_ resource
//associated with this BaseDispatcher.
func (self *BaseDispatcher) Find(d *restObj, num int64, bundle PBundle) bool {
allow, ok := d.find.(Allower)
if !ok {
return true
}
return allow.Allow(num, "GET", bundle)
}
//Put checks with Allower.Allow(PUT) to allow/refuse access to this method on _any_ resource
//associated with this BaseDispatcher.
func (self *BaseDispatcher) Put(d *restObj, num int64, bundle PBundle) bool {
allow, ok := d.put.(Allower)
if !ok {
return true
}
return allow.Allow(num, "PUT", bundle)
}
//Find checks with Allower.Allow(DELETE) to allow/refuse access to this method on _any_ resource
//associated with this BaseDispatcher.
func (self *BaseDispatcher) Delete(d *restObj, num int64, bundle PBundle) bool {
allow, ok := d.del.(Allower)
if !ok {
return true
}
return allow.Allow(num, "DELETE", bundle)
}
//Find checks with Allower.AllowUdid(GET) to allow/refuse access to this method on _any_ resource
//associated with this BaseDispatcher.
func (self *BaseDispatcher) FindUdid(d *restObjUdid, id string, bundle PBundle) bool {
allow, ok := d.find.(AllowerUdid)
if !ok {
return true
}
return allow.Allow(id, "GET", bundle)
}
//Put checks with Allower.AllowUdid(PUT) to allow/refuse access to this method on _any_ resource
//associated with this BaseDispatcher.
func (self *BaseDispatcher) PutUdid(d *restObjUdid, id string, bundle PBundle) bool {
allow, ok := d.put.(AllowerUdid)
if !ok {
return true
}
return allow.Allow(id, "PUT", bundle)
}
//Find checks with AllowerUdid.Allow(DELETE) to allow/refuse access to this method on _any_ resource
//associated with this BaseDispatcher.
func (self *BaseDispatcher) DeleteUdid(d *restObjUdid, id string, bundle PBundle) bool {
allow, ok := d.del.(AllowerUdid)
if !ok {
return true
}
return allow.Allow(id, "DELETE", bundle)
}