This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//User -> model for users table | |
type User struct { | |
gorm.Model | |
Name string `json:"name" ` | |
Email string `json:"email" gorm:"unique"` | |
Role string `json:"role" gorm:"-"` | |
Password string `json:"password" ` | |
} | |
//TableName --> Table for Product Model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//AddUser - Register a user | |
func (h userController) AddUser(enforcer *casbin.Enforcer) gin.HandlerFunc { | |
return func(ctx *gin.Context) { | |
var user model.User | |
if err := ctx.ShouldBindJSON(&user); err != nil { | |
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | |
return | |
} | |
utils.HashPassword(&user.Password) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//DBConnection -> return db instance | |
func DBConnection() (*gorm.DB, error) { | |
USER := "root" | |
PASS := "root" | |
HOST := "localhost" | |
PORT := "3306" | |
DBNAME := "casbin-golang" | |
newLogger := logger.New( | |
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
db, _ := model.DBConnection() | |
route.SetupRoutes(db) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SetupRoutes : all the routes are defined here | |
func SetupRoutes(db *gorm.DB) { | |
httpRouter := gin.Default() | |
// Initialize casbin adapter | |
adapter, err := gormadapter.NewAdapterByDB(db) | |
if err != nil { | |
panic(fmt.Sprintf("failed to initialize casbin adapter: %v", err)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Authorize determines if current user has been authorized to take an action on an object. | |
func Authorize(obj string, act string, enforcer *casbin.Enforcer) gin.HandlerFunc { | |
return func(c *gin.Context) { | |
// Get current user/subject | |
sub, existed := c.Get("userID") | |
if !existed { | |
c.AbortWithStatusJSON(401, gin.H{"msg": "User hasn't logged in yet"}) | |
return | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//AuthorizeJWT -> to authorize JWT Token | |
func AuthorizeJWT() gin.HandlerFunc { | |
return func(ctx *gin.Context) { | |
const BearerSchema string = "Bearer " | |
authHeader := ctx.GetHeader("Authorization") | |
if authHeader == "" { | |
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{ | |
"error": "No Authorization header found"}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[request_definition] | |
r = sub, obj, act | |
[policy_definition] | |
p = sub, obj, act | |
[policy_effect] | |
e = some(where (p.eft == allow)) | |
[matchers] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[request_definition] | |
r = sub, dom, obj, act | |
[policy_definition] | |
p = sub, dom, obj, act | |
[role_definition] | |
g = _, _, _ | |
[policy_effect] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[request_definition] | |
r = sub, obj, act | |
[policy_definition] | |
p = sub, obj, act | |
[role_definition] | |
g = _, _ | |
g2 = _, _ |