From f2e4af32a5436820e96a93fa32fb91254372a513 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Sun, 3 Sep 2023 21:06:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20oauth=5Fprovider=20=E5=B0=8F=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=AE=8C=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../create_oauth_provider_logic.go | 29 +++++++++-- .../delete_oauth_provider_logic.go | 19 +++++-- .../get_oauth_provider_list_logic.go | 52 ++++++++++++++++++- .../get_oauth_provider_logic.go | 35 ++++++++++++- .../update_oauth_provider_logic.go | 28 +++++++++- rpc/core/internal/svc/convert.go | 33 ++++++++++-- 6 files changed, 179 insertions(+), 17 deletions(-) diff --git a/rpc/core/internal/logic/oauth_provider/create_oauth_provider_logic.go b/rpc/core/internal/logic/oauth_provider/create_oauth_provider_logic.go index 48959a9..bcf8c10 100644 --- a/rpc/core/internal/logic/oauth_provider/create_oauth_provider_logic.go +++ b/rpc/core/internal/logic/oauth_provider/create_oauth_provider_logic.go @@ -2,6 +2,9 @@ package oauth_provider import ( "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/types/core" @@ -21,7 +24,27 @@ func NewCreateOauthProviderLogic(ctx context.Context, svcCtx *svc.ServiceContext // OauthProvider management func (l *CreateOauthProviderLogic) CreateOauthProvider(in *core.OauthProviderInfo) (*core.BaseIDResp, error) { - // todo: add your logic here and delete this line - - return &core.BaseIDResp{}, nil + create := l.svcCtx.DB.OauthProvider.Create(). + SetName(in.Name). + 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 } diff --git a/rpc/core/internal/logic/oauth_provider/delete_oauth_provider_logic.go b/rpc/core/internal/logic/oauth_provider/delete_oauth_provider_logic.go index cdbcf13..6cfd206 100644 --- a/rpc/core/internal/logic/oauth_provider/delete_oauth_provider_logic.go +++ b/rpc/core/internal/logic/oauth_provider/delete_oauth_provider_logic.go @@ -2,6 +2,10 @@ package oauth_provider import ( "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/types/core" @@ -20,7 +24,16 @@ func NewDeleteOauthProviderLogic(ctx context.Context, svcCtx *svc.ServiceContext } func (l *DeleteOauthProviderLogic) DeleteOauthProvider(in *core.IDsReq) (*core.BaseResp, error) { - // todo: add your logic here and delete this line - - return &core.BaseResp{}, nil + // 仅允许删除非系统内置数据 + _, err := l.svcCtx.DB.OauthProvider.Delete(). + 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 } diff --git a/rpc/core/internal/logic/oauth_provider/get_oauth_provider_list_logic.go b/rpc/core/internal/logic/oauth_provider/get_oauth_provider_list_logic.go index b1f8a9d..7da7443 100644 --- a/rpc/core/internal/logic/oauth_provider/get_oauth_provider_list_logic.go +++ b/rpc/core/internal/logic/oauth_provider/get_oauth_provider_list_logic.go @@ -2,6 +2,11 @@ package oauth_provider import ( "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/types/core" @@ -20,7 +25,50 @@ func NewGetOauthProviderListLogic(ctx context.Context, svcCtx *svc.ServiceContex } 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 } diff --git a/rpc/core/internal/logic/oauth_provider/get_oauth_provider_logic.go b/rpc/core/internal/logic/oauth_provider/get_oauth_provider_logic.go index 787ffd5..e38e0f5 100644 --- a/rpc/core/internal/logic/oauth_provider/get_oauth_provider_logic.go +++ b/rpc/core/internal/logic/oauth_provider/get_oauth_provider_logic.go @@ -2,6 +2,9 @@ package oauth_provider import ( "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/types/core" @@ -20,7 +23,35 @@ func NewGetOauthProviderLogic(ctx context.Context, svcCtx *svc.ServiceContext) * } 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 } diff --git a/rpc/core/internal/logic/oauth_provider/update_oauth_provider_logic.go b/rpc/core/internal/logic/oauth_provider/update_oauth_provider_logic.go index c98deeb..6549211 100644 --- a/rpc/core/internal/logic/oauth_provider/update_oauth_provider_logic.go +++ b/rpc/core/internal/logic/oauth_provider/update_oauth_provider_logic.go @@ -2,6 +2,10 @@ package oauth_provider import ( "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/types/core" @@ -20,7 +24,27 @@ func NewUpdateOauthProviderLogic(ctx context.Context, svcCtx *svc.ServiceContext } 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 } diff --git a/rpc/core/internal/svc/convert.go b/rpc/core/internal/svc/convert.go index 22980bc..a3bde17 100644 --- a/rpc/core/internal/svc/convert.go +++ b/rpc/core/internal/svc/convert.go @@ -4,8 +4,8 @@ import ( "context" "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/noahlan/ntool/ntime" "github.com/jinzhu/copier" - "time" ) type Convert struct { @@ -24,8 +24,8 @@ func (c *Convert) ConvertRoleRpc(_ context.Context, v *ent.Role) *core.RoleInfo } var tmp core.RoleInfo _ = copier.Copy(&tmp, v) - tmp.CreatedAt = v.CreatedAt.Format(time.DateTime) - tmp.UpdatedAt = v.UpdatedAt.Format(time.DateTime) + tmp.CreatedAt = ntime.Format(v.CreatedAt) + tmp.UpdatedAt = ntime.Format(v.UpdatedAt) tmp.Status = v.Status.String() return &tmp @@ -38,8 +38,8 @@ func (c *Convert) ConvertUserRpc(ctx context.Context, v *ent.User) *core.UserInf var tmp core.UserInfo _ = copier.Copy(&tmp, v) tmp.Status = v.Status.String() - tmp.CreatedAt = v.CreatedAt.Format(time.DateTime) - tmp.UpdatedAt = v.UpdatedAt.Format(time.DateTime) + tmp.CreatedAt = ntime.Format(v.CreatedAt) + tmp.UpdatedAt = ntime.Format(v.UpdatedAt) // Metadata if len(v.Edges.Metas) > 0 { @@ -83,6 +83,9 @@ func (c *Convert) ConvertLoginRecordRpc(_ context.Context, v *ent.LoginRecord) * } var tmp core.LoginRecordInfo _ = copier.Copy(&tmp, v) + tmp.CreatedAt = ntime.Format(v.CreatedAt) + tmp.UpdatedAt = ntime.Format(v.UpdatedAt) + tmp.UserId = v.UserID tmp.LastLoginUa = v.LastLoginUA return &tmp @@ -94,6 +97,9 @@ func (c *Convert) ConvertDepartmentRpc(ctx context.Context, v *ent.Department) * } var tmp core.DepartmentInfo _ = copier.Copy(&tmp, v) + tmp.CreatedAt = ntime.Format(v.CreatedAt) + tmp.UpdatedAt = ntime.Format(v.UpdatedAt) + tmp.LeaderId = v.LeaderID tmp.ParentId = v.ParentID // relationship @@ -118,3 +124,20 @@ func (c *Convert) ConvertDepartmentRpc(ctx context.Context, v *ent.Department) * 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 +}