简体   繁体   English

表记录访问-Zend Framework 2

[英]Table Record Access - Zend Framework 2

I have a login system with Zend\\Authentication, working fine. 我有一个使用Zend \\ Authentication的登录系统,工作正常。 I got user identity saved in the storage, so I also know the logged in user's id. 我将用户身份保存在存储中,因此我也知道登录用户的ID。

Using TableGateway to access database tables. 使用TableGateway访问数据库表。

I am trying to find out the proper way to let users to access only their records (for example, only their customer records marked with that user_id in the customers table) and of course allow them editing only their own records. 我正在尝试找出正确的方法,让用户仅访问其记录(例如,仅访问客户表中标有该user_id的客户记录),并且当然允许他们仅编辑自己的记录。

Yes, I can read identity in Table class and filter records by user_id, or even better call the data retrieval function by user_id from the Controller instead getting Table class involved with the user identity. 是的,我可以读取Table类中的标识并通过user_id过滤记录,甚至更好地通过Controller中的user_id调用数据检索功能,而不是让Table类涉及用户标识。

What I am interested is "What is the proper way to create a module that will return only assigned data for logged in user?". 我感兴趣的是“创建仅返回登录用户的已分配数据的模块的正确方法是什么?”。 I am new to ZF2 and I don't want to learn the wrong way but the already existing and proven one if any. 我是ZF2的新手,我不想学习错误的方法,但是已经学习了并且已经证明的方法。

Thank you for reading. 感谢您的阅读。

the zend authentication service only validate a login against your database but don't take care about in other controllers when calling against the db what data is returned. zend身份验证服务仅针对您的数据库验证登录名,而在针对db调用返回什么数据时不在意其他控制器。

so in this case you are responsable to take care what data is returned. 因此在这种情况下,您有责任注意返回的数据。 something like this 像这样的东西

$assignedUserdata = $database->findBy(array('id' => $this->identity()->getId()));

Old question, but I'll give you and possibly others my solution anyways. 老问题了,但我还是会给您和可能其他人我的解决方案。 Had the same problem and I didn't like the developers to think about the user context every time they access the database. 遇到同样的问题,我不希望开发人员每次访问数据库时都考虑用户上下文。

What I did, was to extend my repositories (=abstraction layer to DB queries) to become user-aware. 我所做的就是将我的存储库(对数据库查询的抽象层)扩展为用户意识。 So my OrderRepository implements UserAwareRepository . 因此,我的OrderRepository implements UserAwareRepository Every UserAwareRepository expects to get a valid user object injected in its constructur, which I use a factory for. 每个UserAwareRepository都希望在其构造文件中注入一个有效的用户对象,我将其用于工厂。

Now what the UserAwareRepository does is to append the WHERE user=$user->getId() to every query. 现在,UserAwareRepository所做的是将WHERE user=$user->getId()附加到每个查询。

Less error-prone. 较少出错。

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

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