简体   繁体   English

会话,多个选项卡和多租户数据库

[英]Sessions, Multiple Tabs and Multi-tenant database

I'm working on a multi-tenanted app in CodeIgniter. 我正在使用CodeIgniter中的多租户应用程序。 Sessions are handled securely through the database. 通过数据库安全地处理会话。 Both the user_id and company_id are stored in a server session, and most tables have company_id (which relates to the id field in the companies table) and user_id fields. user_id和company_id都存储在服务器会话中,并且大多数表都有company_id(与companys表中的id字段相关)和user_id字段。 When an item is saved or updated into the database, the company_id from the session is automatically appended to the query, and populated into the database. 将项目保存或更新到数据库后,会话中的company_id将自动附加到查询中,并填充到数据库中。

Lets say that UserA has access to both CompanyA (company_id 1) and CompanyB (company_id 2). 假设用户A可以访问CompanyA(company_id 1)和CompanyB(company_id 2)。 He opens up CompanyA in his browser, and opens up a form to save a new product for this company. 他在浏览器中打开CompanyA,并打开一个表格来保存该公司的新产品。 Just before he hits save, he opens up a second tab in the browser, and switches to CompanyB. 在单击保存之前,他在浏览器中打开了第二个选项卡,并切换到CompanyB。 This means that the session for company_id will have changed on the server. 这意味着company_id的会话将在服务器上更改。 Now, he goes back to the first tab, and hits save - he thinks that he is saving a product for CompanyA, but actually the server session has changed, and it saves into CompanyB. 现在,他回到第一个选项卡,然后单击保存-他认为他正在为CompanyA保存产品,但是实际上服务器会话已更改,并且保存到CompanyB中。

I think that this theory-crafting is correct. 我认为这种理论技巧是正确的。 If it is, what is the best way to get around it? 如果是的话,最好的解决方法是什么? The only method that I can think of is to use a hidden company_id field in every single form. 我能想到的唯一方法是在每种形式中都使用隐藏的company_id字段。

You have three choices : Set the company id on a hidden field, Set a token for the form that refers to the session or pass the company id on the url. 您有以下三种选择:在隐藏字段中设置公司ID,为引用该会话的表单设置令牌,或在url上传递公司ID。

I believe the right approch but not the easiest is to pass a token so you can extend it to all of your form that needs it, in Zend you have a Zend_Form_Element_Hash , i don't know about CodeIgnitier but you probably have something similar. 我相信正确的方法(但不是最简单的方法)是传递令牌,以便您可以将其扩展到需要它的所有表单,在Zend中,您有一个Zend_Form_Element_Hash ,我不知道CodeIgnitier,但您可能有类似的东西。

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

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