44
55namespace CodeIgniter \Shield \Database \Migrations ;
66
7+ use CodeIgniter \Database \Forge ;
78use CodeIgniter \Database \Migration ;
9+ use CodeIgniter \Shield \Config \Auth ;
810
911class CreateAuthTables extends Migration
1012{
13+ /**
14+ * Auth Table names
15+ */
16+ private array $ tables ;
17+
18+ public function __construct (?Forge $ forge = null )
19+ {
20+ parent ::__construct ($ forge );
21+
22+ /** @var Auth $authConfig */
23+ $ authConfig = config ('Auth ' );
24+ $ this ->tables = $ authConfig ->tables ;
25+ }
26+
1127 public function up (): void
1228 {
1329 // Users Table
@@ -24,7 +40,7 @@ public function up(): void
2440 ]);
2541 $ this ->forge ->addPrimaryKey ('id ' );
2642 $ this ->forge ->addUniqueKey ('username ' );
27- $ this ->forge ->createTable (SHIELD_TABLES ['users ' ]);
43+ $ this ->forge ->createTable ($ this -> tables ['users ' ]);
2844
2945 /*
3046 * Auth Identities Table
@@ -47,8 +63,8 @@ public function up(): void
4763 $ this ->forge ->addPrimaryKey ('id ' );
4864 $ this ->forge ->addUniqueKey (['type ' , 'secret ' ]);
4965 $ this ->forge ->addKey ('user_id ' );
50- $ this ->forge ->addForeignKey ('user_id ' , SHIELD_TABLES ['users ' ], 'id ' , '' , 'CASCADE ' );
51- $ this ->forge ->createTable (SHIELD_TABLES ['identities ' ]);
66+ $ this ->forge ->addForeignKey ('user_id ' , $ this -> tables ['users ' ], 'id ' , '' , 'CASCADE ' );
67+ $ this ->forge ->createTable ($ this -> tables ['identities ' ]);
5268
5369 /**
5470 * Auth Login Attempts Table
@@ -69,7 +85,7 @@ public function up(): void
6985 $ this ->forge ->addKey (['id_type ' , 'identifier ' ]);
7086 $ this ->forge ->addKey ('user_id ' );
7187 // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits
72- $ this ->forge ->createTable (SHIELD_TABLES ['logins ' ]);
88+ $ this ->forge ->createTable ($ this -> tables ['logins ' ]);
7389
7490 /*
7591 * Auth Token Login Attempts Table
@@ -89,7 +105,7 @@ public function up(): void
89105 $ this ->forge ->addKey (['id_type ' , 'identifier ' ]);
90106 $ this ->forge ->addKey ('user_id ' );
91107 // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits
92- $ this ->forge ->createTable (SHIELD_TABLES ['token_logins ' ]);
108+ $ this ->forge ->createTable ($ this -> tables ['token_logins ' ]);
93109
94110 /*
95111 * Auth Remember Tokens (remember-me) Table
@@ -106,8 +122,8 @@ public function up(): void
106122 ]);
107123 $ this ->forge ->addPrimaryKey ('id ' );
108124 $ this ->forge ->addUniqueKey ('selector ' );
109- $ this ->forge ->addForeignKey ('user_id ' , SHIELD_TABLES ['users ' ], 'id ' , '' , 'CASCADE ' );
110- $ this ->forge ->createTable (SHIELD_TABLES ['remember_tokens ' ]);
125+ $ this ->forge ->addForeignKey ('user_id ' , $ this -> tables ['users ' ], 'id ' , '' , 'CASCADE ' );
126+ $ this ->forge ->createTable ($ this -> tables ['remember_tokens ' ]);
111127
112128 // Groups Users Table
113129 $ this ->forge ->addField ([
@@ -117,8 +133,8 @@ public function up(): void
117133 'created_at ' => ['type ' => 'datetime ' , 'null ' => false ],
118134 ]);
119135 $ this ->forge ->addPrimaryKey ('id ' );
120- $ this ->forge ->addForeignKey ('user_id ' , SHIELD_TABLES ['users ' ], 'id ' , '' , 'CASCADE ' );
121- $ this ->forge ->createTable (SHIELD_TABLES ['groups_users ' ]);
136+ $ this ->forge ->addForeignKey ('user_id ' , $ this -> tables ['users ' ], 'id ' , '' , 'CASCADE ' );
137+ $ this ->forge ->createTable ($ this -> tables ['groups_users ' ]);
122138
123139 // Users Permissions Table
124140 $ this ->forge ->addField ([
@@ -128,8 +144,8 @@ public function up(): void
128144 'created_at ' => ['type ' => 'datetime ' , 'null ' => false ],
129145 ]);
130146 $ this ->forge ->addPrimaryKey ('id ' );
131- $ this ->forge ->addForeignKey ('user_id ' , SHIELD_TABLES ['users ' ], 'id ' , '' , 'CASCADE ' );
132- $ this ->forge ->createTable (SHIELD_TABLES ['permissions_users ' ]);
147+ $ this ->forge ->addForeignKey ('user_id ' , $ this -> tables ['users ' ], 'id ' , '' , 'CASCADE ' );
148+ $ this ->forge ->createTable ($ this -> tables ['permissions_users ' ]);
133149 }
134150
135151 // --------------------------------------------------------------------
@@ -138,13 +154,13 @@ public function down(): void
138154 {
139155 $ this ->db ->disableForeignKeyChecks ();
140156
141- $ this ->forge ->dropTable (SHIELD_TABLES ['logins ' ], true );
142- $ this ->forge ->dropTable (SHIELD_TABLES ['token_logins ' ], true );
143- $ this ->forge ->dropTable (SHIELD_TABLES ['remember_tokens ' ], true );
144- $ this ->forge ->dropTable (SHIELD_TABLES ['identities ' ], true );
145- $ this ->forge ->dropTable (SHIELD_TABLES ['groups_users ' ], true );
146- $ this ->forge ->dropTable (SHIELD_TABLES ['permissions_users ' ], true );
147- $ this ->forge ->dropTable (SHIELD_TABLES ['users ' ], true );
157+ $ this ->forge ->dropTable ($ this -> tables ['logins ' ], true );
158+ $ this ->forge ->dropTable ($ this -> tables ['token_logins ' ], true );
159+ $ this ->forge ->dropTable ($ this -> tables ['remember_tokens ' ], true );
160+ $ this ->forge ->dropTable ($ this -> tables ['identities ' ], true );
161+ $ this ->forge ->dropTable ($ this -> tables ['groups_users ' ], true );
162+ $ this ->forge ->dropTable ($ this -> tables ['permissions_users ' ], true );
163+ $ this ->forge ->dropTable ($ this -> tables ['users ' ], true );
148164
149165 $ this ->db ->enableForeignKeyChecks ();
150166 }
0 commit comments