作者 yangfu

增加 :1.项目模板版本控制, 2.项目版本查询,更新

... ... @@ -3,10 +3,12 @@ module gitlab.fjmaimaimai.com/mmm-go/godevp
go 1.15
require (
github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2
github.com/astaxie/beego v1.12.2
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-pg/pg/v10 v10.0.0-beta.2
github.com/tiptok/gocomm v1.0.2
github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2
github.com/tal-tech/go-zero v1.0.11
)
replace github.com/tiptok/gocomm v1.0.2 => F:\go\src\learn_project\gocomm
... ...
... ... @@ -56,7 +56,7 @@ func (svr *CacheService) ValidUserAccess(userId int64, object string, method str
}()
cache.GetObject(UserRoleAccessCacheKey(userId), &userAccess, 3600, svr.CacheUserAccess(userId))
for i := range userAccess {
if KeyMatch3(object, userAccess[i].Object) && KeyEqual(userAccess[i].Action, method) {
if KeyMatch3(object, userAccess[i].Object) && ActionEqual(userAccess[i].Action, method) {
result = true
return
}
... ... @@ -65,18 +65,20 @@ func (svr *CacheService) ValidUserAccess(userId int64, object string, method str
}
// KeyMatch3 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
// For example, "/foo/bar" matches "/foo/*", "/resource1" matches "/{resource}"
// For example, "/foo/bar" matches "/foo/*", "/resource1" matches "/{resource}" ,"/foo" matches "/foo/*"
func KeyMatch3(key1 string, key2 string) bool {
key2 = strings.Replace(key2, "/*", "/.*", -1)
re := regexp.MustCompile(`\{[^/]+\}`)
key2 = re.ReplaceAllString(key2, "$1[^/]+$2")
if RegexMatch(key1+"/", "^"+key2+"$") {
return true
}
return RegexMatch(key1, "^"+key2+"$")
}
// KeyEqual case key1='*' or key1=' ' result=true
func KeyEqual(key1 string, key2 string) bool {
func ActionEqual(key1 string, key2 string) bool {
if key1 == "*" {
return true
}
... ...
package project_module_version
import (
"fmt"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/project_module_version"
"strings"
"time"
)
... ... @@ -20,6 +23,7 @@ func (svr *ProjectModuleVersionService) CreateProjectModuleVersion(header *proto
rsp = &protocolx.CreateProjectModuleVersionResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
... ... @@ -29,18 +33,36 @@ func (svr *ProjectModuleVersionService) CreateProjectModuleVersion(header *proto
transactionContext.RollbackTransaction()
}()
newProjectModuleVersion := &domain.ProjectModuleVersion{
Version: request.Version,
Version: strings.TrimSpace(request.Version),
Description: request.Description,
Status: request.Status,
Status: 0,
CreateTime: time.Now(),
UpdateTime: time.Now(),
}
var ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
if m, err := ProjectModuleVersionRepository.Save(newProjectModuleVersion); err != nil {
return nil, err
var srcVersion, dstVersion *domain.ProjectModuleVersion
var currentPMVersionId int64
if srcVersion, err = ProjectModuleVersionRepository.FindOne(map[string]interface{}{"id": request.CurrentProjectModuleVersionId}); err != nil {
err = protocol.NewCustomMessage(1, fmt.Sprintf("当前版本不存在,请重试"))
return
}
newProjectModuleVersion.ProjectModuleId = srcVersion.ProjectModuleId
if _, e := ProjectModuleVersionRepository.FindOne(map[string]interface{}{"projectModuleId": srcVersion.ProjectModuleId, "version": strings.TrimSpace(request.Version)}); e == nil {
err = protocol.NewCustomMessage(1, fmt.Sprintf("当前版本已存在:"+request.Version))
return
}
if dstVersion, err = ProjectModuleVersionRepository.Save(newProjectModuleVersion); err != nil {
return
} else {
rsp = m
currentPMVersionId = dstVersion.Id
rsp = dstVersion
}
var ProjectModuleDao, _ = dao.NewProjectModuleDao(transactionContext)
if err = ProjectModuleDao.DuplicateVersion(srcVersion.ProjectModuleId, srcVersion.ProjectModuleId, currentPMVersionId); err != nil {
return
}
err = transactionContext.CommitTransaction()
return
... ... @@ -53,6 +75,7 @@ func (svr *ProjectModuleVersionService) UpdateProjectModuleVersion(header *proto
rsp = &protocolx.UpdateProjectModuleVersionResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
... ... @@ -64,9 +87,17 @@ func (svr *ProjectModuleVersionService) UpdateProjectModuleVersion(header *proto
var ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
var projectModuleVersion *domain.ProjectModuleVersion
if projectModuleVersion, err = ProjectModuleVersionRepository.FindOne(common.ObjectToMap(request)); err != nil {
if projectModuleVersion, err = ProjectModuleVersionRepository.FindOne(map[string]interface{}{"id": request.ProjectModuleVersionId}); err != nil {
err = protocol.NewCustomMessage(1, "版本信息不存在")
return
}
// 修改版本号
if len(request.Version) > 0 && projectModuleVersion.Version != request.Version {
if _, err = ProjectModuleVersionRepository.FindOne(map[string]interface{}{"projectModuleId": projectModuleVersion.ProjectModuleId, "version": request.Version}); err == nil {
err = protocol.NewCustomMessage(1, "版本信息已存在:"+request.Version)
return
}
}
if err = projectModuleVersion.Update(common.ObjectToMap(request)); err != nil {
return
}
... ... @@ -84,6 +115,7 @@ func (svr *ProjectModuleVersionService) GetProjectModuleVersion(header *protocol
rsp = &protocolx.GetProjectModuleVersionResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
... ... @@ -96,6 +128,7 @@ func (svr *ProjectModuleVersionService) GetProjectModuleVersion(header *protocol
var ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
var projectModuleVersion *domain.ProjectModuleVersion
if projectModuleVersion, err = ProjectModuleVersionRepository.FindOne(common.ObjectToMap(request)); err != nil {
err = protocol.NewCustomMessage(1, "版本信息不存在")
return
}
rsp = projectModuleVersion
... ... @@ -122,9 +155,15 @@ func (svr *ProjectModuleVersionService) DeleteProjectModuleVersion(header *proto
var ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
var projectModuleVersion *domain.ProjectModuleVersion
if projectModuleVersion, err = ProjectModuleVersionRepository.FindOne(common.ObjectToMap(request)); err != nil {
err = protocol.NewCustomMessage(1, "版本信息不存在")
return
}
if projectModuleVersion, err = ProjectModuleVersionRepository.Remove(projectModuleVersion); err != nil {
if projectModuleVersion.Status == 1 {
err = protocol.NewCustomMessage(1, "已删除")
return
}
projectModuleVersion.SetStatusDelete()
if projectModuleVersion, err = ProjectModuleVersionRepository.Save(projectModuleVersion); err != nil {
return
}
rsp = projectModuleVersion
... ... @@ -151,7 +190,7 @@ func (svr *ProjectModuleVersionService) ListProjectModuleVersion(header *protoco
var ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
var projectModuleVersion []*domain.ProjectModuleVersion
var total int64
if total, projectModuleVersion, err = ProjectModuleVersionRepository.Find(common.ObjectToMap(request)); err != nil {
if total, projectModuleVersion, err = ProjectModuleVersionRepository.Find(map[string]interface{}{"projectModuleId": request.ProjectModuleId, "status": 0}); err != nil {
return
}
rsp = map[string]interface{}{
... ...
... ... @@ -35,5 +35,18 @@ func (m *ProjectModuleVersion) Identify() interface{} {
}
func (m *ProjectModuleVersion) Update(data map[string]interface{}) error {
if version, ok := data["version"]; ok {
m.Version = version.(string)
}
if description, ok := data["description"]; ok {
m.Description = description.(string)
}
if status, ok := data["status"]; ok {
m.Status = status.(int)
}
return nil
}
func (m *ProjectModuleVersion) SetStatusDelete() {
m.Status = 1
}
... ...
package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
)
type ProjectModuleDao struct {
transactionContext *transaction.TransactionContext
}
func (dao *ProjectModuleDao) DuplicateVersion(srcProjectId, srcVersionId int64, curVersionId int64) error {
tx := dao.transactionContext.PgTx
_, err := tx.Exec(`insert into project_module_files(project_module_id,project_module_version_id,file_type,file_name,file_key,code_block,parent_id,sort,remark,create_time,update_time)
select ?,?,file_type,file_name,file_key,code_block,parent_id,sort,remark,now(),now() from project_module_files where project_module_id=? and project_module_version_id=?`,
srcProjectId, curVersionId, srcProjectId, srcVersionId)
return err
}
func NewProjectModuleDao(transactionContext *transaction.TransactionContext) (*ProjectModuleDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &ProjectModuleDao{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -2,6 +2,7 @@ package repository
import (
"fmt"
"github.com/go-pg/pg/v10/orm"
"github.com/tiptok/gocomm/common"
. "github.com/tiptok/gocomm/pkg/orm/pgx"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
... ... @@ -49,7 +50,10 @@ func (repository *ProjectModuleRepository) Remove(ProjectModule *domain.ProjectM
func (repository *ProjectModuleRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProjectModule, error) {
tx := repository.transactionContext.PgTx
ProjectModuleModel := new(models.ProjectModule)
query := NewQuery(tx.Model(ProjectModuleModel).Relation("ProjectModuleVersion"), queryOptions)
query := NewQuery(tx.Model(ProjectModuleModel).Relation("ProjectModuleVersion", func(q *orm.Query) (*orm.Query, error) {
q.Where("status=0")
return q, nil
}), queryOptions)
query.SetWhere("id = ?", "id")
query.SetWhere("project_key = ?", "projectKey")
if err := query.First(); err != nil {
... ... @@ -65,7 +69,10 @@ func (repository *ProjectModuleRepository) Find(queryOptions map[string]interfac
tx := repository.transactionContext.PgTx
var ProjectModuleModels []*models.ProjectModule
ProjectModules := make([]*domain.ProjectModule, 0)
query := NewQuery(tx.Model(&ProjectModuleModels).Relation("ProjectModuleVersion"), queryOptions).
query := NewQuery(tx.Model(&ProjectModuleModels).Relation("ProjectModuleVersion", func(q *orm.Query) (*orm.Query, error) {
q.Where("status=0")
return q, nil
}), queryOptions).
SetOrder("create_time", "sortByCreateTime").
SetOrder("update_time", "sortByUpdateTime")
var err error
... ...
... ... @@ -2,13 +2,23 @@ package repository
import (
"fmt"
//"github.com/tal-tech/go-zero/core/stringx"
"github.com/tiptok/gocomm/common"
. "github.com/tiptok/gocomm/pkg/orm/pgx"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
//"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
//"strings"
)
//var (
// fieldNames = builderx.FieldNames(&models.ProjectModuleVersion{})
// rows = strings.Join(fieldNames, ",")
// rowsExpectAutoSet = strings.Join(stringx.Remove(fieldNames, "id","create_time", "update_time"), ",")
// rowsWithPlaceHolder = strings.Join(stringx.Remove(fieldNames, "id","project_module_id","create_time", "update_time"), "=?,") + "=?"
//)
type ProjectModuleVersionRepository struct {
transactionContext *transaction.TransactionContext
}
... ... @@ -29,7 +39,7 @@ func (repository *ProjectModuleVersionRepository) Save(dm *domain.ProjectModuleV
dm.Id = m.Id
return dm, nil
}
if err = tx.Update(m); err != nil {
if _, err = tx.Exec(`update project_module_version set version=?,description=?,status=?,update_time=? where id = ?`, m.Version, m.Description, m.Status, m.UpdateTime, m.Id); err != nil {
return nil, err
}
return dm, nil
... ... @@ -51,6 +61,8 @@ func (repository *ProjectModuleVersionRepository) FindOne(queryOptions map[strin
ProjectModuleVersionModel := new(models.ProjectModuleVersion)
query := NewQuery(tx.Model(ProjectModuleVersionModel), queryOptions)
query.SetWhere("id = ?", "id")
query.SetWhere("project_module_id = ?", "projectModuleId")
query.SetWhere("version = ?", "version")
if err := query.First(); err != nil {
return nil, fmt.Errorf("query row not found")
}
... ... @@ -65,6 +77,8 @@ func (repository *ProjectModuleVersionRepository) Find(queryOptions map[string]i
var ProjectModuleVersionModels []*models.ProjectModuleVersion
ProjectModuleVersions := make([]*domain.ProjectModuleVersion, 0)
query := NewQuery(tx.Model(&ProjectModuleVersionModels), queryOptions).
SetWhere("status=?", "status").
SetWhere("project_module_id=?", "projectModuleId").
SetOrder("create_time", "sortByCreateTime").
SetOrder("update_time", "sortByUpdateTime")
var err error
... ...
... ... @@ -16,9 +16,10 @@ func init() {
AllowCredentials: true,
}))
beego.InsertFilter("/user/*", beego.BeforeExec, middleware.InspectRoleAccess())
beego.InsertFilter("/role/*", beego.BeforeExec, middleware.InspectRoleAccess())
beego.InsertFilter("/project_module/*", beego.BeforeExec, middleware.CheckAuthorization)
beego.InsertFilter("/project_module_files/*", beego.BeforeExec, middleware.CheckAuthorization)
beego.InsertFilter("/rbac/*", beego.BeforeExec, middleware.CheckAuthorization)
beego.InsertFilter("/user/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
beego.InsertFilter("/role/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
beego.InsertFilter("/project_module/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
beego.InsertFilter("/project_module_version/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
beego.InsertFilter("/project_module_files/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
beego.InsertFilter("/rbac/*", beego.BeforeExec, middleware.InspectRoleAccess("/role/*"))
}
... ...
... ... @@ -49,6 +49,7 @@ func (this *ProjectModuleVersionController) UpdateProjectModuleVersion() {
msg = protocol.BadRequestParam(1)
return
}
request.ProjectModuleVersionId, _ = this.GetInt64(":projectModuleVersionId")
header := this.GetRequestHeader(this.Ctx)
data, err := svr.UpdateProjectModuleVersion(header, request)
if err != nil {
... ... @@ -63,15 +64,12 @@ func (this *ProjectModuleVersionController) GetProjectModuleVersion() {
var (
msg *protocol.ResponseMessage
svr = project_module_version.NewProjectModuleVersionService(nil)
request *protocolx.GetProjectModuleVersionRequest
request = &protocolx.GetProjectModuleVersionRequest{}
)
defer func() {
this.Resp(msg)
}()
if err := this.JsonUnmarshal(&request); err != nil {
msg = protocol.BadRequestParam(1)
return
}
request.Id, _ = this.GetInt64(":projectModuleVersionId")
header := this.GetRequestHeader(this.Ctx)
data, err := svr.GetProjectModuleVersion(header, request)
if err != nil {
... ... @@ -86,15 +84,12 @@ func (this *ProjectModuleVersionController) DeleteProjectModuleVersion() {
var (
msg *protocol.ResponseMessage
svr = project_module_version.NewProjectModuleVersionService(nil)
request *protocolx.DeleteProjectModuleVersionRequest
request = &protocolx.DeleteProjectModuleVersionRequest{}
)
defer func() {
this.Resp(msg)
}()
if err := this.JsonUnmarshal(&request); err != nil {
msg = protocol.BadRequestParam(1)
return
}
request.Id, _ = this.GetInt64(":projectModuleVersionId")
header := this.GetRequestHeader(this.Ctx)
data, err := svr.DeleteProjectModuleVersion(header, request)
if err != nil {
... ... @@ -109,15 +104,12 @@ func (this *ProjectModuleVersionController) ListProjectModuleVersion() {
var (
msg *protocol.ResponseMessage
svr = project_module_version.NewProjectModuleVersionService(nil)
request *protocolx.ListProjectModuleVersionRequest
request = &protocolx.ListProjectModuleVersionRequest{}
)
defer func() {
this.Resp(msg)
}()
if err := this.JsonUnmarshal(&request); err != nil {
msg = protocol.BadRequestParam(1)
return
}
request.ProjectModuleId, _ = this.GetInt64("projectModuleId")
header := this.GetRequestHeader(this.Ctx)
data, err := svr.ListProjectModuleVersion(header, request)
if err != nil {
... ...
... ... @@ -46,7 +46,7 @@ func CheckAuthorization(ctx *context.Context) {
return
}
func CheckRoleAccess(ctx *context.Context) {
func CheckRoleAccess(ctx *context.Context, object, method string) {
var (
msg *protocol.ResponseMessage
)
... ... @@ -56,26 +56,39 @@ func CheckRoleAccess(ctx *context.Context) {
}
}()
userId := ctx.Input.GetData("x-mmm-id")
if userId == nil {
msg = protocol.NewResponseMessage(1, errAuthorization.Error())
return
}
validUserRole := cachex.CacheService{}
if ok, _ := validUserRole.ValidUserAccess(int64(userId.(int)), ctx.Input.URL(), ctx.Input.Method()); !ok {
if ok, _ := validUserRole.ValidUserAccess(int64(userId.(int)), object, method); !ok {
msg = protocol.NewResponseMessage(1, errAuthorization.Error())
return
}
return
}
func InspectRoleAccess(skipUrl ...string) func(*context.Context) {
func InspectRoleAccess(parentObject string, skipUrl ...string) func(*context.Context) {
return func(c *context.Context) {
var validParentPermision bool
if len(skipUrl) > 0 {
requestUrl := c.Input.URL()
for _, url := range skipUrl {
if strings.EqualFold(requestUrl, url) {
return
if cachex.KeyMatch3(requestUrl, url) {
validParentPermision = true
break
}
}
}
// 跳过这个路由底下所有接口,使用父模块权限验证
if len(parentObject) > 0 && len(skipUrl) == 0 {
validParentPermision = true
}
CheckAuthorization(c)
CheckRoleAccess(c)
if validParentPermision {
CheckRoleAccess(c, parentObject, c.Input.Method())
return
}
CheckRoleAccess(c, c.Input.URL(), c.Input.Method())
}
}
... ...
... ... @@ -7,13 +7,11 @@ import (
type CreateProjectModuleVersionRequest struct {
// 当前版本编号
CurrentVersionId int64 `json:"currentVersionId" valid:"Required"`
CurrentProjectModuleVersionId int64 `json:"currentProjectModuleVersionId" valid:"Required"`
// 版本号
Version string `json:"version,omitempty" valid:"Required"`
// 项目描述信息
// 版本描述
Description string `json:"description,omitempty"`
// 状态 0:正常 1:删除
Status int `json:"status,omitempty"`
}
func (CreateProjectModuleVersionRequest *CreateProjectModuleVersionRequest) ValidateCommand() error {
... ...
... ... @@ -6,6 +6,8 @@ import (
)
type UpdateProjectModuleVersionRequest struct {
// 项目版本编号
ProjectModuleVersionId int64 `json:"projectModuleVersionId" valid:"Required"`
// 版本号
Version string `json:"version,omitempty"`
// 项目描述信息
... ...
... ... @@ -6,6 +6,7 @@ import (
)
type ListProjectModuleVersionRequest struct {
ProjectModuleId int64 `json:"projectModuleId" valid:"Required"`
}
func (ListProjectModuleVersionRequest *ListProjectModuleVersionRequest) ValidateCommand() error {
... ...