migrate to go mod, up deps
This commit is contained in:
1 parent
82a3f877a7
commit
00c43353f2
14 files changed
+518
-673
No files matched your search
+34
-37
@@ -2,29 +2,32 @@ package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/integration-system/isp-lib/logger"
|
||||
"msp-admin-service/entity"
|
||||
"msp-admin-service/service"
|
||||
//"github.com/integration-system/isp-lib/token-gen"
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
libUtils "github.com/integration-system/isp-lib/utils"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/utils"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"msp-admin-service/entity"
|
||||
"msp-admin-service/model"
|
||||
"msp-admin-service/service"
|
||||
"msp-admin-service/structure"
|
||||
)
|
||||
|
||||
const adminAuthHeaderName = "x-auth-admin"
|
||||
|
||||
const (
|
||||
ServiceError = "Service is not available now, please try later"
|
||||
ValidationError = "Validation errors"
|
||||
)
|
||||
|
||||
func Logout(metadata metadata.MD) error {
|
||||
token := metadata.Get(adminAuthHeaderName)
|
||||
|
||||
if len(token) == 0 || token[0] == "" {
|
||||
logger.Errorf("Admin AUTH header: %s, not found, received: %v", adminAuthHeaderName, metadata)
|
||||
st := status.New(codes.InvalidArgument, utils.ServiceError)
|
||||
return st.Err()
|
||||
log.Errorf(0, "Admin AUTH header: %s, not found, received: %v", adminAuthHeaderName, metadata)
|
||||
return status.Error(codes.InvalidArgument, ServiceError)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -33,9 +36,8 @@ func GetProfile(metadata metadata.MD) (*structure.AdminUserShort, error) {
|
||||
token := metadata.Get(adminAuthHeaderName)
|
||||
|
||||
if len(token) == 0 || token[0] == "" {
|
||||
logger.Errorf("Admin AUTH header: %s, not found, received: %v", adminAuthHeaderName, metadata)
|
||||
st := status.New(codes.InvalidArgument, utils.ServiceError)
|
||||
return nil, st.Err()
|
||||
log.Errorf(0, "Admin AUTH header: %s, not found, received: %v", adminAuthHeaderName, metadata)
|
||||
return nil, status.Error(codes.InvalidArgument, ServiceError)
|
||||
}
|
||||
|
||||
userId, err := service.GetUserId(token[0])
|
||||
@@ -45,7 +47,7 @@ func GetProfile(metadata metadata.MD) (*structure.AdminUserShort, error) {
|
||||
|
||||
user, err := model.GetUserById(userId)
|
||||
if err != nil {
|
||||
return nil, createUnknownError(err)
|
||||
return nil, err
|
||||
}
|
||||
return &structure.AdminUserShort{
|
||||
Image: user.Image,
|
||||
@@ -59,14 +61,14 @@ func GetProfile(metadata metadata.MD) (*structure.AdminUserShort, error) {
|
||||
func Login(authRequest structure.AuthRequest) (*structure.Auth, error) {
|
||||
user, err := model.GetUserByEmail(authRequest.Email)
|
||||
if user == nil {
|
||||
return nil, status.New(codes.NotFound, "User not found").Err()
|
||||
return nil, status.Error(codes.NotFound, "User not found")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, createUnknownError(err)
|
||||
return nil, err
|
||||
}
|
||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(authRequest.Password))
|
||||
if err != nil {
|
||||
return nil, status.New(codes.Unauthenticated, "Email or password is incorrect").Err()
|
||||
return nil, status.Error(codes.Unauthenticated, "Email or password is incorrect")
|
||||
}
|
||||
|
||||
tokenString, expired, err := service.GenerateToken(user.Id)
|
||||
@@ -83,7 +85,7 @@ func Login(authRequest structure.AuthRequest) (*structure.Auth, error) {
|
||||
func GetUsers(identities structure.UsersRequest) (*structure.UsersResponse, error) {
|
||||
users, err := model.GetUsers(identities)
|
||||
if err != nil {
|
||||
return nil, createUnknownError(err)
|
||||
return nil, err
|
||||
}
|
||||
for i := range *users {
|
||||
(*users)[i].Password = ""
|
||||
@@ -96,20 +98,20 @@ func CreateUpdateUser(user entity.AdminUser) (*entity.AdminUser, error) {
|
||||
if user.Id == 0 {
|
||||
if user.Password == "" {
|
||||
validationErrors := map[string]string{"password": "Requered"}
|
||||
return nil, libUtils.CreateValidationErrorDetails(codes.InvalidArgument,
|
||||
libUtils.ValidationError, validationErrors)
|
||||
return nil, utils.CreateValidationErrorDetails(codes.InvalidArgument,
|
||||
ValidationError, validationErrors)
|
||||
}
|
||||
|
||||
userExists, err := model.GetUserByEmail(user.Email)
|
||||
if err != nil {
|
||||
return nil, createUnknownError(err)
|
||||
return nil, err
|
||||
}
|
||||
if userExists != nil && userExists.Id != 0 {
|
||||
validationErrors := map[string]string{
|
||||
"email": fmt.Sprintf("User with email: %s already exists", user.Email),
|
||||
}
|
||||
return nil, libUtils.CreateValidationErrorDetails(codes.AlreadyExists,
|
||||
libUtils.ValidationError, validationErrors)
|
||||
return nil, utils.CreateValidationErrorDetails(codes.AlreadyExists,
|
||||
ValidationError, validationErrors)
|
||||
}
|
||||
|
||||
err = cryptPassword(&user)
|
||||
@@ -119,16 +121,17 @@ func CreateUpdateUser(user entity.AdminUser) (*entity.AdminUser, error) {
|
||||
|
||||
user, err = model.CreateUser(user)
|
||||
} else {
|
||||
userExists, err := model.GetUserById(user.Id)
|
||||
var userExists *entity.AdminUser
|
||||
userExists, err = model.GetUserById(user.Id)
|
||||
if err != nil {
|
||||
return nil, createUnknownError(err)
|
||||
return nil, err
|
||||
}
|
||||
if userExists.Id == 0 {
|
||||
validationErrors := map[string]string{
|
||||
"id": fmt.Sprintf("User with id: %d not found", user.Id),
|
||||
}
|
||||
return nil, libUtils.CreateValidationErrorDetails(codes.NotFound,
|
||||
libUtils.ValidationError, validationErrors)
|
||||
return nil, utils.CreateValidationErrorDetails(codes.NotFound,
|
||||
ValidationError, validationErrors)
|
||||
}
|
||||
|
||||
if user.Password != "" {
|
||||
@@ -143,7 +146,7 @@ func CreateUpdateUser(user entity.AdminUser) (*entity.AdminUser, error) {
|
||||
user.UpdatedAt = userExists.UpdatedAt
|
||||
}
|
||||
if err != nil {
|
||||
return nil, createUnknownError(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user.Password = ""
|
||||
@@ -156,12 +159,12 @@ func DeleteUser(identities structure.IdentitiesRequest) (*structure.DeleteRespon
|
||||
validationErrors := map[string]string{
|
||||
"ids": "Required",
|
||||
}
|
||||
return nil, libUtils.CreateValidationErrorDetails(codes.InvalidArgument,
|
||||
libUtils.ValidationError, validationErrors)
|
||||
return nil, utils.CreateValidationErrorDetails(codes.InvalidArgument,
|
||||
ValidationError, validationErrors)
|
||||
}
|
||||
count, err := model.DeleteUser(identities)
|
||||
if err != nil {
|
||||
return nil, createUnknownError(err)
|
||||
return nil, err
|
||||
}
|
||||
return &structure.DeleteResponse{Deleted: count}, err
|
||||
}
|
||||
@@ -169,14 +172,8 @@ func DeleteUser(identities structure.IdentitiesRequest) (*structure.DeleteRespon
|
||||
func cryptPassword(user *entity.AdminUser) error {
|
||||
passwordBytes, err := bcrypt.GenerateFromPassword([]byte(user.Password), 12)
|
||||
if err != nil {
|
||||
return createUnknownError(err)
|
||||
return err
|
||||
}
|
||||
user.Password = string(passwordBytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
func createUnknownError(err error) error {
|
||||
logger.Error(err)
|
||||
st := status.New(codes.Unknown, utils.ServiceError)
|
||||
return st.Err()
|
||||
}
|
||||
Reference in new issue
Block a user