-
Notifications
You must be signed in to change notification settings - Fork 1
/
roletemplate.go
51 lines (43 loc) · 1.44 KB
/
roletemplate.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
package roletemplate
import (
"fmt"
"net/http"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
client "github.com/rancher/types/client/management/v3"
)
type Wrapper struct {
RoleTemplateLister v3.RoleTemplateLister
}
func (w Wrapper) Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
if request.Method != http.MethodPut && request.Method != http.MethodPost {
return nil
}
// Only cluster roles can be administrative for now. So this validation will prevent users from creating any "project" context
// roleTemplates with administrative bit set to true
if request.Method == http.MethodPost || request.Method == http.MethodPut {
administrative := convert.ToBool(data[client.RoleTemplateFieldAdministrative])
context := convert.ToString(data[client.RoleTemplateFieldContext])
if administrative && context != "cluster" {
return fmt.Errorf("Only cluster roles can be administrative")
}
}
if request.Method != http.MethodPut {
return nil
}
rt, err := w.RoleTemplateLister.Get("", request.ID)
if err != nil {
return err
}
if rt.Builtin == true {
// Drop everything but locked and defaults. If it's builtin nothing else can change.
for k := range data {
if k == "locked" || k == "clusterCreatorDefault" || k == "projectCreatorDefault" {
continue
}
delete(data, k)
}
}
return nil
}