Skip to content
Snippets Groups Projects
Commit 1f2a213e authored by Martin Jeppesen's avatar Martin Jeppesen
Browse files

Adds Main group and uses now only cn-dumper.yaml

parent 71fb7f0d
Branches
Tags v0.3.0
No related merge requests found
...@@ -7,7 +7,6 @@ require 'nokogiri' ...@@ -7,7 +7,6 @@ require 'nokogiri'
require 'ruby_dig' require 'ruby_dig'
$config = YAML.load_file('/secrets/cn-dumper.yaml') $config = YAML.load_file('/secrets/cn-dumper.yaml')
ignore_groups = YAML.load_file('/secrets/cn-dumper-dups.yaml')
def get(path, course) def get(path, course)
hdr = "-H 'X-appname: #{$config[course]["appname"]}' -H 'X-token: #{$config[course]["token"]}'" hdr = "-H 'X-appname: #{$config[course]["appname"]}' -H 'X-token: #{$config[course]["token"]}'"
...@@ -15,113 +14,104 @@ def get(path, course) ...@@ -15,113 +14,104 @@ def get(path, course)
cmd = "/usr/bin/curl -s #{hdr} #{url}/#{path}" cmd = "/usr/bin/curl -s #{hdr} #{url}/#{path}"
xml = %x{#{cmd}} xml = %x{#{cmd}}
xml = xml.force_encoding('utf-8') # ruby thinks it is US-ASCII xml = xml.force_encoding('utf-8') # ruby thinks it is US-ASCII
return XmlSimple.xml_in(xml, 'KeyAttr' => { 'Element' => 'Id' }) return XmlSimple.xml_in(xml, 'KeyAttr' => { 'Element' => 'Id' }) # returns hash from xml
end end
def get_groups(e, course) def get_group_members(e, course)
r = get("CurrentUser/Elements/" + e + "/Elements", course) return get("CurrentUser/Elements/" + e + "/Participants", course)
if r.empty?
# this course doesn't have any groups
r = get("CurrentUser/Elements", course)
end end
def get_groups(e, course)
r = get("CurrentUser/Elements/" + e + "/Elements", course)
h = {} h = {}
if r.key?("Element") if r.key?("Element")
r["Element"].each do |id, value| r["Element"].each do |id, value|
h[id] = value["Name"] h[id] = value["Name"]
end end
elsif r.key?("Grouping") elsif r.empty? # no group
# course without groups
if r["Grouping"][0].dig("Element", e, "Name")
h[e] = "All"
end
else else
puts "Error: Key is not Grouping or Element" puts "Error: Key is not Element. CampusNet might have changed the course element number."
exit 1 exit 1
end end
return h return h
end end
def add_users_and_groups(h, group)
h["UserElementRelation"].each do |a|
a["User"].each do |b|
if b["Closed"] == "false"
user = b["UserName"]
$users[user]["Name"] = b["GivenName"] + " " + b["FamilyName"]
$users[user]["Email"] = b["Email"]
$groups[group][user] = a["ACL"]
end
end
end
end
#----------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------
$config.each_key do |course| $config.each_key do |course|
all_groups = get_groups($config[course]["element"], course)
unless ignore_groups[course] $users = Hash.new {|h,k| h[k] = Hash.new &h.default_proc }
puts "WARNING: ignore_groups has no entry for #{course}!" $groups = Hash.new {|h,k| h[k] = Hash.new &h.default_proc }
ignore_groups[course] = { "groups" => [], "email" => "fake@email.com" }
end r = get_group_members($config[course]["element"], course)
add_users_and_groups(r, "Main")
all_groups = get_groups($config[course]["element"], course)
# add nested groups # add nested groups
all_groups.clone.each do |element, v| all_groups.clone.each_key do |e|
tmp = get_groups(element, course) tmp = get_groups(e, course)
if !tmp.empty? if !tmp.empty?
all_groups[tmp.keys[0]] = tmp.values[0] all_groups[tmp.keys[0]] = tmp.values[0]
end end
end end
users = Hash.new {|h,k| h[k] = Hash.new &h.default_proc } all_groups.each do |e, group_name|
groups = Hash.new {|h,k| h[k] = Hash.new &h.default_proc } r = get_group_members(e, course)
add_users_and_groups(r, group_name)
all_groups.each do |gid, gname|
r = get("CurrentUser/Elements/" + gid + "/Participants", course)
r["UserElementRelation"].each do |a|
a["User"].each do |b|
if b["Closed"] == "false"
user = b["UserName"]
users[user]["Name"] = b["GivenName"] + " " + b["FamilyName"]
users[user]["Email"] = b["Email"]
groups[gname][user] = a["ACL"]
end
end
end
end end
# if backdoor file exist. add backdoor users each group # add backdoor users
backdoor = "/secrets/" + course + "-backdoor.yaml" if $config[course].key?("add")
if File.file?(backdoor) $config[course]["add"].each do |u, v|
bd = YAML.load_file(backdoor) if v.dig("Email") && v.dig("Name") && v.dig("Acl")
bd.keys.each do |u| $users[u]["Email"] = v["Email"]
if bd[u].dig("Email") && bd[u].dig("Name") && bd[u].dig("Acl") $users[u]["Name"] = v["Name"]
users[u]["Email"] = bd[u]["Email"] $groups.keys.each do |g|
users[u]["Name"] = bd[u]["Name"] $groups[g][u] = v["Acl"]
groups.keys.each do |g|
groups[g][u] = bd[u]["Acl"]
end end
else else
puts "email, name, or acl is missing in " + backdoor puts "email, name, or acl is missing in backdoor"
end end
end end
end end
# a student should only be in one group # a student should only be in one group
# find students that are in multiple groups # find students that are in multiple groups
aoh = Hash.new { |hash, key| hash[key] = [] } if $config[course].key?("dup_users_ignore_groups")
dup = Hash.new { |hash, key| hash[key] = [] }
groups.each do |group, h| $groups.each do |group, h|
h.each_key do |user| h.each_key do |user|
next if ignore_groups[course]["groups"].include? group next if $config[course]["dup_users_ignore_groups"].include? group
next if groups[group][user] != "User" next if $groups[group][user] != "User"
aoh[user] << group dup[user] << group
end end
end end
dup.each do |user, hashes|
aoh.each do |user, hashes|
next if hashes.size < 2 next if hashes.size < 2
hashes.each do |group| hashes.each do |group|
puts "Send email to " + ignore_groups[course][email] + "that #{user} is also in #{group}" puts "Send email to " + $config[course]["dup_users_notify"] + " that #{user} is also in #{group}"
end
end end
end end
File.open("/dumps/" + course + "-users.yaml",'w') do |h| File.open("/dumps/" + course + "-users.yaml",'w') do |h|
h.write users.to_yaml h.write $users.to_yaml
end end
File.open("/dumps/" + course + "-groups.yaml",'w') do |h| File.open("/dumps/" + course + "-groups.yaml",'w') do |h|
h.write groups.to_yaml h.write $groups.to_yaml
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment