dq_1984 2014-12-02 20:31
浏览 62
已采纳

如何允许一些权限,而不是其他使用按位系统?

I am very interested in writing a class that will help me with different permissions for different users (ie. ACL class.) I have been researching for a great method and bitwise operation stuck out!

I found a good article How to write a permission system using bits and bitwise operations in PHP. The article explain How to handle permission using bitwise operation.

The only thing that I am not sure on how to do is how to allow a user to write a comment but not post a thread.

so if the permission are set like so:

<?php

    $perms = array(
        'can_post' => 1,
        'can_comment' => 2,
        'can_edit' => 4,
        'can_delete' => 8
    );


   $user_perms = 2;    

//CONDITION #1
if ($user_perms & $perms['can_comment']) {
    /* He/She has permission to do this */
} else {
    /* He/She doesn't have permission */
}


//CONDITION #2
if ($user_perms & $perms['can_post']) {
    /* He/She has permission to do this */
} else {
    /* He/She doesn't have permission */
}

?>

so above CONDITION #1 show work fine and the comments should be allowed but //CONDITION #2 should not be allowed because The user does not have a permission to post!

My question, how to allow a user to only comment but not post? to post you will have '001' and to comment you will have '010'.

  • 写回答

1条回答 默认 最新

  • duanhuanyou6478 2014-12-02 20:34
    关注

    Once you have your $perms array defined, you can actually create your own maps from it:

    // can comment and post but not edit or delete:
    $poster = $perms['can_post'] | $perms['can_comment'];
    
    // can edit and delete, but not comment or post:
    $janitor = $perms['can_delete'] | $perms['can_edit'];
    

    You can combine any number of permissions with the | operator to build permission values.

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

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