【问题标题】:Broadcast different data for different users为不同的用户广播不同的数据
【发布时间】:2018-01-30 15:27:55
【问题描述】:

我正在运行一个带有评论模块的平台。用户可以在那里评论文章,服务器根据角色系统决定用户是否可以编辑或删除他不拥有的评论。

这些标志在服务器端计算并在页面加载时传输到客户端,但现在我想广播新创建的 cmets。

现在我面临某种架构问题:当用户 #1 创建评论时,会广播基于用户 #1 的带有编辑和删除标志的评论,因此如果我是用户 #2,则不正确。

我是否必须在每个用户的私人频道中广播新评论以确定该用户的编辑和删除权限,或者我可以利用什么优势?

【问题讨论】:

    标签: laravel websocket broadcast pusher


    【解决方案1】:

    角色系统应该在后端中管理,所以广播应该只用于通知已经发布了一条新消息(然后你可以将它推送到你的视图中),但是这不需要对 CRUD 权限造成问题,因为这应该基于用户 id 进行管理。例如在控制器中注入Request objectauthorize() 方法中,您可以这样做:

    /**
     * Determine if the user is authorized to edit the message.
     *
     * @return bool
     */
    public function authorize()
    {
        /** 
        *  The next line compares if the id of the logged in user matches         
        *  the foreign `user_id` in the message object.
        */ 
        return auth()->user()->id == $this->user_id;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 1970-01-01
      • 2011-05-26
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 2016-10-31
      相关资源
      最近更新 更多