mirror of https://github.com/go-sonic/sonic.git
feat: add manage apis for ApplicationPassword
parent
106e6ffa5f
commit
56157ba32a
@ -0,0 +1,359 @@
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
|
||||
package dal
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/schema"
|
||||
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gen/field"
|
||||
|
||||
"gorm.io/plugin/dbresolver"
|
||||
|
||||
"github.com/go-sonic/sonic/model/entity"
|
||||
)
|
||||
|
||||
func newApplicationPassword(db *gorm.DB, opts ...gen.DOOption) applicationPassword {
|
||||
_applicationPassword := applicationPassword{}
|
||||
|
||||
_applicationPassword.applicationPasswordDo.UseDB(db, opts...)
|
||||
_applicationPassword.applicationPasswordDo.UseModel(&entity.ApplicationPassword{})
|
||||
|
||||
tableName := _applicationPassword.applicationPasswordDo.TableName()
|
||||
_applicationPassword.ALL = field.NewAsterisk(tableName)
|
||||
_applicationPassword.ID = field.NewInt32(tableName, "id")
|
||||
_applicationPassword.CreateTime = field.NewTime(tableName, "create_time")
|
||||
_applicationPassword.UpdateTime = field.NewTime(tableName, "update_time")
|
||||
_applicationPassword.Name = field.NewString(tableName, "name")
|
||||
_applicationPassword.Password = field.NewString(tableName, "password")
|
||||
_applicationPassword.UserID = field.NewInt32(tableName, "user_id")
|
||||
_applicationPassword.LastActivateTime = field.NewTime(tableName, "last_activate_time")
|
||||
_applicationPassword.LastActivateIP = field.NewString(tableName, "last_activate_ip")
|
||||
|
||||
_applicationPassword.fillFieldMap()
|
||||
|
||||
return _applicationPassword
|
||||
}
|
||||
|
||||
type applicationPassword struct {
|
||||
applicationPasswordDo applicationPasswordDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Int32
|
||||
CreateTime field.Time
|
||||
UpdateTime field.Time
|
||||
Name field.String
|
||||
Password field.String
|
||||
UserID field.Int32
|
||||
LastActivateTime field.Time
|
||||
LastActivateIP field.String
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
func (a applicationPassword) Table(newTableName string) *applicationPassword {
|
||||
a.applicationPasswordDo.UseTable(newTableName)
|
||||
return a.updateTableName(newTableName)
|
||||
}
|
||||
|
||||
func (a applicationPassword) As(alias string) *applicationPassword {
|
||||
a.applicationPasswordDo.DO = *(a.applicationPasswordDo.As(alias).(*gen.DO))
|
||||
return a.updateTableName(alias)
|
||||
}
|
||||
|
||||
func (a *applicationPassword) updateTableName(table string) *applicationPassword {
|
||||
a.ALL = field.NewAsterisk(table)
|
||||
a.ID = field.NewInt32(table, "id")
|
||||
a.CreateTime = field.NewTime(table, "create_time")
|
||||
a.UpdateTime = field.NewTime(table, "update_time")
|
||||
a.Name = field.NewString(table, "name")
|
||||
a.Password = field.NewString(table, "password")
|
||||
a.UserID = field.NewInt32(table, "user_id")
|
||||
a.LastActivateTime = field.NewTime(table, "last_activate_time")
|
||||
a.LastActivateIP = field.NewString(table, "last_activate_ip")
|
||||
|
||||
a.fillFieldMap()
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *applicationPassword) WithContext(ctx context.Context) *applicationPasswordDo {
|
||||
return a.applicationPasswordDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (a applicationPassword) TableName() string { return a.applicationPasswordDo.TableName() }
|
||||
|
||||
func (a applicationPassword) Alias() string { return a.applicationPasswordDo.Alias() }
|
||||
|
||||
func (a applicationPassword) Columns(cols ...field.Expr) gen.Columns {
|
||||
return a.applicationPasswordDo.Columns(cols...)
|
||||
}
|
||||
|
||||
func (a *applicationPassword) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
_f, ok := a.fieldMap[fieldName]
|
||||
if !ok || _f == nil {
|
||||
return nil, false
|
||||
}
|
||||
_oe, ok := _f.(field.OrderExpr)
|
||||
return _oe, ok
|
||||
}
|
||||
|
||||
func (a *applicationPassword) fillFieldMap() {
|
||||
a.fieldMap = make(map[string]field.Expr, 8)
|
||||
a.fieldMap["id"] = a.ID
|
||||
a.fieldMap["create_time"] = a.CreateTime
|
||||
a.fieldMap["update_time"] = a.UpdateTime
|
||||
a.fieldMap["name"] = a.Name
|
||||
a.fieldMap["password"] = a.Password
|
||||
a.fieldMap["user_id"] = a.UserID
|
||||
a.fieldMap["last_activate_time"] = a.LastActivateTime
|
||||
a.fieldMap["last_activate_ip"] = a.LastActivateIP
|
||||
}
|
||||
|
||||
func (a applicationPassword) clone(db *gorm.DB) applicationPassword {
|
||||
a.applicationPasswordDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
return a
|
||||
}
|
||||
|
||||
func (a applicationPassword) replaceDB(db *gorm.DB) applicationPassword {
|
||||
a.applicationPasswordDo.ReplaceDB(db)
|
||||
return a
|
||||
}
|
||||
|
||||
type applicationPasswordDo struct{ gen.DO }
|
||||
|
||||
func (a applicationPasswordDo) Debug() *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Debug())
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) WithContext(ctx context.Context) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.WithContext(ctx))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) ReadDB() *applicationPasswordDo {
|
||||
return a.Clauses(dbresolver.Read)
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) WriteDB() *applicationPasswordDo {
|
||||
return a.Clauses(dbresolver.Write)
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Session(config *gorm.Session) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Session(config))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Clauses(conds ...clause.Expression) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Clauses(conds...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Returning(value interface{}, columns ...string) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Returning(value, columns...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Not(conds ...gen.Condition) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Not(conds...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Or(conds ...gen.Condition) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Or(conds...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Select(conds ...field.Expr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Select(conds...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Where(conds ...gen.Condition) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Where(conds...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Order(conds ...field.Expr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Order(conds...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Distinct(cols ...field.Expr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Distinct(cols...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Omit(cols ...field.Expr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Omit(cols...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Join(table schema.Tabler, on ...field.Expr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Join(table, on...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) LeftJoin(table schema.Tabler, on ...field.Expr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.LeftJoin(table, on...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) RightJoin(table schema.Tabler, on ...field.Expr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.RightJoin(table, on...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Group(cols ...field.Expr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Group(cols...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Having(conds ...gen.Condition) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Having(conds...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Limit(limit int) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Limit(limit))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Offset(offset int) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Offset(offset))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Scopes(funcs...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Unscoped() *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Unscoped())
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Create(values ...*entity.ApplicationPassword) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return a.DO.Create(values)
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) CreateInBatches(values []*entity.ApplicationPassword, batchSize int) error {
|
||||
return a.DO.CreateInBatches(values, batchSize)
|
||||
}
|
||||
|
||||
// Save : !!! underlying implementation is different with GORM
|
||||
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
|
||||
func (a applicationPasswordDo) Save(values ...*entity.ApplicationPassword) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return a.DO.Save(values)
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) First() (*entity.ApplicationPassword, error) {
|
||||
if result, err := a.DO.First(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*entity.ApplicationPassword), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Take() (*entity.ApplicationPassword, error) {
|
||||
if result, err := a.DO.Take(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*entity.ApplicationPassword), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Last() (*entity.ApplicationPassword, error) {
|
||||
if result, err := a.DO.Last(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*entity.ApplicationPassword), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Find() ([]*entity.ApplicationPassword, error) {
|
||||
result, err := a.DO.Find()
|
||||
return result.([]*entity.ApplicationPassword), err
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*entity.ApplicationPassword, err error) {
|
||||
buf := make([]*entity.ApplicationPassword, 0, batchSize)
|
||||
err = a.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
|
||||
defer func() { results = append(results, buf...) }()
|
||||
return fc(tx, batch)
|
||||
})
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) FindInBatches(result *[]*entity.ApplicationPassword, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||
return a.DO.FindInBatches(result, batchSize, fc)
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Attrs(attrs ...field.AssignExpr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Attrs(attrs...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Assign(attrs ...field.AssignExpr) *applicationPasswordDo {
|
||||
return a.withDO(a.DO.Assign(attrs...))
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Joins(fields ...field.RelationField) *applicationPasswordDo {
|
||||
for _, _f := range fields {
|
||||
a = *a.withDO(a.DO.Joins(_f))
|
||||
}
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Preload(fields ...field.RelationField) *applicationPasswordDo {
|
||||
for _, _f := range fields {
|
||||
a = *a.withDO(a.DO.Preload(_f))
|
||||
}
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) FirstOrInit() (*entity.ApplicationPassword, error) {
|
||||
if result, err := a.DO.FirstOrInit(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*entity.ApplicationPassword), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) FirstOrCreate() (*entity.ApplicationPassword, error) {
|
||||
if result, err := a.DO.FirstOrCreate(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*entity.ApplicationPassword), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) FindByPage(offset int, limit int) (result []*entity.ApplicationPassword, count int64, err error) {
|
||||
result, err = a.Offset(offset).Limit(limit).Find()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if size := len(result); 0 < limit && 0 < size && size < limit {
|
||||
count = int64(size + offset)
|
||||
return
|
||||
}
|
||||
|
||||
count, err = a.Offset(-1).Limit(-1).Count()
|
||||
return
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
|
||||
count, err = a.Count()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = a.Offset(offset).Limit(limit).Scan(result)
|
||||
return
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Scan(result interface{}) (err error) {
|
||||
return a.DO.Scan(result)
|
||||
}
|
||||
|
||||
func (a applicationPasswordDo) Delete(models ...*entity.ApplicationPassword) (result gen.ResultInfo, err error) {
|
||||
return a.DO.Delete(models)
|
||||
}
|
||||
|
||||
func (a *applicationPasswordDo) withDO(do gen.Dao) *applicationPasswordDo {
|
||||
a.DO = *do.(*gen.DO)
|
||||
return a
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/go-sonic/sonic/handler/trans"
|
||||
"github.com/go-sonic/sonic/model/param"
|
||||
"github.com/go-sonic/sonic/service"
|
||||
"github.com/go-sonic/sonic/util"
|
||||
"github.com/go-sonic/sonic/util/xerr"
|
||||
)
|
||||
|
||||
type ApplicationPasswordHandler struct {
|
||||
ApplicationPasswordService service.ApplicationPasswordService
|
||||
}
|
||||
|
||||
func NewApplicationPasswordHandler(applicationPasswordService service.ApplicationPasswordService) *ApplicationPasswordHandler {
|
||||
return &ApplicationPasswordHandler{
|
||||
ApplicationPasswordService: applicationPasswordService,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ApplicationPasswordHandler) Create(ctx *gin.Context) (interface{}, error) {
|
||||
appPwdParam, err := parseAppPwdParam(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return a.ApplicationPasswordService.CreatePwd(ctx, appPwdParam)
|
||||
}
|
||||
|
||||
func (a *ApplicationPasswordHandler) Delete(ctx *gin.Context) (interface{}, error) {
|
||||
name, err := util.ParamString(ctx, "name")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
appPwdParam := ¶m.ApplicationPasswordParam{Name: name}
|
||||
|
||||
return nil, a.ApplicationPasswordService.DeletePwd(ctx, appPwdParam)
|
||||
}
|
||||
|
||||
func (a *ApplicationPasswordHandler) List(ctx *gin.Context) (interface{}, error) {
|
||||
return a.ApplicationPasswordService.List(ctx)
|
||||
}
|
||||
|
||||
func parseAppPwdParam(ctx *gin.Context) (*param.ApplicationPasswordParam, error) {
|
||||
var appPwdParam param.ApplicationPasswordParam
|
||||
err := ctx.ShouldBindJSON(&appPwdParam)
|
||||
if err != nil {
|
||||
e := validator.ValidationErrors{}
|
||||
if errors.As(err, &e) {
|
||||
return nil, xerr.WithStatus(e, xerr.StatusBadRequest).WithMsg(trans.Translate(e))
|
||||
}
|
||||
return nil, xerr.WithStatus(err, xerr.StatusBadRequest)
|
||||
}
|
||||
return &appPwdParam, nil
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package dto
|
||||
|
||||
type ApplicationPasswordDTO struct {
|
||||
Name string `json:"name"`
|
||||
Password string `json:"password"`
|
||||
LastActivateTime int64 `json:"last_activate_time"`
|
||||
LastActivateIp string `json:"last_activate_ip"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const TableNameApplicationPassword = "application_password"
|
||||
|
||||
// ApplicationPassword mapped from table <application_password>
|
||||
type ApplicationPassword struct {
|
||||
ID *int32 `gorm:"column:id;type:INTEGER" json:"id"`
|
||||
CreateTime time.Time `gorm:"column:create_time;type:datetime;not null" json:"create_time"`
|
||||
UpdateTime *time.Time `gorm:"column:update_time;type:datetime" json:"update_time"`
|
||||
Name string `gorm:"column:name;type:varchar(32);not null" json:"name"`
|
||||
Password string `gorm:"column:password;type:varchar(256);not null" json:"password"`
|
||||
UserID int32 `gorm:"column:user_id;type:integer;not null" json:"user_id"`
|
||||
LastActivateTime *time.Time `gorm:"column:last_activate_time;type:datetime" json:"last_activate_time"`
|
||||
LastActivateIP string `gorm:"column:last_activate_ip;type:varchar(128);not null;default:'' not null" json:"last_activate_ip"`
|
||||
}
|
||||
|
||||
// TableName ApplicationPassword's table name
|
||||
func (*ApplicationPassword) TableName() string {
|
||||
return TableNameApplicationPassword
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package param
|
||||
|
||||
type ApplicationPasswordParam struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-sonic/sonic/model/dto"
|
||||
"github.com/go-sonic/sonic/model/param"
|
||||
)
|
||||
|
||||
type ApplicationPasswordService interface {
|
||||
CreatePwd(ctx context.Context, appPwdParam *param.ApplicationPasswordParam) (*dto.ApplicationPasswordDTO, error)
|
||||
DeletePwd(ctx context.Context, appPwdParam *param.ApplicationPasswordParam) error
|
||||
List(ctx context.Context) ([]*dto.ApplicationPasswordDTO, error)
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-sonic/sonic/model/dto"
|
||||
"github.com/go-sonic/sonic/model/entity"
|
||||
"github.com/go-sonic/sonic/util"
|
||||
"github.com/go-sonic/sonic/util/xerr"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-sonic/sonic/dal"
|
||||
"github.com/go-sonic/sonic/model/param"
|
||||
"github.com/go-sonic/sonic/service"
|
||||
)
|
||||
|
||||
type applicationPasswordServiceImpl struct {
|
||||
OptionService service.OptionService
|
||||
AuthenticateService service.AuthenticateService
|
||||
}
|
||||
|
||||
func NewApplicationPasswordService(optionService service.OptionService, authenticateService service.AuthenticateService) service.ApplicationPasswordService {
|
||||
return &applicationPasswordServiceImpl{
|
||||
OptionService: optionService,
|
||||
AuthenticateService: authenticateService,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *applicationPasswordServiceImpl) CreatePwd(ctx context.Context, param *param.ApplicationPasswordParam) (*dto.ApplicationPasswordDTO, error) {
|
||||
var err error
|
||||
appPwdDTO := &dto.ApplicationPasswordDTO{
|
||||
Name: param.Name,
|
||||
}
|
||||
|
||||
user, err := MustGetAuthorizedUser(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = dal.GetQueryByCtx(ctx).Transaction(func(tx *dal.Query) error {
|
||||
appPwdDAL := tx.ApplicationPassword
|
||||
|
||||
count, err := appPwdDAL.WithContext(ctx).Select().Where(appPwdDAL.Name.Eq(param.Name), appPwdDAL.UserID.Eq(user.ID)).Count()
|
||||
if err != nil {
|
||||
return WrapDBErr(err)
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return xerr.BadParam.New("").WithMsg("名称已经存在(Application password name already exists)").WithStatus(xerr.StatusBadRequest)
|
||||
}
|
||||
|
||||
token := util.GenUUIDWithOutDash()
|
||||
tokenMd5 := util.Md5(token)
|
||||
// pass claim token to frond, but save in db after md5
|
||||
appPwdDTO.Password = token
|
||||
|
||||
currentTime := time.Now()
|
||||
|
||||
appPwdEntity := &entity.ApplicationPassword{
|
||||
CreateTime: currentTime,
|
||||
UpdateTime: ¤tTime,
|
||||
Name: param.Name,
|
||||
Password: tokenMd5,
|
||||
UserID: user.ID,
|
||||
LastActivateTime: nil,
|
||||
LastActivateIP: "",
|
||||
}
|
||||
err = appPwdDAL.WithContext(ctx).Create(appPwdEntity)
|
||||
if err != nil {
|
||||
return WrapDBErr(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return appPwdDTO, nil
|
||||
}
|
||||
|
||||
func (a *applicationPasswordServiceImpl) DeletePwd(ctx context.Context, appPwdParam *param.ApplicationPasswordParam) error {
|
||||
var err error
|
||||
user, err := MustGetAuthorizedUser(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if appPwdParam == nil || len(strings.TrimSpace(appPwdParam.Name)) == 0 {
|
||||
return xerr.BadParam.New("name参数为空").WithStatus(xerr.StatusBadRequest).
|
||||
WithMsg("name 参数不能为空")
|
||||
}
|
||||
|
||||
appPwdParam.Name = strings.TrimSpace(appPwdParam.Name)
|
||||
|
||||
appPwdDAL := dal.GetQueryByCtx(ctx).ApplicationPassword
|
||||
if _, err = appPwdDAL.WithContext(ctx).Where(appPwdDAL.UserID.Eq(user.ID), appPwdDAL.Name.Eq(appPwdParam.Name)).Delete(); err != nil {
|
||||
return WrapDBErr(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *applicationPasswordServiceImpl) List(ctx context.Context) ([]*dto.ApplicationPasswordDTO, error) {
|
||||
user, err := MustGetAuthorizedUser(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
appPwdDAL := dal.GetQueryByCtx(ctx).ApplicationPassword
|
||||
entities, err := appPwdDAL.WithContext(ctx).Where(appPwdDAL.UserID.Eq(user.ID)).Find()
|
||||
if err != nil {
|
||||
return nil, WrapDBErr(err)
|
||||
}
|
||||
|
||||
appPwdDTOList := make([]*dto.ApplicationPasswordDTO, len(entities))
|
||||
|
||||
for _, appPwdEntity := range entities {
|
||||
var lastActivateTime int64
|
||||
if appPwdEntity.LastActivateTime == nil {
|
||||
lastActivateTime = appPwdEntity.LastActivateTime.Unix()
|
||||
}
|
||||
appPwdDTO := &dto.ApplicationPasswordDTO{
|
||||
Name: appPwdEntity.Name,
|
||||
LastActivateIp: appPwdEntity.LastActivateIP,
|
||||
LastActivateTime: lastActivateTime,
|
||||
CreateTime: appPwdEntity.CreateTime.Unix(),
|
||||
}
|
||||
appPwdDTOList = append(appPwdDTOList, appPwdDTO)
|
||||
}
|
||||
|
||||
return appPwdDTOList, nil
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func Md5(s string) string {
|
||||
d := []byte(s)
|
||||
m := md5.New()
|
||||
m.Write(d)
|
||||
return hex.EncodeToString(m.Sum(nil))
|
||||
}
|
Loading…
Reference in New Issue