Merge remote-tracking branch 'origin/master'
# Conflicts: # ui/src/pages/OrderList.vue
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
@@ -24,6 +25,30 @@ type Row struct {
|
||||
CanAccess bool `json:"can_access"`
|
||||
}
|
||||
|
||||
type RoleDeptPermissionSummary struct {
|
||||
RoleID int `json:"role_id"`
|
||||
RoleTitle string `json:"role_title"`
|
||||
DepartmentCode string `json:"department_code"`
|
||||
DepartmentTitle string `json:"department_title"`
|
||||
ModuleFlags map[string]bool `json:"module_flags"`
|
||||
}
|
||||
|
||||
type ModuleLookupOption struct {
|
||||
Value string `json:"value"`
|
||||
Label string `json:"label"`
|
||||
}
|
||||
|
||||
type ModuleActionLookupOption struct {
|
||||
ModuleCode string `json:"module_code"`
|
||||
Action string `json:"action"`
|
||||
}
|
||||
|
||||
type RoleDeptPermissionListResponse struct {
|
||||
Modules []ModuleLookupOption `json:"modules"`
|
||||
ModuleActions []ModuleActionLookupOption `json:"module_actions"`
|
||||
Rows []RoleDeptPermissionSummary `json:"rows"`
|
||||
}
|
||||
|
||||
type RoleDepartmentPermissionHandler struct {
|
||||
DB *sql.DB
|
||||
Repo *permissions.RoleDepartmentPermissionRepo
|
||||
@@ -37,6 +62,109 @@ func NewRoleDepartmentPermissionHandler(db *sql.DB) *RoleDepartmentPermissionHan
|
||||
}
|
||||
}
|
||||
|
||||
/* ======================================================
|
||||
LIST
|
||||
====================================================== */
|
||||
|
||||
func (h *RoleDepartmentPermissionHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
claims, ok := auth.GetClaimsFromContext(r.Context())
|
||||
if !ok || claims == nil {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
search := strings.TrimSpace(r.URL.Query().Get("search"))
|
||||
|
||||
modRows, err := h.DB.Query(queries.GetModuleLookup)
|
||||
if err != nil {
|
||||
http.Error(w, "module lookup error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer modRows.Close()
|
||||
|
||||
modules := make([]ModuleLookupOption, 0, 32)
|
||||
for modRows.Next() {
|
||||
var m ModuleLookupOption
|
||||
if err := modRows.Scan(&m.Value, &m.Label); err != nil {
|
||||
http.Error(w, "module lookup scan error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
modules = append(modules, m)
|
||||
}
|
||||
|
||||
if err := modRows.Err(); err != nil {
|
||||
http.Error(w, "module lookup rows error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
actionRows, err := h.DB.Query(queries.GetModuleActionLookup)
|
||||
if err != nil {
|
||||
http.Error(w, "module action lookup error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer actionRows.Close()
|
||||
|
||||
moduleActions := make([]ModuleActionLookupOption, 0, 128)
|
||||
for actionRows.Next() {
|
||||
var a ModuleActionLookupOption
|
||||
if err := actionRows.Scan(&a.ModuleCode, &a.Action); err != nil {
|
||||
http.Error(w, "module action scan error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
moduleActions = append(moduleActions, a)
|
||||
}
|
||||
|
||||
if err := actionRows.Err(); err != nil {
|
||||
http.Error(w, "module action rows error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := h.DB.Query(queries.ListRoleDepartmentPermissionSets, search)
|
||||
if err != nil {
|
||||
http.Error(w, "db error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
list := make([]RoleDeptPermissionSummary, 0, 128)
|
||||
for rows.Next() {
|
||||
var item RoleDeptPermissionSummary
|
||||
var rawFlags []byte
|
||||
if err := rows.Scan(
|
||||
&item.RoleID,
|
||||
&item.RoleTitle,
|
||||
&item.DepartmentCode,
|
||||
&item.DepartmentTitle,
|
||||
&rawFlags,
|
||||
); err != nil {
|
||||
http.Error(w, "scan error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
item.ModuleFlags = map[string]bool{}
|
||||
if len(rawFlags) > 0 {
|
||||
if err := json.Unmarshal(rawFlags, &item.ModuleFlags); err != nil {
|
||||
http.Error(w, "module flags parse error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
list = append(list, item)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
http.Error(w, "rows error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_ = json.NewEncoder(w).Encode(RoleDeptPermissionListResponse{
|
||||
Modules: modules,
|
||||
ModuleActions: moduleActions,
|
||||
Rows: list,
|
||||
})
|
||||
}
|
||||
|
||||
/* ======================================================
|
||||
GET
|
||||
====================================================== */
|
||||
|
||||
Reference in New Issue
Block a user