feat: oauth_provider 小模块完成。

main
NoahLan 1 year ago
parent e7af733d58
commit f2e4af32a5

@ -2,6 +2,9 @@ package oauth_provider
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -21,7 +24,27 @@ func NewCreateOauthProviderLogic(ctx context.Context, svcCtx *svc.ServiceContext
// OauthProvider management // OauthProvider management
func (l *CreateOauthProviderLogic) CreateOauthProvider(in *core.OauthProviderInfo) (*core.BaseIDResp, error) { func (l *CreateOauthProviderLogic) CreateOauthProvider(in *core.OauthProviderInfo) (*core.BaseIDResp, error) {
// todo: add your logic here and delete this line create := l.svcCtx.DB.OauthProvider.Create().
SetName(in.Name).
return &core.BaseIDResp{}, nil SetClientID(in.ClientId).
SetClientSecret(in.ClientSecret).
SetRedirectURL(in.RedirectUrl).
SetScopes(in.Scopes).
SetAuthURL(in.AuthUrl).
SetTokenURL(in.TokenUrl).
SetInfoURL(in.InfoUrl).
SetDescription(in.Description).
SetSystem(in.System)
if in.ClientId != "" && in.ClientSecret != "" {
create.SetInit(true)
}
result, err := create.Save(l.ctx)
if err != nil {
return nil, hander.HandleEntErr(err, in)
}
return &core.BaseIDResp{
ID: result.ID,
Code: code.StatusOK,
Msg: msg.CreateSuccess,
}, nil
} }

@ -2,6 +2,10 @@ package oauth_provider
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/oauthprovider"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -20,7 +24,16 @@ func NewDeleteOauthProviderLogic(ctx context.Context, svcCtx *svc.ServiceContext
} }
func (l *DeleteOauthProviderLogic) DeleteOauthProvider(in *core.IDsReq) (*core.BaseResp, error) { func (l *DeleteOauthProviderLogic) DeleteOauthProvider(in *core.IDsReq) (*core.BaseResp, error) {
// todo: add your logic here and delete this line // 仅允许删除非系统内置数据
_, err := l.svcCtx.DB.OauthProvider.Delete().
return &core.BaseResp{}, nil Where(oauthprovider.IDIn(in.Ids...)).
Where(oauthprovider.SystemEQ(false)).
Exec(l.ctx)
if err != nil {
return nil, hander.HandleEntErr(err, in)
}
return &core.BaseResp{
Code: code.StatusOK,
Msg: msg.DeleteSuccess,
}, nil
} }

@ -2,6 +2,11 @@ package oauth_provider
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/oauthprovider"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -20,7 +25,50 @@ func NewGetOauthProviderListLogic(ctx context.Context, svcCtx *svc.ServiceContex
} }
func (l *GetOauthProviderListLogic) GetOauthProviderList(in *core.OauthProviderReq) (*core.OauthProviderListResp, error) { func (l *GetOauthProviderListLogic) GetOauthProviderList(in *core.OauthProviderReq) (*core.OauthProviderListResp, error) {
// todo: add your logic here and delete this line var predicates []predicate.OauthProvider
if in.Name != "" {
predicates = append(predicates, oauthprovider.NameContains(in.Name))
}
if in.ClientId != "" {
predicates = append(predicates, oauthprovider.ClientIDContains(in.ClientId))
}
if in.System != nil {
predicates = append(predicates, oauthprovider.SystemEQ(in.GetSystem()))
}
if in.Init != nil {
predicates = append(predicates, oauthprovider.InitEQ(in.GetInit()))
}
if len(predicates) == 0 {
return &core.OauthProviderListResp{}, nil
}
return &core.OauthProviderListResp{}, nil query := l.svcCtx.DB.OauthProvider.Query().Where(predicates...)
var (
pagination *core.Pagination
list []*ent.OauthProvider
err error
)
if in.Page != nil {
result, pageErr := query.Page(l.ctx, in.Page.Current, in.Page.Size)
if pageErr == nil {
pagination = utils.ConvertPaginationDb(result.PageDetails)
list = result.List
} else {
err = pageErr
}
} else {
list, err = query.All(l.ctx)
}
if err != nil {
return nil, hander.HandleEntErr(err, in)
}
resp := &core.OauthProviderListResp{
Page: pagination,
Data: make([]*core.OauthProviderInfo, len(list)),
}
for i, v := range list {
resp.Data[i] = l.svcCtx.Convert.ConvertOAuthProviderRpc(l.ctx, v)
}
return resp, nil
} }

@ -2,6 +2,9 @@ package oauth_provider
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/oauthprovider"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -20,7 +23,35 @@ func NewGetOauthProviderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
} }
func (l *GetOauthProviderLogic) GetOauthProvider(in *core.OauthProviderReq) (*core.OauthProviderInfo, error) { func (l *GetOauthProviderLogic) GetOauthProvider(in *core.OauthProviderReq) (*core.OauthProviderInfo, error) {
// todo: add your logic here and delete this line var predicates []predicate.OauthProvider
if in.ID != 0 {
predicates = append(predicates, oauthprovider.IDEQ(in.ID))
goto Query
}
if in.ClientId != "" {
predicates = append(predicates, oauthprovider.ClientIDEQ(in.ClientId))
goto Query
}
if in.Name != "" {
predicates = append(predicates, oauthprovider.NameContains(in.Name))
}
if in.System != nil {
predicates = append(predicates, oauthprovider.SystemEQ(in.GetSystem()))
}
if in.Init != nil {
predicates = append(predicates, oauthprovider.InitEQ(in.GetInit()))
}
if len(predicates) == 0 {
return l.svcCtx.Convert.ConvertOAuthProviderRpc(l.ctx, nil), nil
}
Query:
dbData, err := l.svcCtx.DB.OauthProvider.Query().
Where(predicates...).
First(l.ctx)
if err != nil {
return nil, hander.HandleEntErr(err, in)
}
return &core.OauthProviderInfo{}, nil resp := l.svcCtx.Convert.ConvertOAuthProviderRpc(l.ctx, dbData)
return resp, nil
} }

