dpspn60064 2013-04-02 21:39
浏览 35
已采纳

从数据库向特定用户显示数据(使用角色?)[关闭]

I want to display some data to specific users. I've already done something similar but the difference is that the role number was static. For example I added a column and gave '1' to specific users, so I wrote if($role == 1) { do something }. That was fine because it's a static number. With this way I'm showing secret information only to users with role 1.

Now I've added a new feature; the admin can upload information for specific users that belong to a group. Info will be different for groups. The data is stored and retrieved just fine. That's not my problem. For example I want to display document A to users 'a1','a2','a3' who are in group A (it's random names, i just want to make it easier for you), document B to users 'a4','a7','a35','a40' (who are in group B) and so on.

I know how to store/retrieve document 'A' and link it with users 'a1','a2','a3' and their group... but I'm not sure how I can show each document to a group. Document A can only be linked with one group (same for B,C,D.. etc).

TABLES
DOCUMENTS
Number - Title - File
1 - Document A - A.doc
2 - Document B - B.doc

DOC_USERS
Doc N - User N
1 - 10
1 - 15
2 - 7

DOC_GROUPS
Doc N - Group N
1 - 1
2 - 2

Instead of saying if($role == 1) as I did before.. I will need something like if ($document == $group). I cannot put 'A', or 'B' or whatever because it's not static though.

Any help?

  • 写回答

1条回答 默认 最新

  • doutale7115 2013-04-02 22:01
    关注

    I would move this logic into the query rather than the view. Pass in the user_id as a parameter and return only the records that the user is allowed to view. It looks like your schema allows you to grant permission to a document based on your userID, or by a user's association with a group (which seems to be missing from the table structure, but you mentioned it in your description)

    select documents.*
    from documents
         inner join doc_users on documents.number= doc_users.doc
    where doc_users.user = ?
    
    Union
    
    select documents.*
    from documents
         inner join doc_groups on documents.number= doc_group.doc
         inner join user_groups on user_groups.group = doc_groups.group
    where user_groups.user = ?
    

    i'm sure this query could be done without a union, but this is pretty clear to read and understand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题