Advertisement
Guest User

Final Code

a guest
Mar 26th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: ahnaqvi
  5. """
  6.  
  7. # take grant rules to check for info sharing
  8.  
  9.  
  10. def create(subject, created_entity_type, rights_of_subject_over_created_entity, initial_graph):# rights of sub over created is a string
  11. created_entity_type.append(len(initial_graph[0])+1)
  12. initial_graph[subject].append(rights_of_subject_over_created_entity)
  13. for i in graph.keys():
  14. if i != subject:
  15. graph[i].append("")
  16.  
  17.  
  18.  
  19.  
  20. def tg_connected(nodeA, nodeB, graph):
  21. visited = [False]*len(graph.keys())
  22. queue = []
  23. queue.append(nodeA)
  24. visited[nodeA] = True
  25. while queue:
  26. n = queue.pop()
  27. if n == nodeB:
  28. return True
  29. for i,x in enumerate(graph[n]):
  30. print(i,x)
  31. if 't' in x or 'g' in x:
  32. if visited[i] == False:
  33. queue.append(i)
  34. visited[i] = True
  35. return False
  36.  
  37. def can_share(graph, right, x, p, subjects): # x and p are nodes. right is a character for eg. 'x' or 'w'
  38. if p not in subjects:
  39. return False
  40. if right in graph[p][x] or graph[x][p]:
  41. return True
  42. for subject in subjects:
  43. if right in graph[subject][x]:
  44. if tg_connected(subject,p) or tg_connected(p,subject):
  45. return True
  46. return False
  47.  
  48. def remove(right, s, x, graph, subjects):
  49. if s in subjects:
  50. if right in graph[s][x]:
  51. new_rights = graph[s][x]
  52. new_rights = new_rights.replace(right, "")
  53. graph[s][x] = new_rights
  54.  
  55. # an example graph is given at the top. The function can_share checks if sharing is allowed between two nodes or not. One can use to check if a system is safe or not with regards to a given access profile, for eg a company policy. This system does not deal with theft of rights/flow.
  56.  
  57.  
  58. graph = dict()
  59.  
  60. users = [0,1,2]
  61. files = [3,4,5]
  62.  
  63. # define initial graph
  64. graph[0] = ["","r","","rwx","",""]
  65. graph[1] = ["","","","","",""]
  66. graph[2] = ["","","","","t","r"]
  67. graph[3] = ["","","","","",""]
  68. graph[4] = ["","g","","","",""]
  69. graph[5] = ["","","","","",""]
  70.  
  71. print(can_share(graph, "r", 1, 4, users)) # should return False
  72. print(can_share(graph, "r", 1, 0, users)) # should print True
  73.  
  74.  
  75. #################################################################
  76. # Addendum: The original report stated that Dijkstras and Floyd's algorithm were used. Dijstra's can be used here to find the shortest path to share info. However, it is not needed to KNOW if sharing can happen between two nodes, which is what we're after.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement