Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat(user-center): allow to create user in admin page

LinkinStarsef47d3da0120
diff --git a/internal/controller_admin/user_backyard_controller.go b/internal/controller_admin/user_backyard_controller.go
index f1a93ca3..cb8fe7d8 100644
--- a/internal/controller_admin/user_backyard_controller.go
+++ b/internal/controller_admin/user_backyard_controller.go
@@ -80,10 +80,6 @@ func (uc *UserAdminController) UpdateUserRole(ctx *gin.Context) {
// @Success 200 {object} handler.RespBody
// @Router /answer/admin/api/user [post]
func (uc *UserAdminController) AddUser(ctx *gin.Context) {
- if plugin.UserCenterEnabled() {
- handler.HandleResponse(ctx, errors.Forbidden(reason.ForbiddenError), nil)
- return
- }
req := &schema.AddUserReq{}
if handler.BindAndCheck(ctx, req) {
return
@@ -106,10 +102,6 @@ func (uc *UserAdminController) AddUser(ctx *gin.Context) {
// @Success 200 {object} handler.RespBody
// @Router /answer/admin/api/user/password [put]
func (uc *UserAdminController) UpdateUserPassword(ctx *gin.Context) {
- if plugin.UserCenterEnabled() {
- handler.HandleResponse(ctx, errors.Forbidden(reason.ForbiddenError), nil)
- return
- }
req := &schema.UpdateUserPasswordReq{}
if handler.BindAndCheck(ctx, req) {
return
diff --git a/internal/repo/user/user_backyard_repo.go b/internal/repo/user/user_backyard_repo.go
index 6c7efe3f..cebb2112 100644
--- a/internal/repo/user/user_backyard_repo.go
+++ b/internal/repo/user/user_backyard_repo.go
@@ -86,6 +86,9 @@ func (ur *userAdminRepo) GetUserInfo(ctx context.Context, userID string) (user *
if err != nil {
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ if !exist {
+ return
+ }
err = tryToDecorateUserInfoFromUserCenter(ctx, ur.data, user)
if err != nil {
return nil, false, err
@@ -102,6 +105,9 @@ func (ur *userAdminRepo) GetUserInfoByEmail(ctx context.Context, email string) (
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
return
}
+ if !exist {
+ return
+ }
err = tryToDecorateUserInfoFromUserCenter(ctx, ur.data, user)
if err != nil {
return nil, false, err
diff --git a/internal/repo/user/user_repo.go b/internal/repo/user/user_repo.go
index 8727267f..b8129f20 100644
--- a/internal/repo/user/user_repo.go
+++ b/internal/repo/user/user_repo.go
@@ -196,6 +196,9 @@ func (ur *userRepo) GetUserCount(ctx context.Context) (count int64, err error) {
}
func tryToDecorateUserInfoFromUserCenter(ctx context.Context, data *data.Data, original *entity.User) (err error) {
+ if original == nil {
+ return nil
+ }
uc, ok := plugin.GetUserCenter()
if !ok {
return nil
diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go
index 7f9a801f..9a0b3a0b 100644
--- a/internal/router/answer_api_router.go
+++ b/internal/router/answer_api_router.go
@@ -248,8 +248,8 @@ func (a *AnswerAPIRouter) RegisterAnswerAdminAPIRouter(r *gin.RouterGroup) {
r.GET("/users/page", a.adminUserController.GetUserPage)
r.PUT("/user/status", a.adminUserController.UpdateUserStatus)
r.PUT("/user/role", a.adminUserController.UpdateUserRole)
- r.POST("/user", middleware.BanAPIForUserCenter, a.adminUserController.AddUser)
- r.PUT("/user/password", middleware.BanAPIForUserCenter, a.adminUserController.UpdateUserPassword)
+ r.POST("/user", a.adminUserController.AddUser)
+ r.PUT("/user/password", a.adminUserController.UpdateUserPassword)
// reason
r.GET("/reasons", a.reasonController.Reasons)
diff --git a/internal/schema/user_external_login_schema.go b/internal/schema/user_external_login_schema.go
index 221ff17f..56b5fc5e 100644
--- a/internal/schema/user_external_login_schema.go
+++ b/internal/schema/user_external_login_schema.go
@@ -69,8 +69,10 @@ type UserCenterUserSettingsResp struct {
}
type UserCenterAdminFunctionAgentResp struct {
- UserStatusAgentEnabled bool `json:"user_status_agent_enabled"`
- UserPasswordAgentEnabled bool `json:"user_password_agent_enabled"`
+ AllowCreateUser bool `json:"allow_create_user"`
+ AllowUpdateUserStatus bool `json:"allow_update_user_status"`
+ AllowUpdateUserPassword bool `json:"allow_update_user_password"`
+ AllowUpdateUserRole bool `json:"allow_update_user_role"`
}
type UserSettingAgent struct {
diff --git a/internal/service/user_external_login/user_center_login_service.go b/internal/service/user_external_login/user_center_login_service.go
index 773196e9..14200298 100644
--- a/internal/service/user_external_login/user_center_login_service.go
+++ b/internal/service/user_external_login/user_center_login_service.go
@@ -204,19 +204,28 @@ func (us *UserCenterLoginService) UserCenterUserSettings(ctx context.Context, us
return resp, nil
}
+// UserCenterAdminFunctionAgent Check in the backend administration interface if the user-related functions
+// are turned off due to turning on the User Center plugin.
func (us *UserCenterLoginService) UserCenterAdminFunctionAgent(ctx context.Context) (
resp *schema.UserCenterAdminFunctionAgentResp, err error) {
- resp = &schema.UserCenterAdminFunctionAgentResp{}
+ resp = &schema.UserCenterAdminFunctionAgentResp{
+ AllowCreateUser: true,
+ AllowUpdateUserStatus: true,
+ AllowUpdateUserPassword: true,
+ AllowUpdateUserRole: true,
+ }
userCenter, ok := plugin.GetUserCenter()
if !ok {
return
}
desc := userCenter.Description()
// If user status agent is enabled, admin can not update user status in answer.
- resp.UserStatusAgentEnabled = desc.UserStatusAgentEnabled
- // If original user system is enabled, admin can update user password in answer.
- // So user password agent is disabled.
- resp.UserPasswordAgentEnabled = !desc.EnabledOriginalUserSystem
+ resp.AllowUpdateUserStatus = !desc.UserStatusAgentEnabled
+
+ // If original user system is enabled, admin can update user password and role in answer.
+ resp.AllowUpdateUserPassword = desc.EnabledOriginalUserSystem
+ resp.AllowUpdateUserRole = desc.EnabledOriginalUserSystem
+ resp.AllowCreateUser = desc.EnabledOriginalUserSystem
return resp, nil
}
diff --git a/internal/service/user_external_login/user_external_login_service.go b/internal/service/user_external_login/user_external_login_service.go
index 24b9037d..b5754221 100644
--- a/internal/service/user_external_login/user_external_login_service.go
+++ b/internal/service/user_external_login/user_external_login_service.go
@@ -15,6 +15,7 @@ import (
usercommon "github.com/answerdev/answer/internal/service/user_common"
"github.com/answerdev/answer/pkg/random"
"github.com/answerdev/answer/pkg/token"
+ "github.com/answerdev/answer/plugin"
"github.com/google/uuid"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
@@ -318,3 +319,33 @@ func (us *UserExternalLoginService) ExternalLoginUnbinding(
return nil, us.userExternalLoginRepo.DeleteUserExternalLogin(ctx, req.UserID, req.ExternalID)
}
+
+// CheckUserStatusInUserCenter check user status in user center
+func (us *UserExternalLoginService) CheckUserStatusInUserCenter(ctx context.Context, userID string) (
+ valid bool, err error) {
+ // If enable user center plugin, user status should be checked by user center
+ userCenter, ok := plugin.GetUserCenter()
+ if !ok {
+ return true, nil
+ }
+ userInfoList, err := us.GetExternalLoginUserInfoList(ctx, userID)
+ if err != nil {
+ return false, err
+ }
+ var thisUcUserInfo *entity.UserExternalLogin
+ for _, t := range userInfoList {
+ if t.Provider == userCenter.Info().SlugName {
+ thisUcUserInfo = t
+ break
+ }
+ }
+ // If this user not login by user center, no need to check user status
+ if thisUcUserInfo == nil {
+ return true, nil
+ }
+ userStatus := userCenter.UserStatus(thisUcUserInfo.ExternalID)
+ if userStatus == plugin.UserStatusDeleted {
+ return false, nil
+ }
+ return true, nil
+}
diff --git a/internal/service/user_service.go b/internal/service/user_service.go
index f9a300c2..e417f5a9 100644
--- a/internal/service/user_service.go
+++ b/internal/service/user_service.go
@@ -119,10 +119,17 @@ func (us *UserService) EmailLogin(ctx context.Context, req *schema.UserEmailLogi
if !us.verifyPassword(ctx, req.Pass, userInfo.Pass) {
return nil, errors.BadRequest(reason.EmailOrPasswordWrong)
}
+ ok, err := us.userExternalLoginService.CheckUserStatusInUserCenter(ctx, userInfo.ID)
+ if err != nil {
+ return nil, err
+ }
+ if !ok {
+ return nil, errors.BadRequest(reason.EmailOrPasswordWrong)
+ }
err = us.userRepo.UpdateLastLoginDate(ctx, userInfo.ID)
if err != nil {
- log.Error("UpdateLastLoginDate", err.Error())
+ log.Errorf("update last login data failed, err: %v", err)
}
roleID, err := us.userRoleService.GetUserRole(ctx, userInfo.ID)