forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
privilege.go
168 lines (146 loc) · 3.93 KB
/
privilege.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package privilege
import (
"bytes"
"fmt"
"sort"
"strings"
)
//go:generate stringer -type=Kind
// Kind defines a privilege. This is output by the parser,
// and used to generate the privilege bitfields in the PrivilegeDescriptor.
type Kind uint32
// List of privileges. ALL is specifically encoded so that it will automatically
// pick up new privileges.
const (
_ Kind = iota
ALL
CREATE
DROP
GRANT
SELECT
INSERT
DELETE
UPDATE
)
// Predefined sets of privileges.
var (
ReadData = List{GRANT, SELECT}
ReadWriteData = List{GRANT, SELECT, INSERT, DELETE, UPDATE}
)
// Mask returns the bitmask for a given privilege.
func (k Kind) Mask() uint32 {
return 1 << k
}
// ByValue is just an array of privilege kinds sorted by value.
var ByValue = [...]Kind{
ALL, CREATE, DROP, GRANT, SELECT, INSERT, DELETE, UPDATE,
}
// List is a list of privileges.
type List []Kind
// Len, Swap, and Less implement the Sort interface.
func (pl List) Len() int {
return len(pl)
}
func (pl List) Swap(i, j int) {
pl[i], pl[j] = pl[j], pl[i]
}
func (pl List) Less(i, j int) bool {
return pl[i] < pl[j]
}
// names returns a list of privilege names in the same
// order as 'pl'.
func (pl List) names() []string {
ret := make([]string, len(pl))
for i, p := range pl {
ret[i] = p.String()
}
return ret
}
// Format prints out the list in a buffer.
// This keeps the existing order and uses ", " as separator.
func (pl List) Format(buf *bytes.Buffer) {
for i, p := range pl {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(p.String())
}
}
// String implements the Stringer interface.
// This keeps the existing order and uses ", " as separator.
func (pl List) String() string {
return strings.Join(pl.names(), ", ")
}
// SortedString is similar to String() but returns
// privileges sorted by name and uses "," as separator.
func (pl List) SortedString() string {
names := pl.SortedNames()
return strings.Join(names, ",")
}
// SortedNames returns a list of privilege names
// in sorted order.
func (pl List) SortedNames() []string {
names := pl.names()
sort.Strings(names)
return names
}
// ToBitField returns the bitfield representation of
// a list of privileges.
func (pl List) ToBitField() uint32 {
var ret uint32
for _, p := range pl {
ret |= p.Mask()
}
return ret
}
// ListFromBitField takes a bitfield of privileges and
// returns a list. It is ordered in increasing
// value of privilege.Kind.
func ListFromBitField(m uint32) List {
ret := List{}
for _, p := range ByValue {
if m&p.Mask() != 0 {
ret = append(ret, p)
}
}
return ret
}
// Lists is a list of privilege lists
type Lists []List
// Contains returns whether the list of privilege lists contains exactly the
// privilege list represented by bitfield m. This intentionally treats ALL and
// {CREATE, ..., UPDATE} as distinct privilege lists.
func (pls Lists) Contains(m uint32) bool {
for _, pl := range pls {
if pl.ToBitField() == m {
return true
}
}
return false
}
// String stringifies each constituent list of privileges, groups each inside of
// {} braces, and joins them with " or ".
//
// Examples:
// {{SELECT}}.String() -> "{SELECT}"
// {{SELECT}, {SELECT, GRANT}}.String() -> "{SELECT} or {SELECT, GRANT}"
func (pls Lists) String() string {
ret := make([]string, len(pls))
for i, pl := range pls {
ret[i] = fmt.Sprintf("{%s}", pl)
}
return strings.Join(ret, " or ")
}