列出MySQL中文件和文件夹的隐私设置

时间:2018-09-02 09:43:10

标签: php mysql

我想根据我的隐私设置列出文件夹和文件。向创建者显示它时,它没有看到其他指定的用户,没有任何问题。仅列出公用文件夹。

角色:

1:自定义

2:特定用户

3:专用滚筒

4:公开/所有人开放

按创建者查询:

SELECT 
f.file_id, f.is_folder, f.file_name, f.name as folder_name, f.created_at, f.privacy, f.users, f.roles, (SELECT name FROM users WHERE id = f.created_by) AS name, (SELECT surname FROM users WHERE id = f.created_by) AS surname 
FROM files AS f 
WHERE 
CASE 
WHEN f.privacy = 2 THEN ((f.users IS NOT NULL AND FIND_IN_SET(1, f.users)) OR f.created_by = 1) 
WHEN f.privacy = 3 THEN ((f.roles IS NOT NULL AND FIND_IN_SET(1, f.roles)) OR f.created_by = 1) 
WHEN f.privacy = 4 THEN (f.created_by = 1 OR f.created_by <> 1) 
ELSE f.created_by = 1 
END 
AND f.folder_id = 0 
ORDER BY f.name ASC LIMIT 0, 25

http://sqlfiddle.com/#!9/bae05e/5/0

受让人查询:

SELECT 
f.file_id, f.is_folder, f.file_name, f.name as folder_name, f.created_at, f.privacy, f.users, f.roles, (SELECT name FROM users WHERE id = f.created_by) AS name, (SELECT surname FROM users WHERE id = f.created_by) AS surname 
FROM files AS f 
WHERE 
CASE 
WHEN f.privacy = 2 THEN ((f.users IS NOT NULL AND FIND_IN_SET(10, f.users)) OR f.created_by = 10) 
WHEN f.privacy = 3 THEN ((f.roles IS NOT NULL AND FIND_IN_SET(2, f.roles)) OR f.created_by = 10) 
WHEN f.privacy = 4 THEN (f.created_by = 10 OR f.created_by <> 10) 
ELSE f.created_by = 10 
END 
AND f.folder_id = 0 
ORDER BY f.name ASC LIMIT 0, 25

http://sqlfiddle.com/#!9/bae05e/6/0

1 个答案:

答案 0 :(得分:0)

我对查询做了一些修正

  1. 代替获取用户信息的嵌套查询,向用户简单的LEFT JOIN
  2. 将CASES更改为更简单的独立IFS,每个都返回TRUE或false,这使我可以从中提取u.id = 10
  3. 使用LOCATE而不是FIND_INSET来搜索字段内的用户/滚轴ID

希望这会有所帮助

SELECT
    f.file_id, f.is_folder, f.file_name, f.name as folder_name, f.created_at, f.privacy, f.users, f.roles
    , u.name, u.surname
FROM
    files AS f
    LEFT JOIN users u ON u.id = f.created_by
WHERE
    (
        (f.privacy = 2 AND LOCATE(IFNULL(f.roles,''),10))
    OR
        (f.privacy = 3 AND LOCATE(IFNULL(f.roles,''),2))
    OR
        f.privacy = 4
    )   
    AND f.folder_id = 0
;