- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Note: the questions are at the end of this text but I will explain in detail all the context for a better understanding.
我正在开发一个由 3 个模块组成的 Symfony2 应用程序:
- Module for students -> Needs the role ROLE_STUDENT
- Module for teachers -> Needs the role ROLE_TEACHER
- Module for administrators -> Needs the role ROLE_ADMIN
角色层次结构如下:
- ROLE_TEACHER: [ROLE_STUDENT]
- ROLE_ADMIN: [ROLE_TEACHER]
因此:
学生(具有 ROLE_STUDENT 角色)只能访问其模块的所有页面,例如:
教师(具有ROLE_TEACHER角色)可以访问学生模块的所有页面和教师模块的所有页面,例如:
管理员(具有 ROLE_ADMIN 角色)可以访问学生模块的所有页面、教师模块的所有页面以及管理后端。
<小时/>The system uses OAuth to authenticate students and teachers through Twitter, using the bundle HWIOAuthBundle (using theOAuthUserProvider user provider that provides the bundle).
I've achieved to successfully authenticate users but, by default,all users are automatically authenticated with the following roles:[ROLE_USER, ROLE_OAUTH_USER]
Steps for login using OAuth:
A user logs in to the system and automatically:
If the user does not exist in the database:
- Save the nickname in the users table of the database.
- Save the role ROLE_STUDENT (this is the role by default for me) in the users table of the database.
- Authenticate in the system using OAuth but using the ROLE_STUDENT role.If the user exists in the database:
- Check what role has associated the user in the database.
- Authenticate the user in the system using OAuth but using the role associated in the database (i.e.: ROLE_STUDENT or ROLE_TEACHER).
管理员(从管理后端)可以查看昵称列表(Twitter 使用但保存在数据库中)以及为每个昵称分配的角色。管理员应该能够在 ROLE_STUDENT 和 ROLE_TEACHER 之间更改用户角色。
<小时/>如何通过 OAuth (HWIOAuthBundle) 使用我默认所需的角色(如上面所解释的 ROLE_STUDENT)对用户进行身份验证?
如果数据库中存在与角色关联的昵称(ROLE_STUDENT 或 ROLE_TEACHER),如何通过 OAuth (HWIOAuthBundle) 对用户进行身份验证,但使用从数据库加载的角色?
我已经阅读了很多关于这个主题的文章,但我是 Symfony2 的新手,我不知道解决它的最佳和最简单的方法是什么。
提前谢谢您!
PS:如果您对任何事情有任何疑问或疑问,我很乐意尽可能地解释。
最佳答案
更新:
我尝试在最近的一个项目中使用此代码,但它不再起作用了。原因是HWIOAuthBundle更新了几次,配置文件不一样。我将上面的代码放在 github 中,并登录了其他一些社交网络,您可以在 HWIOAuthBundleByExample 找到它。 .
<小时/>我有一周的 symfony2 经验,在过去的几天里,我自己就是这么做的。今天发现你的问题(当我还在研究时)。
我将向您展示我必须做什么、基于哪些数据以及我是如何做的。之后,我将尝试为您提供一些关键链接,希望您能够根据您的需求进行建模。
我的应用程序需要 Facebook 登录和 ADMIN 角色。因为只有几个管理员,所以我在身份验证时只需要几个 Facebook ID,所以我将它们存储在 yaml 数组中。 (请参阅最后如何从数据库加载它们)。
这就是我所做的:
#/app/config.yml #the setup looks different (I need the picture and email credentials)
hwi_oauth:
# name of the firewall in which this bundle is active, this setting MUST be set
firewall_name: secured_area
resource_owners:
facebook:
type: facebook
client_id: %facebook_client_id%
client_secret: %facebook_client_secret%
scope: "email"
infos_url: "https://graph.facebook.com/me?fields=username,name,email,picture.type(square)"
paths:
email: email
profilepicture: picture.data.url
services: #here's where the magic happens
hwi_oauth.user.provider.entity:
class: HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider
ib_user.oauth_user_provider:
class: Acme\DemoBundle\Provider\Provider
arguments: [@session, @doctrine, %admins%]
#app/security.yml
security:
providers:
my_custom_hwi_provider:
id: ib_user.oauth_user_provider
access_control:
- { path: ^/admin, roles: ROLE_SUPER_ADMIN }
#app/parameters.yml
parameters:
#...
facebook_client_id: ###
facebook_client_secret: ###
admins:
- "my.facebook.id"
#Acme\DemoBundle\Provider\Provider
<?php
namespace Acme\DemoBundle\Provider;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use Acme\DemoBundle\Entity\User;
use Acme\DemoBundle\Provider\OAuthUser;
class Provider extends OAuthUserProvider
{
protected $session, $doctrine, $admins;
public function __construct($session, $doctrine, $admins) {
$this->session = $session;
$this->doctrine = $doctrine;
$this->admins = $admins;
}
public function loadUserByUsername($username)
{
return new OAuthUser($username, $this->isUserAdmin($username)); //look at the class below
}
private function isUserAdmin($nickname)
{
return in_array($nickname, $this->admins);
}
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
//data from facebook response
$facebook_id = $response->getUsername();
$nickname = $response->getNickname();
$realname = $response->getRealName();
$email = $response->getEmail();
$avatar = $response->getProfilePicture();
//set data in session
$this->session->set('nickname', $nickname);
$this->session->set('realname', $realname);
$this->session->set('email', $email);
$this->session->set('avatar', $avatar);
//get user by fid
$qb = $this->doctrine->getManager()->createQueryBuilder();
$qb ->select('u.id')
->from('AcmeDemoBundle:User', 'u')
->where('u.fid = :fid')
->setParameter('fid', $facebook_id)
->setMaxResults(1);
$result = $qb->getQuery()->getResult();
//add to database if doesn't exists
if ( !count($result) ) {
$User = new User();
$User->setCreatedAt(new \DateTime());
$User->setNickname($nickname);
$User->setRealname($realname);
$User->setEmail($email);
$User->setAvatar($avatar);
$User->setFID($facebook_id);
$em = $this->doctrine->getManager();
$em->persist($User);
$id = $em->flush();
} else {
$id = $result[0]['id'];
}
//set id
$this->session->set('id', $id);
//@TODO: hmm : is admin
if ($this->isUserAdmin($nickname)) {
$this->session->set('is_admin', true);
}
//parent:: returned value
return $this->loadUserByUsername($response->getNickname());
}
public function supportsClass($class)
{
return $class === 'Acme\\DemoBundle\\Provider\\OAuthUser';
}
}
#Acme\DemoBundle\Provider\OAuthUser
<?php
namespace Acme\DemoBundle\Provider;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUser as HWIOAuthUser;
class OAuthUser extends HWIOAuthUser{
private $isAdmin = false;
public function __construct($username, $isAdmin = false)
{
parent::__construct($username);
$this->isAdmin = $isAdmin;
}
public function getRoles()
{
$roles = array('ROLE_USER', 'ROLE_OAUTH_USER');
if ($this->isAdmin) {
array_push($roles, 'ROLE_SUPER_ADMIN');
}
return $roles;
}
}
所以你几乎可以看到,当 facebook 登录响应出现时,我会进行数据库检查(基于 facebook 图形 ID),并在需要时添加它。另外,我在 session 中设置了一些东西(你不需要这个),之后我还必须返回一个 HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUser 对象(这是 sf2 获取其角色的地方)。所以我扩展了它(这样我就可以访问 $isAdmin)。正如您所说,您需要每个用户的角色,并且还必须编辑它们。为此,您可以使用 ManyToMany 关系实现 getRoles() (通过构造函数授予对原则实体管理器的访问权限)。您可以在此处看到应用:http://symfony.com/doc/current/cookbook/security/entity_provider.html#managing-roles-in-the-database .
正如我所说,你必须对其进行很多调整(我唯一的应用程序是仅限 facebook 的登录,并且具有类似 in_memory 的安全访问权限),但我希望自己在开始时拥有这样的代码。所以我真的希望这对你有帮助。发布您的问题(如果有)。
关于Symfony2 : How to login using OAuth (HWIOAuthBundle) + custom roles (by default and loaded from DB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18264800/
我在大学学习C++时学习了这段代码..后来我在C#中使用了同样的东西...但现在我想在Java中使用它...我在互联网上寻找类似的东西,但我什至不知道如何表达它,以便我得到正确的结果。 所以嗯,请让我
我正在我的 Ruby on Rails Controller 上运行 RSPEC 测试,这是我正在测试的 Controller 操作: Controller 代码: class Customers::
想为我选择的选项卡设置自定义背景,到目前为止,子类化是我自定义 UITAbBar/UITabBarItem 的方式。 问题是:有谁知道(或知道我在哪里可以找到)设置背景的属性是什么? 所选选项卡周围有
您好,我在 commerefacades-beans.xml 中创建了 eProductForm bean,我添加了 ProductData 的自定义属性。 然后在commercewebs
我有两个表:1. 客户2. customer_order 客户表包含客户数据(duh),customer_order 包含所有订单。我可以在 customer.id=customer_order.id
在我的 TableView 中,我有一个 NSMutableArray *currList 的数据源 - 它包含对象 Agent 的对象。我创建了自定义的 TableCell 并正确设置了所有内容。我
是否建议使用自引用泛型继承? public abstract class Entity { public Guid Id {get; set;} public int Version
我正在尝试为我的 Grafana 安装使用自定义文件 ( custom.ini )。不幸的是,这不起作用。 我做了什么: 安装了一台装有 CentOS 7 的虚拟机 添加了 Grafana Yum R
我被分配了两个给定类的作业,一个是抽象父类 Lot.java,另一个是测试类 TestLots.java。我不应该编辑其中任何一个。任务是创建Lot的两个子类,使TestLots中的错误不再是错误。
我是 Botpress 的新手。 我刚刚安装了 Botpress 的最新版本“botpress-ce-v11_0_1-win-x64”。 我浏览了文档,发现了一些关于内容类型、内容元素和内容渲染的解释
我一直在四处寻找,但我还没有找到任何东西,除了 Qt3 的旧文档和 qt 设计器的 3.x 版。 我会举个例子,并不是因为我的项目是 GPL 而不能提供代码,而是为了简单起见。 示例:您正在为您的应用
场景 我有一个自定义规则来验证订单的运费: public class OrderValidator : BaseValidator { private string CustomInfo {
我有用于身份验证的自定义拦截器: @Named("authInterceptor") @Provides @Singleton fun providesAuthIntercep
如果有人没有添加照片,我想显示默认头像图像。我假设我需要在模型或助手中执行自定义 getter。 如果我做 getter,它会看起来像这样吗: def avatar_url "default_ur
我正在使用 Google Search API,但遇到了一些麻烦。这个请求(在 Python 中,使用 requests 库)工作正常 res = requests.get("https://www.
我使用 MSKLC 制作了自定义键盘布局。 我以为我仔细按照说明操作了chose appropriate values对于LOCALENAME和 LOCALID参数。 但是,在通过按 Win+Spac
我正在使用 simpleframework解析 XML 字符串并将其转换为对象。 Serializer serializer = new Persister(); try { Customer
我正在使用 C# 控制台应用程序从 MySql 数据库获取一些数据,但在正确查询时遇到一些问题 现在的情况: SELECT * FROM Customer WHERE EXISTS ( SELECT
我在我的 iPhone 4S 上运行我的应用程序,我正在使用自定义表格 View Controller 和自定义表格 View 单元格,当我将表格 View 向上滑动到空白区域并同样向下滑动到空白区域
我有一个自定义的 JavaScript 变量,它正在检查 eventAction 是什么,这样我就可以知道是否触发一些转换像素。自定义 Javascript 称为“FacebookConversion
我是一名优秀的程序员,十分优秀!