Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing UserID in UserRole struct #12

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/harranali/authority
module github.com/pooriaghaedi/authority

go 1.16

Expand Down
2 changes: 1 addition & 1 deletion user-roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down