-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cd6810
commit ab15629
Showing
2 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/backend/src/services/database/sqlite_setup/0025_system-user.dbmig.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Add a user called `system`. | ||
If a user called `system` already exists, first rename the existing | ||
user to the first username in this sequence: | ||
system_, system_0, system_1, system_2, ... | ||
*/ | ||
|
||
let existing_user; | ||
|
||
;[existing_user] = await read( | ||
"SELECT username FROM `user` WHERE username='system'", | ||
); | ||
|
||
if ( existing_user ) { | ||
let replace_num = 0; | ||
let replace_name = 'system_'; | ||
|
||
for (;;) { | ||
;[existing_user] = await read( | ||
'SELECT username FROM `user` WHERE username=?', | ||
[replace_name] | ||
); | ||
if ( ! existing_user ) break; | ||
replace_name = 'system_' + (replace_num++); | ||
} | ||
|
||
log.noticeme('updating existing user called system', { | ||
replace_num, | ||
replace_name, | ||
}); | ||
|
||
await write( | ||
`UPDATE \`user\` SET username=? WHERE username='system' LIMIT 1`, | ||
[replace_name] | ||
); | ||
} | ||
|
||
const { insertId: system_user_id } = await write( | ||
'INSERT INTO `user` (`uuid`, `username`) VALUES (?, ?)', | ||
[ | ||
'5d4adce0-a381-4982-9c02-6e2540026238', | ||
'system', | ||
] | ||
); | ||
|
||
const [{id: system_group_id}] = await read( | ||
'SELECT id FROM `group` WHERE uid=?', | ||
['26bfb1fb-421f-45bc-9aa4-d81ea569e7a5'] | ||
); | ||
|
||
const [{id: admin_group_id}] = await read( | ||
'SELECT id FROM `group` WHERE uid=?', | ||
['ca342a5e-b13d-4dee-9048-58b11a57cc55'] | ||
); | ||
|
||
// admin group has unlimited access to all drivers | ||
await write( | ||
'INSERT INTO `user_to_group_permissions` ' + | ||
'(`user_id`, `group_id`, `permission`, `extra`) ' + | ||
'VALUES (?, ?, ?, ?)', | ||
[system_user_id, admin_group_id, 'driver', '{}'] | ||
); |