support user role attachment
This commit is contained in:
@@ -18,6 +18,10 @@ func init() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(controller.NewUserController); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(controller.NewPermissionController); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -27,11 +31,24 @@ func init() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(service.NewUserService); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// dao
|
||||
if err := container.Container.Provide(dao.NewRoleDao); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(dao.NewUserDao); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(dao.NewUserRoleDao); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// routes
|
||||
if err := container.Container.Provide(routes.NewRoute, dig.Group("route")); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
23
modules/auth/controller/user.go
Executable file
23
modules/auth/controller/user.go
Executable file
@@ -0,0 +1,23 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"atom/providers/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type UserController interface {
|
||||
GetName(*gin.Context) (string, error)
|
||||
}
|
||||
|
||||
type userControllerImpl struct {
|
||||
conf *config.Config
|
||||
}
|
||||
|
||||
func NewUserController(conf *config.Config) UserController {
|
||||
return &userControllerImpl{conf: conf}
|
||||
}
|
||||
|
||||
func (c *userControllerImpl) GetName(ctx *gin.Context) (string, error) {
|
||||
return "User",nil
|
||||
}
|
||||
31
modules/auth/dao/user.go
Executable file
31
modules/auth/dao/user.go
Executable file
@@ -0,0 +1,31 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"atom/database/models"
|
||||
"atom/database/query"
|
||||
"context"
|
||||
)
|
||||
|
||||
type UserDao interface {
|
||||
Create(context.Context, *models.User) (*models.User, error)
|
||||
}
|
||||
|
||||
type userDaoImpl struct {
|
||||
query *query.Query
|
||||
}
|
||||
|
||||
func NewUserDao(query *query.Query) UserDao {
|
||||
return &userDaoImpl{query: query}
|
||||
}
|
||||
func (dao *userDaoImpl) FindByID(ctx context.Context, id uint64) (*models.User, error) {
|
||||
user := dao.query.User
|
||||
return user.WithContext(ctx).Where(user.ID.Eq(id)).First()
|
||||
}
|
||||
|
||||
func (dao *userDaoImpl) Create(ctx context.Context, model *models.User) (*models.User, error) {
|
||||
user := dao.query.User
|
||||
if err := user.WithContext(ctx).Create(model); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
51
modules/auth/dao/user_role.go
Executable file
51
modules/auth/dao/user_role.go
Executable file
@@ -0,0 +1,51 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"atom/database/models"
|
||||
"atom/database/query"
|
||||
"context"
|
||||
)
|
||||
|
||||
type UserRoleDao interface {
|
||||
Exists(context.Context, int) bool
|
||||
Create(context.Context, int, int) error
|
||||
Update(context.Context, int, int) error
|
||||
Delete(context.Context, int, int) error
|
||||
}
|
||||
|
||||
type userRoleDaoImpl struct {
|
||||
query *query.Query
|
||||
}
|
||||
|
||||
func NewUserRoleDao(query *query.Query) UserRoleDao {
|
||||
return &userRoleDaoImpl{query: query}
|
||||
}
|
||||
|
||||
func (dao *userRoleDaoImpl) Exists(ctx context.Context, userID int) bool {
|
||||
userRole := dao.query.UserRole
|
||||
count, _ := userRole.WithContext(ctx).Where(userRole.UserID.Eq(uint64(userID))).Count()
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (dao *userRoleDaoImpl) Create(ctx context.Context, userID, roleID int) error {
|
||||
userRole := dao.query.UserRole
|
||||
return userRole.WithContext(ctx).Create(&models.UserRole{
|
||||
UserID: uint64(userID),
|
||||
RoleID: uint64(roleID),
|
||||
})
|
||||
}
|
||||
|
||||
func (dao *userRoleDaoImpl) Update(ctx context.Context, userID, roleID int) error {
|
||||
userRole := dao.query.UserRole
|
||||
_, err := userRole.WithContext(ctx).Where(userRole.UserID.Eq(uint64(userID))).Update(userRole.RoleID, roleID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *userRoleDaoImpl) Delete(ctx context.Context, userID, roleID int) error {
|
||||
userRole := dao.query.UserRole
|
||||
_, err := userRole.WithContext(ctx).
|
||||
Where(userRole.UserID.Eq(uint64(userID))).
|
||||
Where(userRole.RoleID.Eq(uint64(roleID))).
|
||||
Delete()
|
||||
return err
|
||||
}
|
||||
79
modules/auth/dao/user_role_test.go
Executable file
79
modules/auth/dao/user_role_test.go
Executable file
@@ -0,0 +1,79 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
// 这里的依赖需要被导入,否则会报错
|
||||
"atom/container"
|
||||
"atom/database/models"
|
||||
"atom/database/query"
|
||||
_ "atom/providers"
|
||||
"atom/utils"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v6"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserRoleInjectParams struct {
|
||||
dig.In
|
||||
|
||||
DB *gorm.DB
|
||||
Dao UserRoleDao
|
||||
Query *query.Query
|
||||
Faker *gofakeit.Faker
|
||||
}
|
||||
|
||||
type UserRoleSuite struct {
|
||||
suite.Suite
|
||||
UserRoleInjectParams
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(NewUserRoleDao); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_UserRoleSuite(t *testing.T) {
|
||||
err := container.Container.Invoke(func(p UserRoleInjectParams) {
|
||||
s := &UserRoleSuite{}
|
||||
s.UserRoleInjectParams = p
|
||||
|
||||
suite.Run(t, s)
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func (s *UserRoleSuite) BeforeTest(suiteName, testName string) {
|
||||
utils.TruncateTable(s.DB, s.Query.UserRole.TableName())
|
||||
|
||||
}
|
||||
|
||||
func (s *UserRoleSuite) Test_GetByUserID() {
|
||||
Convey("Test_GetByUserID", s.T(), func() {
|
||||
Reset(func() {
|
||||
s.BeforeTest("_", "Test_GetByUserID")
|
||||
})
|
||||
|
||||
Convey("not exists", func() {
|
||||
has := s.Dao.Exists(context.Background(), 1)
|
||||
So(has, ShouldBeFalse)
|
||||
})
|
||||
|
||||
Convey("exists", func() {
|
||||
_ = s.Query.UserRole.WithContext(context.Background()).Create(&models.UserRole{
|
||||
UserID: 1,
|
||||
RoleID: 1,
|
||||
})
|
||||
|
||||
has := s.Dao.Exists(context.Background(), 1)
|
||||
So(has, ShouldBeTrue)
|
||||
})
|
||||
})
|
||||
}
|
||||
93
modules/auth/dao/user_test.go
Executable file
93
modules/auth/dao/user_test.go
Executable file
@@ -0,0 +1,93 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
// 这里的依赖需要被导入,否则会报错
|
||||
"atom/container"
|
||||
"atom/database/models"
|
||||
"atom/database/query"
|
||||
_ "atom/providers"
|
||||
"atom/utils"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v6"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserInjectParams struct {
|
||||
dig.In
|
||||
|
||||
DB *gorm.DB
|
||||
Dao UserDao
|
||||
Query *query.Query
|
||||
Faker *gofakeit.Faker
|
||||
}
|
||||
|
||||
type UserSuite struct {
|
||||
suite.Suite
|
||||
UserInjectParams
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(NewUserDao); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_UserSuite(t *testing.T) {
|
||||
err := container.Container.Invoke(func(p UserInjectParams) {
|
||||
s := &UserSuite{}
|
||||
s.UserInjectParams = p
|
||||
|
||||
suite.Run(t, s)
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func (s *UserSuite) BeforeTest(suiteName, testName string) {
|
||||
log.Println("BeforeTest: ", testName)
|
||||
|
||||
utils.TruncateTable(s.DB, s.Query.User.TableName())
|
||||
|
||||
switch testName {
|
||||
case "":
|
||||
log.Println("BeforeTest: insert test data")
|
||||
_, _ = s.Dao.Create(context.Background(), &models.User{
|
||||
UUID: s.Faker.UUID(),
|
||||
Username: s.Faker.Username(),
|
||||
Password: s.Faker.Password(true, true, true, true, false, 16),
|
||||
Nickname: s.Faker.Name(),
|
||||
Avatar: s.Faker.ImageURL(100, 100),
|
||||
RoleID: 0,
|
||||
Phone: s.Faker.Phone(),
|
||||
Email: s.Faker.Email(),
|
||||
Status: s.Faker.RandomString([]string{"enable", "disabled"}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UserSuite) Test_Create() {
|
||||
Convey("Test_Create", s.T(), func() {
|
||||
Convey("create", func() {
|
||||
model, err := s.Dao.Create(context.Background(), &models.User{
|
||||
UUID: s.Faker.UUID(),
|
||||
Username: s.Faker.Username(),
|
||||
Password: s.Faker.Password(true, true, true, true, false, 16),
|
||||
Nickname: s.Faker.Name(),
|
||||
Avatar: s.Faker.ImageURL(100, 100),
|
||||
RoleID: 0,
|
||||
Phone: s.Faker.Phone(),
|
||||
Email: s.Faker.Email(),
|
||||
Status: s.Faker.RandomString([]string{"enable", "disabled"}),
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(model.ID, ShouldEqual, 1)
|
||||
})
|
||||
})
|
||||
}
|
||||
39
modules/auth/service/user.go
Executable file
39
modules/auth/service/user.go
Executable file
@@ -0,0 +1,39 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"atom/modules/auth/dao"
|
||||
"context"
|
||||
)
|
||||
|
||||
type UserService interface {
|
||||
AttachRole(context.Context, int, int) error
|
||||
}
|
||||
|
||||
type userService struct {
|
||||
userRoleDao dao.UserRoleDao
|
||||
userDao dao.UserDao
|
||||
}
|
||||
|
||||
func NewUserService(
|
||||
userRoleDao dao.UserRoleDao,
|
||||
userDao dao.UserDao,
|
||||
) UserService {
|
||||
return &userService{
|
||||
userRoleDao: userRoleDao,
|
||||
userDao: userDao,
|
||||
}
|
||||
}
|
||||
|
||||
func (svc *userService) AttachRole(ctx context.Context, userID, roleID int) error {
|
||||
if svc.userRoleDao.Exists(ctx, userID) {
|
||||
return svc.userRoleDao.Update(ctx, userID, roleID)
|
||||
}
|
||||
return svc.userRoleDao.Create(ctx, userID, roleID)
|
||||
}
|
||||
|
||||
func (svc *userService) DetachRole(ctx context.Context, userID, roleID int) error {
|
||||
if !svc.userRoleDao.Exists(ctx, userID) {
|
||||
return nil
|
||||
}
|
||||
return svc.userRoleDao.Delete(ctx, userID, roleID)
|
||||
}
|
||||
Reference in New Issue
Block a user