@@ -89,7 +89,7 @@ console.info('Initialization complete');
8989MongoDB . MongoClient . connect ( 'mongodb://localhost:27017' , {
9090 useNewUrlParser : true ,
9191 useUnifiedTopology : true
92- } ) . then ( client => {
92+ } ) . then ( async ( client ) => {
9393 const db = client . db ( 'ctf' ) ;
9494 const collections = {
9595 users : db . collection ( 'users' ) ,
@@ -101,6 +101,20 @@ MongoDB.MongoClient.connect('mongodb://localhost:27017', {
101101 } ;
102102 console . info ( 'MongoDB connected' ) ;
103103
104+ createCache = async ( ) => {
105+ try {
106+ await collections . cache . insertOne ( { announcements : 0 , challenges : 0 , registerDisable : false } ) ;
107+ console . info ( "Cache created" )
108+ }
109+ catch ( err ) {
110+ errors ( err ) ;
111+ }
112+
113+ }
114+
115+ let cache = await collections . cache . findOne ( null , { projection : { _id : 0 } } )
116+ if ( cache === null ) await createCache ( )
117+
104118 async function checkPermissions ( username ) {
105119 if ( permissions . includes ( username ) ) return permissions . username ;
106120 const type = ( await collections . users . findOne ( { username : username } , { projection : { type : 1 , _id : 0 } } ) ) ;
@@ -111,14 +125,55 @@ MongoDB.MongoClient.connect('mongodb://localhost:27017', {
111125 }
112126 }
113127
114- createCache = async ( ) => {
115- await collections . cache . insertOne ( { announcements : 0 , challenges : 0 } )
116- console . log ( 'Cache created' )
117- return 0 ;
118- }
128+ app . get ( '/v1/account/disableCreate' , async ( req , res ) => {
129+ try {
130+ if ( req . headers . authorization == undefined ) throw new Error ( 'MissingToken' ) ;
131+ const username = signer . unsign ( req . headers . authorization ) ;
132+ if ( await checkPermissions ( username ) < 2 ) throw new Error ( 'Permissions' ) ;
133+ res . send ( {
134+ success : true ,
135+ state : ( await collections . cache . findOne ( null , { projection : { _id : 0 , registerDisable : 1 } } ) ) . registerDisable
136+ } ) ;
137+ }
138+ catch ( err ) {
139+ errors ( err , res ) ;
140+ }
141+ } ) ;
142+
143+
144+ app . post ( '/v1/account/disableCreate' , async ( req , res ) => {
145+ try {
146+ if ( req . headers . authorization == undefined ) throw new Error ( 'MissingToken' ) ;
147+ const username = signer . unsign ( req . headers . authorization ) ;
148+ if ( await checkPermissions ( username ) < 2 ) throw new Error ( 'Permissions' ) ;
149+ if ( ( await collections . cache . updateOne ( { } , { "$set" : { registerDisable : req . body . disable } } ) ) . matchedCount > 0 ) {
150+ res . send ( {
151+ success : true
152+ } ) ;
153+ }
154+ else {
155+ throw new Error ( 'Unknown' ) ;
156+ }
157+
158+ }
159+ catch ( err ) {
160+ errors ( err , res ) ;
161+ }
162+ } ) ;
119163
120164 app . post ( '/v1/account/create' , async ( req , res ) => {
165+
166+ let admin = false
121167 try {
168+
169+ if ( req . headers . authorization !== undefined ) {
170+ const username = signer . unsign ( req . headers . authorization ) ;
171+ if ( await checkPermissions ( username ) >= 2 ) admin = true
172+ }
173+ if ( ! admin && ( ( await collections . cache . findOne ( null , { projection : { _id : 0 , registerDisable : 1 } } ) ) . registerDisable ) ) {
174+ return res . send ( { success : false , error : 'registration-disabled' } )
175+ }
176+
122177 if ( ! / ^ [ a - z A - Z 0 - 9 . ! # $ % & ' * + / = ? ^ _ ` { | } ~ - ] + @ [ a - z A - Z 0 - 9 ] (?: [ a - z A - Z 0 - 9 - ] { 0 , 61 } [ a - z A - Z 0 - 9 ] ) ? (?: \. [ a - z A - Z 0 - 9 ] (?: [ a - z A - Z 0 - 9 - ] { 0 , 61 } [ a - z A - Z 0 - 9 ] ) ? ) * $ / . test ( req . body . email ) ) throw new Error ( 'BadEmail' ) ;
123178 await collections . users . insertOne ( {
124179 username : req . body . username . toLowerCase ( ) ,
@@ -349,7 +404,7 @@ MongoDB.MongoClient.connect('mongodb://localhost:27017', {
349404 const permissions = await checkPermissions ( username ) ;
350405 if ( permissions === false ) throw new Error ( 'BadToken' ) ;
351406
352- let announcement = await collections . announcements . findOne ( { _id : MongoDB . ObjectID ( req . params . id ) } , { projection : { _id : 0 } } )
407+ let announcement = await collections . announcements . findOne ( { _id : MongoDB . ObjectID ( req . params . id ) } , { projection : { _id : 0 } } )
353408 if ( announcement !== null ) {
354409 res . send ( {
355410 success : true ,
@@ -1043,7 +1098,7 @@ MongoDB.MongoClient.connect('mongodb://localhost:27017', {
10431098 if ( await checkPermissions ( signer . unsign ( req . headers . authorization ) ) === false ) throw new Error ( 'BadToken' ) ;
10441099 const score = ( await collections . users . findOne ( { username : req . params . username } , { projection : { score : 1 , type : 1 , _id : 0 } } ) ) ;
10451100 if ( score === null ) throw new Error ( 'NotFound' ) ;
1046- if ( score . type === 2 ) return res . send ( { success : true , score : "hidden" } )
1101+ if ( score . type === 2 ) return res . send ( { success : true , score : "hidden" } )
10471102 res . send ( {
10481103 success : true ,
10491104 score : score . score
0 commit comments