duanmei4149 2012-07-31 22:32
浏览 25
已采纳

使用Zend进行隐私控制

I am making a social website using Zend. The site allows users to become friends and access each other's profiles and blogs. I also want users to have control over their privacy, which can take parameters "Friends Only" and "Public". I looked at Zend_Acl but it seems to be only able to to handle single user's accessibility not users have relationship. Any ideas about the best way to do this?

  • 写回答

1条回答 默认 最新

  • dongsi8812 2012-07-31 22:52
    关注

    For your purposes, if you use Zend_Acl, you should look at assertions.

    Given the complex nature of the relationships between users in your applications, most of the access rules you will query seem very dynamic so they will largely rely on assertions that can use more complex logic to determine accessibility.

    You should be able to accomplish what you want using Zend_Acl though.

    You may set up an ACL rule like this:

    $acl->allow('user', 'profile', 'view', new My_Acl_Assertion_UsersAreFriends());
    

    The ACL assertion itself:

    <?php
    
    class My_Acl_Assertion_UsersAreFriends implements Zend_Acl_Assert_Interface
    {
        public function assert(Zend_Acl $acl,
                               Zend_Acl_Role_Interface $role = null,
                               Zend_Acl_Resource_Interface $resource = null,
                               $privilege = null)
        {
            return $this->_usersAreFriends();
        }
    
        protected function _usersAreFriends()
        {
            // get UserID of current logged in user
            // assumes Zend_Auth has stored a User object of the logged in user
            $user   = Zend_Auth::getInstance()->getStorage();
            $userId = $user->getId();
    
            // get the ID of the user profile they are trying to view
            // assume you can pull it from the URL
            // or your controller or a plugin can set this value another way
            $userToView = $this->getRequest()->getParam('id', null);
    
            // call your function that checks the database for the friendship
            $usersAreFriends = usersAreFriends($userId, $userToView);
    
            return $usersAreFriends;
        }
    }
    

    Now with this assertion in place, the access will be denied if the 2 user IDs are not friends.

    Check it like:

    if ($acl->isAllowed('user', 'profile', 'view')) {
        // This will use the UsersAreFriends assertion
    
        // they can view profile
    } else {
        // sorry, friend this person to view their profile
    }
    

    Hope that helps.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大