@@ -26,6 +26,9 @@ class Permission {
2626 }
2727
2828 async get ( args ) {
29+ args = JSON . parse ( JSON . stringify ( args ) )
30+ if ( args . deleted === undefined ) args . deleted = false
31+
2932 const query = `SELECT p.nt_perm_id AS id
3033 , p.nt_user_id AS uid
3134 , p.nt_group_id AS gid
@@ -34,8 +37,6 @@ class Permission {
3437 ${ getPermFields ( ) }
3538 , p.deleted
3639 FROM nt_perm p`
37- // Mysql.debug(1)
38- if ( args . deleted === undefined ) args . deleted = false
3940
4041 const rows = await Mysql . execute (
4142 ...Mysql . select ( query , mapToDbColumn ( args , permDbMap ) ) ,
@@ -46,7 +47,9 @@ class Permission {
4647 `permissions.get found ${ rows . length } rows for uid ${ args . uid } ` ,
4748 )
4849 }
49- return dbToObject ( rows [ 0 ] )
50+ const row = dbToObject ( rows [ 0 ] )
51+ if ( args . deleted === false ) delete row . deleted
52+ return row
5053 }
5154
5255 async getGroup ( args ) {
@@ -59,18 +62,19 @@ class Permission {
5962 , p.deleted
6063 FROM nt_perm p
6164 INNER JOIN nt_user u ON p.nt_group_id = u.nt_group_id
62- WHERE p.deleted=0
65+ WHERE p.deleted=${ args . deleted === true ? 1 : 0 }
6366 AND u.deleted=0
6467 AND u.nt_user_id=?`
6568 const rows = await Mysql . execute ( ...Mysql . select ( query , [ args . uid ] ) )
66- return dbToObject ( rows [ 0 ] )
69+ const row = dbToObject ( rows [ 0 ] )
70+ if ( [ false , undefined ] . includes ( args . deleted ) ) delete row . deleted
71+ return row
6772 }
6873
6974 async put ( args ) {
7075 if ( ! args . id ) return false
7176 const id = args . id
7277 delete args . id
73- // Mysql.debug(1)
7478 const r = await Mysql . execute (
7579 ...Mysql . update (
7680 `nt_perm` ,
@@ -82,17 +86,20 @@ class Permission {
8286 }
8387
8488 async delete ( args ) {
85- await Mysql . execute ( `UPDATE nt_perm SET deleted=? WHERE nt_perm_id=?` , [
86- args . deleted ?? 1 ,
87- args . id ,
88- ] )
89- return true
89+ if ( ! args . id ) return false
90+ const r = await Mysql . execute (
91+ ...Mysql . update ( `nt_perm` , `nt_perm_id=${ args . id } ` , {
92+ deleted : args . deleted ?? 1 ,
93+ } ) ,
94+ )
95+ return r . changedRows === 1
9096 }
9197
9298 async destroy ( args ) {
93- return await Mysql . execute (
99+ const r = await Mysql . execute (
94100 ...Mysql . delete ( `nt_perm` , mapToDbColumn ( args , permDbMap ) ) ,
95101 )
102+ return r . affectedRows === 1
96103 }
97104}
98105
@@ -185,60 +192,61 @@ JSON object format:
185192const boolFields = [ 'self_write' , 'inherit' , 'deleted' ]
186193
187194function dbToObject ( row ) {
188- const newRow = JSON . parse ( JSON . stringify ( row ) )
195+ row = JSON . parse ( JSON . stringify ( row ) )
189196 for ( const f of [ 'group' , 'nameserver' , 'zone' , 'zonerecord' , 'user' ] ) {
190197 for ( const p of [ 'create' , 'write' , 'delete' , 'delegate' ] ) {
191- if ( newRow [ `${ f } _${ p } ` ] !== undefined ) {
192- if ( newRow [ f ] === undefined ) newRow [ f ] = { }
193- newRow [ f ] [ p ] = newRow [ `${ f } _${ p } ` ] === 1
194- delete newRow [ `${ f } _${ p } ` ]
198+ if ( row [ `${ f } _${ p } ` ] !== undefined ) {
199+ if ( row [ f ] === undefined ) row [ f ] = { }
200+ row [ f ] [ p ] = row [ `${ f } _${ p } ` ] === 1
201+ delete row [ `${ f } _${ p } ` ]
195202 }
196203 }
197204 }
198205 for ( const b of boolFields ) {
199- newRow [ b ] = newRow [ b ] === 1
206+ row [ b ] = row [ b ] === 1
200207 }
201- if ( newRow . uid !== undefined ) {
202- newRow . user . id = newRow . uid
203- delete newRow . uid
208+
209+ if ( row . uid !== undefined ) {
210+ row . user . id = row . uid
211+ delete row . uid
204212 }
205- if ( newRow . gid !== undefined ) {
206- newRow . group . id = newRow . gid
207- delete newRow . gid
213+ if ( row . gid !== undefined ) {
214+ row . group . id = row . gid
215+ delete row . gid
208216 }
209- newRow . nameserver . usable = [ ]
210- if ( ! [ undefined , '' ] . includes ( newRow . usable_ns ) ) {
211- newRow . nameserver . usable = newRow . usable_ns . split ( ',' )
217+ row . nameserver . usable = [ ]
218+ if ( ! [ undefined , '' ] . includes ( row . usable_ns ) ) {
219+ row . nameserver . usable = row . usable_ns . split ( ',' )
212220 }
213- delete newRow . usable_ns
214- return newRow
221+ delete row . usable_ns
222+ return row
215223}
216224
217225function objectToDb ( row ) {
218- const newRow = JSON . parse ( JSON . stringify ( row ) )
219- if ( newRow ?. user ?. id !== undefined ) {
220- newRow . uid = newRow . user . id
221- delete newRow . user . id
226+ row = JSON . parse ( JSON . stringify ( row ) )
227+ if ( row ?. user ?. id !== undefined ) {
228+ row . uid = row . user . id
229+ delete row . user . id
222230 }
223- if ( newRow ?. group ?. id !== undefined ) {
224- newRow . gid = newRow . group . id
225- delete newRow . group . id
231+ if ( row ?. group ?. id !== undefined ) {
232+ row . gid = row . group . id
233+ delete row . group . id
226234 }
227- if ( newRow ?. nameserver ?. usable !== undefined ) {
228- newRow . usable_ns = newRow . nameserver . usable . join ( ',' )
229- delete newRow . nameserver . usable
235+ if ( row ?. nameserver ?. usable !== undefined ) {
236+ row . usable_ns = row . nameserver . usable . join ( ',' )
237+ delete row . nameserver . usable
230238 }
231239 for ( const f of [ 'group' , 'nameserver' , 'zone' , 'zonerecord' , 'user' ] ) {
232240 for ( const p of [ 'create' , 'write' , 'delete' , 'delegate' ] ) {
233- if ( newRow [ f ] === undefined ) continue
234- if ( newRow [ f ] [ p ] === undefined ) continue
235- newRow [ `${ f } _${ p } ` ] = newRow [ f ] [ p ] === true ? 1 : 0
236- delete newRow [ f ] [ p ]
241+ if ( row [ f ] === undefined ) continue
242+ if ( row [ f ] [ p ] === undefined ) continue
243+ row [ `${ f } _${ p } ` ] = row [ f ] [ p ] === true ? 1 : 0
244+ delete row [ f ] [ p ]
237245 }
238- delete newRow [ f ]
246+ delete row [ f ]
239247 }
240248 for ( const b of boolFields ) {
241- newRow [ b ] = newRow [ b ] === true ? 1 : 0
249+ row [ b ] = row [ b ] === true ? 1 : 0
242250 }
243- return newRow
251+ return row
244252}
0 commit comments