Skip to content

Commit 20d4ec6

Browse files
Jordan MeleJordan Mele
authored andcommitted
Azure SQL Server and SQL Server Support
1 parent 961e822 commit 20d4ec6

2 files changed

Lines changed: 27 additions & 25 deletions

File tree

userfrosting/controllers/InstallController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ public function setupMasterAccount(){
230230

231231
// Create the master user
232232
$user = new User($data);
233+
233234
$user->id = $this->_app->config('user_id_master');
234235

235236
// Add user to default groups, including default primary group
@@ -243,8 +244,18 @@ public function setupMasterAccount(){
243244
// Add sign-up event
244245
$user->newEventSignUp();
245246

246-
// Store new user to database
247-
$user->save();
247+
// If using SQL Server or Azure SQL Server, use this alternative insert method.
248+
if (\Illuminate\Database\Capsule\Manager::connection()->getPdo()->getAttribute(\PDO::ATTR_DRIVER_NAME) == "sqlsrv") {
249+
\Illuminate\Database\Capsule\Manager::transaction(function() use ($user) {
250+
// Manually build insert statement to overcome session IDENTITY_INSERT restriction.
251+
$insert = "INSERT INTO ".$this->_app->config('db')['db_prefix']."user (user_name, display_name, email, password, flag_verified, locale, primary_group_id, title, id, created_at, updated_at) VALUES ('".$user->user_name."', '".$user->display_name."', '".$user->email."', '".$user->password."', ".$user->flag_verified.", '".$user->locale."', ".$user->primary_group_id.", '".$user->title."', ".$user->id.", '".date("Y-m-d h:i:s")."', '".date("Y-m-d h:i:s")."');";
252+
\Illuminate\Database\Capsule\Manager::statement("SET IDENTITY_INSERT ".$this->_app->config('db')['db_prefix']."user ON; ".$insert);
253+
});
254+
}
255+
else {
256+
// Store new user to database
257+
$user->save();
258+
}
248259

249260
// No activation required
250261
$ms->addMessageTranslated("success", "ACCOUNT_REGISTRATION_COMPLETE_TYPE1");

userfrosting/models/database/Database.php

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ public static function install(){
305305
// Setup default groups
306306
Capsule::table(static::getSchemaTable('group')->name)->insert([
307307
[
308-
'id' => 1,
309308
'name' => 'User',
310309
'is_default' => GROUP_DEFAULT_PRIMARY,
311310
'can_delete' => 0,
@@ -315,7 +314,6 @@ public static function install(){
315314
'icon' => 'fa fa-user'
316315
],
317316
[
318-
'id' => 2,
319317
'name' => 'Administrator',
320318
'is_default' => GROUP_NOT_DEFAULT,
321319
'can_delete' => 0,
@@ -325,7 +323,6 @@ public static function install(){
325323
'icon' => 'fa fa-flag'
326324
],
327325
[
328-
'id' => 3,
329326
'name' => 'Zerglings',
330327
'is_default' => GROUP_NOT_DEFAULT,
331328
'can_delete' => 1,
@@ -336,60 +333,54 @@ public static function install(){
336333
]
337334
]);
338335

336+
// Get default groups id's.
337+
$user = Capsule::table(static::getSchemaTable('group')->name)->where('name', '=', 'User')->first()['id'];
338+
$admin = Capsule::table(static::getSchemaTable('group')->name)->where('name', '=', 'Administrator')->first()['id'];
339339

340340
// Setup default authorizations
341341
Capsule::table(static::getSchemaTable('authorize_group')->name)->insert([
342342
[
343-
'id' => 1,
344-
'group_id' => 1,
343+
'group_id' => $user,
345344
'hook' => 'uri_dashboard',
346345
'conditions' => 'always()'
347346
],
348347
[
349-
'id' => 2,
350-
'group_id' => 2,
348+
'group_id' => $admin,
351349
'hook' => 'uri_dashboard',
352350
'conditions' => 'always()'
353351
],
354352
[
355-
'id' => 3,
356-
'group_id' => 2,
353+
'group_id' => $admin,
357354
'hook' => 'uri_users',
358355
'conditions' => 'always()'
359356
],
360357
[
361-
'id' => 4,
362-
'group_id' => 1,
358+
'group_id' => $user,
363359
'hook' => 'uri_account_settings',
364360
'conditions' => 'always()'
365361
],
366362
[
367-
'id' => 5,
368-
'group_id' => 1,
363+
'group_id' => $user,
369364
'hook' => 'update_account_setting',
370365
'conditions' => 'equals(self.id, user.id)&&in(property,[\"email\",\"locale\",\"password\"])'
371366
],
372367
[
373-
'id' => 6,
374-
'group_id' => 2,
368+
'group_id' => $admin,
375369
'hook' => 'update_account_setting',
376-
'conditions' => '!in_group(user.id,2)&&in(property,[\"email\",\"display_name\",\"title\",\"locale\",\"flag_password_reset\",\"flag_enabled\"])'
370+
'conditions' => '!in_group(user.id,'.$admin.')&&in(property,[\"email\",\"display_name\",\"title\",\"locale\",\"flag_password_reset\",\"flag_enabled\"])'
377371
],
378372
[
379-
'id' => 7,
380-
'group_id' => 2,
373+
'group_id' => $admin,
381374
'hook' => 'view_account_setting',
382375
'conditions' => 'in(property,[\"user_name\",\"email\",\"display_name\",\"title\",\"locale\",\"flag_enabled\",\"groups\",\"primary_group_id\"])'
383376
],
384377
[
385-
'id' => 8,
386-
'group_id' => 2,
378+
'group_id' => $admin,
387379
'hook' => 'delete_account',
388-
'conditions' => '!in_group(user.id,2)'
380+
'conditions' => '!in_group(user.id,'.$admin.')'
389381
],
390382
[
391-
'id' => 9,
392-
'group_id' => 2,
383+
'group_id' => $admin,
393384
'hook' => 'create_account',
394385
'conditions' => 'always()'
395386
]

0 commit comments

Comments
 (0)