Class: ZuulViz

Inherits:
Object
  • Object
show all
Defined in:
lib/zuul_viz.rb

Instance Method Summary collapse

Instance Method Details

#graph(*args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zuul_viz.rb', line 3

def graph(*args)
  opts = {:subject_class => Zuul.configuration.subject_class.to_s.camelize.singularize.constantize}
  opts = opts.merge(args[0]) if args.length > 0
  subject_class = opts[:subject_class]

  g = GraphViz.new(:G, :type => :graph)
  g["compound"] = true
  g.edge["lhead"] = ""
  g.edge["ltail"] = ""
  #g["splines"] = "line"
  #g["rankdir"] = "LR"
  #g["nodesep"] = "2"
  #g["ranksep"] = "2"

  roles = subject_class.auth_scope.role_class.all
  raise "No roles to graph" if roles.length == 0

  ug = g.add_graph("cluster0")
  ug["label"] = "Assigned #{subject_class.name.pluralize}"
  rg = g.add_graph("cluster1")
  rg["label"] = subject_class.auth_scope.role_class_name.pluralize
  
  graph_roles = {}
  roles.each do |role|
    graph_roles[role.id] = rg.add_nodes(node_str(role))
  
    subject_class.auth_scope.role_subject_class.where(subject_class.auth_scope.role_foreign_key.to_sym => role.id).group(:context_type, :context_id).each do |role_subject|
      g.add_edges(graph_roles[role.id], ug.add_nodes("#{subject_class.auth_scope.role_subject_class.where(subject_class.auth_scope.role_foreign_key.to_sym => role.id, :context_type => role_subject.context_type, :context_id => role_subject.context_id).count} #{subject_class.name.pluralize}"), :label => context_str(role_subject.context), :color => context_color(role_subject.context), :fontcolor => context_color(role_subject.context))
    end
  end
  
  permissions = subject_class.auth_scope.permission_class.all
  if permissions.length > 0
    graph_permissions = {}
    pg = g.add_graph("cluster2")
    pg["label"] = subject_class.auth_scope.permission_class_name.pluralize
    
    permissions.each do |permission|
      graph_permissions[permission.id] = pg.add_nodes(node_str(permission))
      
      subject_class.auth_scope.permission_role_class.where(subject_class.auth_scope.permission_foreign_key.to_sym => permission.id).each do |permission_role|
        g.add_edges(graph_roles[permission_role.send(subject_class.auth_scope.role_foreign_key.to_sym)], graph_permissions[permission.id], :label => context_str(permission_role.context), :color => context_color(permission_role.context), :fontcolor => context_color(permission_role.context))
      end
      
      subject_class.auth_scope.permission_subject_class.where(subject_class.auth_scope.permission_foreign_key.to_sym => permission.id).group(:context_type, :context_id).each do |permission_subject|
        g.add_edges(graph_permissions[permission.id], ug.add_nodes("#{subject_class.auth_scope.permission_subject_class.where(subject_class.auth_scope.permission_foreign_key.to_sym => permission.id, :context_type => permission_subject.context_type, :context_id => permission_subject.context_id).count} #{subject_class.name.pluralize}"), :label => context_str(permission_subject.context), :color => context_color(permission_subject.context), :fontcolor => context_color(permission_subject.context))
      end
    end
  end

  g
end

#graph_permission(permission, *args) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/zuul_viz.rb', line 131

def graph_permission(permission, *args)
  opts = {:permission_class => Zuul.configuration.permission_class.to_s.camelize.singularize.constantize}
  opts = opts.merge(args[0]) if args.length > 0
  if permission.is_a?(Integer)
    permission = opts[:permission_class].find(permission)
  else
    opts[:permission_class] = permission.class
  end
  
  g = GraphViz.new(:G, :type => :graph)
  g["compound"] = true
  g.edge["lhead"] = ""
  g.edge["ltail"] = ""
  #g["splines"] = "line"
  
  rg = g.add_graph("cluster0")
  rg["label"] = permission.auth_scope.role_class_name.pluralize
  ug = g.add_graph("cluster1")
  ug["label"] = permission.auth_scope.subject_class_name.pluralize

  permission_node = g.add_nodes(node_str(permission))
    
  permission.send(permission.auth_scope.permission_roles_table_name).each do |permission_role|
    role = permission_role.send(permission.auth_scope.role_class_name.underscore)
    g.add_edges(permission_node, rg.add_nodes(node_str(role)), :label => context_str(permission_role.context), :color => context_color(permission_role.context), :fontcolor => context_color(permission_role.context))
  end
  
  permission.auth_scope.permission_subject_class.where(permission.auth_scope.permission_foreign_key.to_sym => permission.id).group(:context_type, :context_id).each do |permission_subject|
    g.add_edges(permission_node, ug.add_nodes("#{permission.auth_scope.permission_subject_class.where(permission.auth_scope.permission_foreign_key.to_sym => permission.id, :context_type => permission_subject.context_type, :context_id => permission_subject.context_id).count} #{permission.auth_scope.subject_class_name.pluralize}"), :label => context_str(permission_subject.context), :color => context_color(permission_subject.context), :fontcolor => context_color(permission_subject.context))
  end

  g
