Skip to content

Commit

Permalink
permission updates
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 29, 2024
1 parent 7d00736 commit 2a599f3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Config {
function applyDefaults(cfg = {}, defaults = {}) {
for (const d in defaults) {
/* c8 ignore next */
if (d === "__proto__" || d === "constructor") continue;
if (d === '__proto__' || d === 'constructor') continue
if ([undefined, null].includes(cfg[d])) {
cfg[d] = defaults[d]
} else if (typeof cfg[d] === 'object' && typeof defaults[d] === 'object') {
Expand Down
32 changes: 19 additions & 13 deletions lib/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class Permission {
const rows = await Mysql.select(query, mapToDbColumn(args, permDbMap))
if (rows.length === 0) return
if (rows.length > 1) {
throw new Error(`permissions.get found ${rows.length} rows for uid ${args.uid}`)
throw new Error(
`permissions.get found ${rows.length} rows for uid ${args.uid}`,
)

Check warning on line 44 in lib/permission.js

View check run for this annotation

Codecov / codecov/patch

lib/permission.js#L42-L44

Added lines #L42 - L44 were not covered by tests
}
return dbToObject(rows[0])
}
Expand All @@ -56,8 +58,8 @@ class Permission {
INNER JOIN nt_user u ON p.nt_group_id = u.nt_group_id
WHERE p.deleted=0
AND u.deleted=0
AND u.nt_user_id=?`;
const rows = await Mysql.select(query, [ args.uid ])
AND u.nt_user_id=?`
const rows = await Mysql.select(query, [args.uid])
return dbToObject(rows[0])
}

Expand Down Expand Up @@ -166,10 +168,9 @@ JSON object format:
"inherit": true,
"name": "Test Permission",
"self_write": false,
"usable_ns": "",
"deleted": false,
"group": { "id": 4096, "create": false, "write": false, "delete": false },
"nameserver": { "create": false, "write": false, "delete": false },
"nameserver": { "usable": [], "create": false, "write": false, "delete": false },
"zone": { "create": true, "write": true, "delete": true, "delegate": true },
"zonerecord": {
"create": false,
Expand All @@ -181,16 +182,12 @@ JSON object format:
}
*/

const boolFields = [
'self_write',
'inherit',
'deleted',
]
const boolFields = ['self_write', 'inherit', 'deleted']

function dbToObject(row) {
const newRow = JSON.parse(JSON.stringify(row))
for (const f of ['group', 'nameserver', 'zone', 'zonerecord', 'user']) {
for (const p of ['create','write','delete','delegate']) {
for (const p of ['create', 'write', 'delete', 'delegate']) {
if (newRow[`${f}_${p}`] !== undefined) {
if (newRow[f] === undefined) newRow[f] = {}
newRow[f][p] = newRow[`${f}_${p}`] === 1
Expand All @@ -209,10 +206,15 @@ function dbToObject(row) {
newRow.group.id = newRow.gid
delete newRow.gid
}
newRow.nameserver.usable = []
if (![undefined, ''].includes(newRow.usable_ns)) {
newRow.nameserver.usable = newRow.usable_ns.split(',')
}

Check warning on line 212 in lib/permission.js

View check run for this annotation

Codecov / codecov/patch

lib/permission.js#L211-L212

Added lines #L211 - L212 were not covered by tests
delete newRow.usable_ns
return newRow
}

function objectToDb (row) {
function objectToDb(row) {
const newRow = JSON.parse(JSON.stringify(row))
if (newRow?.user?.id !== undefined) {
newRow.uid = newRow.user.id
Expand All @@ -222,8 +224,12 @@ function objectToDb (row) {
newRow.gid = newRow.group.id
delete newRow.group.id
}
if (newRow?.nameserver?.usable !== undefined) {
newRow.usable_ns = newRow.nameserver.usable.join(',')
delete newRow.nameserver.usable
}
for (const f of ['group', 'nameserver', 'zone', 'zonerecord', 'user']) {
for (const p of ['create','write','delete','delegate']) {
for (const p of ['create', 'write', 'delete', 'delegate']) {
if (newRow[f] === undefined) continue
if (newRow[f][p] === undefined) continue
newRow[`${f}_${p}`] = newRow[f][p] === true ? 1 : 0
Expand Down
20 changes: 16 additions & 4 deletions lib/permission.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,31 @@ describe('permission', function () {
})

it('get: by id', async () => {
assert.deepEqual(await Permission.get({ id: permTestCase.id }), permTestCase)
assert.deepEqual(
await Permission.get({ id: permTestCase.id }),
permTestCase,
)
})

it('get: by user id', async () => {
assert.deepEqual(await Permission.get({ uid: permTestCase.user.id }), permTestCase)
assert.deepEqual(
await Permission.get({ uid: permTestCase.user.id }),
permTestCase,
)
})

it('get: by group id', async () => {
assert.deepEqual(await Permission.get({ gid: permTestCase.group.id }), permTestCase)
assert.deepEqual(
await Permission.get({ gid: permTestCase.group.id }),
permTestCase,
)
})

it('getGroup: gets group permissions', async () => {
assert.deepEqual(await Permission.getGroup({ uid: permTestCase.user.id }), permTestCase)
assert.deepEqual(
await Permission.getGroup({ uid: permTestCase.user.id }),
permTestCase,
)
})

it('changes a permission', async () => {
Expand Down
10 changes: 7 additions & 3 deletions lib/test/permission.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
"inherit": true,
"name": "Test Permission",
"self_write": false,
"usable_ns": "",
"deleted": false,
"group": { "id": 4096, "create": false, "write": false, "delete": false },
"nameserver": { "create": false, "write": false, "delete": false },
"nameserver": {
"usable": [],
"create": false,
"write": false,
"delete": false
},
"zone": { "create": true, "write": true, "delete": true, "delegate": true },
"zonerecord": {
"create": false,
Expand All @@ -15,4 +19,4 @@
"delegate": false
},
"user": { "id": 4096, "create": false, "write": false, "delete": false }
}
}

0 comments on commit 2a599f3

Please sign in to comment.