避免同时访问Web应用程序中的共享资源

I have a web application consisting of users having projects , projects belong to to a single user and are access by the URI:root/project/[pid] .

Now I have a middleware to restrict users from accessing each others projects. Although I also have a feature in my web application by which an user may share their projects with other users, so multiple users can access a single shared project.

The problem is I don't want them to access the same project simultaneously , say if project1 is shared by user1 and user2 . URI root/project/project1_id could be accessed by both but not at the same time .

I am currently thinking how I could do this , I have some ideas but wanted to know if there is best or easier way to do this.

I am thinking of altering the project table in the database to have an active flag which would be set to true every time any user accesses the project web page and unset when he/she leaves the webpage or the session ends.

But I have not sure if this would be the best way to go .

I am using laravel as a framework for my web application . So a solution within that framework would be great

I ended up using the below mentioned solution to the problem:

I added a updated_at timestamp and update_by column in my project table which get update evertime someone updates the project

In the front end I set up a long poll function which checks the project table every couple of seconds

If there is a update in project in the past 5 seconds or so by someone other than me I fetched the entire updated project in the front end.

Store a version number with each project.

When someone starts to edit something in the project, send the current version number to the browser. When saving the edits, send the version number back to the server. If the version numbers match, then save the changes and increment the version. If the version numbers don't match, then someone else has edited the project in the meantime. While a user is editing, periodically call the server to check if the version number has changed and display "another user has changed this project" if it has. You could also save something to the database every time someone starts editing something, so if a second person starts editing the project within a certain time interval, you can display "X is editing this project."

I would use this strategy rather than having a lock that prevents a second person from even starting a new edit, because on the web it's hard to determine if an action that was started will ever be completed. If a user starts an edit but then gets distracted and leaves the browser open, other edits to that project will be blocked until your lock times out.