【问题标题】:I have searched for FOSUserBundle private profile, to no avail我搜索了 FOSUserBundle 私人资料,但无济于事
【发布时间】:2016-03-23 09:08:41
【问题描述】:

所以我尝试制作一个包含私人配置文件的 symfony 项目,并且我使用 FriendsOfSymfony,但是如果我创建了两个用户,每个用户都可以看到其他人上传的文件。我试图在多个网站上进行搜索,但未能找到适合我的东西。 例子: Adding extended profile entity to FOS UserBundle

我想为每个用户创建私人配置文件以上传文件,除了他们之外没有人应该能够看到它们(只有管理员和特定用户)。

【问题讨论】:

  • 最简单的方法是扩展 FOSUser ProfileController(覆盖 showAction)并将一些逻辑应用于 showAction 以拒绝其他用户查看配置文件。

标签: php symfony fosuserbundle


【解决方案1】:

如果你已经延长了

Symfony\Bundle\FrameworkBundle\Controller\Controller

或 FOSUserBundle 控制器,您可以尝试执行以下操作:

public function publicProfileAction($handleOrHash = '')
{
    if ($handleOrHash == '') {
        throw new NotFoundHttpException("Unable to find this user");
    }

    $user = $this->getUserService()->getUserByHandleOrHash($handleOrHash);

    if (!$user) {
        throw new NotFoundHttpException("Unable to find this user");
    }

    if ($user != $this->getUser()) {
        throw new AccessDeniedException("You cannot see this user profile");
    }

    return $this->render('MyUserBundle:Default:public-profile.html.twig',
        array('user' => $user)
    );
}

getUserService() 将返回一个对象,该对象可以使用名为 getUserByHandleOrHash() 的方法访问 userRepository,该方法使用存储库进行学说查询。

【讨论】:

  • 我应该把它放在user.php中吗?我对 symfony 没有那么丰富的经验,如果你想给予,我需要更多的指导
  • user.php 是您的用户实体吗?上面的代码将进入控制器内部。您可能最好阅读:symfony.com/doc/master/bundles/FOSUserBundle/… 如果没有看到您的代码,我很难准确地指导您。
  • 感谢您的链接,是的 user.php 是用户实体。我很抱歉回答迟了,会阅读文档,也许会解决问题,感谢您对我的问题的兴趣以及到目前为止您给我的答案!
  • 我将代码添加到 user.php 并扩展了控制器,但我仍然登录两台不同的计算机并在两台计算机上看到相同的内容。有任何想法吗?我已经阅读了 fosuserbundle 覆盖控制器并对其进行了修改。
【解决方案2】:

问题非常广泛。

您需要在您的应用程序中引入身份验证。

并将user iduser relation 添加到您上传的文件中。然后在您列出/显示该文件 (/profile/my-uploads) 的控制器中 - 仅加载属于该特定(登录)用户的文件。

对于管理员访问 - 最好是创建特殊的后台或实施 User Impersonation

在 Symfony 文档中了解更多信息:http://symfony.com/doc/current/components/security/authentication.html

【讨论】:

  • 我在应用程序上进行了身份验证,我可以让需要用户名和密码的用户登录,但任何拥有帐户的人都可以看到任何用户上传的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 2015-01-03
相关资源
最近更新 更多