@@ -85,7 +85,7 @@ def create_vm(body=None): # noqa: E501
85
85
return {"error" : "Your are not allowed to be here" }, 403
86
86
87
87
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
88
-
88
+
89
89
admin = False
90
90
if "attributes" in cas :
91
91
if "memberOf" in cas ["attributes" ]:
@@ -138,6 +138,8 @@ def delete_vm_id_with_error(vmid): #API endpoint to delete a VM when an error oc
138
138
admin = True ;
139
139
140
140
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
141
+ if not admin and dbfct .get_vm_userid (vmid ) != user_id : # if not admin, we check if the user is the owner of the vm
142
+ return {'error' : "Forbidden" } , 403
141
143
if admin :
142
144
freezeAccountState = 0
143
145
else :
@@ -186,6 +188,8 @@ def delete_vm_id(vmid): # noqa: E501
186
188
admin = True ;
187
189
188
190
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
191
+ if not admin and dbfct .get_vm_userid (vmid ) != user_id : # if not admin, we check if the user is the owner of the vm
192
+ return {'error' : "Forbidden" } , 403
189
193
if admin :
190
194
freezeAccountState = 0
191
195
else :
@@ -199,19 +203,6 @@ def delete_vm_id(vmid): # noqa: E501
199
203
200
204
if freezeAccountState >= 3 and not admin : # if freeze state 1 or 2 user still have access to proxmox
201
205
return {"status" : "cotisation expired" }, 403
202
-
203
- user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
204
- body ,statusCode = proxmox .get_freeze_state (user_id )
205
- if statusCode != 200 :
206
- return body , statusCode
207
- try :
208
- freezeAccountState = int (body ["freezeState" ])
209
- except Exception as e :
210
- return {"error" : "error while getting freeze state" }, 500
211
-
212
- if freezeAccountState >= 3 and not admin : # if freeze state 1 or 2 user still have access to proxmox
213
- return {"status" : "cotisation expired" }, 403
214
-
215
206
216
207
node = proxmox .get_node_from_vm (vmid )
217
208
if not node : #doesn't exist
@@ -263,45 +254,12 @@ def delete_vm_in_thread(vmid, user_id, node="", dueToError=False):
263
254
node = proxmox .get_node_from_vm (vmid )
264
255
if not node :
265
256
return {"status" : "vm not exists" }, 404
266
- if "attributes" in cas :
267
- if "memberOf" in cas ["attributes" ]:
268
- if is_admin (cas ["attributes" ]["memberOf" ]):
269
- return proxmox .delete_vm (vmid , node )
270
257
if vmid in map (int , proxmox .get_vm (user_id )[0 ]):
271
258
return proxmox .delete_vm (vmid , node )
272
259
else :
273
260
return {"status" : "error" }, 500
274
261
275
262
276
- ################
277
- ## DEPRECATED ##
278
- ################
279
- # Reason : must be remplaced by the freeze state
280
- def is_cotisation_uptodate ():
281
- headers = {"Authorization" : connexion .request .headers ["Authorization" ]}
282
- status_code , cas = util .check_cas_token (headers )
283
- if status_code != 200 :
284
- return {"Error" : "You are UNAUTHORIZED to connect to CAS" }, 403
285
- if "attributes" in cas :
286
- if "memberOf" in cas ["attributes" ]:
287
- if is_admin (cas ["attributes" ]["memberOf" ]):
288
- return {"status" : "function denied for admin" }, 403
289
-
290
- id = cas ['attributes' ]['id' ]
291
-
292
- r = requests .get ("https://adh6.minet.net/api/member/" + id , headers = headers )
293
-
294
- if status_code != 200 :
295
- return {"Error" : "You are UNAUTHORIZED to connect to adh6" }, 403
296
-
297
- strdate = cas ['departureDate' ]
298
- date = datetime .strptime (strdate , '%Y-%m-%d' )
299
- if date > datetime .today ():
300
- return {"uptodate" : 1 }, 201 ;
301
- else :
302
- return {"uptodate" : 0 }, 201 ;
303
-
304
-
305
263
def get_dns (): # noqa: E501
306
264
"""check if a user has signed the hosting charter
307
265
@@ -463,8 +421,9 @@ def get_vm_id(vmid): # noqa: E501
463
421
464
422
465
423
node = proxmox .get_node_from_vm (vmid )
466
-
467
- if node == None and not admin : # exist in the db but not in proxmox. It's a error
424
+ if not admin and dbfct .get_vm_userid (vmid ) != user_id : # if not admin, we check if the user is the owner of the vm
425
+ return {'error' : "Forbidden" } , 403
426
+ elif node == None and not admin : # exist in the db but not in proxmox. It's a error
468
427
return {"error" : "VM not found in proxmox" }, 500
469
428
elif node == None and admin :
470
429
return {'error' : "VM no found" } , 404
@@ -571,6 +530,8 @@ def renew_ip():
571
530
admin = True ;
572
531
573
532
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
533
+ if not admin and dbfct .get_vm_userid (vmid ) != user_id : # if not admin, we check if the user is the owner of the vm
534
+ return {'error' : "Forbidden" } , 403
574
535
if admin :
575
536
freezeAccountState = 0 # Un admin n'a pas d'expiration de compte
576
537
else :
@@ -618,6 +579,8 @@ def delete_dns_id(dnsid): # noqa: E501
618
579
if is_admin (cas ["attributes" ]["memberOf" ]):
619
580
admin = True ;
620
581
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
582
+ if not admin and dbfct .get_entry_userid (dnsid ) != user_id : # if not admin, we check if the user is the owner of the vm
583
+ return {'error' : "Forbidden" } , 403
621
584
if admin :
622
585
freezeAccountState = 0
623
586
else :
@@ -667,6 +630,8 @@ def get_dns_id(dnsid): # noqa: E501
667
630
if is_admin (cas ["attributes" ]["memberOf" ]):
668
631
admin = True ;
669
632
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
633
+ if not admin and dbfct .get_entry_userid (dnsid ) != user_id : # if not admin, we check if the user is the owner of the vm
634
+ return {'error' : "Forbidden" } , 403
670
635
if admin :
671
636
freezeAccountState = 0
672
637
else :
@@ -720,7 +685,6 @@ def patch_vm(vmid, body=None): # noqa: E501
720
685
"""
721
686
if connexion .request .is_json :
722
687
requetsBody = VmItem .from_dict (connexion .request .get_json ()) # noqa: E501
723
-
724
688
try :
725
689
vmid = int (vmid )
726
690
except :
@@ -739,6 +703,8 @@ def patch_vm(vmid, body=None): # noqa: E501
739
703
admin = True
740
704
741
705
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
706
+ if not admin and dbfct .get_vm_userid (vmid ) != user_id : # if not admin, we check if the user is the owner of the vm
707
+ return {'error' : "Forbidden" } , 403
742
708
if admin :
743
709
freezeAccountState = 0
744
710
else :
@@ -755,7 +721,7 @@ def patch_vm(vmid, body=None): # noqa: E501
755
721
756
722
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
757
723
758
- if vmid in map ( int , proxmox . get_vm ( user_id )[ 0 ]) or admin :
724
+ if admin or dbfct . get_vm_userid ( vmid ) == user_id : # if not admin, we check if the user is the owner of the vm
759
725
node = proxmox .get_node_from_vm (vmid )
760
726
if not node :
761
727
return {"status" : "vm not exists" }, 404
@@ -865,6 +831,8 @@ def update_credentials():
865
831
admin = True ;
866
832
867
833
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
834
+ if not admin and dbfct .get_vm_userid (vmid ) != user_id : # if not admin, we check if the user is the owner of the vm
835
+ return {'error' : "Forbidden" } , 403
868
836
if admin :
869
837
freezeAccountState = 0 # Un admin n'a pas d'expiration de compte
870
838
else :
@@ -904,7 +872,7 @@ def get_need_to_be_restored(vmid):
904
872
905
873
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
906
874
admin = False
907
-
875
+
908
876
try :
909
877
vmid = int (vmid )
910
878
except :
@@ -914,7 +882,8 @@ def get_need_to_be_restored(vmid):
914
882
if "memberOf" in cas ["attributes" ]:
915
883
if is_admin (cas ["attributes" ]["memberOf" ]): # partie admin pour renvoyer l'owner en plus
916
884
admin = True
917
-
885
+ if not admin and dbfct .get_vm_userid (vmid ) != user_id : # if not admin, we check if the user is the owner of the vm
886
+ return {'error' : "Forbidden" } , 403
918
887
if admin :
919
888
freezeAccountState = 0 # Un admin n'a pas d'expiration de compte
920
889
else :
@@ -955,14 +924,14 @@ def get_account_state(username):
955
924
user_id = slugify (cas ['sub' ].replace ('_' , '-' ))
956
925
username = username .replace ('_' , '-' )
957
926
admin = False
958
- print (user_id , username )
959
927
960
928
if "attributes" in cas :
961
929
if "memberOf" in cas ["attributes" ]:
962
930
if is_admin (cas ["attributes" ]["memberOf" ]): # partie admin pour renvoyer l'owner en plus
963
931
admin = True
964
- if not admin and user_id != username :
932
+ if admin and user_id == username :
933
+ return {"freezeState" : "0" }, 200 # we fake it
934
+ elif admin or user_id == username :
935
+ return proxmox .get_freeze_state (username )
936
+ else :
965
937
return {"error" : "You are not allowed to check this account" }, 403
966
- elif admin :
967
- return {"freezeState" : "0" }, 200
968
- return proxmox .get_freeze_state (username )
0 commit comments