fix user id, seed data autority
This commit is contained in:
parent
cac2986b59
commit
43f3ad986e
4 changed files with 85 additions and 16 deletions
|
|
@ -111,8 +111,7 @@ func (a *Authority) AssignPermissionsToRole(c *core.Context, roleSlug string, pe
|
|||
}
|
||||
|
||||
// Assigns a role to a given user
|
||||
func (a *Authority) AssignRoleToUser(c *core.Context, userID interface{}, roleSlug string) error {
|
||||
userIDStr := fmt.Sprintf("%v", userID)
|
||||
func (a *Authority) AssignRoleToUser(c *core.Context, userID uint, roleSlug string) error {
|
||||
var role models.Role
|
||||
res := c.GetGorm().Where("slug = ?", roleSlug).First(&role)
|
||||
if res.Error != nil {
|
||||
|
|
@ -122,9 +121,9 @@ func (a *Authority) AssignRoleToUser(c *core.Context, userID interface{}, roleSl
|
|||
return res.Error
|
||||
}
|
||||
var userRole models.UserRole
|
||||
res = c.GetGorm().Where("user_id = ?", userIDStr).Where("role_id = ?", role.ID).First(&userRole)
|
||||
res = c.GetGorm().Where("user_id = ?", userID).Where("role_id = ?", role.ID).First(&userRole)
|
||||
if res.Error != nil && errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
c.GetGorm().Create(&models.UserRole{UserID: userIDStr, RoleID: role.ID})
|
||||
c.GetGorm().Create(&models.UserRole{UserID: userID, RoleID: role.ID})
|
||||
return nil
|
||||
}
|
||||
if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
|
|
@ -135,8 +134,7 @@ func (a *Authority) AssignRoleToUser(c *core.Context, userID interface{}, roleSl
|
|||
}
|
||||
|
||||
// Checks if a role is assigned to a user
|
||||
func (a *Authority) CheckUserRole(c *core.Context, userID interface{}, roleSlug string) (bool, error) {
|
||||
userIDStr := fmt.Sprintf("%v", userID)
|
||||
func (a *Authority) CheckUserRole(c *core.Context, userID uint, roleSlug string) (bool, error) {
|
||||
// find the role
|
||||
var role models.Role
|
||||
res := c.GetGorm().Where("slug = ?", roleSlug).First(&role)
|
||||
|
|
@ -149,7 +147,7 @@ func (a *Authority) CheckUserRole(c *core.Context, userID interface{}, roleSlug
|
|||
|
||||
// check if the role is a assigned
|
||||
var userRole models.UserRole
|
||||
res = c.GetGorm().Where("user_id = ?", userIDStr).Where("role_id = ?", role.ID).First(&userRole)
|
||||
res = c.GetGorm().Where("user_id = ?", userID).Where("role_id = ?", role.ID).First(&userRole)
|
||||
if res.Error != nil {
|
||||
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
|
|
@ -161,11 +159,10 @@ func (a *Authority) CheckUserRole(c *core.Context, userID interface{}, roleSlug
|
|||
}
|
||||
|
||||
// Checks if a permission is assigned to a user
|
||||
func (a *Authority) CheckUserPermission(c *core.Context, userID interface{}, permSlug string) (bool, error) {
|
||||
userIDStr := fmt.Sprintf("%v", userID)
|
||||
func (a *Authority) CheckUserPermission(c *core.Context, userID uint, permSlug string) (bool, error) {
|
||||
// the user role
|
||||
var userRoles []models.UserRole
|
||||
res := c.GetGorm().Where("user_id = ?", userIDStr).Find(&userRoles)
|
||||
res := c.GetGorm().Where("user_id = ?", userID).Find(&userRoles)
|
||||
if res.Error != nil {
|
||||
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
|
|
@ -280,10 +277,9 @@ func (a *Authority) GetAllRoles(c *core.Context) ([]models.Role, error) {
|
|||
}
|
||||
|
||||
// Returns all user assigned roles
|
||||
func (a *Authority) GetUserRoles(c *core.Context, userID interface{}) ([]models.Role, error) {
|
||||
userIDStr := fmt.Sprintf("%v", userID)
|
||||
func (a *Authority) GetUserRoles(c *core.Context, userID uint) ([]models.Role, error) {
|
||||
var userRoles []models.UserRole
|
||||
res := c.GetGorm().Where("user_id = ?", userIDStr).Find(&userRoles)
|
||||
res := c.GetGorm().Where("user_id = ?", userID).Find(&userRoles)
|
||||
if res.Error != nil {
|
||||
return nil, res.Error
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue