Help with middleware to check `user` access to `asset` based on access to `school`

Posted 1 year ago by nickdavies07

I've recently started learning Laravel and I'm trying to piece together a basic Asset Management System.

I have three tables - Users, Schools and Assets.

Users->Schools is many to many relationship. Schools->Assets is One to Many.

I have assets set up to be displayed through the assets/{asset} view. i.e. assets/1 asset/3 etc calls show() method in AssetController.

I'm trying to set up a middleware class that will check if the current logged in user is allowed to access the asset, depending on whether they can access that particular school the asset is assigned to.

Is there an easy way of doing this?

My tables are:

Users:

| id | name | email | | ------------- | ------------- | ------------- | | 1 | nick | [email protected] | | 2 | ted | test2@test.com | | ... | ... | ... |

Schools:

| id | name | | ------------- | ------------- | | 1 | School A | | 2 | School B | | ... | ... |

School_User:

| id | school_id | user_id | | ------------- | ------------- | ------------- | | 1 | 1 | 1 | | 2 | 2 | 1 | | 3 | 2 | 2 | | 4 | 2 | 3 | | 5 | 3 | 1 | | 6 | 3 | 2 |

Assets:

| id | name | | ------------- | ------------- | | 1 | Asset A | | 2 | Asset B | | 3 | Asset C | | ... | ... |

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.