end

#graph_role(role, *args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/zuul_viz.rb', line 97

def graph_role(role, *args)
  opts = {:role_class => Zuul.configuration.role_class.to_s.camelize.singularize.constantize}
  opts = opts.merge(args[0]) if args.length > 0
  if role.is_a?(Integer)
    role = opts[:role_class].find(role)
  else
    opts[:role_class] = role.class
  end
  
  g = GraphViz.new(:G, :type => :graph)
  g["compound"] = true
  g.edge["lhead"] = ""
  g.edge["ltail"] = ""
  #g["splines"] = "line"
  
  pg = g.add_graph("cluster0")
  pg["label"] = role.auth_scope.permission_class_name.pluralize
  ug = g.add_graph("cluster1")
  ug["label"] = role.auth_scope.subject_class_name.pluralize

  role_node = g.add_nodes(node_str(role))
    
  role.send(role.auth_scope.permission_roles_table_name).each do |permission_role|
    permission = permission_role.send(role.auth_scope.permission_class_name.underscore)
    g.add_edges(role_node, pg.add_nodes(node_str(permission)), :label => context_str(permission_role.context), :color => context_color(permission_role.context), :fontcolor => context_color(permission_role.context))
  end
  
  role.auth_scope.role_subject_class.where(role.auth_scope.role_foreign_key.to_sym => role.id).group(:context_type, :context_id).each do |role_subject|
    g.add_edges(role_node, ug.add_nodes("#{role.auth_scope.role_subject_class.where(role.auth_scope.role_foreign_key.to_sym => role.id, :context_type => role_subject.context_type, :context_id => role_subject.context_id).count} #{role.auth_scope.subject_class_name.pluralize}"), :label => context_str(role_subject.context), :color => context_color(role_subject.context), :fontcolor => context_color(role_subject.context))
  end

  g
end

#graph_subject(subject, *args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/zuul_viz.rb', line 56

def graph_subject(subject, *args)
  opts = {:subject_class => Zuul.configuration.subject_class.to_s.camelize.singularize.constantize}
  opts = opts.merge(args[0]) if args.length > 0
  if subject.is_a?(Integer)
    subject = opts[:subject_class].find(subject)
  else
    opts[:subject_class] = subject.class
  end
  
  g = GraphViz.new(:G, :type => :graph)
  g["compound"] = true
  g.edge["lhead"] = ""
  g.edge["ltail"] = ""
  #g["splines"] = "line"

  rg = g.add_graph("cluster0")
  rg["label"] = subject.auth_scope.role_class_name.pluralize
  pg = g.add_graph("cluster1")
  pg["label"] = subject.auth_scope.permission_class_name.pluralize
  
  subject_node = g.add_nodes("#{subject.class.name} #{subject.id}")
  
  subject.send(subject.auth_scope.role_subjects_table_name).each do |role_subject|
    role = role_subject.send(subject.auth_scope.role_class_name.underscore)
    role_node = rg.add_nodes(node_str(role))
    g.add_edges(subject_node, role_node, :label => context_str(role_subject.context), :color => context_color(role_subject.context), :fontcolor => context_color(role_subject.context))
    
    role.send(subject.auth_scope.permission_roles_table_name).each do |permission_role|
      permission = permission_role.send(subject.auth_scope.permission_class_name.underscore)
      g.add_edges(role_node, pg.add_nodes(node_str(permission)), :label => context_str(permission_role.context), :color => context_color(permission_role.context), :fontcolor => context_color(permission_role.context))
    end
  end
  
  subject.send(subject.auth_scope.permission_subjects_table_name).each do |permission_subject|
      permission = permission_subject.send(subject.auth_scope.permission_class_name.underscore)
      g.add_edges(subject_node, pg.add_nodes(node_str(permission)), :label => context_str(permission_subject.context), :color => context_color(permission_subject.context), :fontcolor => context_color(permission_subject.context))
  end

  g
end