用户加密机制设计初步完成

This commit is contained in:
veypi
2021-10-21 18:09:40 +08:00
parent cd7029c298
commit 82b64a4bb2
40 changed files with 1027 additions and 322 deletions
+5
View File
@@ -2,7 +2,9 @@ package role
import (
"OneAuth/cfg"
"OneAuth/libs/auth"
"OneAuth/libs/base"
"OneAuth/libs/oerr"
"OneAuth/models"
"github.com/veypi/OneBD"
"github.com/veypi/OneBD/core"
@@ -17,6 +19,9 @@ type authHandler struct {
}
func (h *authHandler) Get() (interface{}, error) {
if !h.GetAuth(auth.Auth).CanRead() {
return nil, oerr.NoAuth
}
l := make([]*models.Auth, 0, 10)
return &l, cfg.DB().Find(&l).Error
}
+8 -23
View File
@@ -2,6 +2,7 @@ package role
import (
"OneAuth/cfg"
"OneAuth/libs/auth"
"OneAuth/libs/base"
"OneAuth/libs/oerr"
"OneAuth/models"
@@ -20,8 +21,9 @@ type roleHandler struct {
func (h *roleHandler) Get() (interface{}, error) {
id := h.Meta().ParamsInt("id")
aid := h.Meta().ParamsInt("aid")
act := h.Meta().Params("action")
if !h.GetAuth(auth.Role, h.Meta().Params("id")).CanRead() {
return nil, oerr.NoAuth
}
if id > 0 {
role := &models.Role{}
role.ID = uint(id)
@@ -29,21 +31,6 @@ func (h *roleHandler) Get() (interface{}, error) {
if err != nil {
return nil, err
}
if aid <= 0 {
return role, nil
}
if !h.CheckAuth("admin", "").CanDoAny() {
return nil, oerr.NoAuth
}
at := &models.RoleAuth{}
at.RoleID = role.ID
at.AuthID = uint(aid)
defer models.SyncGlobalRoles()
if act == "bind" {
err = cfg.DB().Where(at).FirstOrCreate(at).Error
} else if act == "unbind" {
err = cfg.DB().Where(at).Delete(at).Error
}
return role, nil
}
roles := make([]*models.Role, 0, 10)
@@ -52,7 +39,7 @@ func (h *roleHandler) Get() (interface{}, error) {
}
func (h *roleHandler) Post() (interface{}, error) {
if !h.CheckAuth("role", "").CanCreate() {
if !h.GetAuth(auth.Role).CanCreate() {
return nil, oerr.NoAuth
}
role := &models.Role{}
@@ -61,15 +48,14 @@ func (h *roleHandler) Post() (interface{}, error) {
return nil, err
}
role.ID = 0
if role.Category == 0 || role.Name == "" {
if role.Name == "" {
return nil, oerr.ApiArgsMissing
}
defer models.SyncGlobalRoles()
return role, cfg.DB().Where(role).FirstOrCreate(role).Error
}
func (h *roleHandler) Patch() (interface{}, error) {
if !h.CheckAuth("role", "").CanUpdate() {
if !h.GetAuth(auth.Role).CanUpdate() {
return nil, oerr.NoAuth
}
query := &struct {
@@ -120,7 +106,7 @@ func (h *roleHandler) Patch() (interface{}, error) {
}
func (h *roleHandler) Delete() (interface{}, error) {
if !h.CheckAuth("role").CanDelete() {
if !h.GetAuth(auth.Role).CanDelete() {
return nil, oerr.NoAuth
}
rid := h.Meta().ParamsInt("id")
@@ -133,6 +119,5 @@ func (h *roleHandler) Delete() (interface{}, error) {
if err != nil {
return nil, err
}
defer models.SyncGlobalRoles()
return nil, cfg.DB().Delete(role).Error
}
+1 -1
View File
@@ -8,6 +8,6 @@ import (
func Router(r OneBD.Router) {
r.Set("/role/", roleP, rfc.MethodGet, rfc.MethodPost)
r.Set("/role/:id", roleP, rfc.MethodGet, rfc.MethodDelete, rfc.MethodPatch)
r.Set("/role/:id/:action/:aid", roleP, rfc.MethodGet)
r.Set("/role/:id/:action/:rid", roleP, rfc.MethodGet)
r.Set("/auth/", authP, rfc.MethodGet)
}