Skip to content

Commit

Permalink
update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
harranali committed May 17, 2021
1 parent 07baa3d commit 48df84d
Showing 1 changed file with 6 additions and 43 deletions.
49 changes: 6 additions & 43 deletions authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func (a *Authority) AssignPermissions(roleName string, permNames []string) error
return errors.New("role record not found")
}

return rRes.Error
}

var perms []Permission
Expand All @@ -100,7 +99,6 @@ func (a *Authority) AssignPermissions(roleName string, permNames []string) error
return errors.New("a permission record not found")
}

return pRes.Error
}

perms = append(perms, perm)
Expand Down Expand Up @@ -135,7 +133,6 @@ func (a *Authority) AssignRole(userID uint, roleName string) error {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return errors.New("missing role record")
}
return res.Error
}

var userRole UserRole
Expand All @@ -150,7 +147,6 @@ func (a *Authority) AssignRole(userID uint, roleName string) error {
return nil
}

return res.Error
}

return errors.New("user have a role assgined")
Expand All @@ -169,7 +165,6 @@ func (a *Authority) CheckRole(userID uint, roleName string) (bool, error) {
return false, errors.New("Role not found")
}

return false, res.Error
}

// check if the role is a ssigned
Expand All @@ -180,7 +175,6 @@ func (a *Authority) CheckRole(userID uint, roleName string) (bool, error) {
return false, nil
}

return false, res.Error
}

return true, nil
Expand All @@ -201,7 +195,6 @@ func (a *Authority) CheckPermission(userID uint, permName string) (bool, error)
return false, errors.New("user doesn't have a role assigned")
}

return false, res.Error
}

// fin the permission
Expand All @@ -212,7 +205,6 @@ func (a *Authority) CheckPermission(userID uint, permName string) (bool, error)
return false, errors.New("permission not found")
}

return false, res.Error
}

// find the role permission
Expand All @@ -223,7 +215,6 @@ func (a *Authority) CheckPermission(userID uint, permName string) (bool, error)
return false, nil
}

return false, res.Error
}

return true, nil
Expand All @@ -243,7 +234,6 @@ func (a *Authority) CheckRolePermission(roleName string, permName string) (bool,
return false, errors.New("role not found")
}

return false, res.Error
}

// find the permission
Expand All @@ -254,7 +244,6 @@ func (a *Authority) CheckRolePermission(roleName string, permName string) (bool,
return false, errors.New("permission not found")
}

return false, res.Error
}

// find the rolePermission
Expand All @@ -265,7 +254,6 @@ func (a *Authority) CheckRolePermission(roleName string, permName string) (bool,
return false, nil
}

return false, res.Error
}

return true, nil
Expand All @@ -282,7 +270,6 @@ func (a *Authority) RevokeRole(userID uint, roleName string) error {
return errors.New("role not found")
}

return res.Error
}

// revoke the role
Expand All @@ -305,7 +292,6 @@ func (a *Authority) RevokePermission(userID uint, permName string) error {
return errors.New("user doesn't have a role assgined")
}

return res.Error
}

// find the permission
Expand All @@ -316,14 +302,10 @@ func (a *Authority) RevokePermission(userID uint, permName string) error {
return errors.New("permission not found")
}

return res.Error
}

// revoke the permission
res = a.DB.Where("role_id = ?", userRole.RoleID).Where("permission_id = ?", perm.ID).Delete(RolePermission{})
if res.Error != nil {
return res.Error
}
a.DB.Where("role_id = ?", userRole.RoleID).Where("permission_id = ?", perm.ID).Delete(RolePermission{})

return nil
}
Expand All @@ -339,7 +321,6 @@ func (a *Authority) RevokeRolePermission(roleName string, permName string) error
return errors.New("role not found")
}

return res.Error
}

// find the permission
Expand All @@ -350,14 +331,10 @@ func (a *Authority) RevokeRolePermission(roleName string, permName string) error
return errors.New("permission not found")
}

return res.Error
}

// revoke the permission
res = a.DB.Where("role_id = ?", role.ID).Where("permission_id = ?", perm.ID).Delete(RolePermission{})
if res.Error != nil {
return res.Error
}
a.DB.Where("role_id = ?", role.ID).Where("permission_id = ?", perm.ID).Delete(RolePermission{})

return nil
}
Expand All @@ -366,10 +343,7 @@ func (a *Authority) RevokeRolePermission(roleName string, permName string) error
func (a *Authority) GetRoles() ([]string, error) {
var result []string
var roles []Role
res := a.DB.Find(&roles)
if res.Error != nil {
return []string{}, res.Error
}
a.DB.Find(&roles)

for _, role := range roles {
result = append(result, role.Name)
Expand All @@ -382,10 +356,7 @@ func (a *Authority) GetRoles() ([]string, error) {
func (a *Authority) GetPermissions() ([]string, error) {
var result []string
var perms []Permission
res := a.DB.Find(&perms)
if res.Error != nil {
return []string{}, res.Error
}
a.DB.Find(&perms)

for _, perm := range perms {
result = append(result, perm.Name)
Expand All @@ -405,7 +376,6 @@ func (a *Authority) DeleteRole(roleName string) error {
return errors.New("role not found")
}

return res.Error
}

// check if the role is assigned to a user
Expand All @@ -420,10 +390,7 @@ func (a *Authority) DeleteRole(roleName string) error {
a.DB.Where("role_id = ?", role.ID).Delete(RolePermission{})

// delete the role
res = a.DB.Where("name = ?", roleName).Delete(Role{})
if res.Error != nil {
return res.Error
}
a.DB.Where("name = ?", roleName).Delete(Role{})

return nil
}
Expand All @@ -439,7 +406,6 @@ func (a *Authority) DeletePermission(permName string) error {
return errors.New("permission not found")
}

return res.Error
}

// check if the permission is assigned to a role
Expand All @@ -451,10 +417,7 @@ func (a *Authority) DeletePermission(permName string) error {
}

// delete the permission
res = a.DB.Where("name = ?", permName).Delete(Permission{})
if res.Error != nil {
return res.Error
}
a.DB.Where("name = ?", permName).Delete(Permission{})

return nil
}
Expand Down

0 comments on commit 48df84d

Please sign in to comment.