Skip to content

Commit 7228135

Browse files
committed
add apijson head support and example; make the request setting check more strict
1 parent ecdf07a commit 7228135

File tree

5 files changed

+116
-20
lines changed

5 files changed

+116
-20
lines changed

demo/apps/apijson_demo/settings.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ moment = 'apijson_demo.models.Moment'
66
[APIJSON_MODELS]
77
moment = {
88
"user_id_field" : "user_id",
9-
"GET" : { "roles" : ["OWNER"] },
9+
"GET" : { "roles" : ["OWNER","LOGIN"] },
10+
"HEAD" : { "roles" : ["OWNER","LOGIN"] },
1011
"POST" : { "roles" : ["OWNER"] },
1112
"PUT" : { "roles" : ["OWNER"] },
1213
"DELETE" : { "roles" : ["OWNER"] },
1314
}
1415
comment = {
1516
"user_id_field" : "user_id",
16-
"GET" : { "roles" : ["OWNER"] },
17+
"GET" : { "roles" : ["OWNER","LOGIN"] },
18+
"HEAD" : { "roles" : ["OWNER","LOGIN"] },
1719
"POST" : { "roles" : ["OWNER"] },
1820
"PUT" : { "roles" : ["OWNER"] },
1921
"DELETE" : { "roles" : ["OWNER"] },

demo/apps/apijson_demo/templates/index.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<div id="app">
88
<tabs v-model:value="tab_current" type="card" :animated="false">
99
<tab-pane label="apijson get" name="tab_get"></tab-pane>
10+
<tab-pane label="apijson head" name="tab_head"></tab-pane>
1011
<tab-pane label="apijson post" name="tab_post"></tab-pane>
1112
<tab-pane label="apijson put" name="tab_put"></tab-pane>
1213
<tab-pane label="apijson delete" name="tab_delete"></tab-pane>
@@ -33,6 +34,20 @@
3334
</i-col>
3435
</Row>
3536

37+
<Row v-if="tab_current==='tab_head'">
38+
<i-col span="3"><div align="center">HTTP HEAD URL</div></i-col>
39+
<i-col span="8"><i-input value="/apijson/head" readonly/></i-col>
40+
</Row>
41+
<Row v-if="tab_current==='tab_head'">
42+
<i-col span="3"><div align="center"><strong>apijson head</strong> request examples</div></i-col>
43+
<i-col span="8">
44+
<i-select v-model="request_data">
45+
<i-option v-for="item in request_head" :value="item.value" :key="item.value">{ item.label }</i-option>
46+
</i-select>
47+
</i-col>
48+
</Row>
49+
50+
3651
<Row v-if="tab_current==='tab_post'">
3752
<i-col span="3"><div align="center">POST URL</div></i-col>
3853
<i-col span="8"><i-input value="/apijson/post" readonly/></i-col>
@@ -88,6 +103,7 @@
88103
delimiters: ['{', '}'],
89104
data: {
90105
request_get : {{=request_get_json}},
106+
request_head : {{=request_head_json}},
91107
request_post : {{=request_post_json}},
92108
request_put : {{=request_put_json}},
93109
request_delete : {{=request_delete_json}},
@@ -97,6 +113,7 @@
97113
tab_current : "tab_get",
98114
tab2url : {
99115
"tab_get":"{{=url_for('uliweb_apijson.apijson.views.ApiJson.get')}}",
116+
"tab_head":"{{=url_for('uliweb_apijson.apijson.views.ApiJson.head')}}",
100117
"tab_post":"{{=url_for('uliweb_apijson.apijson.views.ApiJson.post')}}",
101118
"tab_put":"{{=url_for('uliweb_apijson.apijson.views.ApiJson.put')}}",
102119
"tab_delete":"{{=url_for('uliweb_apijson.apijson.views.ApiJson.delete')}}"
@@ -130,6 +147,9 @@
130147
if (n=="tab_get") {
131148
vm.request_data = vm.request_get[0].value
132149
}
150+
else if (n=="tab_head") {
151+
vm.request_data = vm.request_head[0].value
152+
}
133153
else if (n=="tab_post") {
134154
vm.request_data = vm.request_post[0].value
135155
}

demo/apps/apijson_demo/views.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def index():
88
user_info = "login as user '%s(%s)'"%(request.user.username,request.user)
99
else:
1010
user_info = "not login, you can login with username 'admin/usera/userb/userc', and password '123'"
11+
1112
request_get = [
1213
{
1314
"label":"Single record query: no parameter",
@@ -59,6 +60,17 @@ def index():
5960
},
6061
]
6162

63+
request_head = [
64+
{
65+
"label":"query number of moments for one user",
66+
"value":'''{
67+
"moment": {
68+
"user_id": 1
69+
}
70+
}''',
71+
},
72+
]
73+
6274
request_post = [
6375
{
6476
"label":"Add new moment",
@@ -112,6 +124,7 @@ def index():
112124
return {
113125
"user_info":user_info,
114126
"request_get_json":dumps(request_get),
127+
"request_head_json":dumps(request_head),
115128
"request_post_json":dumps(request_post),
116129
"request_put_json":dumps(request_put),
117130
"request_delete_json":dumps(request_delete),

uliweb_apijson/apijson/settings.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ user = {
1111
"user_id_field" : "id",
1212
"secret_fields" : ["password"],
1313
"GET" : { "roles" : ["ADMIN","OWNER"] },
14+
"HEAD" : { "roles" : ["ADMIN","OWNER"] },
1415
"POST" : { "roles" : ["ADMIN","OWNER"] },
1516
"PUT" : { "roles" : ["ADMIN","OWNER"] },
1617
"DELETE" : { "roles" : ["ADMIN","OWNER"] },

uliweb_apijson/apijson/views.py

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def get(self):
3333
def _get_one(self,key):
3434
modelname = key
3535
params = self.request_data[key]
36+
params_role = params.get("@role")
3637

3738
try:
3839
model = getattr(models,modelname)
@@ -43,14 +44,12 @@ def _get_one(self,key):
4344
model_column_set = None
4445
q = model.all()
4546

46-
#rbac check begin
4747
GET = model_setting.get("GET",{})
4848
if not GET:
49-
return json({"code":401,"msg":"'%s' not accessible by apijson"%(modelname)})
49+
return json({"code":401,"msg":"'%s' not accessible"%(modelname)})
5050

5151
roles = GET.get("roles")
5252
perms = GET.get("perms")
53-
params_role = params.get("@role")
5453
permission_check_ok = False
5554
user_role = None
5655
if params_role:
@@ -76,7 +75,6 @@ def _get_one(self,key):
7675

7776
if not permission_check_ok:
7877
return json({"code":401,"msg":"no permission"})
79-
#rbac check end
8078

8179
filtered = False
8280

@@ -252,6 +250,64 @@ def _filter_owner(self,model,model_setting,q):
252250
owner_filtered = True
253251
return owner_filtered,q
254252

253+
def head(self):
254+
for key in self.request_data:
255+
rsp = self._head(key)
256+
if rsp: return rsp
257+
258+
return json(self.rdict)
259+
260+
def _head(self,key):
261+
modelname = key
262+
params = self.request_data[key]
263+
params_role = params.get("@role")
264+
265+
try:
266+
model = getattr(models,modelname)
267+
model_setting = settings.APIJSON_MODELS.get(modelname,{})
268+
except ModelNotFound as e:
269+
log.error("try to find model '%s' but not found: '%s'"%(modelname,e))
270+
return json({"code":400,"msg":"model '%s' not found"%(modelname)})
271+
272+
q = model.all()
273+
274+
HEAD = model_setting.get("HEAD",{})
275+
if not HEAD:
276+
return json({"code":401,"msg":"'%s' not accessible"%(modelname)})
277+
278+
roles = HEAD.get("roles")
279+
permission_check_ok = False
280+
if not params_role:
281+
if request.user:
282+
params_role = "LOGIN"
283+
else:
284+
params_role = "UNKNOWN"
285+
if params_role not in roles:
286+
return json({"code":401,"msg":"'%s' not accessible by role '%s'"%(modelname,params_role)})
287+
if functions.has_role(request.user,params_role):
288+
permission_check_ok = True
289+
else:
290+
return json({"code":401,"msg":"user doesn't have role '%s'"%(params_role)})
291+
if params_role=="OWNER":
292+
owner_filtered,q = self._filter_owner(model,model_setting,q)
293+
if not owner_filtered:
294+
return json({"code":400,"msg":"'%s' cannot filter with owner"%(modelname)})
295+
for n in params:
296+
if n[0]=="@":
297+
pass
298+
else:
299+
param = params[n]
300+
if not hasattr(model.c,n):
301+
return json({"code":400,"msg":"'%s' don't have field '%s'"%(modelname,n)})
302+
q = model.filter(getattr(model.c,n)==param)
303+
rdict = {
304+
"code":200,
305+
"msg":"success",
306+
"count":q.count(),
307+
}
308+
309+
self.rdict[key] = rdict
310+
255311
def post(self):
256312
tag = self.request_data.get("@tag")
257313
if not tag:
@@ -274,15 +330,17 @@ def _post_one(self,key,tag):
274330
try:
275331
model = getattr(models,modelname)
276332
model_setting = settings.APIJSON_MODELS.get(modelname,{})
277-
request_setting_tag = settings.APIJSON_REQUESTS.get(tag,{})
278333
user_id_field = model_setting.get("user_id_field")
279334
except ModelNotFound as e:
280335
log.error("try to find model '%s' but not found: '%s'"%(modelname,e))
281336
return json({"code":400,"msg":"model '%s' not found"%(modelname)})
282337

283-
request_setting_model = request_setting_tag.get(modelname,{})
284-
request_setting_POST = request_setting_model.get("POST",{})
285-
ADD = request_setting_POST.get("ADD")
338+
request_tag = settings.APIJSON_REQUESTS.get(tag,{})
339+
request_tag_tag = request_tag.get(tag,{})
340+
if not request_tag_tag:
341+
return json({"code":400,"msg":"tag '%s' not found"%(tag)})
342+
tag_POST = request_tag_tag.get("POST",{})
343+
ADD = tag_POST.get("ADD")
286344
if ADD:
287345
ADD_role = ADD.get("@role")
288346
if ADD_role and not params_role:
@@ -371,16 +429,17 @@ def _put_one(self,key,tag):
371429
try:
372430
model = getattr(models,modelname)
373431
model_setting = settings.APIJSON_MODELS.get(modelname,{})
374-
request_setting_tag = settings.APIJSON_REQUESTS.get(tag,{})
375432
user_id_field = model_setting.get("user_id_field")
376433
except ModelNotFound as e:
377434
log.error("try to find model '%s' but not found: '%s'"%(modelname,e))
378435
return json({"code":400,"msg":"model '%s' not found"%(modelname)})
379436

380-
request_setting_model = request_setting_tag.get(modelname,{})
381-
request_setting_PUT = request_setting_model.get("PUT",{})
382-
383-
ADD = request_setting_PUT.get("ADD")
437+
request_tag = settings.APIJSON_REQUESTS.get(tag,{})
438+
request_tag_tag = request_tag.get(tag,{})
439+
if not request_tag_tag:
440+
return json({"code":400,"msg":"tag '%s' not found"%(tag)})
441+
tag_PUT = request_tag_tag.get("PUT",{})
442+
ADD = tag_PUT.get("ADD")
384443
if ADD:
385444
ADD_role = ADD.get("@role")
386445
if ADD_role and not params_role:
@@ -468,16 +527,17 @@ def _delete_one(self,key,tag):
468527
try:
469528
model = getattr(models,modelname)
470529
model_setting = settings.APIJSON_MODELS.get(modelname,{})
471-
request_setting_tag = settings.APIJSON_REQUESTS.get(tag,{})
472530
user_id_field = model_setting.get("user_id_field")
473531
except ModelNotFound as e:
474532
log.error("try to find model '%s' but not found: '%s'"%(modelname,e))
475533
return json({"code":400,"msg":"model '%s' not found"%(modelname)})
476534

477-
request_setting_model = request_setting_tag.get(modelname,{})
478-
request_setting_DELETE = request_setting_model.get("DELETE",{})
479-
480-
ADD = request_setting_DELETE.get("ADD")
535+
request_tag = settings.APIJSON_REQUESTS.get(tag,{})
536+
request_tag_tag = request_tag.get(tag,{})
537+
if not request_tag_tag:
538+
return json({"code":400,"msg":"tag '%s' not found"%(tag)})
539+
tag_DELETE = request_tag_tag.get("DELETE",{})
540+
ADD = tag_DELETE.get("ADD")
481541
if ADD:
482542
ADD_role = ADD.get("@role")
483543
if ADD_role and not params_role:

0 commit comments

Comments
 (0)