ducky/devices

Paddy 2015-12-19 Parent:c24a6c5fcd8c

16:a700ede02f91 Go to Latest

ducky/devices/vendor/code.secondbit.org/trout.hg/trie.go

Validate device creation. Update our uuid package to the latest, which is now based on the GitHub fork instead of the Google Code. Also, update our api package to its latest version, which now needs the pqarrays package as a dependency. We fleshed out the validateDeviceCreation. We now pass in the scopes we have (for broad access control) and the user ID (for fine-grained access control). This helper returns the first error it encounters, though it should probably return a slice so we can return multiple errors all at once. Before we even decode the request to create a Device, let's check if the user is even logged in. If we can't ascertain that or they're not, there's no point in even consuming the memory necessary to read the request, because we know we're not going to use it anyways. Finally actually validate the devices we're creating, and return an appropriate error for each error we can get. Also, the api.CheckScopes helper function now takes the scopes passed in as a string slice, and we have an api.GetScopes helper function to retrieve the scopes associated with the request. Let's not keep parsing that. We need two new scopes to control access for device creation; ScopeImport lets users import devices in and is pretty much admin access. ScopeCreateOtherUserDevices allows a user to create Devices that are owned by another user.

History
1 package trout
3 import (
4 "net/http"
5 "sort"
6 "sync"
7 )
9 type trie struct {
10 branch *branch
11 sync.RWMutex
12 }
14 func (t *trie) match(input []string) ([]int, bool) {
15 if t.branch == nil {
16 t.branch = &branch{}
17 }
18 b := t.branch
19 path := []int{}
20 offset := 0
21 num := len(input)
22 for i := 0; i < num; i++ {
23 // if we're on a nil branch, we're at the end of our line
24 if b == nil {
25 return path, false
26 }
27 offset = pickNextBranch(b, offset, input[i])
28 if offset == -1 {
29 if len(path) == 0 {
30 // can't find it, bail
31 return path, false
32 }
33 // no match, backup
34 path, offset = backup(path)
35 offset = offset + 1 // we want the next index from the previously matched one
36 b = b.parent // we need to be picking a branch from our parent, again
37 i = i - 2 // back up to the choice before the one that got us here
38 } else {
39 path = append(path, offset)
40 b = b.children[offset]
41 offset = 0
42 }
43 }
44 return path, true
45 }
47 type branch struct {
48 parent *branch
49 children []*branch
50 key string
51 isParam bool
52 methods map[string]http.Handler
53 }
55 func (b *branch) Less(i, j int) bool {
56 if b.children[i].key == b.children[j].key {
57 return b.children[j].isParam && !b.children[i].isParam
58 }
59 return b.children[i].key < b.children[j].key
60 }
62 func (b *branch) Len() int {
63 return len(b.children)
64 }
66 func (b *branch) Swap(i, j int) {
67 b.children[i], b.children[j] = b.children[j], b.children[i]
68 }
70 func (b *branch) addChild(key string, param bool) *branch {
71 child := &branch{key: key, parent: b, isParam: param}
72 if b.children == nil {
73 b.children = []*branch{child}
74 return child
75 }
76 b.children = append(b.children, child)
77 sort.Sort(b)
78 return child
79 }
81 func (b *branch) check(input string) bool {
82 if b.isParam && b.key != "" {
83 return true
84 }
85 if b.key == input {
86 return true
87 }
88 return false
89 }
91 func (b *branch) setHandler(method string, handler http.Handler) {
92 if b.methods == nil {
93 b.methods = map[string]http.Handler{}
94 }
95 b.methods[method] = handler
96 }
98 func pickNextBranch(b *branch, offset int, input string) int {
99 count := len(b.children)
100 for i := offset; i < count; i++ {
101 if b.children[i].check(input) {
102 return i
103 }
104 }
105 return -1
106 }
108 func backup(path []int) ([]int, int) {
109 last := len(path) - 1
110 pos := path[last]
111 path = path[:last]
112 return path, pos
113 }