Guest User

Untitled

a guest
May 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. CREATE TABLE `users` (
  2. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  3. `username` VARCHAR(100),
  4. -- other user fields --
  5. );
  6.  
  7. CREATE TABLE `permissions` (
  8. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  9. `name` VARCHAR(50) NOT NULL UNIQUE,
  10. );
  11.  
  12. CREATE TABLE `users_permissions` (
  13. `id` INT NOT NULL AUTO_INCREMENT PRIMARY_KEY,
  14. `user_id` INT NOT NULL,
  15. `permission_id` INT NOT NULL
  16. );
  17.  
  18. INSERT INTO `users` (DEFAULT, 'joe');
  19. INSERT INTO `users` (DEFAULT, 'beth');
  20. INSERT INTO `users` (DEFAULT, 'frank');
  21.  
  22. INSERT INTO `permissions` (DEFAULT, 'Administrator');
  23. INSERT INTO `permissions` (DEFAULT, 'Write Blog');
  24. INSERT INTO `permissions` (DEFAULT, 'Edit Blog');
  25. INSERT INTO `permissions` (DEFAULT, 'Delete Blog');
  26.  
  27. -- joe gets all permissions
  28. INSERT INTO `permissions` (DEFAULT, 1, 1);
  29. INSERT INTO `permissions` (DEFAULT, 1, 2);
  30. INSERT INTO `permissions` (DEFAULT, 1, 3);
  31. INSERT INTO `permissions` (DEFAULT, 1, 4);
  32.  
  33. -- beth can write and edit
  34. INSERT INTO `permissions` (DEFAULT, 2, 2);
  35. INSERT INTO `permissions` (DEFAULT, 2, 3);
  36.  
  37. -- frank can only write
  38. INSERT INTO `permissions` (DEFAULT, 3, 2);
  39.  
  40. const READ = 1;
  41. const WRITE = 2;
  42. const DELETE = 4;
  43. ...
  44.  
  45. read-only: 1
  46. read-write: 3
  47. read & delete, but not write: 5
  48. and so on...
  49.  
  50. SELECT * FROM table t WHERE t.permission & required_permission
Add Comment
Please, Sign In to add comment