Skip to content

Commit

Permalink
fix: error judgement in database
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Oct 21, 2022
1 parent 9e7b929 commit 08954ca
Show file tree
Hide file tree
Showing 21 changed files with 317 additions and 205 deletions.
4 changes: 2 additions & 2 deletions api/api_desc/token.api
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type (
Source string `json:"source"`

// JWT status 0 ban 1 active | JWT状态, 0 禁止 1 正常
Status bool `json:"status"`
Status uint32 `json:"status"`

// Expire time | 过期时间
ExpireAt int64 `json:"expireAt"`
Expand Down Expand Up @@ -62,7 +62,7 @@ type (

// JWT status 0 ban 1 active | JWT状态, 0 禁止 1 正常
// Required: true
Status bool `json:"status" validate:"boolean"`
Status uint32 `json:"status" validate:"number"`

// Expire time | 过期时间
// Required: true
Expand Down
17 changes: 17 additions & 0 deletions api/internal/logic/oauth/oauthcallbacklogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oauth
import (
"context"
"net/http"
"strings"
"time"

"github.com/suyuan32/simple-admin-core/api/internal/logic/user"
Expand Down Expand Up @@ -42,6 +43,22 @@ func (l *OauthCallbackLogic) OauthCallback() (resp *types.CallbackResp, err erro
token, err := user.GetJwtToken(l.svcCtx.Config.Auth.AccessSecret, result.Id, time.Now().Unix(),
l.svcCtx.Config.Auth.AccessExpire, int64(result.RoleId))

// add token into database
expireAt := time.Now().Add(time.Second * 259200).Unix()
_, err = l.svcCtx.CoreRpc.CreateOrUpdateToken(context.Background(), &core.TokenInfo{
Id: 0,
CreateAt: 0,
UUID: result.Id,
Token: token,
Source: strings.Split(l.r.FormValue("state"), "-")[1],
Status: 1,
ExpireAt: expireAt,
})

if err != nil {
return nil, err
}

return &types.CallbackResp{
UserId: result.Id,
Role: types.RoleInfoSimple{
Expand Down
1 change: 1 addition & 0 deletions api/internal/logic/token/gettokenlistlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (l *GetTokenListLogic) GetTokenList(req *types.TokenListReq) (resp *types.T
Page: req.Page,
PageSize: req.PageSize,
},
UUID: req.UUID,
Username: req.Username,
Nickname: req.Nickname,
Email: req.Email,
Expand Down
19 changes: 18 additions & 1 deletion api/internal/logic/user/loginlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,27 @@ func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err erro
if err != nil {
return nil, err
}

// add token into database
expireAt := time.Now().Add(time.Second * 259200).Unix()
_, err = l.svcCtx.CoreRpc.CreateOrUpdateToken(context.Background(), &core.TokenInfo{
Id: 0,
CreateAt: 0,
UUID: user.Id,
Token: token,
Source: "core",
Status: 1,
ExpireAt: expireAt,
})

if err != nil {
return nil, err
}

resp = &types.LoginResp{
UserId: user.Id,
Token: token,
Expire: uint64(time.Now().Add(time.Second * 259200).Unix()),
Expire: uint64(expireAt),
Role: types.RoleInfoSimple{
Value: user.RoleValue,
RoleName: user.RoleName,
Expand Down
13 changes: 13 additions & 0 deletions api/internal/middleware/authoritymiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ func (m *AuthorityMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return
}

// check jwt blacklist
res, err := m.Rds.Get("token_" + r.Header.Get("Authorization"))
if err != nil {
logx.Errorw("Redis error in jwt", logx.Field("Detail", err.Error()))
httpx.Error(w, errorx.NewApiError(http.StatusInternalServerError, err.Error()))
return
}
if res == "1" {
logx.Errorw("Token in blacklist", logx.Field("Detail", r.Header.Get("Authorization")))
httpx.Error(w, errorx.NewApiErrorWithoutMsg(http.StatusUnauthorized))
return
}

sub := roleId
result, err := m.Cbn.Enforce(sub, obj, act)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions api/internal/svc/servicecontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ func TestAddTrans(t *testing.T) {
t.Error(result)
}
}

func TestRedis(t *testing.T) {

}
4 changes: 2 additions & 2 deletions api/internal/types/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rpc/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ message TokenListReq {
string username = 2;
string nickname = 3;
string email = 4;
string UUID = 5;
}

// service
Expand Down
12 changes: 8 additions & 4 deletions rpc/internal/logic/changepasswordlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package logic

import (
"context"
"errors"

"gorm.io/gorm"

"github.com/suyuan32/simple-admin-core/common/logmessage"
"github.com/suyuan32/simple-admin-core/common/message"
Expand Down Expand Up @@ -33,14 +36,15 @@ func NewChangePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ch
func (l *ChangePasswordLogic) ChangePassword(in *core.ChangePasswordReq) (*core.BaseResp, error) {
var target model.User
result := l.svcCtx.DB.Where("uuid = ?", in.Uuid).First(&target)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
logx.Errorw("User does not exist", logx.Field("UUID", in.Uuid))
return nil, status.Error(codes.NotFound, errorx.TargetNotExist)
}

if result.Error != nil {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", result.Error.Error()))
return nil, status.Error(codes.Internal, errorx.DatabaseError)
}
if result.RowsAffected == 0 {
logx.Errorw("User does not exist", logx.Field("UUID", in.Uuid))
return nil, status.Error(codes.NotFound, errorx.TargetNotExist)
}

if ok := util.BcryptCheck(in.OldPassword, target.Password); ok {
target.Password = util.BcryptEncrypt(in.NewPassword)
Expand Down
11 changes: 7 additions & 4 deletions rpc/internal/logic/createorupdateapilogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logic

import (
"context"
"errors"
"time"

"github.com/suyuan32/simple-admin-core/common/logmessage"
Expand Down Expand Up @@ -60,14 +61,16 @@ func (l *CreateOrUpdateApiLogic) CreateOrUpdateApi(in *core.ApiInfo) (*core.Base
} else {
var origin *model.Api
check := l.svcCtx.DB.Where("id = ?", in.Id).First(&origin)

if errors.Is(check.Error, gorm.ErrRecordNotFound) {
logx.Errorw(errorx.TargetNotExist, logx.Field("id", in.Id))
return nil, status.Error(codes.InvalidArgument, errorx.UpdateFailed)
}

if check.Error != nil {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", check.Error.Error()))
return nil, status.Error(codes.Internal, check.Error.Error())
}
if check.RowsAffected == 0 {
logx.Errorw(errorx.TargetNotExist, logx.Field("id", in.Id))
return nil, status.Error(codes.InvalidArgument, errorx.UpdateFailed)
}

data := &model.Api{
Model: gorm.Model{ID: origin.ID, CreatedAt: origin.CreatedAt, UpdatedAt: time.Now()},
Expand Down
33 changes: 18 additions & 15 deletions rpc/internal/logic/createorupdatedictionarydetaillogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package logic

import (
"context"
"github.com/suyuan32/simple-admin-core/common/logmessage"
"github.com/suyuan32/simple-admin-core/common/message"
"github.com/suyuan32/simple-admin-core/rpc/internal/model"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/types/core"
"errors"

"github.com/zeromicro/go-zero/core/errorx"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"

"github.com/suyuan32/simple-admin-core/common/logmessage"
"github.com/suyuan32/simple-admin-core/common/message"
"github.com/suyuan32/simple-admin-core/rpc/internal/model"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/types/core"
)

type CreateOrUpdateDictionaryDetailLogic struct {
Expand All @@ -31,16 +34,16 @@ func NewCreateOrUpdateDictionaryDetailLogic(ctx context.Context, svcCtx *svc.Ser
func (l *CreateOrUpdateDictionaryDetailLogic) CreateOrUpdateDictionaryDetail(in *core.DictionaryDetail) (*core.BaseResp, error) {
var parent model.Dictionary
check := l.svcCtx.DB.Where("id = ?", in.ParentId).First(&parent)
if check.Error != nil {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", check.Error.Error()))
return nil, status.Error(codes.Internal, check.Error.Error())
}

if check.RowsAffected == 0 {
if errors.Is(check.Error, gorm.ErrRecordNotFound) {
logx.Errorw(message.ParentNotExist, logx.Field("Detail", in))
return nil, status.Error(codes.InvalidArgument, message.ParentNotExist)
}

if check.Error != nil {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", check.Error.Error()))
return nil, status.Error(codes.Internal, check.Error.Error())
}
if in.Id == 0 {
result := l.svcCtx.DB.Create(&model.DictionaryDetail{
Model: gorm.Model{},
Expand All @@ -63,16 +66,16 @@ func (l *CreateOrUpdateDictionaryDetailLogic) CreateOrUpdateDictionaryDetail(in
var origin model.DictionaryDetail
checkOrigin := l.svcCtx.DB.Where("id = ?", in.Id).First(&origin)

if errors.Is(checkOrigin.Error, gorm.ErrRecordNotFound) {
logx.Errorw(logmessage.TargetNotFound, logx.Field("Detail", in))
return nil, status.Error(codes.InvalidArgument, errorx.TargetNotExist)
}

if checkOrigin.Error != nil {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", checkOrigin.Error.Error()))
return nil, status.Error(codes.Internal, checkOrigin.Error.Error())
}

if checkOrigin.RowsAffected == 0 {
logx.Errorw(logmessage.TargetNotFound, logx.Field("Detail", in))
return nil, status.Error(codes.InvalidArgument, errorx.TargetNotExist)
}

origin.Title = in.Title
origin.Key = in.Key
origin.Value = in.Value
Expand Down
11 changes: 7 additions & 4 deletions rpc/internal/logic/createorupdatedictionarylogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package logic

import (
"context"
"github.com/suyuan32/simple-admin-core/common/logmessage"
"github.com/suyuan32/simple-admin-core/common/message"
"github.com/suyuan32/simple-admin-core/rpc/internal/model"
"errors"

"github.com/zeromicro/go-zero/core/errorx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"

"github.com/suyuan32/simple-admin-core/common/logmessage"
"github.com/suyuan32/simple-admin-core/common/message"
"github.com/suyuan32/simple-admin-core/rpc/internal/model"

"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/types/core"

Expand Down Expand Up @@ -59,7 +62,7 @@ func (l *CreateOrUpdateDictionaryLogic) CreateOrUpdateDictionary(in *core.Dictio
return nil, status.Error(codes.Internal, check.Error.Error())
}

if check.RowsAffected == 0 {
if errors.Is(check.Error, gorm.ErrRecordNotFound) {
logx.Errorw(logmessage.TargetNotFound, logx.Field("Detail", in))
return nil, status.Error(codes.InvalidArgument, errorx.TargetNotExist)
}
Expand Down
17 changes: 10 additions & 7 deletions rpc/internal/logic/createorupdatemenulogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logic

import (
"context"
"errors"
"time"

"github.com/suyuan32/simple-admin-core/common/logmessage"
Expand Down Expand Up @@ -37,14 +38,14 @@ func (l *CreateOrUpdateMenuLogic) CreateOrUpdateMenu(in *core.CreateOrUpdateMenu
if in.ParentId != 0 {
var parent model.Menu
result := l.svcCtx.DB.Where("id = ?", in.ParentId).First(&parent)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
logx.Errorw("Wrong parent ID", logx.Field("parentId", in.ParentId))
return nil, status.Error(codes.InvalidArgument, message.ParentNotExist)
}
if result.Error != nil {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", result.Error.Error()))
return nil, status.Error(codes.Internal, result.Error.Error())
}
if result.RowsAffected == 0 {
logx.Errorw("Wrong parent ID", logx.Field("parentId", in.ParentId))
return nil, status.Error(codes.InvalidArgument, message.ParentNotExist)
}
menuLevel = parent.MenuLevel + 1
} else {
in.ParentId = 1
Expand Down Expand Up @@ -94,13 +95,15 @@ func (l *CreateOrUpdateMenuLogic) CreateOrUpdateMenu(in *core.CreateOrUpdateMenu
} else {
var origin *model.Menu
result := l.svcCtx.DB.Where("id = ?", in.Id).First(&origin)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.InvalidArgument, message.MenuNotExists)
}

if result.Error != nil {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", result.Error.Error()))
return nil, status.Error(codes.Internal, errorx.DatabaseError)
}
if result.RowsAffected == 0 {
return nil, status.Error(codes.InvalidArgument, message.MenuNotExists)
}

data = &model.Menu{
Model: gorm.Model{ID: uint(in.Id), CreatedAt: origin.CreatedAt, UpdatedAt: time.Now()},
MenuLevel: menuLevel,
Expand Down
15 changes: 9 additions & 6 deletions rpc/internal/logic/createorupdatemenuparamlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package logic

import (
"context"
"github.com/suyuan32/simple-admin-core/common/logmessage"
"github.com/suyuan32/simple-admin-core/rpc/internal/model"
"errors"

"github.com/zeromicro/go-zero/core/errorx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"

"github.com/suyuan32/simple-admin-core/common/logmessage"
"github.com/suyuan32/simple-admin-core/rpc/internal/model"

"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/types/core"

Expand Down Expand Up @@ -51,14 +54,14 @@ func (l *CreateOrUpdateMenuParamLogic) CreateOrUpdateMenuParam(in *core.CreateOr
} else {
var origin model.MenuParam
check := l.svcCtx.DB.Where("id = ?", in.Id).First(&origin)
if errors.Is(check.Error, gorm.ErrRecordNotFound) {
logx.Errorw("Update menu parameter error, menu parameter does not find", logx.Field("Detail", in))
return nil, status.Error(codes.InvalidArgument, errorx.TargetNotExist)
}
if check.Error != nil {
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", check.Error.Error()))
return nil, status.Error(codes.Internal, check.Error.Error())
}
if check.RowsAffected == 0 {
logx.Errorw("Update menu parameter error, menu parameter does not find", logx.Field("Detail", in))
return nil, status.Error(codes.InvalidArgument, errorx.TargetNotExist)
}
origin.MenuId = uint(in.MenuId)
origin.Type = in.Type
origin.Value = in.Value
Expand Down
Loading

0 comments on commit 08954ca

Please sign in to comment.