作者 yangfu

添加修改密码、检查密码

... ... @@ -14,7 +14,7 @@ type DestroyAccountCommand struct {
}
func (destroyAccountCommand *DestroyAccountCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (destroyAccountCommand *DestroyAccountCommand) ValidateCommand() error {
... ...
... ... @@ -18,7 +18,7 @@ type PhoneAuthChangePasswordCommand struct {
}
func (phoneAuthChangePasswordCommand *PhoneAuthChangePasswordCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (phoneAuthChangePasswordCommand *PhoneAuthChangePasswordCommand) ValidateCommand() error {
... ...
... ... @@ -16,7 +16,7 @@ type PhoneAuthCheckCommand struct {
}
func (phoneAuthCheckCommand *PhoneAuthCheckCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (phoneAuthCheckCommand *PhoneAuthCheckCommand) ValidateCommand() error {
... ...
... ... @@ -16,7 +16,7 @@ type PhoneAuthResetPasswordCommand struct {
}
func (phoneAuthResetPasswordCommand *PhoneAuthResetPasswordCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (phoneAuthResetPasswordCommand *PhoneAuthResetPasswordCommand) ValidateCommand() error {
... ...
... ... @@ -16,7 +16,7 @@ type PhoneAuthResetPhoneCommand struct {
}
func (phoneAuthResetPhoneCommand *PhoneAuthResetPhoneCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (phoneAuthResetPhoneCommand *PhoneAuthResetPhoneCommand) ValidateCommand() error {
... ...
... ... @@ -14,7 +14,7 @@ type RefreshIMCommand struct {
}
func (refreshIMCommand *RefreshIMCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (refreshIMCommand *RefreshIMCommand) ValidateCommand() error {
... ...
... ... @@ -114,10 +114,30 @@ func (authService *AuthService) PhoneAuthCheck(phoneAuthCheckCommand *command.Ph
defer func() {
transactionContext.RollbackTransaction()
}()
var userBaseRepository domain.UserBaseRepository
if value, err := factory.CreateUserBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userBaseRepository = value
}
userBase, err := userBaseRepository.FindOne(map[string]interface{}{"account": phoneAuthCheckCommand.Phone})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err == domain.ErrorNotFound {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "该用户不存在")
}
if err := userBase.CheckAccountPassword(phoneAuthCheckCommand.Phone, phoneAuthCheckCommand.Password); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
return struct{}{}, nil
}
// 重置密码(忘记密码)
... ... @@ -135,10 +155,34 @@ func (authService *AuthService) PhoneAuthResetPassword(phoneAuthResetPasswordCom
defer func() {
transactionContext.RollbackTransaction()
}()
var userBaseRepository domain.UserBaseRepository
if value, err := factory.CreateUserBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userBaseRepository = value
}
userBase, err := userBaseRepository.FindOne(map[string]interface{}{"account": phoneAuthResetPasswordCommand.Phone})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err == domain.ErrorNotFound {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "该用户不存在")
}
if err := userBase.ResetPassword(phoneAuthResetPasswordCommand.Phone, phoneAuthResetPasswordCommand.Password); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if _, err = userBaseRepository.Save(userBase); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
return struct{}{}, nil
}
// 重置手机号
... ...
package domain
import "time"
import (
"fmt"
"strings"
"time"
)
// 用户基础
type UserBase struct {
... ... @@ -80,3 +84,63 @@ func (userBase *UserBase) Update(data map[string]interface{}) error {
}
return nil
}
/***** 1.模块基础函数 *****/
// AddRelatedUser 添加账号关联的用户
//
// userId 用户id
func (userBase *UserBase) AddRelatedUser(userId int64) bool {
if userId <= 0 {
return false
}
var res = true
for i := range userBase.RelatedUsers {
if userBase.RelatedUsers[i] == userId {
return false
}
}
userBase.RelatedUsers = append(userBase.RelatedUsers, userId)
return res
}
// CheckAccountPassword 检查账号密码
//
// account 账号 (手机号)
// password 密码(sha1)
func (userBase *UserBase) CheckAccountPassword(account, password string) error {
if userBase.Status != int(UserStatusEnable) {
return fmt.Errorf("该用户不存在")
}
if !strings.EqualFold(account, userBase.Account) {
return fmt.Errorf("该用户不存在")
}
if !strings.EqualFold(userBase.Password, password) {
return fmt.Errorf("密码输入错误")
}
return nil
}
// ResetPassword 重置密码
//
// account 账号 (手机号)
// password 密码(sha1)
func (userBase *UserBase) ResetPassword(account, password string) error {
if userBase.Status != int(UserStatusEnable) {
return fmt.Errorf("该用户不存在")
}
if !strings.EqualFold(account, userBase.Account) {
return fmt.Errorf("该用户不存在")
}
userBase.Password = password
return nil
}
func (userBase *UserBase) ResetPhone(oldPhone, newPhone string) error {
if !strings.EqualFold(oldPhone, userBase.Account) {
return fmt.Errorf("该用户不存在")
}
userBase.Account = newPhone
userBase.UserInfo.Phone = newPhone
return nil
}
... ...
... ... @@ -60,6 +60,13 @@ func (ptr *PgCreateUserService) CreateUser(optUser *domain.User, newUser *domain
if user, err = userRepository.Save(newUser); err != nil {
return nil, err
}
// 5.更新绑定用户到userBase
if userBase.AddRelatedUser(user.UserId) {
if userBase, err = userBaseRepository.Save(userBase); err != nil {
return nil, err
}
}
return user, nil
}
... ...
... ... @@ -33,19 +33,19 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp
"created_at",
"updated_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "company_id"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "company_id"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "company_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if company.Identify() == nil {
companyId, err := repository.nextIdentify()
if err != nil {
return company, err
} else {
company.CompanyId = companyId
}
//companyId, err := repository.nextIdentify()
//if err != nil {
// return company, err
//} else {
// company.CompanyId = companyId
//}
if _, err := tx.QueryOne(
pg.Scan(
&company.CompanyId,
... ... @@ -56,7 +56,7 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp
&company.UpdatedAt,
),
fmt.Sprintf("INSERT INTO users.company (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
company.CompanyId,
//company.CompanyId,
company.CompanyConfig,
company.CompanyInfo,
company.Status,
... ...
... ... @@ -39,19 +39,19 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
"parent_id",
"parent_path",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "org_id"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "org_id"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "org_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if org.Identify() == nil {
orgId, err := repository.nextIdentify()
if err != nil {
return org, err
} else {
org.OrgId = orgId
}
//orgId, err := repository.nextIdentify()
//if err != nil {
// return org, err
//} else {
// org.OrgId = orgId
//}
if _, err := tx.QueryOne(
pg.Scan(
&org.OrgId,
... ... @@ -68,7 +68,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
&org.ParentPath,
),
fmt.Sprintf("INSERT INTO users.org (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
org.OrgId,
//org.OrgId,
org.CompanyId,
org.CreatedAt,
org.UpdatedAt,
... ...
... ... @@ -37,19 +37,19 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error)
"created_at",
"updated_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "role_id"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "role_id"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "role_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if role.Identify() == nil {
roleId, err := repository.nextIdentify()
if err != nil {
return role, err
} else {
role.RoleId = roleId
}
//roleId, err := repository.nextIdentify()
//if err != nil {
// return role, err
//} else {
// role.RoleId = roleId
//}
if _, err := tx.QueryOne(
pg.Scan(
&role.RoleId,
... ... @@ -64,7 +64,7 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error)
&role.UpdatedAt,
),
fmt.Sprintf("INSERT INTO users.role (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
role.RoleId,
//role.RoleId,
role.CompanyId,
role.OrgId,
role.RoleType,
... ...
... ... @@ -24,8 +24,9 @@ func (repository *UserBaseRepository) nextIdentify() (int64, error) {
id, err := IdWorker.NextId()
return id, err
}
func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.UserBase, error) {
sqlBuildFields := []string{
var (
sqlBuildFields = []string{
"user_base_id",
"user_info",
"account",
... ... @@ -36,19 +37,23 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
"created_at",
"updated_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "userBase_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
insertFieldsSnippet = sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "user_base_id"))
insertPlaceHoldersSnippet = sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "user_base_id"))
returningFieldsSnippet = sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields = sqlbuilder.RemoveSqlFields(sqlBuildFields, "user_base_id")
updateFieldsSnippet = sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
)
func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.UserBase, error) {
tx := repository.transactionContext.PgTx
if userBase.Identify() == nil {
userBaseId, err := repository.nextIdentify()
if err != nil {
return userBase, err
} else {
userBase.UserBaseId = userBaseId
}
//userBaseId, err := repository.nextIdentify()
//if err != nil {
// return userBase, err
//} else {
// userBase.UserBaseId = userBaseId
//}
if _, err := tx.QueryOne(
pg.Scan(
&userBase.UserBaseId,
... ... @@ -62,7 +67,7 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
&userBase.UpdatedAt,
),
fmt.Sprintf("INSERT INTO users.user_base (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
userBase.UserBaseId,
//userBase.UserBaseId,
userBase.UserInfo,
userBase.Account,
userBase.Password,
... ... @@ -120,7 +125,7 @@ func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{
query.SetWhereByQueryOption("user_base.user_base_id = ?", "userBaseId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
return nil, domain.ErrorNotFound
} else {
return nil, err
}
... ...
... ... @@ -42,19 +42,19 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
"created_at",
"updated_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "user_id"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "user_id"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "user_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if user.Identify() == nil {
userId, err := repository.nextIdentify()
if err != nil {
return user, err
} else {
user.UserId = userId
}
//userId, err := repository.nextIdentify()
//if err != nil {
// return user, err
//} else {
// user.UserId = userId
//}
if _, err := tx.QueryOne(
pg.Scan(
&user.UserId,
... ... @@ -74,7 +74,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
&user.UpdatedAt,
),
fmt.Sprintf("INSERT INTO users.\"user\" (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
user.UserId,
//user.UserId,
user.CompanyId,
user.UserBaseId,
user.UserType,
... ...
... ... @@ -10,7 +10,7 @@ import (
)
var _ = Describe("企业注册", func() {
//var Id int64
return
BeforeEach(func() {
//_, err := pG.DB.QueryOne(
// pg.Scan(&Id),
... ... @@ -23,9 +23,9 @@ var _ = Describe("企业注册", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"companyName": "string",
"companyName": "string1",
"contacts": "string",
"phone": "18860183030",
"phone": "18860183031",
"scale": "string",
"industryCategory": "string",
"password": "string",
... ...
... ... @@ -11,12 +11,11 @@ import (
)
var _ = Describe("手机账号密码检查", func() {
return
var Id int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&Id),
"INSERT INTO s () VALUES () RETURNING _id",
"INSERT INTO \"users\".\"user_base\"(\"user_base_id\", \"user_info\", \"account\", \"password\", \"im\", \"related_user\", \"status\", \"created_at\", \"updated_at\") VALUES (999, '{\"phone\": \"phone\", \"userName\": \"string\"}', 'phone', 'string', '{\"accid\": \"\", \"imToken\": \"\", \"csAccountId\": \"\"}', '{4}', 1, '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08') RETURNING user_base_id;",
)
Expect(err).NotTo(HaveOccurred())
})
... ... @@ -25,7 +24,7 @@ var _ = Describe("手机账号密码检查", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"phone": "string",
"phone": "phone",
"password": "string",
}
httpExpect.POST("/auth/check-password").
... ... @@ -41,7 +40,7 @@ var _ = Describe("手机账号密码检查", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM s WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.user_base WHERE user_base_id = 999")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -11,12 +11,11 @@ import (
)
var _ = Describe("重置密码(忘记密码)", func() {
return
var Id int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&Id),
"INSERT INTO s () VALUES () RETURNING _id",
"INSERT INTO \"users\".\"user_base\"(\"user_base_id\", \"user_info\", \"account\", \"password\", \"im\", \"related_user\", \"status\", \"created_at\", \"updated_at\") VALUES (999, '{\"phone\": \"phone\", \"userName\": \"string\"}', 'phone', 'string', '{\"accid\": \"\", \"imToken\": \"\", \"csAccountId\": \"\"}', '{4}', 1, '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08') RETURNING user_base_id;",
)
Expect(err).NotTo(HaveOccurred())
})
... ... @@ -25,7 +24,7 @@ var _ = Describe("重置密码(忘记密码)", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"phone": "string",
"phone": "phone",
"password": "string",
}
httpExpect.POST("/auth/reset-password").
... ... @@ -41,7 +40,7 @@ var _ = Describe("重置密码(忘记密码)", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM s WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.user_base WHERE user_base_id = 999")
Expect(err).NotTo(HaveOccurred())
})
})
... ...