milvus/internal/metastore/db/dao/user_role.go
jaime c9d0c157ec
Move some modules from internal to public package (#22572)
Signed-off-by: jaime <yun.zhang@zilliz.com>
2023-04-06 19:14:32 +08:00

50 lines
1.4 KiB
Go

package dao
import (
"go.uber.org/zap"
"gorm.io/gorm"
"github.com/milvus-io/milvus/internal/metastore/db/dbmodel"
"github.com/milvus-io/milvus/pkg/log"
)
type userRoleDb struct {
db *gorm.DB
}
func (u *userRoleDb) GetUserRoles(tenantID string, userID int64, roleID int64) ([]*dbmodel.UserRole, error) {
var (
userRoles []*dbmodel.UserRole
err error
)
err = u.db.Model(&dbmodel.UserRole{}).
Where(&dbmodel.UserRole{UserID: userID, RoleID: roleID}).
Where(dbmodel.GetCommonCondition(tenantID, false)).
Preload("User").Preload("Role").
Find(&userRoles).Error
if err != nil {
log.Error("fail to get user-roles", zap.String("tenant_id", tenantID), zap.Int64("userID", userID), zap.Int64("roleID", roleID), zap.Error(err))
return nil, err
}
return userRoles, nil
}
func (u *userRoleDb) Insert(in *dbmodel.UserRole) error {
err := u.db.Create(in).Error
if err != nil {
log.Error("fail to insert the user-role", zap.Any("in", in), zap.Error(err))
}
return err
}
func (u *userRoleDb) Delete(tenantID string, userID int64, roleID int64) error {
err := u.db.Model(dbmodel.UserRole{}).
Where(&dbmodel.UserRole{UserID: userID, RoleID: roleID}).
Where(dbmodel.GetCommonCondition(tenantID, false)).
Update("is_deleted", true).Error
if err != nil {
log.Error("fail to delete the user-role", zap.String("tenant_id", tenantID), zap.Int64("userID", userID), zap.Int64("roleID", roleID), zap.Error(err))
}
return err
}