/
nexus_user.go
120 lines (105 loc) · 3.23 KB
/
nexus_user.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package dao
import (
"database/sql/driver"
"encoding/json"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/pkg/database/dbengine"
"github.com/erda-project/erda/pkg/nexus"
)
type NexusUser struct {
dbengine.BaseModel
RepoID *uint64 `json:"repoID"` // 所属 repo,可以为空
PublisherID *uint64 `json:"publisherID"` // 所属 publisher,可以为空
OrgID *uint64 `json:"orgID"` // 所属 org,可以为空
ClusterName string `json:"clusterName"` // 所属集群
Name string `json:"name"`
Password string `json:"password"` // 加密存储
Config NexusUserConfig `json:"config"`
}
type NexusUserConfig nexus.User
func (config NexusUserConfig) Value() (driver.Value, error) {
if b, err := json.Marshal(config); err != nil {
return nil, errors.Wrapf(err, "failed to marshal user config")
} else {
return string(b), nil
}
}
func (config *NexusUserConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
v, ok := value.([]byte)
if !ok {
return errors.New("invalid scan source for user config")
}
if len(v) == 0 {
return nil
}
if err := json.Unmarshal(v, config); err != nil {
return errors.Wrapf(err, "failed to unmarshal user config")
}
return nil
}
func (NexusUser) TableName() string {
return "dice_nexus_users"
}
func (client *DBClient) CreateOrUpdateNexusUser(user *NexusUser) error {
var query NexusUser
err := client.Where("name = ?", user.Name).First(&query).Error
if err != nil {
if !gorm.IsRecordNotFoundError(err) {
return err
}
// not found, need create
return client.Create(user).Error
}
// already exist, do update
user.ID = query.ID
user.CreatedAt = query.CreatedAt
return client.Save(user).Error
}
func (client *DBClient) GetNexusUser(id uint64) (*NexusUser, error) {
var user NexusUser
if err := client.First(&user, id).Error; err != nil {
return nil, err
}
return &user, nil
}
func (client *DBClient) GetNexusUserByName(name string) (*NexusUser, error) {
var user NexusUser
if err := client.Where("name = ?", name).First(&user).Error; err != nil {
return nil, err
}
return &user, nil
}
func (client *DBClient) ListNexusUsers(req apistructs.NexusUserListRequest) ([]NexusUser, error) {
query := client.New()
if req.UserIDs != nil {
query = query.Where("id IN (?)", req.UserIDs)
}
if req.RepoID != nil {
query = query.Where("repo_id = ?", req.RepoID)
}
if req.PublisherID != nil {
query = query.Where("publisher_id = ?", req.PublisherID)
}
var users []NexusUser
if err := query.Find(&users).Error; err != nil {
return nil, err
}
return users, nil
}