Skip to content

Commit

Permalink
default GET sets deleted=false
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Mar 7, 2024
1 parent e3b9c82 commit fdb0520
Show file tree
Hide file tree
Showing 23 changed files with 164 additions and 101 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: msimerson
2 changes: 1 addition & 1 deletion .release
Submodule .release updated 1 files
+0 −2 CHANGELOG.md
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

- feat(lib/zone): added, with tests, fixes #22
- feat(lib/zone_record): added, with tests, fixes #23
- feat: default get sets deleted=false
- group, nameserver, permission, user, zone


### [3.0.0-alpha.4] - 2024-03-05
Expand Down
8 changes: 6 additions & 2 deletions lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Group {
}

async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false

const rows = await Mysql.execute(
...Mysql.select(
`SELECT nt_group_id AS id
Expand All @@ -31,10 +34,11 @@ class Group {
mapToDbColumn(args, groupDbMap),
),
)
for (const r of rows) {
for (const row of rows) {
for (const b of boolFields) {
r[b] = r[b] === 1
row[b] = row[b] === 1
}
if (args.deleted === false) delete row.deleted
}
return rows
}
Expand Down
5 changes: 1 addition & 4 deletions lib/group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('group', function () {
id: testCase.id,
name: testCase.name,
parent_gid: 0,
deleted: false,
})
})

Expand All @@ -30,7 +29,6 @@ describe('group', function () {
id: testCase.id,
name: testCase.name,
parent_gid: 0,
deleted: false,
})
})

Expand All @@ -41,7 +39,6 @@ describe('group', function () {
id: testCase.id,
name: 'example.net',
parent_gid: 0,
deleted: false,
},
])
assert.ok(await Group.put({ id: testCase.id, name: testCase.name }))
Expand All @@ -53,6 +50,6 @@ describe('group', function () {
assert.equal(g[0]?.deleted, true)
await Group.delete({ id: testCase.id, deleted: 0 }) // restore
g = await Group.get({ id: testCase.id })
assert.equal(g[0].deleted, false)
assert.equal(g[0].deleted, undefined)
})
})
4 changes: 4 additions & 0 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Mysql {
}

if (/^(REPLACE|INSERT) INTO/.test(query)) return rows.insertId
if (/^UPDATE /.test(query)) {
console.log(rows)
console.log(fields)
}

return rows
}
Expand Down
8 changes: 6 additions & 2 deletions lib/nameserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Nameserver {
}

async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false

if (args.name !== undefined) {
args['ns.name'] = args.name
delete args.name
Expand All @@ -61,10 +64,11 @@ class Nameserver {
mapToDbColumn(args, nsDbMap),
),
)
for (const r of rows) {
for (const row of rows) {
for (const b of boolFields) {
r[b] = r[b] === 1
row[b] = row[b] === 1
}
if (args.deleted === false) delete row.deleted
}
return dbToObject(rows)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/nameserver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ describe('nameserver', function () {
assert.equal(g[0]?.deleted, true)
await Nameserver.delete({ id: testCase.id, deleted: 0 }) // restore
g = await Nameserver.get({ id: testCase.id })
assert.equal(g[0].deleted, false)
assert.equal(g[0].deleted, undefined)
})
})
83 changes: 45 additions & 38 deletions lib/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Permission {
}

async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false

const query = `SELECT p.nt_perm_id AS id
, p.nt_user_id AS uid
, p.nt_group_id AS gid
Expand All @@ -35,7 +38,6 @@ class Permission {
, p.deleted
FROM nt_perm p`
// Mysql.debug(1)
if (args.deleted === undefined) args.deleted = false

const rows = await Mysql.execute(
...Mysql.select(query, mapToDbColumn(args, permDbMap)),
Expand All @@ -46,7 +48,9 @@ class Permission {
`permissions.get found ${rows.length} rows for uid ${args.uid}`,
)
}
return dbToObject(rows[0])
const row = dbToObject(rows[0])
if (args.deleted === false) delete row.deleted
return row
}

async getGroup(args) {
Expand All @@ -59,11 +63,13 @@ class Permission {
, p.deleted
FROM nt_perm p
INNER JOIN nt_user u ON p.nt_group_id = u.nt_group_id
WHERE p.deleted=0
WHERE p.deleted=${args.deleted === true ? 1 : 0}
AND u.deleted=0
AND u.nt_user_id=?`
const rows = await Mysql.execute(...Mysql.select(query, [args.uid]))
return dbToObject(rows[0])
const row = dbToObject(rows[0])
if ([false, undefined].includes(args.deleted)) delete row.deleted
return row
}

async put(args) {
Expand Down Expand Up @@ -185,60 +191,61 @@ JSON object format:
const boolFields = ['self_write', 'inherit', 'deleted']

function dbToObject(row) {
const newRow = JSON.parse(JSON.stringify(row))
row = JSON.parse(JSON.stringify(row))
for (const f of ['group', 'nameserver', 'zone', 'zonerecord', 'user']) {
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
delete newRow[`${f}_${p}`]
if (row[`${f}_${p}`] !== undefined) {
if (row[f] === undefined) row[f] = {}
row[f][p] = row[`${f}_${p}`] === 1
delete row[`${f}_${p}`]
}
}
}
for (const b of boolFields) {
newRow[b] = newRow[b] === 1
row[b] = row[b] === 1
}
if (newRow.uid !== undefined) {
newRow.user.id = newRow.uid
delete newRow.uid

if (row.uid !== undefined) {
row.user.id = row.uid
delete row.uid
}
if (newRow.gid !== undefined) {
newRow.group.id = newRow.gid
delete newRow.gid
if (row.gid !== undefined) {
row.group.id = row.gid
delete row.gid
}
newRow.nameserver.usable = []
if (![undefined, ''].includes(newRow.usable_ns)) {
newRow.nameserver.usable = newRow.usable_ns.split(',')
row.nameserver.usable = []
if (![undefined, ''].includes(row.usable_ns)) {
row.nameserver.usable = row.usable_ns.split(',')

Check warning on line 218 in lib/permission.js

View check run for this annotation

Codecov / codecov/patch

lib/permission.js#L218

Added line #L218 was not covered by tests
}
delete newRow.usable_ns
return newRow
delete row.usable_ns
return row
}

function objectToDb(row) {
const newRow = JSON.parse(JSON.stringify(row))
if (newRow?.user?.id !== undefined) {
newRow.uid = newRow.user.id
delete newRow.user.id
row = JSON.parse(JSON.stringify(row))
if (row?.user?.id !== undefined) {
row.uid = row.user.id
delete row.user.id
}
if (newRow?.group?.id !== undefined) {
newRow.gid = newRow.group.id
delete newRow.group.id
if (row?.group?.id !== undefined) {
row.gid = row.group.id
delete row.group.id
}
if (newRow?.nameserver?.usable !== undefined) {
newRow.usable_ns = newRow.nameserver.usable.join(',')
delete newRow.nameserver.usable
if (row?.nameserver?.usable !== undefined) {
row.usable_ns = row.nameserver.usable.join(',')
delete row.nameserver.usable
}
for (const f of ['group', 'nameserver', 'zone', 'zonerecord', 'user']) {
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
delete newRow[f][p]
if (row[f] === undefined) continue
if (row[f][p] === undefined) continue
row[`${f}_${p}`] = row[f][p] === true ? 1 : 0
delete row[f][p]
}
delete newRow[f]
delete row[f]
}
for (const b of boolFields) {
newRow[b] = newRow[b] === true ? 1 : 0
row[b] = row[b] === true ? 1 : 0
}
return newRow
return row
}
2 changes: 1 addition & 1 deletion lib/permission.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('permission', function () {
assert.equal(p?.deleted, true)
await Permission.delete({ id: permTestCase.id, deleted: 0 }) // restore
p = await Permission.get({ id: permTestCase.id })
assert.equal(p.deleted, false)
assert.equal(p.deleted, undefined)
})

it('destroys a permission', async () => {
Expand Down
3 changes: 1 addition & 2 deletions lib/test/group.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"id": 4096,
"parent_gid": 0,
"name": "example.com",
"deleted": false
"name": "example.com"
}
3 changes: 1 addition & 2 deletions lib/test/nameserver.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
"status": "last run:03-05 15:25<br>last cp :09-20 12:59",
"type": "NSD"
},
"ttl": 3600,
"deleted": false
"ttl": 3600
}
1 change: 0 additions & 1 deletion lib/test/permission.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"inherit": true,
"name": "Test Permission",
"self_write": false,
"deleted": false,
"group": { "id": 4096, "create": false, "write": false, "delete": false },
"nameserver": {
"usable": [],
Expand Down
3 changes: 1 addition & 2 deletions lib/test/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
"password": "Wh@tA-Decent#P6ssw0rd",
"first_name": "Unit",
"last_name": "Test",
"is_admin": false,
"deleted": false
"is_admin": false
}
3 changes: 1 addition & 2 deletions lib/test/zone.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
"minimum": 4,
"ttl": 3600,
"location": "",
"last_publish": null,
"deleted": false
"last_publish": null
}
2 changes: 1 addition & 1 deletion lib/test/zone_record.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"ttl": 86400,
"type": "A",
"address": "1.1.1.1"
}
}
5 changes: 5 additions & 0 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class User {
const u = await this.get({ id: args.id, gid: args.gid })
if (u.length === 1) return u[0].id

args = JSON.parse(JSON.stringify(args))

if (args.password) {
if (!args.pass_salt) args.pass_salt = this.generateSalt()
args.password = await this.hashAuthPbkdf2(args.password, args.pass_salt)
Expand All @@ -78,7 +80,9 @@ class User {
}

async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false

const rows = await Mysql.execute(
...Mysql.select(
`SELECT email
Expand All @@ -97,6 +101,7 @@ class User {
for (const b of boolFields) {
r[b] = r[b] === 1
}
if (args.deleted === false) delete r.deleted
}
return rows
}
Expand Down
2 changes: 1 addition & 1 deletion lib/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('user', function () {
assert.equal(u[0].deleted, true)
await User.delete({ id: testCase.id, deleted: 0 }) // restore
u = await User.get({ id: testCase.id })
assert.equal(u[0].deleted, false)
assert.equal(u[0].deleted, undefined)
})
})

Expand Down
10 changes: 7 additions & 3 deletions lib/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Zone {
}

async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false

const rows = await Mysql.execute(
...Mysql.select(
`SELECT nt_zone_id AS id
Expand All @@ -42,13 +45,14 @@ class Zone {
mapToDbColumn(args, zoneDbMap),
),
)
for (const r of rows) {
for (const row of rows) {
for (const b of boolFields) {
r[b] = r[b] === 1
row[b] = row[b] === 1
}
for (const f of ['description', 'location']) {
if ([null].includes(r[f])) r[f] = ''
if ([null].includes(row[f])) row[f] = ''
}
if (args.deleted === false) delete row.deleted
}

return rows
Expand Down
Loading

0 comments on commit fdb0520

Please sign in to comment.