From 080a9d882b3d9b307f02b3e044d9ebe44a18b67c Mon Sep 17 00:00:00 2001
From: Fawwaz Anugrah Wiradhika Dharmasatya <anugrahdwfawwaz@gmail.com>
Date: Tue, 4 Jun 2024 14:39:29 +0700
Subject: [PATCH] fix: acl check if acl is a method in a class

---
 src/lib/ACLAnalyzer.py     | 16 ++++++++++++++--
 src/lib/MainMenu.py        |  1 +
 tests/tc1/class_views.py   |  4 ++--
 tests/tc1/lib/RoleCheck.py |  2 +-
 todo.txt                   |  8 ++------
 5 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/src/lib/ACLAnalyzer.py b/src/lib/ACLAnalyzer.py
index b7968de..467b213 100644
--- a/src/lib/ACLAnalyzer.py
+++ b/src/lib/ACLAnalyzer.py
@@ -459,7 +459,9 @@ class ACLAnalyzer():
         parent_class = ""
         for k in acl_class.context:
           if k.startswith("Parent::"):
+            # print("Name",name)
             parent_class = k.split("::")[1]
+            # print("Parent",parent_class)
         if acl_class.location==route.location:
           # Kelas/fungsi lokal
           # Kali aja ada di module ini
@@ -491,8 +493,14 @@ class ACLAnalyzer():
                 acl_list = list(set(acl_list) & set(self.analyze_function(acl_class,False)))
 #print("aclku",acl_list)
                 # acl_list = self.acl_info.acl_context[acl_class.get_base_element_name()]
-              elif(function['type']=='class_method' and function['name']==acl_class.get_base_element_name() and (name==f"{function['parent']}.{function['name']}")):
+              elif(function['type']=='class_method' and function['name']==acl_class.get_base_element_name() and (name==f"{function['parent']}.{function['name']}" or function['parent']==parent_class)):
+                # print("MASOK",acl_class)
                 acl_list = list(set(acl_list) & set(self.analyze_function(acl_class,False)))
+            # elif(acl_class.type=='function' and function['type']=='class_method' and function['parent']==parent_class and function['name']==acl_class.get_base_element_name()):
+            #   # Cukup cek apakah kelas atau fungsinya sama
+            #   if(name==f'{parent_class}.{function["name"]}'):
+            #   # Cek apakah dia manggil fungsi yang diimport di acl
+            #     return True
         else:
           # Beda file, cek di import ada gak
           # Kali aja ada di module ini
@@ -545,6 +553,9 @@ class ACLAnalyzer():
                   # print("compa",comparator)
                   # print("name",name)
                   # print("kongteks",self.acl_info.acl_context)
+                  tmp_name = name
+                  if parent_class:
+                    tmp_name = f"{parent_class}.{name}"
                   for method in acl_class.cfg.source_code_method_list:
                     # print("metoda",method)
                     # if method['type']=='class_method' and (f"{method['parent']}.{method['name']}"==name or f"{parent_class}.{method['name']}"==name):
@@ -554,8 +565,9 @@ class ACLAnalyzer():
                       continue
                     elif method['type']=='class_method':
                       comparator = f"{comparator}.{method['name']}"
+                      # and function['name']==acl_class.get_base_element_name() and (name==f"{function['parent']}.{function['name']}" or function['parent']==parent_class)
 #print("compereteg",name,comparator)
-                    if(name==comparator):
+                    if(tmp_name==comparator):
                       # Cek apakah dia manggil fungsi yang diimport di acl
 #print(601)           
                       # if key in self.acl_info.acl_context
diff --git a/src/lib/MainMenu.py b/src/lib/MainMenu.py
index 869a6e7..155fb1c 100644
--- a/src/lib/MainMenu.py
+++ b/src/lib/MainMenu.py
@@ -32,6 +32,7 @@ class MainMenu():
         format_log("ACL data acquired.")
         self.project_ctx = FileReader(self.project_path).analyze_project()
         format_log("ACL and routes context gathered...")
+        print("KONGTEKS",self.project_ctx)
       except FileNotFoundError:
         format_log("File not found. Exiting...",status='error')
       else:
diff --git a/tests/tc1/class_views.py b/tests/tc1/class_views.py
index 67a9fd4..445d92c 100644
--- a/tests/tc1/class_views.py
+++ b/tests/tc1/class_views.py
@@ -67,7 +67,7 @@ class Views():
     @views.route('/logs', methods=['GET'])
     @login_required
     def class_get_logs(self):
-        # if (RoleCheck().is_admin(current_user)):
-        #     abort(403)
+        if (RoleCheck().is_admin(current_user)):
+            abort(403)
         logs = Log.query.get()
         return jsonify(logs)
\ No newline at end of file
diff --git a/tests/tc1/lib/RoleCheck.py b/tests/tc1/lib/RoleCheck.py
index 1acfd2c..c7932b6 100644
--- a/tests/tc1/lib/RoleCheck.py
+++ b/tests/tc1/lib/RoleCheck.py
@@ -1,5 +1,5 @@
-# @ACL
 class RoleCheck():
+    # @ACL
   def is_admin(self,user)->bool:
     return user.role == 'admin'
   def is_authenticated(self, user)->bool:
diff --git a/todo.txt b/todo.txt
index 7c8b44f..b482725 100644
--- a/todo.txt
+++ b/todo.txt
@@ -1,7 +1,3 @@
-1. deteksi pemanggilan
-- dekorator: cek kalo dekorator ada di modul ini, fungsi atau kelas
-- traversing fungsi: cek assignment, operasi,  dsb
-- traversing kelas: cek tiap fungsi di kelas, idem kek fungsi habis itu
-- traversing modul: cek tiap fungsi di kelas dan langsung di modul.
 - exempt function in module
-- tes buat function in class
\ No newline at end of file
+- tes buat function in class
+- acl function in class called from function in class
\ No newline at end of file
-- 
GitLab