diff options
Diffstat (limited to 'user')
| -rw-r--r-- | user/controller.go | 24 | ||||
| -rw-r--r-- | user/service.go | 2 | ||||
| -rw-r--r-- | user/user.go | 15 | ||||
| -rw-r--r-- | user/validators.go | 68 | 
4 files changed, 55 insertions, 54 deletions
diff --git a/user/controller.go b/user/controller.go index 7dd519a..3b8c587 100644 --- a/user/controller.go +++ b/user/controller.go @@ -18,32 +18,32 @@  package user  import ( -  e "vidhukant.com/openbills/errors"  	"github.com/gin-gonic/gin"  	"net/http" +	e "vidhukant.com/openbills/errors"  ) -func handleGetUser (ctx *gin.Context) { +func handleGetUser(ctx *gin.Context) {  	var user User -  uId, ok := ctx.Get("UserID") -  if !ok { -    ctx.Error(e.ErrUnauthorized) -    ctx.Abort() -    return -  } +	uId, ok := ctx.Get("UserID") +	if !ok { +		ctx.Error(e.ErrUnauthorized) +		ctx.Abort() +		return +	} -  userId := uId.(uint) +	userId := uId.(uint) -  err := GetUserById(&user, userId) +	err := GetUserById(&user, userId)  	if err != nil {  		ctx.Error(err)  		ctx.Abort()  		return  	} -  // remove password hash from response -  user.Password = "" +	// remove password hash from response +	user.Password = ""  	ctx.JSON(http.StatusOK, gin.H{  		"data": user, diff --git a/user/service.go b/user/service.go index 4dec8bc..a4fc359 100644 --- a/user/service.go +++ b/user/service.go @@ -32,7 +32,7 @@ func GetUserByAccountName(user *User, accountName, method string) error {  		return e.ErrInvalidLoginMethod  	} -	res := db.Where(method + " = ?", accountName).Preload("Roles").Find(&user) +	res := db.Where(method+" = ?", accountName).Preload("Roles").Find(&user)  	if res.Error != nil {  		return res.Error  	} diff --git a/user/user.go b/user/user.go index 4d0ffcb..8f321eb 100644 --- a/user/user.go +++ b/user/user.go @@ -18,16 +18,17 @@  package user  import ( -	d "vidhukant.com/openbills/db" -	e "vidhukant.com/openbills/errors" +	"errors" +	"github.com/spf13/viper"  	"golang.org/x/crypto/bcrypt"  	"gorm.io/gorm" -	"github.com/spf13/viper" -	"errors" +	d "vidhukant.com/openbills/db" +	e "vidhukant.com/openbills/errors"  )  var COST int  var db *gorm.DB +  func init() {  	db = d.DB @@ -36,7 +37,7 @@ func init() {  	COST = viper.GetInt("cryptography.password_hashing_cost")  } -var VALID_ROLES []string = []string { +var VALID_ROLES []string = []string{  	"customer.*", "customer.read", "customer.write", "customer.delete",  	"item.*", "item.read", "item.write", "item.delete",  	"invoice.*", "invoice.read", "invoice.write", "invoice.delete", @@ -52,8 +53,8 @@ type Role struct {  type User struct {  	ID           uint  	TokenVersion uint // this can be incremented to disable existing refresh token(s) -  Username     string -  Email        string +	Username     string +	Email        string  	Password     string  	Roles        []Role `gorm:"constraint:OnDelete:CASCADE;"`  } diff --git a/user/validators.go b/user/validators.go index e9a894c..3207f3a 100644 --- a/user/validators.go +++ b/user/validators.go @@ -18,8 +18,8 @@  package user  import ( -	"strings"  	"github.com/spf13/viper" +	"strings"  	"vidhukant.com/openbills/errors"  	"vidhukant.com/openbills/util"  ) @@ -48,49 +48,49 @@ func validateUsername(username string) error {  	if len(username) > 32 {  		return errors.ErrUsernameTooLong  	} -  -	// (11th October 2025) what the fuck even is this  + +	// (11th October 2025) what the fuck even is this  	// I'm not even deleting this I can't stop laughing -  //  -  // for _, char := range username { -  //   if !strings.Contains(username, string(char)) { -  //     return errors.ErrInvalidUsername -  //   } -  // } +	// +	// for _, char := range username { +	//   if !strings.Contains(username, string(char)) { +	//     return errors.ErrInvalidUsername +	//   } +	// } -  return nil +	return nil  }  func (u *User) validate() error {  	u.Email = strings.TrimSpace(u.Email) -  u.Username = strings.TrimSpace(u.Username) +	u.Username = strings.TrimSpace(u.Username)  	// don't accept empty username -  if u.Username == "" { -    return errors.ErrEmptyUsername -  } else { -    // validate username -    err := validateUsername(u.Username) -    if err != nil { -      return err -    } -  } +	if u.Username == "" { +		return errors.ErrEmptyUsername +	} else { +		// validate username +		err := validateUsername(u.Username) +		if err != nil { +			return err +		} +	} -  // don't accept empty email -  if u.Email == "" { -    return errors.ErrEmptyEmail -  } else { -    // validate email -    if !util.ValidateEmail(u.Email) { -      return errors.ErrInvalidEmail -    } -  } +	// don't accept empty email +	if u.Email == "" { +		return errors.ErrEmptyEmail +	} else { +		// validate email +		if !util.ValidateEmail(u.Email) { +			return errors.ErrInvalidEmail +		} +	} -  // validate password -  err := validatePassword(u.Password) -  if err != nil { -    return err -  } +	// validate password +	err := validatePassword(u.Password) +	if err != nil { +		return err +	}  	return nil  }  |