@@ -772,7 +772,7 @@ def _show_role(self, command):
772772 role_name_tree : Tree = command ['role_name' ]
773773 role_name : str = role_name_tree .children [0 ].strip ("'\" " )
774774 print (f"show role: { role_name } " )
775- url = f'http://{ self .host } :{ self .port } /api/v1/admin/roles/{ role_name } /permissions '
775+ url = f'http://{ self .host } :{ self .port } /api/v1/admin/roles/{ role_name } /permission '
776776 response = requests .get (
777777 url ,
778778 auth = HTTPBasicAuth (self .admin_account , self .admin_password )
@@ -794,7 +794,18 @@ def _grant_permission(self, command):
794794 action_str : str = action_tree .children [0 ].strip ("'\" " )
795795 actions .append (action_str )
796796 print (f"grant role_name: { role_name_str } , resource: { resource_str } , actions: { actions } " )
797- pass
797+ url = f'http://{ self .host } :{ self .port } /api/v1/admin/roles/{ role_name_str } /permission'
798+ response = requests .post (
799+ url ,
800+ auth = HTTPBasicAuth (self .admin_account , self .admin_password ),
801+ json = {'actions' : actions , 'resource' : resource_str }
802+ )
803+ res_json = response .json ()
804+ if response .status_code == 200 :
805+ self ._print_table_simple (res_json ['data' ])
806+ else :
807+ print (
808+ f"Fail to grant role { role_name_str } with { actions } on { resource_str } , code: { res_json ['code' ]} , message: { res_json ['message' ]} " )
798809
799810 def _revoke_permission (self , command ):
800811 role_name_tree : Tree = command ['role_name' ]
@@ -807,21 +818,53 @@ def _revoke_permission(self, command):
807818 action_str : str = action_tree .children [0 ].strip ("'\" " )
808819 actions .append (action_str )
809820 print (f"revoke role_name: { role_name_str } , resource: { resource_str } , actions: { actions } " )
810- pass
821+ url = f'http://{ self .host } :{ self .port } /api/v1/admin/roles/{ role_name_str } /permission'
822+ response = requests .delete (
823+ url ,
824+ auth = HTTPBasicAuth (self .admin_account , self .admin_password ),
825+ json = {'actions' : actions , 'resource' : resource_str }
826+ )
827+ res_json = response .json ()
828+ if response .status_code == 200 :
829+ self ._print_table_simple (res_json ['data' ])
830+ else :
831+ print (
832+ f"Fail to revoke role { role_name_str } with { actions } on { resource_str } , code: { res_json ['code' ]} , message: { res_json ['message' ]} " )
811833
812834 def _alter_user_role (self , command ):
813835 role_name_tree : Tree = command ['role_name' ]
814836 role_name_str : str = role_name_tree .children [0 ].strip ("'\" " )
815837 user_name_tree : Tree = command ['user_name' ]
816838 user_name_str : str = user_name_tree .children [0 ].strip ("'\" " )
817839 print (f"alter_user_role user_name: { user_name_str } , role_name: { role_name_str } " )
818- pass
840+ url = f'http://{ self .host } :{ self .port } /api/v1/admin/users/{ user_name_str } /role'
841+ response = requests .put (
842+ url ,
843+ auth = HTTPBasicAuth (self .admin_account , self .admin_password ),
844+ json = {'role_name' : role_name_str }
845+ )
846+ res_json = response .json ()
847+ if response .status_code == 200 :
848+ self ._print_table_simple (res_json ['data' ])
849+ else :
850+ print (
851+ f"Fail to alter user: { user_name_str } to role { role_name_str } , code: { res_json ['code' ]} , message: { res_json ['message' ]} " )
819852
820853 def _show_user_permission (self , command ):
821854 user_name_tree : Tree = command ['user_name' ]
822855 user_name_str : str = user_name_tree .children [0 ].strip ("'\" " )
823856 print (f"show_user_permission user_name: { user_name_str } " )
824- pass
857+ url = f'http://{ self .host } :{ self .port } /api/v1/admin/users/{ user_name_str } /permission'
858+ response = requests .get (
859+ url ,
860+ auth = HTTPBasicAuth (self .admin_account , self .admin_password )
861+ )
862+ res_json = response .json ()
863+ if response .status_code == 200 :
864+ self ._print_table_simple (res_json ['data' ])
865+ else :
866+ print (
867+ f"Fail to show user: { user_name_str } permission, code: { res_json ['code' ]} , message: { res_json ['message' ]} " )
825868
826869 def _handle_meta_command (self , command ):
827870 meta_command = command ['command' ]
0 commit comments