Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 55489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:23:07+00:00 2026-05-10T17:23:07+00:00

I’m trying to create an access control system. Here’s a stripped down example of

  • 0

I’m trying to create an access control system.

Here’s a stripped down example of what the table I’m trying to control access to looks like:

things table: id   group_id   name 1    1          thing 1 2    1          thing 2 3    1          thing 3 4    1          thing 4 5    2          thing 5 

And the access control table looks like this:

access table: user_id  type   object_id  access 1        group  1          50 1        thing  1          10 1        thing  2          100 

Access can be granted either by specifying the id of the ‘thing’ directly, or granted for an entire group of things by specifying a group id. In the above example, user 1 has been granted an access level of 50 to group 1, which should apply unless there are any other rules granting more specific access to an individual thing.

I need a query that returns a list of things (ids only is okay) along with the access level for a specific user. So using the example above I’d want something like this for user id 1:

desired result: thing_id   access 1          10 2          100 3          50   (things 3 and 4 have no specific access rule, 4          50   so this '50' is from the group rule) 5               (thing 5 has no rules at all, so although I                  still want it in the output, there's no access         level for it) 

The closest I can come up with is this:

SELECT *  FROM things  LEFT JOIN access ON      user_id = 1     AND (         (access.type = 'group' AND access.object_id = things.group_id)         OR (access.type = 'thing' AND access.object_id = things.id)     ) 

But that returns multiple rows, when I only want one for each row in the ‘things’ table. I’m not sure how to get down to a single row for each ‘thing’, or how to prioritise ‘thing’ rules over ‘group’ rules.

If it helps, the database I’m using is PostgreSQL.

Please feel free to leave a comment if there’s any information I’ve missed out.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:23:08+00:00Added an answer on May 10, 2026 at 5:23 pm

    I don’t know the Postgres SQL dialect, but maybe something like:

    select thing.*, coalesce ( ( select access                              from   access                              where  userid = 1                              and    type = 'thing'                              and    object_id = thing.id                            ),                            ( select access                              from   access                              where  userid = 1                              and    type = 'group'                              and    object_id = thing.group_id                            )                          ) from things 

    Incidentally, I don’t like the design. I would prefer the access table to be split into two:

    thing_access (user_id, thing_id, access) group_access (user_id, group_id, access) 

    My query then becomes:

    select thing.*, coalesce ( ( select access                              from   thing_access                              where  userid = 1                              and    thing_id = thing.id                            ),                            ( select access                              from   group_access                              where  userid = 1                              and    group_id = thing.group_id                            )                          ) from things 

    I prefer this because foreign keys can now be used in the access tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 151k
  • Answers 151k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I like the Toad Data Modeller http://www.toadsoft.com/toaddm/toad_data_modeler.htm May 12, 2026 at 10:00 am
  • Editorial Team
    Editorial Team added an answer Adapted from the log4j documentation: import org.apache.log4j.Level; import org.apache.log4j.Logger; import… May 12, 2026 at 10:00 am
  • Editorial Team
    Editorial Team added an answer What I would suggest is something that would cost you… May 12, 2026 at 10:00 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.