auth-schema-mysql.sql 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. CREATE TABLE IF NOT EXISTS `roles` (
  2. `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  3. `name` varchar(32) NOT NULL,
  4. `description` varchar(255) NOT NULL,
  5. PRIMARY KEY (`id`),
  6. UNIQUE KEY `uniq_name` (`name`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  8. INSERT INTO `roles` (`id`, `name`, `description`) VALUES(1, 'login', 'Login privileges, granted after account confirmation');
  9. INSERT INTO `roles` (`id`, `name`, `description`) VALUES(2, 'admin', 'Administrative user, has access to everything.');
  10. CREATE TABLE IF NOT EXISTS `roles_users` (
  11. `user_id` int(10) UNSIGNED NOT NULL,
  12. `role_id` int(10) UNSIGNED NOT NULL,
  13. PRIMARY KEY (`user_id`,`role_id`),
  14. KEY `fk_role_id` (`role_id`)
  15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  16. CREATE TABLE IF NOT EXISTS `users` (
  17. `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  18. `email` varchar(254) NOT NULL,
  19. `username` varchar(32) NOT NULL DEFAULT '',
  20. `password` varchar(64) NOT NULL,
  21. `logins` int(10) UNSIGNED NOT NULL DEFAULT '0',
  22. `last_login` int(10) UNSIGNED,
  23. PRIMARY KEY (`id`),
  24. UNIQUE KEY `uniq_username` (`username`),
  25. UNIQUE KEY `uniq_email` (`email`)
  26. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  27. CREATE TABLE IF NOT EXISTS `user_tokens` (
  28. `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  29. `user_id` int(11) UNSIGNED NOT NULL,
  30. `user_agent` varchar(40) NOT NULL,
  31. `token` varchar(40) NOT NULL,
  32. `created` int(10) UNSIGNED NOT NULL,
  33. `expires` int(10) UNSIGNED NOT NULL,
  34. PRIMARY KEY (`id`),
  35. UNIQUE KEY `uniq_token` (`token`),
  36. KEY `fk_user_id` (`user_id`),
  37. KEY `expires` (`expires`)
  38. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  39. ALTER TABLE `roles_users`
  40. ADD CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  41. ADD CONSTRAINT `roles_users_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE;
  42. ALTER TABLE `user_tokens`
  43. ADD CONSTRAINT `user_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;