From e8a43ddde4deb3e9a76f25c3bb13cb039289e8d6 Mon Sep 17 00:00:00 2001 From: Pooria Ghaedi <36617391+pooriaghaedi@users.noreply.github.com> Date: Fri, 1 Sep 2023 16:11:50 +0200 Subject: [PATCH 1/4] Update user-roles.go --- user-roles.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user-roles.go b/user-roles.go index c27a282..4be87a2 100644 --- a/user-roles.go +++ b/user-roles.go @@ -3,7 +3,7 @@ package authority // The link between the users and roles type UserRole struct { ID uint // Unique id (it gets set automatically by the database) - UserID string // The user id + UserID uint // The user id RoleID uint // The role id } From 3f8421bae5082ee1356529c4518da1e395ddb245 Mon Sep 17 00:00:00 2001 From: Pooria Ghaedi <36617391+pooriaghaedi@users.noreply.github.com> Date: Fri, 1 Sep 2023 16:28:40 +0200 Subject: [PATCH 2/4] Update user-roles.go --- user-roles.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user-roles.go b/user-roles.go index 4be87a2..2c01c6b 100644 --- a/user-roles.go +++ b/user-roles.go @@ -3,7 +3,7 @@ package authority // The link between the users and roles type UserRole struct { ID uint // Unique id (it gets set automatically by the database) - UserID uint // The user id + UserID uint // The user id RoleID uint // The role id } From 383e077b35dd6ecb7889737d6eb60886357b1f86 Mon Sep 17 00:00:00 2001 From: Pooria Ghaedi <36617391+pooriaghaedi@users.noreply.github.com> Date: Fri, 1 Sep 2023 17:50:51 +0200 Subject: [PATCH 3/4] Update authority.go --- authority.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/authority.go b/authority.go index 7e8d901..81bbc34 100644 --- a/authority.go +++ b/authority.go @@ -163,8 +163,7 @@ func (a *Authority) AssignPermissionsToRole(roleSlug string, permSlugs []string) // it returns an error in case of any // it returns an error in case the role does not exists // it returns an error in case the role is already assigned -func (a *Authority) AssignRoleToUser(userID interface{}, roleSlug string) error { - userIDStr := fmt.Sprintf("%v", userID) +func (a *Authority) AssignRoleToUser(userID uint, roleSlug string) error { var role Role res := a.DB.Where("slug = ?", roleSlug).First(&role) if res.Error != nil { @@ -174,16 +173,16 @@ func (a *Authority) AssignRoleToUser(userID interface{}, roleSlug string) error return res.Error } var userRole UserRole - res = a.DB.Where("user_id = ?", userIDStr).Where("role_id = ?", role.ID).First(&userRole) + res = a.DB.Where("user_id = ?", userID).Where("role_id = ?", role.ID).First(&userRole) if res.Error != nil && errors.Is(res.Error, gorm.ErrRecordNotFound) { - a.DB.Create(&UserRole{UserID: userIDStr, RoleID: role.ID}) + a.DB.Create(&UserRole{UserID: userID, RoleID: role.ID}) return nil } if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) { return res.Error } - return errors.New(fmt.Sprintf("this role '%v' is aleady assigned to the user", roleSlug)) + return errors.New(fmt.Sprintf("this role '%v' is already assigned to the user", roleSlug)) } // Checks if a role is assigned to a user From 01a1bf2409b5a92c3a07c7b0ca397347c0f4f6ff Mon Sep 17 00:00:00 2001 From: pooriaghaedi Date: Fri, 1 Sep 2023 18:26:58 +0200 Subject: [PATCH 4/4] Fix UID --- README.md | 10 +++++----- authority_test.go | 2 +- go.mod | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 36f58ef..b9b5d9d 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Authority -![Build Status](https://github.com/harranali/authority/actions/workflows/build-main.yml/badge.svg) -![Test Status](https://github.com/harranali/authority/actions/workflows/test-main.yml/badge.svg) -[![Go Report Card](https://goreportcard.com/badge/github.com/harranali/authority)](https://goreportcard.com/report/github.com/harranali/authority) -[![GoDoc](https://godoc.org/github.com/harranali/authority?status.svg)](https://godoc.org/github.com/harranali/authority) +![Build Status](https://github.com/pooriaghaedi/authority/actions/workflows/build-main.yml/badge.svg) +![Test Status](https://github.com/pooriaghaedi/authority/actions/workflows/test-main.yml/badge.svg) +[![Go Report Card](https://goreportcard.com/badge/github.com/pooriaghaedi/authority)](https://goreportcard.com/report/github.com/pooriaghaedi/authority) +[![GoDoc](https://godoc.org/github.com/pooriaghaedi/authority?status.svg)](https://godoc.org/github.com/pooriaghaedi/authority) [![Coverage Status](https://coveralls.io/repos/github/harranali/authority/badge.svg?branch=main)](https://coveralls.io/github/harranali/authority?branch=main&cache=false) Role Based Access Control (RBAC) Go package with database persistence @@ -28,7 +28,7 @@ Role Based Access Control (RBAC) Go package with database persistence # Install 1. Go get the package ```bash -go get github.com/harranali/authority +go get github.com/pooriaghaedi/authority ``` 2. `Authority` uses the `orm` [gorm](https://gorm.io) to communicate with the database. [gorm](https://gorm.io) needs a database driver in order to work properly. you can install the database driver by runnig a command from the list below, for example if you are using `mysql` database, simply run `go get gorm.io/driver/mysql` and so. ```bash diff --git a/authority_test.go b/authority_test.go index e933dec..190d801 100644 --- a/authority_test.go +++ b/authority_test.go @@ -6,8 +6,8 @@ import ( "os" "testing" - "github.com/harranali/authority" "github.com/joho/godotenv" + "github.com/pooriaghaedi/authority" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" diff --git a/go.mod b/go.mod index 0c6ed5c..840e75a 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/harranali/authority +module github.com/pooriaghaedi/authority go 1.16