Skip to content

Commit fd3ebab

Browse files
committed
Use Laravel's Schema interface to create tables and default rows, instead of constructing them with SQL
1 parent 9b07b9b commit fd3ebab

4 files changed

Lines changed: 239 additions & 101 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## v0.3.1.21
4+
5+
- Use Laravel's Schema interface to create tables and default rows, instead of constructing them with SQL
6+
37
## v0.3.1.20
48

59
- Added `pushAlert()`,`clearAlerts()` in `public/js/userfrosting.js` and updated `flashAlerts()`

userfrosting/initialize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
'guest_theme' => 'default',
156156
'minify_css' => '0',
157157
'minify_js' => '0',
158-
'version' => '0.3.1.20',
158+
'version' => '0.3.1.21',
159159
'author' => 'Alex Weissman',
160160
'show_terms_on_register' => '1',
161161
'site_location' => 'The State of Indiana'

userfrosting/models/database/Database.php

Lines changed: 226 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace UserFrosting;
44

55
use \Illuminate\Database\Capsule\Manager as Capsule;
6+
use Illuminate\Database\Schema\Blueprint;
67

78
/**
89
* Represents the UserFrosting database.
@@ -146,7 +147,7 @@ public static function getCreatedTables(){
146147

147148
foreach ($test_list as $table){
148149
try {
149-
$stmt = $connection->select("SELECT 1 FROM `$table` LIMIT 1;");
150+
$stmt = $connection->select("SELECT 1 FROM $table LIMIT 1;");
150151
} catch (\PDOException $e){
151152
continue;
152153
}
@@ -162,109 +163,238 @@ public static function getCreatedTables(){
162163
* Creates all tables, and loads the configuration table with the default config data. Also, sets install_status to `pending`.
163164
*/
164165
public static function install(){
165-
$connection = Capsule::connection();
166-
167-
$connection->statement("CREATE TABLE IF NOT EXISTS `" . static::getSchemaTable('configuration')->name . "` (
168-
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
169-
`plugin` varchar(50) NOT NULL COMMENT 'The name of the plugin that manages this setting (set to ''userfrosting'' for core settings)',
170-
`name` varchar(150) NOT NULL COMMENT 'The name of the setting.',
171-
`value` longtext NOT NULL COMMENT 'The current value of the setting.',
172-
`description` text NOT NULL COMMENT 'A brief description of this setting.',
173-
PRIMARY KEY (`id`)
174-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='A configuration table, mapping global configuration options to their values.' AUTO_INCREMENT=1 ;");
175-
176-
$connection->statement("CREATE TABLE IF NOT EXISTS `" . static::getSchemaTable('authorize_group')->name . "` (
177-
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
178-
`group_id` int(10) unsigned NOT NULL,
179-
`hook` varchar(200) NOT NULL COMMENT 'A code that references a specific action or URI that the group has access to.',
180-
`conditions` text NOT NULL COMMENT 'The conditions under which members of this group have access to this hook.',
181-
PRIMARY KEY (`id`)
182-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;");
183-
184-
$connection->statement("CREATE TABLE IF NOT EXISTS `" . static::getSchemaTable('authorize_user')->name . "` (
185-
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
186-
`user_id` int(10) unsigned NOT NULL,
187-
`hook` varchar(200) NOT NULL COMMENT 'A code that references a specific action or URI that the user has access to.',
188-
`conditions` text NOT NULL COMMENT 'The conditions under which the user has access to this action.',
189-
PRIMARY KEY (`id`)
190-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;");
191-
192-
$connection->statement("CREATE TABLE IF NOT EXISTS `" . static::getSchemaTable('group')->name . "` (
193-
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
194-
`name` varchar(150) NOT NULL,
195-
`is_default` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Specifies whether this permission is a default setting for new accounts.',
196-
`can_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Specifies whether this permission can be deleted from the control panel.',
197-
`theme` varchar(100) NOT NULL DEFAULT 'default' COMMENT 'The theme assigned to primary users in this group.',
198-
`landing_page` varchar(200) NOT NULL DEFAULT 'dashboard' COMMENT 'The page to take primary members to when they first log in.',
199-
`new_user_title` varchar(200) NOT NULL DEFAULT 'New User' COMMENT 'The default title to assign to new primary users.',
200-
`icon` varchar(100) NOT NULL DEFAULT 'fa fa-user' COMMENT 'The icon representing primary users in this group.',
201-
PRIMARY KEY (`id`)
202-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");
203-
204-
205-
$connection->statement("CREATE TABLE IF NOT EXISTS `" . static::getSchemaTable('group_user')->name . "` (
206-
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
207-
`user_id` int(10) unsigned NOT NULL,
208-
`group_id` int(10) unsigned NOT NULL,
209-
PRIMARY KEY (`id`)
210-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Maps users to their group(s)' AUTO_INCREMENT=1 ;");
211-
212-
$connection->statement("CREATE TABLE IF NOT EXISTS `" . static::getSchemaTable('user')->name . "` (
213-
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
214-
`user_name` varchar(50) NOT NULL,
215-
`display_name` varchar(50) NOT NULL,
216-
`email` varchar(150) NOT NULL,
217-
`title` varchar(150) NOT NULL,
218-
`locale` varchar(10) NOT NULL DEFAULT 'en_US' COMMENT 'The language and locale to use for this user.',
219-
`primary_group_id` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'The id of this user''s primary group.',
220-
`secret_token` varchar(32) NOT NULL DEFAULT '' COMMENT 'The current one-time use token for various user activities confirmed via email.',
221-
`flag_verified` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Set to ''1'' if the user has verified their account via email, ''0'' otherwise.',
222-
`flag_enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Set to ''1'' if the user''s account is currently enabled, ''0'' otherwise. Disabled accounts cannot be logged in to, but they retain all of their data and settings.',
223-
`flag_password_reset` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Set to ''1'' if the user has an outstanding password reset request, ''0'' otherwise.',
224-
`created_at` timestamp NULL DEFAULT NULL,
225-
`updated_at` timestamp NULL DEFAULT NULL,
226-
`password` varchar(255) NOT NULL,
227-
PRIMARY KEY (`id`)
228-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;");
229-
230-
$connection->statement("CREATE TABLE IF NOT EXISTS `" . static::getSchemaTable('user_event')->name . "` (
231-
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
232-
`user_id` int(10) unsigned NOT NULL,
233-
`event_type` varchar(255) NOT NULL COMMENT 'An identifier used to track the type of event.',
234-
`occurred_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
235-
`description` text NOT NULL,
236-
PRIMARY KEY (`id`)
237-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");
166+
$schema = Capsule::schema();
167+
168+
/**
169+
* `configuration` table.
170+
*/
171+
if (!$schema->hasTable(static::getSchemaTable('configuration')->name)) {
172+
$schema->create(static::getSchemaTable('configuration')->name, function (Blueprint $table) {
173+
$table->increments('id');
174+
$table->string('plugin', 50)->comment("The name of the plugin that manages this setting (set to ''userfrosting'' for core settings)");
175+
$table->string('name', 150)->comment('The name of the setting.');
176+
$table->text('value')->comment('The current value of the setting.');
177+
$table->text('description')->comment('A brief description of this setting.');
178+
$table->engine = 'InnoDB';
179+
$table->collation = 'utf8_unicode_ci';
180+
$table->charset = 'utf8';
181+
});
182+
}
183+
184+
/**
185+
* `authorize_group` table.
186+
*/
187+
if (!$schema->hasTable(static::getSchemaTable('authorize_group')->name)) {
188+
$schema->create(static::getSchemaTable('authorize_group')->name, function (Blueprint $table) {
189+
$table->increments('id');
190+
$table->integer('group_id')->unsigned();
191+
$table->string('hook', 200)->comment('A code that references a specific action or URI that the group has access to.');
192+
$table->text('conditions')->comment('The conditions under which members of this group have access for this hook.');
193+
$table->engine = 'InnoDB';
194+
$table->collation = 'utf8_unicode_ci';
195+
$table->charset = 'utf8';
196+
});
197+
}
238198

239-
$connection->statement("CREATE TABLE IF NOT EXISTS `" . static::$app->remember_me_table['tableName'] . "` (
240-
`user_id` int(11) NOT NULL,
241-
`token` varchar(40) NOT NULL,
242-
`persistent_token` varchar(40) NOT NULL,
243-
`expires` datetime NOT NULL
244-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
199+
/**
200+
* `authorize_user` table.
201+
*/
202+
if (!$schema->hasTable(static::getSchemaTable('authorize_user')->name)) {
203+
$schema->create(static::getSchemaTable('authorize_user')->name, function (Blueprint $table) {
204+
$table->increments('id');
205+
$table->integer('user_id')->unsigned();
206+
$table->string('hook', 200)->comment('A code that references a specific action or URI that the user has access to.');
207+
$table->text('conditions')->comment('The conditions under which the user has access for this hook.');
208+
$table->engine = 'InnoDB';
209+
$table->collation = 'utf8_unicode_ci';
210+
$table->charset = 'utf8';
211+
});
212+
}
213+
214+
/**
215+
* `group` table.
216+
*/
217+
if (!$schema->hasTable(static::getSchemaTable('group')->name)) {
218+
$schema->create(static::getSchemaTable('group')->name, function (Blueprint $table) {
219+
$table->increments('id');
220+
$table->string('name', 150);
221+
$table->boolean('is_default')->default(0)->comment('Specifies whether this permission is a default setting for new accounts.');
222+
$table->boolean('can_delete')->default(1)->comment('Specifies whether this permission can be deleted from the control panel.');
223+
$table->string('theme', 100)->default('default')->comment('The theme assigned to primary users in this group.');
224+
$table->string('landing_page', 200)->default('dashboard')->comment('The page to take primary members to when they first log in.');
225+
$table->string('new_user_title', 200)->default('New User')->comment('The default title to assign to new primary users.');
226+
$table->string('icon', 100)->default('fa fa-user')->comment('The icon representing primary users in this group.');
227+
$table->engine = 'InnoDB';
228+
$table->collation = 'utf8_unicode_ci';
229+
$table->charset = 'utf8';
230+
});
231+
}
232+
233+
/**
234+
* `group_user` table.
235+
*/
236+
if (!$schema->hasTable(static::getSchemaTable('group_user')->name)) {
237+
$schema->create(static::getSchemaTable('group_user')->name, function (Blueprint $table) {
238+
$table->increments('id');
239+
$table->integer('user_id')->unsigned();
240+
$table->integer('group_id')->unsigned();
241+
$table->engine = 'InnoDB';
242+
$table->collation = 'utf8_unicode_ci';
243+
$table->charset = 'utf8';
244+
});
245+
}
246+
247+
/**
248+
* `user` table.
249+
*/
250+
if (!$schema->hasTable(static::getSchemaTable('user')->name)) {
251+
$schema->create(static::getSchemaTable('user')->name, function (Blueprint $table) {
252+
$table->increments('id');
253+
$table->string('user_name', 50);
254+
$table->string('display_name', 50);
255+
$table->string('email', 150);
256+
$table->string('title', 150);
257+
$table->string('locale', 10)->default('en_US')->comment('The language and locale to use for this user.');
258+
$table->integer('primary_group_id')->unsigned()->default(1)->comment("The id of this user''s primary group.");
259+
$table->string('secret_token', 32)->default('')->comment('The current one-time use token for various user activities confirmed via email.');
260+
$table->boolean('flag_verified')->default(1)->comment("Set to ''1'' if the user has verified their account via email, ''0'' otherwise.");
261+
$table->boolean('flag_enabled')->default(1)->comment("Set to ''1'' if the user''s account is currently enabled, ''0'' otherwise. Disabled accounts cannot be logged in to, but they retain all of their data and settings.");
262+
$table->boolean('flag_password_reset')->default(0)->comment("Set to ''1'' if the user has an outstanding password reset request, ''0'' otherwise.");
263+
$table->timestamps();
264+
$table->string('password', 255);
265+
$table->engine = 'InnoDB';
266+
$table->collation = 'utf8_unicode_ci';
267+
$table->charset = 'utf8';
268+
});
269+
}
270+
271+
/**
272+
* `user_event` table.
273+
*/
274+
if (!$schema->hasTable(static::getSchemaTable('user_event')->name)) {
275+
$schema->create(static::getSchemaTable('user_event')->name, function (Blueprint $table) {
276+
$table->increments('id');
277+
$table->integer('user_id')->unsigned();
278+
$table->string('event_type', 255)->comment('An identifier used to track the type of event.');
279+
$table->timestamp('occurred_at');
280+
$table->text('description');
281+
$table->engine = 'InnoDB';
282+
$table->collation = 'utf8_unicode_ci';
283+
$table->charset = 'utf8';
284+
});
285+
}
286+
287+
/**
288+
* 'remember me' table.
289+
*/
290+
if (!$schema->hasTable(static::$app->remember_me_table['tableName'])) {
291+
$schema->create(static::$app->remember_me_table['tableName'], function (Blueprint $table) {
292+
$table->integer('user_id')->unsigned();
293+
$table->string('token', 40);
294+
$table->string('persistent_token', 40);
295+
$table->dateTime('expires');
296+
$table->engine = 'InnoDB';
297+
$table->collation = 'utf8_unicode_ci';
298+
$table->charset = 'utf8';
299+
});
300+
}
245301

246302
// Setup initial configuration settings
247303
static::$app->site->install_status = "pending";
248304
static::$app->site->root_account_config_token = md5(uniqid(mt_rand(), false));
249305
static::$app->site->store();
250306

251-
// Setup default groups. TODO: finish Group API so they can be created through objects
252-
$connection->insert("INSERT INTO `" . static::getSchemaTable('group')->name . "` (`name`, `is_default`, `can_delete`, `theme`, `landing_page`, `new_user_title`, `icon`) VALUES
253-
('User', " . GROUP_DEFAULT_PRIMARY . ", 0, 'default', 'dashboard', 'New User', 'fa fa-user'),
254-
('Administrator', " . GROUP_NOT_DEFAULT . ", 0, 'nyx', 'dashboard', 'Brood Spawn', 'fa fa-flag'),
255-
('Zerglings', " . GROUP_NOT_DEFAULT . ", 1, 'nyx', 'dashboard', 'Tank Fodder', 'sc sc-zergling');");
307+
// Setup default groups
308+
Capsule::table(static::getSchemaTable('group')->name)->insert([
309+
[
310+
'id' => 1,
311+
'name' => 'User',
312+
'is_default' => GROUP_DEFAULT_PRIMARY,
313+
'can_delete' => 0,
314+
'theme' => 'default',
315+
'landing_page' => 'dashboard',
316+
'new_user_title' => 'New User',
317+
'icon' => 'fa fa-user'
318+
],
319+
[
320+
'id' => 2,
321+
'name' => 'Administrator',
322+
'is_default' => GROUP_NOT_DEFAULT,
323+
'can_delete' => 0,
324+
'theme' => 'nyx',
325+
'landing_page' => 'dashboard',
326+
'new_user_title' => 'Brood Spawn',
327+
'icon' => 'fa fa-flag'
328+
],
329+
[
330+
'id' => 3,
331+
'name' => 'Zerglings',
332+
'is_default' => GROUP_NOT_DEFAULT,
333+
'can_delete' => 1,
334+
'theme' => 'nyx',
335+
'landing_page' => 'dashboard',
336+
'new_user_title' => 'Tank Fodder',
337+
'icon' => 'sc sc-zergling'
338+
]
339+
]);
340+
256341

257342
// Setup default authorizations
258-
$connection->insert("INSERT INTO `" . static::getSchemaTable('authorize_group')->name . "` (`group_id`, `hook`, `conditions`) VALUES
259-
(1, 'uri_dashboard', 'always()'),
260-
(2, 'uri_dashboard', 'always()'),
261-
(2, 'uri_users', 'always()'),
262-
(1, 'uri_account_settings', 'always()'),
263-
(1, 'update_account_setting', 'equals(self.id, user.id)&&in(property,[\"email\",\"locale\",\"password\"])'),
264-
(2, 'update_account_setting', '!in_group(user.id,2)&&in(property,[\"email\",\"display_name\",\"title\",\"locale\",\"flag_password_reset\",\"flag_enabled\"])'),
265-
(2, 'view_account_setting', 'in(property,[\"user_name\",\"email\",\"display_name\",\"title\",\"locale\",\"flag_enabled\",\"groups\",\"primary_group_id\"])'),
266-
(2, 'delete_account', '!in_group(user.id,2)'),
267-
(2, 'create_account', 'always()');");
343+
Capsule::table(static::getSchemaTable('authorize_group')->name)->insert([
344+
[
345+
'id' => 1,
346+
'group_id' => 1,
347+
'hook' => 'uri_dashboard',
348+
'conditions' => 'always()'
349+
],
350+
[
351+
'id' => 2,
352+
'group_id' => 2,
353+
'hook' => 'uri_dashboard',
354+
'conditions' => 'always()'
355+
],
356+
[
357+
'id' => 3,
358+
'group_id' => 2,
359+
'hook' => 'uri_users',
360+
'conditions' => 'always()'
361+
],
362+
[
363+
'id' => 4,
364+
'group_id' => 1,
365+
'hook' => 'uri_account_settings',
366+
'conditions' => 'always()'
367+
],
368+
[
369+
'id' => 5,
370+
'group_id' => 1,
371+
'hook' => 'update_account_setting',
372+
'conditions' => 'equals(self.id, user.id)&&in(property,[\"email\",\"locale\",\"password\"])'
373+
],
374+
[
375+
'id' => 6,
376+
'group_id' => 2,
377+
'hook' => 'update_account_setting',
378+
'conditions' => '!in_group(user.id,2)&&in(property,[\"email\",\"display_name\",\"title\",\"locale\",\"flag_password_reset\",\"flag_enabled\"])'
379+
],
380+
[
381+
'id' => 7,
382+
'group_id' => 2,
383+
'hook' => 'view_account_setting',
384+
'conditions' => 'in(property,[\"user_name\",\"email\",\"display_name\",\"title\",\"locale\",\"flag_enabled\",\"groups\",\"primary_group_id\"])'
385+
],
386+
[
387+
'id' => 8,
388+
'group_id' => 2,
389+
'hook' => 'delete_account',
390+
'conditions' => '!in_group(user.id,2)'
391+
],
392+
[
393+
'id' => 9,
394+
'group_id' => 2,
395+
'hook' => 'create_account',
396+
'conditions' => 'always()'
397+
]
398+
]);
268399
}
269-
270400
}

0 commit comments

Comments
 (0)