mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-16 07:46:35 +08:00
初步完成用户电子邮箱绑定(激活)
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
// MessageMediaService 消息媒介服务
|
||||
type MessageMediaService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// FindAllMessageMedias 获取所有支持的媒介
|
||||
func (this *MessageMediaService) FindAllMessageMedias(ctx context.Context, req *pb.FindAllMessageMediasRequest) (*pb.FindAllMessageMediasResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tx = this.NullTx()
|
||||
medias, err := models.SharedMessageMediaDAO.FindAllEnabledMessageMedias(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbMedias := []*pb.MessageMedia{}
|
||||
for _, media := range medias {
|
||||
pbMedias = append(pbMedias, &pb.MessageMedia{
|
||||
Id: int64(media.Id),
|
||||
Type: media.Type,
|
||||
Name: media.Name,
|
||||
Description: media.Description,
|
||||
UserDescription: media.UserDescription,
|
||||
IsOn: media.IsOn,
|
||||
})
|
||||
}
|
||||
return &pb.FindAllMessageMediasResponse{MessageMedias: pbMedias}, nil
|
||||
}
|
||||
|
||||
// UpdateMessageMedias 设置所有支持的媒介
|
||||
func (this *MessageMediaService) UpdateMessageMedias(ctx context.Context, req *pb.UpdateMessageMediasRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateMonitorNode(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mediaMaps := []maps.Map{}
|
||||
for _, media := range req.MessageMedias {
|
||||
mediaMaps = append(mediaMaps, maps.Map{
|
||||
"name": media.Name,
|
||||
"type": media.Type,
|
||||
"description": media.Description,
|
||||
"userDescription": media.UserDescription,
|
||||
"isOn": media.IsOn,
|
||||
})
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = models.SharedMessageMediaDAO.UpdateMessageMedias(tx, mediaMaps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// FindUserPriceInfo 读取用户计费信息
|
||||
func (this *UserService) FindUserPriceInfo(ctx context.Context, req *pb.FindUserPriceInfoRequest) (*pb.FindUserPriceInfoResponse, error) {
|
||||
return nil, this.NotImplementedYet()
|
||||
}
|
||||
|
||||
// UpdateUserPriceType 修改用户计费方式
|
||||
func (this *UserService) UpdateUserPriceType(ctx context.Context, req *pb.UpdateUserPriceTypeRequest) (*pb.RPCSuccess, error) {
|
||||
return nil, this.NotImplementedYet()
|
||||
}
|
||||
|
||||
// UpdateUserPricePeriod 修改用户计费周期
|
||||
func (this *UserService) UpdateUserPricePeriod(ctx context.Context, req *pb.UpdateUserPricePeriodRequest) (*pb.RPCSuccess, error) {
|
||||
return nil, this.NotImplementedYet()
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package services
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
@@ -13,15 +14,15 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// UserService 用户相关服务
|
||||
type UserService struct {
|
||||
BaseService
|
||||
services.BaseService
|
||||
}
|
||||
|
||||
// CreateUser 创建用户
|
||||
@@ -40,59 +41,6 @@ func (this *UserService) CreateUser(ctx context.Context, req *pb.CreateUserReque
|
||||
return &pb.CreateUserResponse{UserId: userId}, nil
|
||||
}
|
||||
|
||||
// RegisterUser 注册用户
|
||||
func (this *UserService) RegisterUser(ctx context.Context, req *pb.RegisterUserRequest) (*pb.RPCSuccess, error) {
|
||||
userId, err := this.ValidateUserNode(ctx, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if userId > 0 {
|
||||
return nil, this.PermissionError()
|
||||
}
|
||||
|
||||
// 注册配置
|
||||
configJSON, err := models.SharedSysSettingDAO.ReadSetting(nil, systemconfigs.SettingCodeUserRegisterConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(configJSON) == 0 {
|
||||
return nil, errors.New("the registration has been disabled")
|
||||
}
|
||||
var config = userconfigs.DefaultUserRegisterConfig()
|
||||
err = json.Unmarshal(configJSON, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !config.IsOn {
|
||||
return nil, errors.New("the registration has been disabled")
|
||||
}
|
||||
|
||||
err = this.RunTx(func(tx *dbs.Tx) error {
|
||||
// 检查用户名
|
||||
exists, err := models.SharedUserDAO.ExistUser(tx, 0, req.Username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
return errors.New("the username exists already")
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
_, err = models.SharedUserDAO.CreateUser(tx, req.Username, req.Password, req.Fullname, req.Mobile, "", req.Email, "", req.Source, config.ClusterId, config.Features, req.Ip, !config.RequireVerification)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// VerifyUser 审核用户
|
||||
func (this *UserService) VerifyUser(ctx context.Context, req *pb.VerifyUserRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
@@ -300,6 +248,7 @@ func (this *UserService) FindEnabledUser(ctx context.Context, req *pb.FindEnable
|
||||
Mobile: user.Mobile,
|
||||
Tel: user.Tel,
|
||||
Email: user.Email,
|
||||
VerifiedEmail: user.VerifiedEmail,
|
||||
Remark: user.Remark,
|
||||
IsOn: user.IsOn,
|
||||
CreatedAt: int64(user.CreatedAt),
|
||||
@@ -360,6 +309,28 @@ func (this *UserService) LoginUser(ctx context.Context, req *pb.LoginUserRequest
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
// 邮箱登录
|
||||
if strings.Contains(req.Username, "@") {
|
||||
// 是否允许
|
||||
registerConfig, err := models.SharedSysSettingDAO.ReadUserRegisterConfig(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if registerConfig != nil && registerConfig.EmailVerification.CanLogin {
|
||||
userId, err := models.SharedUserDAO.CheckUserEmailPassword(tx, req.Username, req.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if userId > 0 {
|
||||
return &pb.LoginUserResponse{
|
||||
UserId: userId,
|
||||
IsOk: true,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 用户名登录
|
||||
userId, err := models.SharedUserDAO.CheckUserPassword(tx, req.Username, req.Password)
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
@@ -825,3 +796,25 @@ func (this *UserService) RenewUserServersState(ctx context.Context, req *pb.Rene
|
||||
IsEnabled: isEnabled,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CheckUserEmailIsUsing 检查邮箱是否被使用
|
||||
func (this *UserService) CheckUserEmailIsUsing(ctx context.Context, req *pb.CheckUserEmailIsUsingRequest) (*pb.CheckUserEmailIsUsingResponse, error) {
|
||||
userId, err := this.ValidateUserNode(ctx, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(req.Email) == 0 {
|
||||
return nil, errors.New("'email' required")
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
emailOwnerUserId, err := models.SharedUserDAO.FindUserIdWithVerifiedEmail(tx, req.Email)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if emailOwnerUserId > 0 && userId != emailOwnerUserId {
|
||||
return &pb.CheckUserEmailIsUsingResponse{IsUsing: true}, nil
|
||||
}
|
||||
return &pb.CheckUserEmailIsUsingResponse{IsUsing: false}, nil
|
||||
}
|
||||
91
internal/rpc/services/users/service_user_ext.go
Normal file
91
internal/rpc/services/users/service_user_ext.go
Normal file
@@ -0,0 +1,91 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
// FindUserPriceInfo 读取用户计费信息
|
||||
func (this *UserService) FindUserPriceInfo(ctx context.Context, req *pb.FindUserPriceInfoRequest) (*pb.FindUserPriceInfoResponse, error) {
|
||||
return nil, this.NotImplementedYet()
|
||||
}
|
||||
|
||||
// UpdateUserPriceType 修改用户计费方式
|
||||
func (this *UserService) UpdateUserPriceType(ctx context.Context, req *pb.UpdateUserPriceTypeRequest) (*pb.RPCSuccess, error) {
|
||||
return nil, this.NotImplementedYet()
|
||||
}
|
||||
|
||||
// UpdateUserPricePeriod 修改用户计费周期
|
||||
func (this *UserService) UpdateUserPricePeriod(ctx context.Context, req *pb.UpdateUserPricePeriodRequest) (*pb.RPCSuccess, error) {
|
||||
return nil, this.NotImplementedYet()
|
||||
}
|
||||
|
||||
// RegisterUser 注册用户
|
||||
func (this *UserService) RegisterUser(ctx context.Context, req *pb.RegisterUserRequest) (*pb.RegisterUserResponse, error) {
|
||||
currentUserId, err := this.ValidateUserNode(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if currentUserId > 0 {
|
||||
return nil, this.PermissionError()
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
// 检查邮箱是否已被使用
|
||||
if len(req.Email) > 0 {
|
||||
emailUserId, err := models.SharedUserDAO.FindUserIdWithVerifiedEmail(tx, req.Email)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if emailUserId > 0 {
|
||||
return nil, errors.New("the email address '" + req.Email + "' is using by other user")
|
||||
}
|
||||
}
|
||||
|
||||
// 注册配置
|
||||
registerConfig, err := models.SharedSysSettingDAO.ReadUserRegisterConfig(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if registerConfig == nil || !registerConfig.IsOn {
|
||||
return nil, errors.New("the registration has been disabled")
|
||||
}
|
||||
|
||||
var requireEmailVerification = false
|
||||
var createdUserId int64
|
||||
err = this.RunTx(func(tx *dbs.Tx) error {
|
||||
// 检查用户名
|
||||
exists, err := models.SharedUserDAO.ExistUser(tx, 0, req.Username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
return errors.New("the username exists already")
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
userId, err := models.SharedUserDAO.CreateUser(tx, req.Username, req.Password, req.Fullname, req.Mobile, "", req.Email, "", req.Source, registerConfig.ClusterId, registerConfig.Features, req.Ip, !registerConfig.RequireVerification)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
createdUserId = userId
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.RegisterUserResponse{
|
||||
UserId: createdUserId,
|
||||
RequireEmailVerification: requireEmailVerification,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user