2022-04-09 21:53:37 +08:00
|
|
|
// Copyright 2022 The Goploy Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a GPLv3-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-08-04 14:28:25 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-05 19:15:04 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-08-04 20:00:21 +08:00
|
|
|
|
2020-08-04 14:28:25 +08:00
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
|
|
)
|
|
|
|
|
|
|
|
const namespaceUserTable = "`namespace_user`"
|
|
|
|
|
2020-08-15 13:38:06 +08:00
|
|
|
// NamespaceUser -
|
2020-08-04 14:28:25 +08:00
|
|
|
type NamespaceUser struct {
|
2022-04-05 19:15:04 +08:00
|
|
|
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"`
|
|
|
|
RoleID int64 `json:"roleId,omitempty"`
|
2022-04-08 15:06:52 +08:00
|
|
|
RoleName string `json:"roleName,omitempty"`
|
2022-04-05 19:15:04 +08:00
|
|
|
PermissionIDs map[int64]struct{} `json:"permissionIds,omitempty"`
|
|
|
|
InsertTime string `json:"insertTime,omitempty"`
|
|
|
|
UpdateTime string `json:"updateTime,omitempty"`
|
2020-08-04 14:28:25 +08:00
|
|
|
}
|
|
|
|
|
2020-08-15 13:38:06 +08:00
|
|
|
// NamespaceUsers -
|
2020-08-04 14:28:25 +08:00
|
|
|
type NamespaceUsers []NamespaceUser
|
|
|
|
|
2022-01-28 10:57:07 +08:00
|
|
|
func (nu NamespaceUser) GetUserNamespaceList() (NamespaceUsers, error) {
|
|
|
|
rows, err := sq.
|
2022-04-08 15:06:52 +08:00
|
|
|
Select("namespace_id, namespace.name").
|
2022-01-28 10:57:07 +08:00
|
|
|
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
|
|
|
|
|
2022-04-08 15:06:52 +08:00
|
|
|
if err := rows.Scan(&namespaceUser.NamespaceID, &namespaceUser.NamespaceName); err != nil {
|
2022-01-28 10:57:07 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
namespaceUsers = append(namespaceUsers, namespaceUser)
|
|
|
|
}
|
|
|
|
return namespaceUsers, nil
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:28:25 +08:00
|
|
|
func (nu NamespaceUser) GetBindUserListByNamespaceID() (NamespaceUsers, error) {
|
|
|
|
rows, err := sq.
|
2022-04-08 15:06:52 +08:00
|
|
|
Select(
|
|
|
|
"namespace_user.id",
|
|
|
|
"namespace_user.user_id",
|
|
|
|
"user.name",
|
|
|
|
"namespace_user.role_id",
|
|
|
|
"IFNULL(role.name, '')",
|
|
|
|
"namespace_user.insert_time",
|
|
|
|
"namespace_user.update_time",
|
|
|
|
).
|
2020-08-04 14:28:25 +08:00
|
|
|
From(namespaceUserTable).
|
|
|
|
LeftJoin(userTable + " ON namespace_user.user_id = user.id").
|
2022-04-08 15:06:52 +08:00
|
|
|
LeftJoin(roleTable + " ON namespace_user.role_id = role.id").
|
2020-08-04 14:28:25 +08:00
|
|
|
Where(sq.Eq{"namespace_id": nu.NamespaceID}).
|
|
|
|
RunWith(DB).
|
|
|
|
Query()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
namespaceUsers := NamespaceUsers{}
|
|
|
|
for rows.Next() {
|
|
|
|
var namespaceUser NamespaceUser
|
|
|
|
|
2022-04-08 15:06:52 +08:00
|
|
|
if err := rows.Scan(
|
|
|
|
&namespaceUser.ID,
|
|
|
|
&namespaceUser.UserID,
|
|
|
|
&namespaceUser.UserName,
|
|
|
|
&namespaceUser.RoleID,
|
|
|
|
&namespaceUser.RoleName,
|
|
|
|
&namespaceUser.InsertTime,
|
|
|
|
&namespaceUser.UpdateTime,
|
|
|
|
); err != nil {
|
2020-08-04 14:28:25 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
namespaceUsers = append(namespaceUsers, namespaceUser)
|
|
|
|
}
|
|
|
|
return namespaceUsers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nu NamespaceUser) GetAllUserByNamespaceID() (NamespaceUsers, error) {
|
|
|
|
rows, err := sq.
|
2022-04-08 15:06:52 +08:00
|
|
|
Select("user_id, user.name, namespace_user.role_id").
|
2020-08-04 14:28:25 +08:00
|
|
|
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
|
|
|
|
|
2022-04-08 15:06:52 +08:00
|
|
|
if err := rows.Scan(&namespaceUser.UserID, &namespaceUser.UserName, &namespaceUser.RoleID); err != nil {
|
2020-08-04 14:28:25 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
namespaceUsers = append(namespaceUsers, namespaceUser)
|
|
|
|
}
|
|
|
|
return namespaceUsers, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 19:15:04 +08:00
|
|
|
func (nu NamespaceUser) GetDataByUserNamespace() (NamespaceUser, error) {
|
|
|
|
var namespaceUser NamespaceUser
|
|
|
|
var permissionIDs string
|
2022-04-10 12:19:17 +08:00
|
|
|
err := sq.Select("namespace_user.role_id, IFNULL(GROUP_CONCAT(permission_id), '')").
|
2022-04-05 19:15:04 +08:00
|
|
|
From(namespaceUserTable).
|
2022-04-10 12:19:17 +08:00
|
|
|
LeftJoin(fmt.Sprintf("%s ON %[1]s.role_id = %s.role_id", rolePermissionTable, namespaceUserTable)).
|
2022-04-05 19:15:04 +08:00
|
|
|
Where(sq.Eq{"user_id": nu.UserID, "namespace_id": nu.NamespaceID}).
|
|
|
|
GroupBy("namespace_user.role_id").
|
|
|
|
RunWith(DB).
|
|
|
|
QueryRow().
|
|
|
|
Scan(&namespaceUser.RoleID, &permissionIDs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return namespaceUser, err
|
|
|
|
}
|
|
|
|
namespaceUser.PermissionIDs = map[int64]struct{}{}
|
2022-04-10 12:19:17 +08:00
|
|
|
if permissionIDs == "" {
|
|
|
|
return namespaceUser, err
|
|
|
|
}
|
2022-04-05 19:15:04 +08:00
|
|
|
for _, permissionID := range strings.Split(permissionIDs, ",") {
|
|
|
|
v, err := strconv.ParseInt(permissionID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return namespaceUser, err
|
|
|
|
}
|
|
|
|
namespaceUser.PermissionIDs[v] = struct{}{}
|
|
|
|
}
|
|
|
|
return namespaceUser, err
|
|
|
|
}
|
|
|
|
|
2022-04-10 15:26:46 +08:00
|
|
|
func (nu NamespaceUser) GetDataByRoleID() (NamespaceUser, error) {
|
|
|
|
var namespaceUser NamespaceUser
|
|
|
|
err := sq.Select("id, namespace_id, role_id").
|
|
|
|
From(namespaceUserTable).
|
|
|
|
Where(sq.Eq{"role_id": nu.RoleID}).
|
|
|
|
RunWith(DB).
|
|
|
|
QueryRow().
|
|
|
|
Scan(&namespaceUser.ID, &namespaceUser.NamespaceID, &namespaceUser.RoleID)
|
|
|
|
if err != nil {
|
|
|
|
return namespaceUser, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return namespaceUser, err
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:28:25 +08:00
|
|
|
func (nu NamespaceUsers) AddMany() error {
|
|
|
|
if len(nu) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
builder := sq.
|
|
|
|
Replace(namespaceUserTable).
|
2022-04-05 19:15:04 +08:00
|
|
|
Columns("namespace_id", "user_id", "role_id")
|
2020-08-04 14:28:25 +08:00
|
|
|
|
|
|
|
for _, row := range nu {
|
2022-04-05 19:15:04 +08:00
|
|
|
builder = builder.Values(row.NamespaceID, row.UserID, row.RoleID)
|
2020-08-04 14:28:25 +08:00
|
|
|
}
|
|
|
|
_, err := builder.RunWith(DB).Exec()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nu NamespaceUser) AddAdminByNamespaceID() error {
|
|
|
|
|
|
|
|
builder := sq.
|
|
|
|
Insert(namespaceUserTable).
|
2022-04-08 15:06:52 +08:00
|
|
|
Columns("namespace_id", "user_id", "role_id").
|
2020-08-04 14:28:25 +08:00
|
|
|
Select(sq.
|
2022-04-08 15:06:52 +08:00
|
|
|
Select(fmt.Sprintf("%d as namespace_id, id as user_id, 0 as role_id", nu.NamespaceID)).
|
2020-08-04 14:28:25 +08:00
|
|
|
From(userTable).
|
|
|
|
Where(sq.Eq{"super_manager": SuperManager}))
|
|
|
|
_, err := builder.RunWith(DB).Exec()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nu NamespaceUser) AddAdminByUserID() error {
|
|
|
|
builder := sq.
|
|
|
|
Replace(namespaceUserTable).
|
2022-04-08 15:06:52 +08:00
|
|
|
Columns("namespace_id", "user_id", "role_id").
|
2020-08-04 14:28:25 +08:00
|
|
|
Select(sq.
|
2022-04-08 15:06:52 +08:00
|
|
|
Select(fmt.Sprintf("id as namespace_id, %d as user_id, 0 as role_id", nu.UserID)).
|
2020-08-04 14:28:25 +08:00
|
|
|
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
|
|
|
|
}
|