feat: add save
This commit is contained in:
@@ -65,8 +65,3 @@ func (t *tenant) SetUserRole(ctx context.Context, tenantID, userID int64, role .
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Users
|
|
||||||
func (t *tenant) Users(ctx context.Context, tenantID int64) ([]*models.User, int64, error) {
|
|
||||||
return nil, 0, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
|
"quyun/v2/pkg/consts"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"go.ipao.vip/gen"
|
"go.ipao.vip/gen"
|
||||||
@@ -34,16 +35,23 @@ func (t *user) FindByUsername(ctx context.Context, username string) (*models.Use
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *user) Create(ctx context.Context, user *models.User) (*models.User, error) {
|
func (t *user) Create(ctx context.Context, user *models.User) (*models.User, error) {
|
||||||
if err := user.EncryptPassword(ctx); err != nil {
|
|
||||||
return nil, errors.Wrap(err, "encrypt user password failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := user.Create(ctx); err != nil {
|
if err := user.Create(ctx); err != nil {
|
||||||
return nil, errors.Wrapf(err, "Create user failed, %s", user.Username)
|
return nil, errors.Wrapf(err, "Create user failed, %s", user.Username)
|
||||||
}
|
}
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStatus
|
||||||
|
func (t *user) SetStatus(ctx context.Context, userID int64, status consts.UserStatus) error {
|
||||||
|
m, err := t.FindByID(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Status = status
|
||||||
|
return m.Save(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
type UserPageFilter struct {
|
type UserPageFilter struct {
|
||||||
requests.Pagination
|
requests.Pagination
|
||||||
requests.SortQueryFilter
|
requests.SortQueryFilter
|
||||||
|
|||||||
@@ -40,3 +40,5 @@ field_relate:
|
|||||||
table: users
|
table: users
|
||||||
pivot: tenant_users
|
pivot: tenant_users
|
||||||
json: users
|
json: users
|
||||||
|
join_foreign_key: tenant_id
|
||||||
|
join_references: user_id
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ type Tenant struct {
|
|||||||
ExpiredAt time.Time `gorm:"column:expired_at;type:timestamp with time zone" json:"expired_at"`
|
ExpiredAt time.Time `gorm:"column:expired_at;type:timestamp with time zone" json:"expired_at"`
|
||||||
CreatedAt time.Time `gorm:"column:created_at;type:timestamp with time zone;not null;default:now()" json:"created_at"`
|
CreatedAt time.Time `gorm:"column:created_at;type:timestamp with time zone;not null;default:now()" json:"created_at"`
|
||||||
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp with time zone;not null;default:now()" json:"updated_at"`
|
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp with time zone;not null;default:now()" json:"updated_at"`
|
||||||
Users []*User `gorm:"many2many:tenant_users" json:"users,omitempty"`
|
Users []*User `gorm:"joinForeignKey:TenantID;joinReferences:UserID;many2many:tenant_users" json:"users,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quick operations without importing query package
|
// Quick operations without importing query package
|
||||||
|
|||||||
12
backend/database/models/tenants.go
Normal file
12
backend/database/models/tenants.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.ipao.vip/gen/types"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BeforeCreate
|
||||||
|
func (m *Tenant) BeforeCreate(tx *gorm.DB) error {
|
||||||
|
m.UUID = types.NewUUIDv4()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -15,20 +15,17 @@ func (m *User) ComparePassword(ctx context.Context, password string) bool {
|
|||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *User) EncryptPassword(ctx context.Context) error {
|
|
||||||
bytes, err := bcrypt.GenerateFromPassword([]byte(m.Password), bcrypt.DefaultCost)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m.Password = string(bytes)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *User) HasRole(ctx context.Context, role consts.Role) bool {
|
func (m *User) HasRole(ctx context.Context, role consts.Role) bool {
|
||||||
return lo.Contains(m.Roles, role)
|
return lo.Contains(m.Roles, role)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeforeCreate
|
// BeforeCreate
|
||||||
func (m *User) BeforeCreate(tx *gorm.DB) error {
|
func (m *User) BeforeCreate(tx *gorm.DB) error {
|
||||||
return m.EncryptPassword(tx.Statement.Context)
|
bytes, err := bcrypt.GenerateFromPassword([]byte(m.Password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m.Password = string(bytes)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user