Prevent multiple people from editing the same form

北城以北 提交于 2021-02-08 07:51:09

问题


I have a laravel app that is being used by multiple users at once, they all have the possibility to edit some projects in our system.

What I dont want is to have 2 people at the same time editing the same form.

If that happens I would like to tell other users that the current form is locked and is being edited by username.

If user then leaves or submits the form, then the form is opened for other users to work with.

I have checked out both optimistic and pessimistic locking, I don't think this is correct for me

What I was thinking of doing, was to create a unique identifier in the table, that is being edited, and if the user on the page has the identifier, I would lock all other out, but how would I know if the user closes the browser window? then all other users still can't access the page/form?

How would you guys suggest I go about this?


回答1:


Add a nullable locked_to column to your projects database. Then in the Project model add this field to $dates array so it can be converted to Carbon instances automatically.

When someone opens the project for edit just set locked_to field to future date, i think +5 seconds may be a good choice. Edit form should send ajax request every 5 seconds to keep project locked for next 5 seconds.

When user saves project changes, locking will be stopped cause no ajax requests will be send. In that case you also have field which may tell you when the project was opened for the last time.

User won't be able to edit project if locked_to field is equal or greater than new Carbon instance.

UPDATE - more info

There is no need to clear this field. If user didn't finish editing - he just reloaded page, navigated to other page, closed window or browser, died etc... ajax for locking won't be executed anymore, so in the next 5 second currently edited project will be unlocked - locked_to field will contain earlier date than current.



来源:https://stackoverflow.com/questions/40962943/prevent-multiple-people-from-editing-the-same-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!