SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2003
    Location
    Netherlands
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    4 tables, 1 query

    I need to create a permission system, where multiple permissions can be assigned to multiple roles. These roles can then be assigned to multiple users.

    These are the tables where the permissions and roles are stored:
    Code:
    CREATE TABLE some_perms (
    	perm_id mediumint(8) NOT NULL auto_increment,
    	perm_name varchar(32) NOT NULL default '',
    	perm_desc varchar(255) NOT NULL default '',
    	PRIMARY KEY  (perm_id)
    ) TYPE=MyISAM;
    
    CREATE TABLE some_roles (
    	role_id mediumint(8) NOT NULL auto_increment,
    	role_name varchar(32) NOT NULL default '',
    	role_desc varchar(255) NOT NULL default '',
    	PRIMARY KEY  (role_id)
    ) TYPE=MyISAM;
    Now, I need to create 2 tables (don't know if I got it right) to hold what permissions belong to what roles, and what roles belong to what users:
    Code:
    CREATE TABLE some_role_perms (
    	role_id mediumint(8) NOT NULL default '0',
    	perm_id mediumint(8) NOT NULL default '0',
    	perm_status tinyint(1) NOT NULL default '0',
    	KEY role_perm_id (role_id, perm_id)
    ) TYPE=MyISAM;
    
    CREATE TABLE some_user_roles (
    	user_id mediumint(8) NOT NULL default '0',
    	role_id mediumint(8) NOT NULL default '0',
    	KEY user_role_id (user_id, role_id)
    ) TYPE=MyISAM;
    I know how to assign permissions/roles, add new ones, etc. But I have no idea how to get all available permissions for a user id in a single query. I know how to do it in multiple queries, but I want to do it in 1 query. I know it's possible, I just don't know how.

    Could anyone help me on this issue?

    PS: Excuse my english
    Last edited by franky; Aug 1, 2003 at 21:23.

  2. #2
    SitePoint Member
    Join Date
    May 2003
    Location
    Netherlands
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT p.*
    FROM some_user_roles ur,
    some_role_perms rp,
    some_perms p
    WHERE ur.user_id='my_id'
    AND ur.role_id=rp.role_id
    AND rp.perm_id=p.perm_id
    Just off the top of my head, but I think that would work. Let me know.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •