@@ -93,22 +93,21 @@ module.exports = function() {
93
93
94
94
var appKey = req . body . key || req . params . key ;
95
95
96
- global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
97
- if ( isMasterKey ) {
98
- //delete all code here.
96
+ // to delete table authorize on app level
97
+ global . appService . isClientAuthorized ( appId , appKey , 'app' , null ) . then ( function ( isAuthorized ) {
98
+ if ( isAuthorized ) {
99
99
global . appService . deleteTable ( appId , tableName ) . then ( function ( table ) {
100
100
res . status ( 200 ) . send ( table ) ;
101
101
} , function ( error ) {
102
102
console . log ( "Table Delete Error" ) ;
103
103
console . log ( error ) ;
104
104
res . status ( 500 ) . send ( 'Cannot delete table at this point in time. Please try again later.' ) ;
105
105
} ) ;
106
- } else {
107
- res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
108
- }
109
- } , function ( error ) {
110
- return res . status ( 500 ) . send ( 'Cannot retrieve security keys.' ) ;
111
- } ) ;
106
+ } else return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
107
+ } , function ( error ) {
108
+ return res . status ( 401 ) . send ( { status : 'Unauthorized' , message :error } ) ;
109
+ } )
110
+
112
111
} catch ( e ) {
113
112
console . log ( "Delete Table Error" ) ;
114
113
console . log ( e ) ;
@@ -137,30 +136,32 @@ module.exports = function() {
137
136
var sdk = req . body . sdk || "REST" ;
138
137
var appKey = req . body . key || req . params . key ;
139
138
140
- global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
141
- if ( isMasterKey ) {
142
- //delete all code here.
139
+ if ( global . mongoDisconnected ) {
140
+ return res . status ( 500 ) . send ( 'Storage / Cache Backend are temporarily down.' ) ;
141
+ }
143
142
144
- if ( global . mongoDisconnected ) {
145
- return res . status ( 500 ) . send ( 'Storage / Cache Backend are temporarily down.' ) ;
146
- }
143
+ // check if table already exists
144
+ global . appService . getTable ( appId , tableName ) . then ( function ( table ) {
145
+ // authorize client for table level, if table found then authorize on table level else on app level for creating new table.
146
+ let authorizationLevel = table ? 'table' : 'app'
147
+ global . appService . isClientAuthorized ( appId , appKey , authorizationLevel , table ) . then ( function ( isAuthorized ) {
148
+ if ( isAuthorized ) {
149
+ global . appService . upsertTable ( appId , tableName , body . data . columns , body . data ) . then ( function ( table ) {
150
+ return res . status ( 200 ) . send ( table ) ;
151
+ } , function ( err ) {
152
+ return res . status ( 500 ) . send ( err ) ;
153
+ } ) ;
154
+ } else return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
155
+ } , function ( error ) {
156
+ return res . status ( 401 ) . send ( { status : 'Unauthorized' , message :error } ) ;
157
+ } )
147
158
148
- global . appService . upsertTable ( appId , tableName , body . data . columns ) . then ( function ( table ) {
149
- return res . status ( 200 ) . send ( table ) ;
159
+ } , function ( err ) {
160
+ return res . status ( 500 ) . send ( err ) ;
161
+ } ) ;
150
162
151
- } , function ( err ) {
152
- return res . status ( 500 ) . send ( err ) ;
153
- } ) ;
154
- } else {
155
- return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
156
- }
157
- } , function ( error ) {
158
- return res . status ( 500 ) . send ( 'Cannot retrieve security keys.' ) ;
159
- } ) ;
160
163
global . apiTracker . log ( appId , "App / Table / Create" , req . url , sdk ) ;
161
-
162
164
}
163
-
164
165
} ) ;
165
166
166
167
//get a table.
@@ -175,29 +176,36 @@ module.exports = function() {
175
176
var sdk = req . body . sdk || "REST" ;
176
177
var appKey = req . body . key || req . params . key ;
177
178
178
- global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
179
- if ( isMasterKey ) {
180
- //delete all code here.
181
- if ( tableName === "_getAll" ) {
182
- global . appService . getAllTables ( appId ) . then ( function ( tables ) {
183
- return res . status ( 200 ) . send ( tables ) ;
184
- } , function ( err ) {
185
- return res . status ( 500 ) . send ( 'Error' ) ;
186
- } ) ;
187
- } else {
188
- global . appService . getTable ( appId , tableName ) . then ( function ( table ) {
189
- return res . status ( 200 ) . send ( table ) ;
190
- } , function ( err ) {
191
- return res . status ( 500 ) . send ( 'Error' ) ;
192
- } ) ;
193
- }
179
+ if ( tableName === "_getAll" ) {
180
+ // to get all tables authorize on app level;
181
+ global . appService . isClientAuthorized ( appId , appKey , 'app' , null ) . then ( function ( isAuthorized ) {
182
+ if ( isAuthorized ) {
183
+ global . appService . getAllTables ( appId ) . then ( function ( tables ) {
184
+ return res . status ( 200 ) . send ( tables ) ;
185
+ } , function ( err ) {
186
+ return res . status ( 500 ) . send ( 'Error' ) ;
187
+ } ) ;
188
+ } else return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
189
+ } , function ( error ) {
190
+ return res . status ( 401 ) . send ( { status : 'Unauthorized' , message :error } ) ;
191
+ } )
194
192
195
193
} else {
196
- return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
194
+
195
+ global . appService . getTable ( appId , tableName ) . then ( function ( table ) {
196
+ // to get a tables authorize on table level;
197
+ global . appService . isClientAuthorized ( appId , appKey , 'table' , table ) . then ( function ( isAuthorized ) {
198
+ if ( isAuthorized ) {
199
+ return res . status ( 200 ) . send ( table ) ;
200
+ } else return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
201
+ } , function ( error ) {
202
+ return res . status ( 401 ) . send ( { status : 'Unauthorized' , message :error } ) ;
203
+ } )
204
+
205
+ } , function ( err ) {
206
+ return res . status ( 500 ) . send ( 'Error' ) ;
207
+ } ) ;
197
208
}
198
- } , function ( error ) {
199
- return res . status ( 500 ) . send ( 'Cannot retrieve security keys.' ) ;
200
- } ) ;
201
209
202
210
global . apiTracker . log ( appId , "App / Table / Get" , req . url , sdk ) ;
203
211
}
0 commit comments