Merge remote-tracking branch 'origin/master'

This commit is contained in:
zhangwenjian committed 2022-08-04 17:25:17 +08:00
commit b231705a67
16 files changed
+81 -124

No files matched your search

-60
View File
@@ -1,60 +0,0 @@
name: build
on:
push:
branches: [ dev-lwx ]
pull_request:
branches: [ dev-lwx ]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v1
with:
go-version: 1.15
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build
run: |
CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -a -installsuffix cgo -o go-admin .
mv go-admin ./scripts
- uses: Azure/docker-login@v1
with:
login-server: registry.cn-shanghai.aliyuncs.com
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- run: |
docker build ./scripts -t registry.cn-shanghai.aliyuncs.com/go-admin-team/go-admin:${{ github.sha }}
docker push registry.cn-shanghai.aliyuncs.com/go-admin-team/go-admin:${{ github.sha }}
- uses: Azure/k8s-set-context@v1
with:
kubeconfig: ${{ secrets.KUBE_CONFIG }}
- uses: Azure/k8s-create-secret@v1
with:
namespace: 'go-admin'
container-registry-url: registry.cn-shanghai.aliyuncs.com
container-registry-username: ${{ secrets.REGISTRY_USERNAME }}
container-registry-password: ${{ secrets.REGISTRY_PASSWORD }}
secret-name: aliyuncs-k8s-secret
- uses: Azure/k8s-deploy@v1
with:
namespace: 'go-admin'
manifests: 'scripts/k8s/deploy.yml'
images: 'registry.cn-shanghai.aliyuncs.com/go-admin-team/go-admin:${{ github.sha }}'
imagepullsecrets: 'aliyuncs-k8s-secret'
kubectl-version: 'latest'
+18 -16
View File
@@ -16,29 +16,23 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Set up Go 1.14 - name: Set up Go 1.15
uses: actions/setup-go@v1 uses: actions/setup-go@v3
with: with:
go-version: 1.14 go-version: 1.15
id: go id: go
- name: Check out code into the Go module directory - name: Check out code into the Go module directory
uses: actions/checkout@v2 uses: actions/checkout@v3
- name: Get dependencies - name: Get dependencies
run: | run: go mod tidy
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build - name: Build
run: | run: make build
ref=${{ github.ref }}
CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -a -installsuffix cgo -o go-admin .
- name: Log in to the Container registry - name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 uses: docker/login-action@v2
if: startsWith(${{github.ref}}, 'refs/tags/')
with: with:
registry: ${{ env.REGISTRY }} registry: ${{ env.REGISTRY }}
username: ${{ github.actor }} username: ${{ github.actor }}
@@ -46,12 +40,20 @@ jobs:
- name: Extract metadata (tags, labels) for Docker - name: Extract metadata (tags, labels) for Docker
id: meta id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 if: startsWith(${{github.ref}}, 'refs/tags/')
uses: docker/metadata-action@v4
with: with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
flavor: |
latest=auto
tags: |
type=schedule
type=ref,event=tag
type=sha,prefix=,format=long,enable=true,priority=100
- name: Build and push Docker image - name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc uses: docker/build-push-action@v3
if: startsWith(${{github.ref}}, 'refs/tags/')
with: with:
context: . context: .
file: scripts/Dockerfile file: scripts/Dockerfile
+2
View File
@@ -19,6 +19,8 @@ FROM alpine
COPY --from=builder /go/release/go-admin / COPY --from=builder /go/release/go-admin /
COPY --from=builder /go/release/config/settings.yml /config/settings.yml
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /etc/localtime COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 8000 EXPOSE 8000
+3 -3
View File
@@ -2,11 +2,11 @@ PROJECT:=go-admin
.PHONY: build .PHONY: build
build: build:
CGO_ENABLED=0 go build -o go-admin main.go CGO_ENABLED=0 go build -ldflags="-w -s" -a -installsuffix -o go-admin .
build-linux: build-linux:
env GOOS=linux GOARCH=amd64 go build CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -a -installsuffix -o go-admin .
build-sqlite: build-sqlite:
go build -tags sqlite3 -o go-admin main.go go build -tags sqlite3 -ldflags="-w -s" -a -installsuffix -o go-admin .
#.PHONY: test #.PHONY: test
#test: #test:
# go test -v ./... -cover # go test -v ./... -cover
+1 -1
View File
@@ -166,7 +166,7 @@ D:\Code\go-admin>go build
cgo: exec gcc: exec: "gcc": executable file not found in %PATH% cgo: exec gcc: exec: "gcc": executable file not found in %PATH%
``` ```
[解决cgo问题进入](https://doc.go-admin.dev/guide/other/faq.html#_5-cgo-exec-missing-cc-exec-missing-cc-file-does-not-exist) [解决cgo问题进入](https://doc.go-admin.dev/zh-CN/guide/faq#cgo-%E7%9A%84%E9%97%AE%E9%A2%98)
::: :::
+1 -1
View File
@@ -160,7 +160,7 @@ D:\Code\go-admin>go build
cgo: exec gcc: exec: "gcc": executable file not found in %PATH% cgo: exec gcc: exec: "gcc": executable file not found in %PATH%
``` ```
[Solve the cgo problem and enter](https://doc.go-admin.dev/guide/other/faq.html#_5-cgo-exec-missing-cc-exec-missing-cc-file-does-not-exist) [Solve the cgo problem and enter](https://doc.go-admin.dev/guide/faq#cgo-%E7%9A%84%E9%97%AE%E9%A2%98)
::: :::
+7 -18
View File
@@ -2,10 +2,11 @@ package apis
import ( import (
"fmt" "fmt"
"net/http"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"github.com/go-admin-team/go-admin-core/sdk" "github.com/go-admin-team/go-admin-core/sdk"
"go-admin/app/admin/models" "go-admin/app/admin/models"
"net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk/api" "github.com/go-admin-team/go-admin-core/sdk/api"
@@ -14,7 +15,6 @@ import (
"go-admin/app/admin/service" "go-admin/app/admin/service"
"go-admin/app/admin/service/dto" "go-admin/app/admin/service/dto"
"go-admin/common/global"
) )
type SysRole struct { type SysRole struct {
@@ -128,11 +128,7 @@ func (e SysRole) Insert(c *gin.Context) {
e.Error(500, err, "创建失败,"+err.Error()) e.Error(500, err, "创建失败,"+err.Error())
return return
} }
_, err = global.LoadPolicy(c)
if err != nil {
e.Error(500, err, "")
return
}
e.OK(req.GetId(), "创建成功") e.OK(req.GetId(), "创建成功")
} }
@@ -168,11 +164,7 @@ func (e SysRole) Update(c *gin.Context) {
e.Logger.Error(err) e.Logger.Error(err)
return return
} }
_, err = global.LoadPolicy(c)
if err != nil {
e.Error(500, err, "")
return
}
e.OK(req.GetId(), "更新成功") e.OK(req.GetId(), "更新成功")
} }
@@ -198,17 +190,14 @@ func (e SysRole) Delete(c *gin.Context) {
return return
} }
err = s.Remove(&req) cb := sdk.Runtime.GetCasbinKey(c.Request.Host)
err = s.Remove(&req, cb)
if err != nil { if err != nil {
e.Logger.Error(err) e.Logger.Error(err)
e.Error(500, err, "") e.Error(500, err, "")
return return
} }
_, err = global.LoadPolicy(c)
if err != nil {
e.Error(500, err, fmt.Sprintf("删除角色 %v 失败,失败信息 %s", req.GetId(), err.Error()))
return
}
e.OK(req.GetId(), fmt.Sprintf("删除角色角色 %v 状态成功!", req.GetId())) e.OK(req.GetId(), fmt.Sprintf("删除角色角色 %v 状态成功!", req.GetId()))
} }
+7
View File
@@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"github.com/go-admin-team/go-admin-core/sdk/config" "github.com/go-admin-team/go-admin-core/sdk/config"
"go-admin/app/admin/models" "go-admin/app/admin/models"
"golang.org/x/crypto/bcrypt"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -356,6 +357,12 @@ func (e SysUser) UpdatePwd(c *gin.Context) {
if user.GetUserId(c) == 1 && config.ApplicationConfig.Mode == "demo" { if user.GetUserId(c) == 1 && config.ApplicationConfig.Mode == "demo" {
req.NewPassword = "123456" req.NewPassword = "123456"
} }
var hash []byte
if hash, err = bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost); err != nil {
req.NewPassword = string(hash)
}
err = s.UpdatePwd(user.GetUserId(c), req.OldPassword, req.NewPassword, p) err = s.UpdatePwd(user.GetUserId(c), req.OldPassword, req.NewPassword, p)
if err != nil { if err != nil {
e.Logger.Error(err) e.Logger.Error(err)
+17 -4
View File
@@ -2,6 +2,7 @@ package service
import ( import (
"errors" "errors"
"github.com/go-admin-team/go-admin-core/sdk/config" "github.com/go-admin-team/go-admin-core/sdk/config"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
@@ -102,12 +103,21 @@ func (e *SysRole) Insert(c *dto.SysRoleInsertReq, cb *casbin.SyncedEnforcer) err
return err return err
} }
mp := make(map[string]interface{}, 0)
polices := make([][]string, 0)
for _, menu := range dataMenu { for _, menu := range dataMenu {
for _, api := range menu.SysApi { for _, api := range menu.SysApi {
_, err = cb.AddNamedPolicy("p", data.RoleKey, api.Path, api.Action) if mp[data.RoleKey+"-"+api.Path+"-"+api.Action] != "" {
mp[data.RoleKey+"-"+api.Path+"-"+api.Action] = ""
polices = append(polices, []string{data.RoleKey, api.Path, api.Action})
}
} }
} }
_ = cb.SavePolicy() _, err = cb.AddNamedPolicies("p", polices)
if err != nil {
return err
}
//if len(c.MenuIds) > 0 { //if len(c.MenuIds) > 0 {
// s := SysRoleMenu{} // s := SysRoleMenu{}
// s.Orm = e.Orm // s.Orm = e.Orm
@@ -176,12 +186,12 @@ func (e *SysRole) Update(c *dto.SysRoleUpdateReq, cb *casbin.SyncedEnforcer) err
if err != nil { if err != nil {
return err return err
} }
_ = cb.SavePolicy()
return nil return nil
} }
// Remove 删除SysRole // Remove 删除SysRole
func (e *SysRole) Remove(c *dto.SysRoleDeleteReq) error { func (e *SysRole) Remove(c *dto.SysRoleDeleteReq, cb *casbin.SyncedEnforcer) error {
var err error var err error
tx := e.Orm tx := e.Orm
if config.DatabaseConfig.Driver != "sqlite3" { if config.DatabaseConfig.Driver != "sqlite3" {
@@ -205,6 +215,9 @@ func (e *SysRole) Remove(c *dto.SysRoleDeleteReq) error {
if db.RowsAffected == 0 { if db.RowsAffected == 0 {
return errors.New("无权更新该数据") return errors.New("无权更新该数据")
} }
_, _ = cb.RemoveFilteredPolicy(0, model.RoleKey)
return nil return nil
} }
+3 -1
View File
@@ -233,7 +233,9 @@ func (e *SysUser) UpdatePwd(id int, oldPassword, newPassword string, p *actions.
return err return err
} }
c.Password = newPassword c.Password = newPassword
db := e.Orm.Model(c).Where("user_id = ?", id).Select("Password", "Salt").Updates(c) db := e.Orm.Model(c).Where("user_id = ?", id).
Select("Password", "Salt").
Updates(c)
if err = db.Error; err != nil { if err = db.Error; err != nil {
e.Log.Errorf("db error: %s", err) e.Log.Errorf("db error: %s", err)
return err return err
+1 -1
View File
@@ -1,7 +1,7 @@
package service package service
import ( import (
"github.com/pkg/errors" "errors"
"time" "time"
"github.com/go-admin-team/go-admin-core/sdk/service" "github.com/go-admin-team/go-admin-core/sdk/service"
+9 -11
View File
@@ -4,20 +4,18 @@ import (
"log" "log"
"path/filepath" "path/filepath"
"sort" "sort"
"strconv"
"sync" "sync"
"github.com/spf13/cast"
"gorm.io/gorm" "gorm.io/gorm"
) )
var Migrate = &Migration{ var Migrate = &Migration{
version: make(map[int]func(db *gorm.DB, version string) error), version: make(map[string]func(db *gorm.DB, version string) error),
} }
type Migration struct { type Migration struct {
db *gorm.DB db *gorm.DB
version map[int]func(db *gorm.DB, version string) error version map[string]func(db *gorm.DB, version string) error
mutex sync.Mutex mutex sync.Mutex
} }
@@ -29,19 +27,19 @@ func (e *Migration) SetDb(db *gorm.DB) {
e.db = db e.db = db
} }
func (e *Migration) SetVersion(k int, f func(db *gorm.DB, version string) error) { func (e *Migration) SetVersion(k string, f func(db *gorm.DB, version string) error) {
e.mutex.Lock() e.mutex.Lock()
defer e.mutex.Unlock() defer e.mutex.Unlock()
e.version[k] = f e.version[k] = f
} }
func (e *Migration) Migrate() { func (e *Migration) Migrate() {
versions := make([]int, 0) versions := make([]string, 0)
for k := range e.version { for k := range e.version {
versions = append(versions, k) versions = append(versions, k)
} }
if !sort.IntsAreSorted(versions) { if !sort.StringsAreSorted(versions) {
sort.Ints(versions) sort.Strings(versions)
} }
var err error var err error
var count int64 var count int64
@@ -55,14 +53,14 @@ func (e *Migration) Migrate() {
count = 0 count = 0
continue continue
} }
err = (e.version[v])(e.db, strconv.Itoa(v)) err = (e.version[v])(e.db.Debug(), v)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
} }
} }
func GetFilename(s string) int { func GetFilename(s string) string {
s = filepath.Base(s) s = filepath.Base(s)
return cast.ToInt(s[:13]) return s[:13]
} }
+8 -3
View File
@@ -3,10 +3,11 @@ package models
import ( import (
"fmt" "fmt"
"go-admin/common/global" "go-admin/common/global"
"gorm.io/gorm"
"io/ioutil" "io/ioutil"
"log" "log"
"strings" "strings"
"gorm.io/gorm"
) )
func InitDb(db *gorm.DB) (err error) { func InitDb(db *gorm.DB) (err error) {
@@ -16,9 +17,13 @@ func InitDb(db *gorm.DB) (err error) {
err = ExecSql(db, filePath) err = ExecSql(db, filePath)
} else if global.Driver == "mysql" { } else if global.Driver == "mysql" {
filePath = "config/db-begin-mysql.sql" filePath = "config/db-begin-mysql.sql"
err = ExecSql(db, filePath) if err = ExecSql(db, filePath); err != nil {
return err
}
filePath = "config/db.sql" filePath = "config/db.sql"
err = ExecSql(db, filePath) if err = ExecSql(db, filePath); err != nil {
return err
}
filePath = "config/db-end-mysql.sql" filePath = "config/db-end-mysql.sql"
err = ExecSql(db, filePath) err = ExecSql(db, filePath)
} else { } else {
@@ -3,11 +3,11 @@ package version
import ( import (
"runtime" "runtime"
"gorm.io/gorm"
"go-admin/cmd/migrate/migration" "go-admin/cmd/migrate/migration"
"go-admin/cmd/migrate/migration/models" "go-admin/cmd/migrate/migration/models"
common "go-admin/common/models" common "go-admin/common/models"
"gorm.io/gorm"
) )
func init() { func init() {
@@ -41,7 +41,7 @@ func _1599190683659Tables(db *gorm.DB, version string) error {
return err return err
} }
if err := models.InitDb(tx); err != nil { if err := models.InitDb(tx); err != nil {
return err
} }
return tx.Create(&common.Migration{ return tx.Create(&common.Migration{
Version: version, Version: version,
+1 -1
View File
@@ -2,7 +2,7 @@ package global
const ( const (
// Version go-admin version info // Version go-admin version info
Version = "2.0.11" Version = "2.0.12"
) )
var ( var (
-1
View File
@@ -21,7 +21,6 @@ require (
github.com/qiniu/go-sdk/v7 v7.11.1 github.com/qiniu/go-sdk/v7 v7.11.1
github.com/robfig/cron/v3 v3.0.1 github.com/robfig/cron/v3 v3.0.1
github.com/shirou/gopsutil/v3 v3.22.1 github.com/shirou/gopsutil/v3 v3.22.1
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.0.0 github.com/spf13/cobra v1.0.0
github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.6.7 github.com/swaggo/swag v1.6.7