dpjr86761 2017-03-15 18:41
浏览 107
已采纳

根据用户角色控制字段的可见性

I'd like to hide/show some fields of a model depending on User role.

What would be the most idiomatic way to implement it?

I don't really want to create N different types of the same model (where N is amount of User roles). Like: UserEmployee, AdminEmployee, WhateverEmployee.

It would be perfect if there is some solution that uses the tags for it:

type Employee struct {
   ID string `visibility:"admin,user"`
   Name string `visibility:"admin,user"`
   Salary int `visibility:"admin"`
}

jsonBytes, _ := someLib.Marshal(Employee{"1", "John", 5000}, "user")

fmt.Println(string(jsonBytes)) // {"id":"1","name":"John"}

The question is really pretty broad. I just wanted to know how you handle this situation or what is the most common way to do it in the Go community. I want clean and centralized (same for all models) solution that won't require to produce tons of duplicated code.

What have I tried before: I've just tried to use separate models for all cases and cast between them.

  • 写回答

2条回答 默认 最新

  • dphphvs496524 2017-03-17 06:39
    关注
    1. Create an empty struct of your type (Employee in this problem) that will hold the filtered data.
    2. Use the reflect package to compare if the field tag contains the desired tag value (visibility role).
    3. Copy values of base struct to our filter struct when we find a tag match and json marshal the output struct:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "reflect"
        "strings"
    )
    
    type Employee struct {
        ID       string          `visibility:"admin, hr, user" json:"id,omitempty"`
        Name     string          `visibility:"admin, hr, user" json:"name,omitempty"`
        Salary   int             `visibility:"admin, hr" json:"salary,omitempty"`
        Password string          `visibility:"admin" json:"password,omitempty"`
        Rights   map[string]bool `visibility:"admin" json:"rights,omitempty"`
        Boss     *Employee       `visibility:"admin, hr" json:"boss,omitempty"`
    }
    
    func filterEmployee(emp Employee, role string) Employee {
        var fEmployee Employee
        ev := reflect.ValueOf(emp)
        et := reflect.TypeOf(emp)
    
        // Iterate through each field within the struct
        for i := 0; i < ev.NumField(); i++ {
            v := ev.Field(i)
            t := et.Field(i)
            roles := t.Tag.Get("visibility")
    
            if strings.Contains(roles, role) {
                switch i {
                case 0: // ID
                    fEmployee.ID = v.String()
                case 1: // Name
                    fEmployee.Name = v.String()
                case 2: // Salary
                    fEmployee.Salary = int(v.Int())
                case 3: // Password
                    fEmployee.Password = v.String()
                case 4: // Rights
                    fEmployee.Rights = v.Interface().(map[string]bool)
                case 5: // Boss
                    fEmployee.Boss = v.Interface().(*Employee)
                }
            }
        }
        return fEmployee
    }
    
    func main() {
    
        e := Employee{
            "1",
            "Jack",
            100000,
            "password321",
            map[string]bool{"create": false, "update": false},
            &Employee{
                "2",
                "John",
                120000,
                "pwd",
                map[string]bool{"create": true, "update": true},
                nil,
            },
        }
    
        fuser := filterEmployee(e, "user")
        fhr := filterEmployee(e, "hr")
        fadmin := filterEmployee(e, "admin")
    
        buser, err := json.MarshalIndent(fuser, "", "  ")
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println("Filtering with role user: ")
        fmt.Println(string(buser))
    
        bhr, err := json.MarshalIndent(fhr, "", "  ")
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println("
    Filtering with role hr: ")
        fmt.Println(string(bhr))
    
        badmin, err := json.MarshalIndent(fadmin, "", "  ")
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println("
    Filtering with role admin: ")
        fmt.Println(string(badmin))
    }
    

    Output:

    Filtering with role user: 
    {
      "id": "1",
      "name": "Jack"
    }
    
    Filtering with role hr: 
    {
      "id": "1",
      "name": "Jack",
      "salary": 100000,
      "boss": {
        "id": "2",
        "name": "John",
        "salary": 120000,
        "password": "pwd",
        "rights": {
          "create": true,
          "update": true
        }
      }
    }
    
    Filtering with role admin: 
    {
      "id": "1",
      "name": "Jack",
      "salary": 100000,
      "password": "password321",
      "rights": {
        "create": false,
        "update": false
      },
      "boss": {
        "id": "2",
        "name": "John",
        "salary": 120000,
        "password": "pwd",
        "rights": {
          "create": true,
          "update": true
        }
      }
    }
    

    Playground

    EDIT: Updated answer for asker's request.

    View the old playground for previous answer that ran into issues.

    Old Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 专家修改了标签 5月16日

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP