简体   繁体   English

Symfony3授权流程

[英]Symfony3 Authorization Process

I'm starting with Symfony 3, and I need some guidance about how to proceed with the process of implementing dynamic authorization and authentication. 我从Symfony 3开始,我需要一些关于如何继续实施动态授权和身份验证的指导。

First of all, the authentication bit is mostly done, I've done it according to this link from symfony docs: http://symfony.com/doc/current/security/entity_provider.html . 首先,身份验证位大部分已经完成,我已经根据symfony docs的这个链接完成了它: http//symfony.com/doc/current/security/entity_provider.html What I'm yet to understand is how to implement the interface function getRoles() in order to return a value from the database (I have a table Role related to the User table). 我还要了解的是如何实现接口函数getRoles()以便从数据库返回一个值(我有一个与User表相关的表Role)。

Secondly, the authorization part. 其次,授权部分。 My application will require the end user to create his own access mechanism, in other words, I have an interface where the user creates a Role, then defines what pages that Role will be able to access and what privileges it'll have (create, read, update, delete, and so on). 我的应用程序将要求最终用户创建自己的访问机制,换句话说,我有一个用户创建角色的界面,然后定义角色将能够访问的页面以及它将拥有的权限(创建,读取,更新,删除等)。 Afterwards the roles are attributed to the application users. 之后,角色将归属于应用程序用户。

All in all it is pretty standard stuff so Symfony must have a clean way to do it. 总而言之,它是相当标准的东西,所以Symfony必须有一个干净的方法来做到这一点。 What I figured out so far is that I'll have to use ACLs, so I did as in the docs: http://symfony.com/doc/current/security/acl.html 到目前为止我想到的是我将不得不使用ACL,所以我在文档中做了: http//symfony.com/doc/current/security/acl.html

My honest question here is: What now? 我这里诚实的问题是:现在怎么办? What steps do I take to fully implement the authentication mechanism? 我将采取哪些步骤来完全实现身份验证机制? What should I do now in order to persist and retrieve the access rules? 为了坚持和检索访问规则,我现在应该怎么做? How do I relate them with the user Roles? 如何将它们与用户角色联系起来?

PS: This question might be sort of a duplicate of some other questions here, but truth being told, those questions did not help me either, nor my scavenging in the docs did. PS:这个问题可能与其他一些问题有些重复,但事实是,这些问题对我没有帮助,也没有我在文档中的掠夺。

So, your question is very broad. 所以,你的问题非常广泛。 Anyway it is a good question, so I'm going to try to reply. 无论如何,这是一个很好的问题,所以我将尝试回复。

THE AUTHENTICATION 认证

Nothing to say here, I simply hope you used FOSUserBundle as suggested by the article itself: is the best way to implement a registration/login system in Symfony and it will give you an idea of how the entire process works. 这里没什么好说的,我只希望你按照文章本身的建议使用FOSUserBundle:是在Symfony中实现注册/登录系统的最佳方式,它会让你了解整个过程是如何工作的。 Starting from scratch if you are not a Symfony experienced developer seems to not be the best idea. 如果您不是Symfony经验丰富的开发人员,从头开始似乎不是最好的主意。

Give FOSUserBundle a try! 试试FOSUserBundle吧!

THE AUTHORIZATION PROCESS 授权程序

About authorization you have basically two options: the use of Voters and the use of ACL . 关于授权,您基本上有两个选择:使用选民和使用ACL

In my experience the best option is ever the use of Voters . 根据我的经验,最好的选择是使用Voters

In most cases, in fact, you will have a bidirectional reference (see Doctrine's documentation about this ) in your entities between the User and the object on which (s)he have rights. 实际上,在大多数情况下,您将在用户和他拥有权利的对象之间的实体中具有双向引用(请参阅Doctrine的文档 )。 In this case the ACL is not required and even discouraged. 在这种情况下,ACL不是必需的,甚至不鼓励。

In fact, the ACL does nothing more than creating a relationship between two objects (the User and the Article in your use case). 实际上,ACL只是创建两个对象(用户和用例中的文章)之间的关系。 To manage this relationship it uses a table in the database and so it has to query it to get the relationship and check for the authorization rights. 要管理此关系,它使用数据库中的表,因此必须查询它以获取关系并检查授权权限。

But if you already have a bidirectional reference between the User and the Article/Group directly in your entities, then you already have this relationship in place and so you can use Voters and the use of the ACL is superfluous and even, as said, discouraged as it is a useless duplication. 但是如果您已经在实体中直接在用户和文章/组之间进行双向引用,那么您已经建立了这种关系,因此您可以使用选民,并且使用ACL是多余的,甚至如上所述,不鼓励使用因为它是无用的重复。

If you don't have this bidirectional relationship in your entities, then create it: it will be certainly useful in the future for other things and, anyway, you will have ever the ability to access your linked entities directly from your entities tree! 如果您的实体中没有这种双向关系,那么创建它:它将来对其他事物肯定会有用,无论如何,您将能够直接从实体树访问链接的实体!

More, in your scenario, you cannot use ACL as you will have custom rights/privileges on your objects: the voters, again, are the best option to build this kind of things. 此外,在您的方案中,您不能使用ACL,因为您将拥有对象的自定义权限/权限:选民再次是构建此类事物的最佳选择。

Don't use ACL, but Voters instead 不要使用ACL,而是使用选民

HOW TO PROCEED 如何进行

The first thing I would do, is to list in an interface all the available privileges: after all, they are strictly related to your application business logic, as it is not possible for a user to make someone able to do something that your app cannot do: if your application doesn't implement an editing flow, then it is not possible for a user to give someone the ability to edit an article. 我要做的第一件事就是在interface列出所有可用的权限:毕竟,它们与您的应用程序业务逻辑严格相关,因为用户无法让某人能够执行您的应用无法做到的事情do:如果您的应用程序没有实现编辑流程,则用户无法授予某人编辑文章的权限。 It's obvious. 很明显。

So, something like this may be good: 所以,这样的事情可能会很好:

interface PrivilegesEnum
{
   const CREATE = 1;
   const EDIT   = 2;
   const DELETE = 4;
   const READ   = 8;
   const OTHER  = 16;
   // ... Other privileges
}

As you can see, I've given a numeric value to each privilege: this will give you the ability to use bitmasks that is a really powerful mechanism to manage this kind of things: it allows you to use only one field in the database to list all the privileges. 正如您所看到的,我为每个权限赋予了一个数值:这将使您能够使用位掩码,这是一种非常强大的机制来管理这类事情:它允许您只使用数据库中的一个字段来列出所有权限。

You can read more about bitmasks here: 您可以在此处阅读有关位掩码的更多信息:

I used this system in the past and those are some useful links I collected. 我过去使用过这个系统,这些是我收集的一些有用的链接。 They might help you! 他们可能会帮助你!

BUILD A FORM TO LIST PRIVILEGES 建立一个表格列出特权

Another thing you may find useful is a FormType to list your available privileges: you can do this writing a simple custom FormType . 您可能会觉得有用的另一件事是FormType来列出您的可用权限:您可以编写一个简单的自定义FormType

HOW TO MANAGE ROLES 如何管理角色

To manage roles read how Roles are managed by the Security Component and in FOSUserBundle ( on Stackoverflow ) 要管理角色,请阅读安全组件FOSUserBundle在Stackoverflow上如何管理角色

THE RELATIONSHIP BETWEEN USERS AND GROUPS AND ARTICLES 用户与群体和文章之间的关系

Once you reached this point you should have some more entities, read more about Doctrine relationship mechanisms knowing it better and you should be able to relate your users with their role, their group and the articles. 一旦达到这一点,你应该有更多的实体,阅读更多关于Doctrine关系机制的知识,你应该能够将你的用户与他们的角色,他们的小组和文章联系起来。

Anyway you will have all the required conceptual and practical tools needed to think better at your concrete implementation. 无论如何,您将拥有在具体实施中更好地思考所需的所有必需的概念和实用工具。

FINAL NOTE 最后的说明

As you can see, implement such type how authorization process isn't so simple. 如您所见,实现此类型授权过程不是那么简单。

I suggest you to think very carefully if it is really required in this stage of the development of your app, because if you can defer it in the future, then I suggest you to do it. 我建议你仔细考虑是否真的需要在你的应用程序开发的这个阶段,因为如果你将来推迟它,那么我建议你这样做。

If you want to go online as fast as possible, implementing this system will require a lot of time to learn, implement, debug and refact the code (I'm speaking of weeks, not days!). 如果你想尽快上线,实现这个系统将需要大量的时间来学习,实现,调试和重构代码(我说的是几周,而不是几天!)。

So, if you have all this time, then, go to implement this system. 所以,如果你有这么多时间,那么,去实现这个系统。 But if you feel you haven't all this time, then go with a more "static" system, go online, and then make it more "dynamic". 但是如果你觉得你没有这么久,那就选择一个更“静态”的系统,上线,然后让它更“动态”。

After all, this is the Lean Startup era ! 毕竟,这是精益创业时代

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM