mirror of https://gitee.com/answerdev/answer.git
feat(user-center): allow to create user in admin page
This commit is contained in:
parent
758a7cde95
commit
ef47d3da01
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue