Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

176 linhas
8.8KB

  1. <?php
  2. namespace App\Database\Migrations;
  3. use CodeIgniter\Database\Migration;
  4. class CreateAuthTables extends Migration
  5. {
  6. public function up()
  7. {
  8. // Users
  9. $this->forge->addField([
  10. 'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
  11. 'email' => ['type' => 'varchar', 'constraint' => 255],
  12. 'password_hash' => ['type' => 'varchar', 'constraint' => 255],
  13. 'reset_hash' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
  14. 'reset_at' => ['type' => 'datetime', 'null' => true],
  15. 'reset_expires' => ['type' => 'datetime', 'null' => true],
  16. 'activate_hash' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
  17. 'status' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
  18. 'status_message' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
  19. 'active' => ['type' => 'tinyint', 'constraint' => 1, 'null' => 0, 'default' => 0],
  20. 'force_pass_reset' => ['type' => 'tinyint', 'constraint' => 1, 'null' => 0, 'default' => 0],
  21. 'created_at' => ['type' => 'datetime', 'null' => true],
  22. 'updated_at' => ['type' => 'datetime', 'null' => true],
  23. 'deleted_at' => ['type' => 'datetime', 'null' => true],
  24. 'idprestador' => ['type' => 'int', 'null' => true],
  25. ]);
  26. $this->forge->addKey('id', true);
  27. $this->forge->addUniqueKey('email');
  28. $this->forge->createTable('users', true);
  29. // Auth Login Attempts
  30. $this->forge->addField([
  31. 'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
  32. 'ip_address' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
  33. 'email' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
  34. 'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true], // Only for successful logins
  35. 'date' => ['type' => 'datetime'],
  36. 'success' => ['type' => 'tinyint', 'constraint' => 1],
  37. ]);
  38. $this->forge->addKey('id', true);
  39. $this->forge->addKey('email');
  40. $this->forge->addKey('user_id');
  41. // NOTE: Do NOT delete the user_id or email when the user is deleted for security audits
  42. $this->forge->createTable('auth_logins', true);
  43. /*
  44. * Auth Tokens
  45. * @see https://paragonie.com/blog/2015/04/secure-authentication-php-with-long-term-persistence
  46. */
  47. $this->forge->addField([
  48. 'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
  49. 'selector' => ['type' => 'varchar', 'constraint' => 255],
  50. 'hashedValidator' => ['type' => 'varchar', 'constraint' => 255],
  51. 'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
  52. 'expires' => ['type' => 'datetime'],
  53. ]);
  54. $this->forge->addKey('id', true);
  55. $this->forge->addKey('selector');
  56. $this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE');
  57. $this->forge->createTable('auth_tokens', true);
  58. // Password Reset Table
  59. $this->forge->addField([
  60. 'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
  61. 'email' => ['type' => 'varchar', 'constraint' => 255],
  62. 'ip_address' => ['type' => 'varchar', 'constraint' => 255],
  63. 'user_agent' => ['type' => 'varchar', 'constraint' => 255],
  64. 'token' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
  65. 'created_at' => ['type' => 'datetime', 'null' => false],
  66. ]);
  67. $this->forge->addKey('id', true);
  68. $this->forge->createTable('auth_reset_attempts', true);
  69. // Activation Attempts Table
  70. $this->forge->addField([
  71. 'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
  72. 'ip_address' => ['type' => 'varchar', 'constraint' => 255],
  73. 'user_agent' => ['type' => 'varchar', 'constraint' => 255],
  74. 'token' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
  75. 'created_at' => ['type' => 'datetime', 'null' => false],
  76. ]);
  77. $this->forge->addKey('id', true);
  78. $this->forge->createTable('auth_activation_attempts', true);
  79. // Groups Table
  80. $fields = [
  81. 'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
  82. 'name' => ['type' => 'varchar', 'constraint' => 255],
  83. 'description' => ['type' => 'varchar', 'constraint' => 255],
  84. ];
  85. $this->forge->addField($fields);
  86. $this->forge->addKey('id', true);
  87. $this->forge->createTable('auth_groups', true);
  88. // Permissions Table
  89. $fields = [
  90. 'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
  91. 'name' => ['type' => 'varchar', 'constraint' => 255],
  92. 'description' => ['type' => 'varchar', 'constraint' => 255],
  93. ];
  94. $this->forge->addField($fields);
  95. $this->forge->addKey('id', true);
  96. $this->forge->createTable('auth_permissions', true);
  97. // Groups/Permissions Table
  98. $fields = [
  99. 'group_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => 0],
  100. 'permission_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => 0],
  101. ];
  102. $this->forge->addField($fields);
  103. $this->forge->addKey(['group_id', 'permission_id']);
  104. $this->forge->addForeignKey('group_id', 'auth_groups', 'id', '', 'CASCADE');
  105. $this->forge->addForeignKey('permission_id', 'auth_permissions', 'id', '', 'CASCADE');
  106. $this->forge->createTable('auth_groups_permissions', true);
  107. // Users/Groups Table
  108. $fields = [
  109. 'group_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => 0],
  110. 'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => 0],
  111. ];
  112. $this->forge->addField($fields);
  113. $this->forge->addKey(['group_id', 'user_id']);
  114. $this->forge->addForeignKey('group_id', 'auth_groups', 'id', '', 'CASCADE');
  115. $this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE');
  116. $this->forge->createTable('auth_groups_users', true);
  117. // Users/Permissions Table
  118. $fields = [
  119. 'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => 0],
  120. 'permission_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => 0],
  121. ];
  122. $this->forge->addField($fields);
  123. $this->forge->addKey(['user_id', 'permission_id']);
  124. $this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE');
  125. $this->forge->addForeignKey('permission_id', 'auth_permissions', 'id', '', 'CASCADE');
  126. $this->forge->createTable('auth_users_permissions', true);
  127. }
  128. //--------------------------------------------------------------------
  129. public function down()
  130. {
  131. // drop constraints first to prevent errors
  132. if ($this->db->DBDriver !== 'SQLite3') { // @phpstan-ignore-line
  133. $this->forge->dropForeignKey('auth_tokens', 'auth_tokens_user_id_foreign');
  134. $this->forge->dropForeignKey('auth_groups_permissions', 'auth_groups_permissions_group_id_foreign');
  135. $this->forge->dropForeignKey('auth_groups_permissions', 'auth_groups_permissions_permission_id_foreign');
  136. $this->forge->dropForeignKey('auth_groups_users', 'auth_groups_users_group_id_foreign');
  137. $this->forge->dropForeignKey('auth_groups_users', 'auth_groups_users_user_id_foreign');
  138. $this->forge->dropForeignKey('auth_users_permissions', 'auth_users_permissions_user_id_foreign');
  139. $this->forge->dropForeignKey('auth_users_permissions', 'auth_users_permissions_permission_id_foreign');
  140. }
  141. $this->forge->dropTable('users', true);
  142. $this->forge->dropTable('auth_logins', true);
  143. $this->forge->dropTable('auth_tokens', true);
  144. $this->forge->dropTable('auth_reset_attempts', true);
  145. $this->forge->dropTable('auth_activation_attempts', true);
  146. $this->forge->dropTable('auth_groups', true);
  147. $this->forge->dropTable('auth_permissions', true);
  148. $this->forge->dropTable('auth_groups_permissions', true);
  149. $this->forge->dropTable('auth_groups_users', true);
  150. $this->forge->dropTable('auth_users_permissions', true);
  151. }
  152. }