Can I authorize a user depending the projects they're assigned?

I want users can access (or not) a restricted page, depending a database.

I have 3 tables:

  1. Users:

  2. Projects:
    image

  3. Assignments:
    image

Can I authorize or unauthorize an user, depending the assignment?

I use this:

Sometimes works, sometimes not. So I assume is not an apropiate way to achieve this.