workspaces.go 4.5 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*

 Copyright 2019 The KubeSphere 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 iam

import (
	"github.com/emicklei/go-restful"
H
hongming 已提交
22
	k8serr "k8s.io/apimachinery/pkg/api/errors"
H
hongming 已提交
23 24 25 26 27
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/errors"
	"kubesphere.io/kubesphere/pkg/models"
	"kubesphere.io/kubesphere/pkg/models/iam"
	"kubesphere.io/kubesphere/pkg/models/workspaces"
H
hongming 已提交
28 29
	"kubesphere.io/kubesphere/pkg/params"
	"net/http"
H
hongming 已提交
30 31
)

H
hongming 已提交
32
func ListWorkspaceRoles(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
33

H
hongming 已提交
34 35 36 37 38
	workspace := req.PathParameter("workspace")
	conditions, err := params.ParseConditions(req.QueryParameter(params.ConditionsParam))
	orderBy := req.QueryParameter(params.OrderByParam)
	limit, offset := params.ParsePaging(req.QueryParameter(params.PagingParam))
	reverse := params.ParseReverse(req)
H
hongming 已提交
39 40

	if err != nil {
H
hongming 已提交
41
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
H
hongming 已提交
42 43 44
		return
	}

H
hongming 已提交
45
	result, err := iam.ListWorkspaceRoles(workspace, conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
46 47 48 49 50 51

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
52
	resp.WriteAsJson(result.Items)
H
hongming 已提交
53 54
}

H
hongming 已提交
55 56 57
func ListWorkspaceRoleRules(req *restful.Request, resp *restful.Response) {
	workspace := req.PathParameter("workspace")
	role := req.PathParameter("role")
H
hongming 已提交
58

H
hongming 已提交
59
	rules := iam.GetWorkspaceRoleSimpleRules(workspace, role)
H
hongming 已提交
60

H
hongming 已提交
61
	resp.WriteAsJson(rules)
H
hongming 已提交
62 63
}

H
hongming 已提交
64 65 66
func DescribeWorkspaceRole(req *restful.Request, resp *restful.Response) {
	workspace := req.PathParameter("workspace")
	roleName := req.PathParameter("role")
H
hongming 已提交
67

H
hongming 已提交
68
	role, err := iam.GetWorkspaceRole(workspace, roleName)
H
hongming 已提交
69 70 71 72 73 74

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
75
	resp.WriteAsJson(role)
H
hongming 已提交
76 77
}

H
hongming 已提交
78 79 80
func DescribeWorkspaceUser(req *restful.Request, resp *restful.Response) {
	workspace := req.PathParameter("workspace")
	username := req.PathParameter("username")
H
hongming 已提交
81

H
hongming 已提交
82
	workspaceRole, err := iam.GetUserWorkspaceRole(workspace, username)
H
hongming 已提交
83 84

	if err != nil {
H
hongming 已提交
85 86 87 88 89
		if k8serr.IsNotFound(err) {
			resp.WriteHeaderAndEntity(http.StatusNotFound, errors.Wrap(err))
		} else {
			resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		}
H
hongming 已提交
90 91 92 93

		return
	}

H
hongming 已提交
94
	user, err := iam.GetUserInfo(username)
H
hongming 已提交
95 96 97 98 99 100

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
101
	user.WorkspaceRole = workspaceRole.Annotations[constants.DisplayNameAnnotationKey]
H
hongming 已提交
102

H
hongming 已提交
103
	resp.WriteAsJson(user)
H
hongming 已提交
104 105
}

H
hongming 已提交
106 107
func ListDevopsRoleRules(req *restful.Request, resp *restful.Response) {
	role := req.PathParameter("role")
H
hongming 已提交
108

H
hongming 已提交
109
	rules := iam.GetDevopsRoleSimpleRules(role)
H
hongming 已提交
110

H
hongming 已提交
111
	resp.WriteAsJson(rules)
H
hongming 已提交
112 113
}

H
hongming 已提交
114 115 116 117
func InviteUser(req *restful.Request, resp *restful.Response) {
	workspace := req.PathParameter("workspace")
	var user models.User
	err := req.ReadEntity(&user)
H
hongming 已提交
118 119 120 121 122
	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
		return
	}

H
hongming 已提交
123
	err = workspaces.InviteUser(workspace, &user)
H
hongming 已提交
124 125 126 127 128 129 130 131 132

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

	resp.WriteAsJson(errors.None)
}

H
hongming 已提交
133 134
func RemoveUser(req *restful.Request, resp *restful.Response) {
	workspace := req.PathParameter("workspace")
H
hongming 已提交
135
	username := req.PathParameter("username")
H
hongming 已提交
136

H
hongming 已提交
137
	err := workspaces.RemoveUser(workspace, username)
H
hongming 已提交
138 139 140 141 142
	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
143
	resp.WriteAsJson(errors.None)
H
hongming 已提交
144 145
}

H
hongming 已提交
146
func ListWorkspaceUsers(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
147
	workspace := req.PathParameter("workspace")
H
hongming 已提交
148 149 150 151
	conditions, err := params.ParseConditions(req.QueryParameter(params.ConditionsParam))
	orderBy := req.QueryParameter(params.OrderByParam)
	limit, offset := params.ParsePaging(req.QueryParameter(params.PagingParam))
	reverse := params.ParseReverse(req)
H
hongming 已提交
152 153

	if err != nil {
H
hongming 已提交
154
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
H
hongming 已提交
155 156 157
		return
	}

H
hongming 已提交
158
	result, err := iam.ListWorkspaceUsers(workspace, conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
159 160 161 162 163 164

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
165
	resp.WriteAsJson(result)
H
hongming 已提交
166
}