role.go 4.1 KB
Newer Older
R
runzexia 已提交
1
package jenkins
R
runzexia 已提交
2 3 4

import (
	"errors"
S
shaowenchen 已提交
5
	"kubesphere.io/kubesphere/pkg/simple/client/devops"
R
runzexia 已提交
6 7 8 9 10 11 12
	"net/http"
	"reflect"
	"strconv"
	"strings"
)

type GlobalRoleResponse struct {
S
shaowenchen 已提交
13 14
	RoleName      string                     `json:"roleName"`
	PermissionIds devops.GlobalPermissionIds `json:"permissionIds"`
R
runzexia 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
}

type GlobalRole struct {
	Jenkins *Jenkins
	Raw     GlobalRoleResponse
}

type ProjectRole struct {
	Jenkins *Jenkins
	Raw     ProjectRoleResponse
}

type ProjectRoleResponse struct {
S
shaowenchen 已提交
28 29 30
	RoleName      string                      `json:"roleName"`
	PermissionIds devops.ProjectPermissionIds `json:"permissionIds"`
	Pattern       string                      `json:"pattern"`
R
runzexia 已提交
31 32
}

S
shaowenchen 已提交
33
func (j *GlobalRole) Update(ids devops.GlobalPermissionIds) error {
R
runzexia 已提交
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
	var idArray []string
	values := reflect.ValueOf(ids)
	for i := 0; i < values.NumField(); i++ {
		field := values.Field(i)
		if field.Bool() {
			idArray = append(idArray, values.Type().Field(i).Tag.Get("json"))
		}
	}
	param := map[string]string{
		"roleName":      j.Raw.RoleName,
		"type":          GLOBAL_ROLE,
		"permissionIds": strings.Join(idArray, ","),
		"overwrite":     strconv.FormatBool(true),
	}
	responseString := ""
	response, err := j.Jenkins.Requester.Post("/role-strategy/strategy/addRole", nil, &responseString, param)
	if err != nil {
		return err
	}
	if response.StatusCode != http.StatusOK {
		return errors.New(strconv.Itoa(response.StatusCode))
	}
	return nil
}

S
shaowenchen 已提交
59
// call jenkins api to update global role
R
runzexia 已提交
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
func (j *GlobalRole) AssignRole(sid string) error {
	param := map[string]string{
		"type":     GLOBAL_ROLE,
		"roleName": j.Raw.RoleName,
		"sid":      sid,
	}
	responseString := ""
	response, err := j.Jenkins.Requester.Post("/role-strategy/strategy/assignRole", nil, &responseString, param)
	if err != nil {
		return err
	}
	if response.StatusCode != http.StatusOK {
		return errors.New(strconv.Itoa(response.StatusCode))
	}
	return nil
}

func (j *GlobalRole) UnAssignRole(sid string) error {
	param := map[string]string{
		"type":     GLOBAL_ROLE,
		"roleName": j.Raw.RoleName,
		"sid":      sid,
	}
	responseString := ""
	response, err := j.Jenkins.Requester.Post("/role-strategy/strategy/unassignRole", nil, &responseString, param)
	if err != nil {
		return err
	}
	if response.StatusCode != http.StatusOK {
		return errors.New(strconv.Itoa(response.StatusCode))
	}
	return nil
}

S
shaowenchen 已提交
94 95 96
// update ProjectPermissionIds to Project
// pattern string means some project, like project-name/*
func (j *ProjectRole) Update(pattern string, ids devops.ProjectPermissionIds) error {
R
runzexia 已提交
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
	var idArray []string
	values := reflect.ValueOf(ids)
	for i := 0; i < values.NumField(); i++ {
		field := values.Field(i)
		if field.Bool() {
			idArray = append(idArray, values.Type().Field(i).Tag.Get("json"))
		}
	}
	param := map[string]string{
		"roleName":      j.Raw.RoleName,
		"type":          PROJECT_ROLE,
		"permissionIds": strings.Join(idArray, ","),
		"overwrite":     strconv.FormatBool(true),
		"pattern":       pattern,
	}
	responseString := ""
	response, err := j.Jenkins.Requester.Post("/role-strategy/strategy/addRole", nil, &responseString, param)
	if err != nil {
		return err
	}
	if response.StatusCode != http.StatusOK {
		return errors.New(strconv.Itoa(response.StatusCode))
	}
	return nil
}

func (j *ProjectRole) AssignRole(sid string) error {
	param := map[string]string{
		"type":     PROJECT_ROLE,
		"roleName": j.Raw.RoleName,
		"sid":      sid,
	}
	responseString := ""
	response, err := j.Jenkins.Requester.Post("/role-strategy/strategy/assignRole", nil, &responseString, param)
	if err != nil {
		return err
	}
	if response.StatusCode != http.StatusOK {
		return errors.New(strconv.Itoa(response.StatusCode))
	}
	return nil
}

func (j *ProjectRole) UnAssignRole(sid string) error {
	param := map[string]string{
		"type":     PROJECT_ROLE,
		"roleName": j.Raw.RoleName,
		"sid":      sid,
	}
	responseString := ""
	response, err := j.Jenkins.Requester.Post("/role-strategy/strategy/unassignRole", nil, &responseString, param)
	if err != nil {
		return err
	}
	if response.StatusCode != http.StatusOK {
		return errors.New(strconv.Itoa(response.StatusCode))
	}
	return nil
}