forked from HeyPuter/puter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: grant user driver perms from admin
- Loading branch information
1 parent
f0c36a1
commit c9ded89
Showing
6 changed files
with
241 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
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
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
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
95 changes: 95 additions & 0 deletions
95
src/backend/src/services/database/sqlite_setup/0026_user-groups.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,95 @@ | ||
const { insertId: temp_group_id } = await write( | ||
'INSERT INTO `group` (`uid`, `owner_user_id`, `extra`, `metadata`) '+ | ||
'VALUES (?, ?, ?, ?)', | ||
[ | ||
'b7220104-7905-4985-b996-649fdcdb3c8f', | ||
1, | ||
'{"critical": true, "type": "default", "name": "temp"}', | ||
'{"title": "Guest", "color": "#777777"}' | ||
] | ||
); | ||
const [{id: system_user_id}] = await read( | ||
"SELECT id FROM `user` WHERE username='system'" | ||
); | ||
const [{id: user_group_id}] = await read( | ||
'SELECT id FROM `group` WHERE uid=?', | ||
['78b1b1dd-c959-44d2-b02c-8735671f9997'] | ||
); | ||
|
||
const user_types = structutil.apply_keys( | ||
['name', 'group_id'], | ||
['temp', temp_group_id], | ||
['user', user_group_id], | ||
); | ||
const drivers = structutil.apply_keys( | ||
['driver_id', 'selector'], | ||
['driver:puter-kvstore', 'kv'], | ||
['driver:puter-notifications', 'es'], | ||
['driver:puter-apps', 'es'], | ||
['driver:puter-subdomains', 'es'], | ||
); | ||
|
||
const perms = structutil.cart_product( | ||
[user_types, drivers]); | ||
|
||
for ( const perm of perms ) { | ||
const [user_type, driver] = perm; | ||
log.info('permission info', { user_type, driver }); | ||
debugger; | ||
// temp user drivers | ||
await write( | ||
'INSERT INTO `user_to_group_permissions` ' + | ||
'(`user_id`, `group_id`, `permission`, `extra`) ' + | ||
'VALUES (?, ?, ?, ?)', | ||
[ | ||
system_user_id, user_type.group_id, | ||
driver.driver_id, | ||
JSON.stringify({ | ||
policy: { | ||
$: 'json-address', | ||
path: '/admin/.policy/drivers.json', | ||
selector: user_type.name + '.' + | ||
driver.selector, | ||
} | ||
}), | ||
] | ||
); | ||
} | ||
|
||
/* | ||
// temp user drivers | ||
await write( | ||
'INSERT INTO `user_to_group_permissions` ' + | ||
'(`user_id`, `group_id`, `permission`, `extra`) ' + | ||
'VALUES (?, ?, ?, ?)', | ||
[ | ||
system_user_id, temp_group_id, | ||
'driver:puter-kvstore', | ||
JSON.stringify({ | ||
policy: { | ||
$: 'json-address', | ||
path: '/admin/.policy/drivers.json', | ||
selector: 'temp.kv', | ||
} | ||
}), | ||
] | ||
); | ||
// registered user drivers | ||
await write( | ||
'INSERT INTO `user_to_group_permissions` ' + | ||
'(`user_id`, `group_id`, `permission`, `extra`) ' + | ||
'VALUES (?, ?, ?, ?)', | ||
[ | ||
system_user_id, user_group_id, | ||
'driver:puter-kvstore', | ||
JSON.stringify({ | ||
policy: { | ||
$: 'json-address', | ||
path: '/admin/.policy/drivers.json', | ||
selector: 'user.kv', | ||
} | ||
}), | ||
] | ||
); | ||
*/ |
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,35 @@ | ||
const cart_product = (obj) => { | ||
// Get array of keys | ||
let keys = Object.keys(obj); | ||
|
||
// Generate the Cartesian Product | ||
return keys.reduce((acc, key) => { | ||
let appendArrays = Array.isArray(obj[key]) ? obj[key] : [obj[key]]; | ||
|
||
let newAcc = []; | ||
acc.forEach(arr => { | ||
appendArrays.forEach(item => { | ||
newAcc.push([...arr, item]); | ||
}); | ||
}); | ||
|
||
return newAcc; | ||
}, [[]]); // start with the "empty product" | ||
} | ||
|
||
const apply_keys = (keys, ...entries) => { | ||
const l = []; | ||
for ( const entry of entries ) { | ||
const o = {}; | ||
for ( let i=0 ; i < keys.length ; i++ ) { | ||
o[keys[i]] = entry[i]; | ||
} | ||
l.push(o); | ||
} | ||
return l; | ||
} | ||
|
||
module.exports = { | ||
cart_product, | ||
apply_keys, | ||
}; |