@ -2,6 +2,10 @@ package oauth_provider
import ( import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/oauthprovider"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
@ -20,7 +24,27 @@ func NewUpdateOauthProviderLogic(ctx context.Context, svcCtx *svc.ServiceContext
} }
func (l *UpdateOauthProviderLogic) UpdateOauthProvider(in *core.OauthProviderInfo) (*core.BaseResp, error) { func (l *UpdateOauthProviderLogic) UpdateOauthProvider(in *core.OauthProviderInfo) (*core.BaseResp, error) {
// todo: add your logic here and delete this line update := l.svcCtx.DB.OauthProvider.Update().
Where(oauthprovider.IDEQ(in.ID)).
SetNotEmptyName(in.Name).
SetNotEmptyClientID(in.ClientId).
SetNotEmptyClientSecret(in.ClientSecret).
SetNotEmptyRedirectURL(in.RedirectUrl).
SetNotEmptyScopes(in.Scopes).
SetNotEmptyAuthURL(in.AuthUrl).
SetNotEmptyTokenURL(in.TokenUrl).
SetNotEmptyInfoURL(in.InfoUrl).
SetNotEmptyDescription(in.Description)
if in.ClientId != "" && in.ClientSecret != "" {
update.SetInit(true)
}
err := update.Exec(l.ctx)
if err != nil {
return nil, hander.HandleEntErr(err, in)
}
return &core.BaseResp{}, nil return &core.BaseResp{
Code: code.StatusOK,
Msg: msg.UpdateSuccess,
}, nil
} }

@ -4,8 +4,8 @@ import (
"context" "context"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent"
"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
"git.noahlan.cn/noahlan/ntool/ntime"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"time"
) )
type Convert struct { type Convert struct {
@ -24,8 +24,8 @@ func (c *Convert) ConvertRoleRpc(_ context.Context, v *ent.Role) *core.RoleInfo
} }
var tmp core.RoleInfo var tmp core.RoleInfo
_ = copier.Copy(&tmp, v) _ = copier.Copy(&tmp, v)
tmp.CreatedAt = v.CreatedAt.Format(time.DateTime) tmp.CreatedAt = ntime.Format(v.CreatedAt)
tmp.UpdatedAt = v.UpdatedAt.Format(time.DateTime) tmp.UpdatedAt = ntime.Format(v.UpdatedAt)
tmp.Status = v.Status.String() tmp.Status = v.Status.String()
return &tmp return &tmp
@ -38,8 +38,8 @@ func (c *Convert) ConvertUserRpc(ctx context.Context, v *ent.User) *core.UserInf
var tmp core.UserInfo var tmp core.UserInfo
_ = copier.Copy(&tmp, v) _ = copier.Copy(&tmp, v)
tmp.Status = v.Status.String() tmp.Status = v.Status.String()
tmp.CreatedAt = v.CreatedAt.Format(time.DateTime) tmp.CreatedAt = ntime.Format(v.CreatedAt)
tmp.UpdatedAt = v.UpdatedAt.Format(time.DateTime) tmp.UpdatedAt = ntime.Format(v.UpdatedAt)
// Metadata // Metadata
if len(v.Edges.Metas) > 0 { if len(v.Edges.Metas) > 0 {
@ -83,6 +83,9 @@ func (c *Convert) ConvertLoginRecordRpc(_ context.Context, v *ent.LoginRecord) *
} }
var tmp core.LoginRecordInfo var tmp core.LoginRecordInfo
_ = copier.Copy(&tmp, v) _ = copier.Copy(&tmp, v)
tmp.CreatedAt = ntime.Format(v.CreatedAt)
tmp.UpdatedAt = ntime.Format(v.UpdatedAt)
tmp.UserId = v.UserID tmp.UserId = v.UserID
tmp.LastLoginUa = v.LastLoginUA tmp.LastLoginUa = v.LastLoginUA
return &tmp return &tmp
@ -94,6 +97,9 @@ func (c *Convert) ConvertDepartmentRpc(ctx context.Context, v *ent.Department) *
} }
var tmp core.DepartmentInfo var tmp core.DepartmentInfo
_ = copier.Copy(&tmp, v) _ = copier.Copy(&tmp, v)
tmp.CreatedAt = ntime.Format(v.CreatedAt)
tmp.UpdatedAt = ntime.Format(v.UpdatedAt)
tmp.LeaderId = v.LeaderID tmp.LeaderId = v.LeaderID
tmp.ParentId = v.ParentID tmp.ParentId = v.ParentID
// relationship // relationship
@ -118,3 +124,20 @@ func (c *Convert) ConvertDepartmentRpc(ctx context.Context, v *ent.Department) *
return &tmp return &tmp
} }
func (c *Convert) ConvertOAuthProviderRpc(_ context.Context, v *ent.OauthProvider) *core.OauthProviderInfo {
if v == nil {
return nil
}
var tmp core.OauthProviderInfo
_ = copier.Copy(&tmp, v)
tmp.CreatedAt = ntime.Format(v.CreatedAt)
tmp.UpdatedAt = ntime.Format(v.UpdatedAt)
tmp.ClientId = v.ClientID
tmp.RedirectUrl = v.RedirectURL
tmp.AuthUrl = v.AuthURL
tmp.TokenUrl = v.TokenURL
tmp.InfoUrl = v.InfoURL
return &tmp
}

Loading…
Cancel
Save