forked from TencentBlueKing/bk-cmdb
/
public.go
124 lines (110 loc) · 4.17 KB
/
public.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
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 user
import (
"encoding/json"
"strings"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/core/cc/api"
"configcenter/src/common/metadata"
"configcenter/src/web_server/application/middleware/user/plugins"
webCommon "configcenter/src/web_server/common"
)
type publicUser struct {
}
// LoginUser user login
func (m *publicUser) LoginUser(c *gin.Context) bool {
ccapi := api.NewAPIResource()
config, _ := ccapi.ParseConfig()
user := plugins.CurrentPlugin(c)
isMultiOwner := false
multipleOwner, ok := config["session.multiple_owner"]
if ok && common.LoginSystemMultiSupplierTrue == multipleOwner {
isMultiOwner = true
}
userInfo, loginSucc := user.LoginUser(c, config, isMultiOwner)
if !loginSucc {
return false
}
if true == isMultiOwner || true == userInfo.MultiSupplier {
err := NewOwnerManager(userInfo.UserName, userInfo.OnwerUin, userInfo.Language).InitOwner()
if nil != err {
blog.Error("InitOwner error: %v", err)
return false
}
}
strOwnerUinlist := []byte("")
if 0 != len(userInfo.OwnerUinArr) {
strOwnerUinlist, _ = json.Marshal(userInfo.OwnerUinArr)
}
cookielanguage, _ := c.Cookie("blueking_language")
session := sessions.Default(c)
session.Set(common.WEBSessionUinKey, userInfo.UserName)
session.Set(common.WEBSessionChineseNameKey, userInfo.ChName)
session.Set(common.WEBSessionPhoneKey, userInfo.Phone)
session.Set(common.WEBSessionEmailKey, userInfo.Email)
session.Set(common.WEBSessionRoleKey, userInfo.Role)
session.Set(common.HTTPCookieBKToken, userInfo.BkToken)
session.Set(common.WEBSessionOwnerUinKey, userInfo.OnwerUin)
session.Set(common.WEBSessionAvatarUrlKey, userInfo.AvatarUrl)
session.Set(common.WEBSessionOwnerUinListeKey, string(strOwnerUinlist))
if userInfo.MultiSupplier {
session.Set(common.WEBSessionMultiSupplierKey, common.LoginSystemMultiSupplierTrue)
} else {
session.Set(common.WEBSessionMultiSupplierKey, common.LoginSystemMultiSupplierFalse)
}
session.Set(webCommon.IsSkipLogin, "0")
if "" != cookielanguage {
session.Set(common.WEBSessionLanguageKey, cookielanguage)
} else {
session.Set(common.WEBSessionLanguageKey, userInfo.Language)
}
session.Save()
return true
}
// GetUserList get user list from paas
func (m *publicUser) GetUserList(c *gin.Context) (int, interface{}) {
ccapi := api.NewAPIResource()
config, _ := ccapi.ParseConfig()
user := plugins.CurrentPlugin(c)
userList, err := user.GetUserList(c, config)
rspBody := metadata.LonginSystemUserListResult{}
if nil != err {
rspBody.Code = common.CCErrCommHTTPDoRequestFailed
rspBody.ErrMsg = err.Error()
rspBody.Result = false
}
rspBody.Result = true
rspBody.Data = userList
return 200, rspBody
}
func (m *publicUser) GetLoginUrl(c *gin.Context) string {
ccapi := api.NewAPIResource()
config, _ := ccapi.ParseConfig()
siteUrl, ok := config["site.domain_url"]
if ok {
siteUrl = strings.Trim(siteUrl, "/")
}
params := new(metadata.LogoutRequestParams)
err := json.NewDecoder(c.Request.Body).Decode(params)
if nil != err || (common.LogoutHTTPSchemeHTTP != params.HTTPScheme && common.LogoutHTTPSchemeHTTPS != params.HTTPScheme) {
params.HTTPScheme, err = c.Cookie(common.LogoutHTTPSchemeCookieKey)
if nil != err || (common.LogoutHTTPSchemeHTTP != params.HTTPScheme && common.LogoutHTTPSchemeHTTPS != params.HTTPScheme) {
params.HTTPScheme = common.LogoutHTTPSchemeHTTP
}
}
user := plugins.CurrentPlugin(c)
return user.GetLoginUrl(c, config, params)
}