diff options
Diffstat (limited to 'user/user.go')
| -rw-r--r-- | user/user.go | 55 | 
1 files changed, 32 insertions, 23 deletions
diff --git a/user/user.go b/user/user.go index dbcbad0..4d0ffcb 100644 --- a/user/user.go +++ b/user/user.go @@ -1,5 +1,5 @@  /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023-2024  Vidhu Kant Sharma <vidhukant@vidhukant.com> + * Copyright (C) 2023-2025  Vidhu Kant Sharma <vidhukant@vidhukant.com>   *   * This program is free software: you can redistribute it and/or modify   * it under the terms of the GNU General Public License as published by @@ -20,7 +20,6 @@ package user  import (  	d "vidhukant.com/openbills/db"  	e "vidhukant.com/openbills/errors" -  u "vidhukant.com/openbills/util"  	"golang.org/x/crypto/bcrypt"  	"gorm.io/gorm"  	"github.com/spf13/viper" @@ -32,35 +31,45 @@ var db *gorm.DB  func init() {  	db = d.DB -	db.AutoMigrate(&User{}) +	db.AutoMigrate(&User{}, &Role{})  	COST = viper.GetInt("cryptography.password_hashing_cost")  } +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", +	"admin", "*.*", +} + +type Role struct { +	ID     uint +	UserID uint +	Name   string +} +  type User struct { -	gorm.Model -  u.Address -	TokenVersion  uint // this can be incremented to disable existing refresh token(s) -  FullName      string -  FirmName      string -  Gstin         string -  Phone         string -  Email         string -  Website       string -	Username      string -	Password      string -	LogoFile      string -	SignatureFile string -	IsVerified    bool // this should be removed and tokens should be issued upon verification -	// will be printed with address on the invoice -	Details       string -	// a note is printed on every invoice. -	// This is the default that gets automatically set -	DefaultInvoiceNote string +	ID           uint +	TokenVersion uint // this can be incremented to disable existing refresh token(s) +  Username     string +  Email        string +	Password     string +	Roles        []Role `gorm:"constraint:OnDelete:CASCADE;"` +} + +func RolesToStringList(roles []Role) []string { +	x := []string{} + +	for _, i := range roles { +		x = append(x, i.Name) +	} + +	return x  }  func CheckPassword(user *User, accountName, method, pass string) error { -	err := GetUserWithAccountName(user, accountName, method) +	err := GetUserByAccountName(user, accountName, method)  	if err != nil {  		return err  	}  |