所以我尝试制作一个包含私人配置文件的symfony项目,我使用FriendsOfSymfony,但如果我创建两个用户,每个用户都可以看到其他上传的文件。我试着在多个网站上搜索,但没有找到对我有用的东西。示例:将扩展配置文件实体添加到FOS UserBundle
我想为每个用户创建私人档案来上传文件,除了他们之外,没有人能看到他们(只有管理员和特定用户)。
如果您已扩展
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 id
或user relation
添加到上载的文件中。然后在控制器中列出/显示文件(/profile/my-uploads
)-仅加载属于该特定(已登录)用户的文件。
对于管理员访问,最好是创建特殊的后台或实现用户模拟。
在Symfony文档中阅读更多信息:http://symfony.com/doc/current/components/security/authentication.html