mirror of
https://gitee.com/goploy/goploy.git
synced 2024-12-02 20:20:32 +08:00
176 lines
4.7 KiB
Go
176 lines
4.7 KiB
Go
package model
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
)
|
|
|
|
const namespaceUserTable = "`namespace_user`"
|
|
|
|
// NamespaceUser -
|
|
type NamespaceUser struct {
|
|
ID int64 `json:"id,omitempty"`
|
|
NamespaceID int64 `json:"namespaceId,omitempty"`
|
|
NamespaceName string `json:"namespaceName,omitempty"`
|
|
UserID int64 `json:"userId,omitempty"`
|
|
UserName string `json:"userName,omitempty"`
|
|
Role string `json:"role,omitempty"`
|
|
InsertTime string `json:"insertTime,omitempty"`
|
|
UpdateTime string `json:"updateTime,omitempty"`
|
|
}
|
|
|
|
// NamespaceUsers -
|
|
type NamespaceUsers []NamespaceUser
|
|
|
|
func (nu NamespaceUser) GetUserNamespaceList() (NamespaceUsers, error) {
|
|
rows, err := sq.
|
|
Select("namespace_id, namespace.name, namespace_user.role").
|
|
From(namespaceUserTable).
|
|
Join(namespaceTable + " ON namespace_user.namespace_id = namespace.id").
|
|
Where(sq.Eq{"user_id": nu.UserID}).
|
|
RunWith(DB).
|
|
Query()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
namespaceUsers := NamespaceUsers{}
|
|
for rows.Next() {
|
|
var namespaceUser NamespaceUser
|
|
|
|
if err := rows.Scan(&namespaceUser.NamespaceID, &namespaceUser.NamespaceName, &namespaceUser.Role); err != nil {
|
|
return nil, err
|
|
}
|
|
namespaceUsers = append(namespaceUsers, namespaceUser)
|
|
}
|
|
return namespaceUsers, nil
|
|
}
|
|
|
|
func (nu NamespaceUser) GetBindUserListByNamespaceID() (NamespaceUsers, error) {
|
|
rows, err := sq.
|
|
Select("namespace_user.id, namespace_id, user_id, user.name, namespace_user.role, namespace_user.insert_time, namespace_user.update_time").
|
|
From(namespaceUserTable).
|
|
LeftJoin(userTable + " ON namespace_user.user_id = user.id").
|
|
Where(sq.Eq{"namespace_id": nu.NamespaceID}).
|
|
RunWith(DB).
|
|
Query()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
namespaceUsers := NamespaceUsers{}
|
|
for rows.Next() {
|
|
var namespaceUser NamespaceUser
|
|
|
|
if err := rows.Scan(&namespaceUser.ID, &namespaceUser.NamespaceID, &namespaceUser.UserID, &namespaceUser.UserName, &namespaceUser.Role, &namespaceUser.InsertTime, &namespaceUser.UpdateTime); err != nil {
|
|
return nil, err
|
|
}
|
|
namespaceUsers = append(namespaceUsers, namespaceUser)
|
|
}
|
|
return namespaceUsers, nil
|
|
}
|
|
|
|
func (nu NamespaceUser) GetAllUserByNamespaceID() (NamespaceUsers, error) {
|
|
rows, err := sq.
|
|
Select("user_id, user.name, namespace_user.role").
|
|
From(namespaceUserTable).
|
|
LeftJoin(userTable + " ON namespace_user.user_id = user.id").
|
|
Where(sq.Eq{"namespace_id": nu.NamespaceID}).
|
|
RunWith(DB).
|
|
Query()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
namespaceUsers := NamespaceUsers{}
|
|
for rows.Next() {
|
|
var namespaceUser NamespaceUser
|
|
|
|
if err := rows.Scan(&namespaceUser.UserID, &namespaceUser.UserName, &namespaceUser.Role); err != nil {
|
|
return nil, err
|
|
}
|
|
namespaceUsers = append(namespaceUsers, namespaceUser)
|
|
}
|
|
return namespaceUsers, nil
|
|
}
|
|
|
|
func (nu NamespaceUser) GetAllGteManagerByNamespaceID() (NamespaceUsers, error) {
|
|
rows, err := sq.
|
|
Select("user_id, role").
|
|
From(namespaceUserTable).
|
|
Where(sq.Eq{
|
|
"namespace_id": nu.NamespaceID,
|
|
"role": []string{"admin", "manager"},
|
|
}).
|
|
RunWith(DB).
|
|
Query()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
namespaceUsers := NamespaceUsers{}
|
|
for rows.Next() {
|
|
var namespaceUser NamespaceUser
|
|
|
|
if err := rows.Scan(&namespaceUser.UserID, &namespaceUser.Role); err != nil {
|
|
return nil, err
|
|
}
|
|
namespaceUsers = append(namespaceUsers, namespaceUser)
|
|
}
|
|
return namespaceUsers, nil
|
|
}
|
|
|
|
func (nu NamespaceUsers) AddMany() error {
|
|
if len(nu) == 0 {
|
|
return nil
|
|
}
|
|
builder := sq.
|
|
Replace(namespaceUserTable).
|
|
Columns("namespace_id", "user_id", "role")
|
|
|
|
for _, row := range nu {
|
|
builder = builder.Values(row.NamespaceID, row.UserID, row.Role)
|
|
}
|
|
_, err := builder.RunWith(DB).Exec()
|
|
return err
|
|
}
|
|
|
|
func (nu NamespaceUser) AddAdminByNamespaceID() error {
|
|
|
|
builder := sq.
|
|
Insert(namespaceUserTable).
|
|
Columns("namespace_id", "user_id", "role").
|
|
Select(sq.
|
|
Select(fmt.Sprintf("%d as namespace_id, id as user_id, 'admin' as role", nu.NamespaceID)).
|
|
From(userTable).
|
|
Where(sq.Eq{"super_manager": SuperManager}))
|
|
_, err := builder.RunWith(DB).Exec()
|
|
return err
|
|
}
|
|
|
|
func (nu NamespaceUser) AddAdminByUserID() error {
|
|
builder := sq.
|
|
Replace(namespaceUserTable).
|
|
Columns("namespace_id", "user_id", "role").
|
|
Select(sq.
|
|
Select(fmt.Sprintf("id as namespace_id, %d as user_id, 'admin' as role", nu.UserID)).
|
|
From(namespaceTable))
|
|
_, err := builder.RunWith(DB).Exec()
|
|
return err
|
|
}
|
|
|
|
func (nu NamespaceUser) DeleteRow() error {
|
|
_, err := sq.
|
|
Delete(namespaceUserTable).
|
|
Where(sq.Eq{"id": nu.ID}).
|
|
RunWith(DB).
|
|
Exec()
|
|
return err
|
|
}
|
|
|
|
func (nu NamespaceUser) DeleteByUserID() error {
|
|
_, err := sq.
|
|
Delete(namespaceUserTable).
|
|
Where(sq.Eq{"user_id": nu.UserID}).
|
|
RunWith(DB).
|
|
Exec()
|
|
return err
|
|
}
|