-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_wall.rb
199 lines (170 loc) · 5.25 KB
/
create_wall.rb
1
2
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
55
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
96
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
130
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env jruby
require 'net/http'
require 'rubygems'
require 'json'
require 'markaby'
#require 'random'
@dry_run = true
@url = URI.parse("http://localhost:8080")
@user = "admin"
@pass = "admin"
puts "-- This is a script to create a My Wall dashboard for a user"
puts "-- for use with Sakai OAE 1.2"
if ARGV.size === 1
@userid = ARGV[0]
else
@userid = ["payten"]
end
def get_json(path, raw = false)
Net::HTTP.start(@url.host, @url.port) do |http|
#print "-- GET: #{path}\n"
req = Net::HTTP::Get.new(path)
req.basic_auth @user, @pass
response = http.request(req)
if response.code.to_s === "404" then
return nil
end
#puts response
#puts response.body
if raw
return response.body()
else
return JSON.parse(response.body())
end
end
end
def post_json(path, json)
if @dry_run then
"POST: #{path}"
else
prim_post(path, {
":content" => json,
":operation" => "import",
":replace" => "true",
":replaceProperties" => "true",
":contentType" => "json"
})
end
end
def prim_post(path, formData)
Net::HTTP.start(@url.host, @url.port) do |http|
req = Net::HTTP::Post.new(path)
req.basic_auth @user, @pass
req.add_field("Referer", "#{@url.to_s}/dev")
req.set_form_data(formData)
return http.request(req)
end
end
def generateWidgetId()
"id" + (1_000_000 + rand(10_000_000 - 1_000_000)).to_s
end
def all_users()
page = 0
while true
result = get_json("/var/search/users-all.json?page=#{page}")
if result['results'].empty?
break
end
result['results'].each do |result|
yield result['rep:userId']
end
page += 1
end
end
def do_stuff
#puts "*** Update account prefs for all users:\n"
#all_users() do |userid|
print "\n* Looking at: #{@userid}\n"
# get user's pub structure /~userid/public/pubspace
pubspace = get_json("/~#{@userid}/public/pubspace.infinity.json")
#puts pubspace
if pubspace.nil?
puts "user hasn't logged in yet! they'll get the structure set once they login."
next
end
# insert the wall into this structure
pubstructure = JSON.parse(pubspace["structure0"])
if pubstructure.has_key?("wall") then
user_had_wall_already = true
puts "user has a wall... update it"
wall_id = pubstructure["wall"]["_ref"]
htmlblock_id = pubspace["#{wall_id}"]["rows"]["__array__0__"]["columns"]["__array__0__"]["elements"]["__array__0__"]["id"]
dashboard_id = pubspace["#{wall_id}"]["rows"]["__array__0__"]["columns"]["__array__0__"]["elements"]["__array__1__"]["id"]
else
puts "no wall for user... create it"
wall_id = generateWidgetId()
htmlblock_id = generateWidgetId()
dashboard_id = generateWidgetId()
end
pubstructure["wall"] = {
"_title" => "__MSG__USER_PUBLIC_DASHBOARD_TITLE__",
"_altTitle"=> "__MSG__USER_PUBLIC_DASHBOARD_ALT_TITLE__",
"_order"=> -1,
"_view"=> "private",
"_canEdit"=> true,
"_reorderOnly"=> false,
"_nonEditable"=> true,
"_ref"=> "#{wall_id}",
"main"=> {
"_ref"=> "#{wall_id}",
"_order"=> 0,
"_title"=> "__MSG__USER_PUBLIC_DASHBOARD_TITLE__"
}
};
pubspace["#{wall_id}"] = {
"#{htmlblock_id}" => {
'htmlblock' => {
'content' => "<div class='fl-force-right dashboard-admin-actions' style='display:none;'><button type='button' class='s3d-button s3d-margin"+
"-top-5 s3d-header-button s3d-header-smaller-button dashboard_change_layout' dat"+
"a-tuid='#{dashboard_id}'>__MSG__EDIT_LAYOUT__</button><button type='button' class='s3d-button "+
"s3d-margin-top-5 s3d-header-button s3d-header-smaller-button dashboard_global_a"+
"dd_widget' data-tuid='#{dashboard_id}'>__MSG__ADD_WIDGET__</button></div><div class='s3d-conte"+
"ntpage-title'>__MSG__USER_PUBLIC_DASHBOARD_TITLE__</div><div id='widget_dashboard_#{dashboard_id}' class='widget_inline'></div>"
}
},
'rows' => {
'__array__0__' => {
'id' => "#{generateWidgetId()}",
'columns' => {
'__array__0__'=> {
'width' => 1,
'elements' => {
'__array__0__' => {
'id' => "#{htmlblock_id}",
'type' => 'htmlblock'
},
'__array__1__' => {
'id' => "#{dashboard_id}",
'type' => "dashboard"
}
}
}
}
}
}
};
# don't overwrite existing dashboard
unless user_had_wall_already
pubspace["#{wall_id}"]["#{dashboard_id}"] = {
"dashboard"=> {
"layout"=> "threecolumn",
"columns"=> {
"column1"=> [],
"column2"=> [],
"column3"=> []
}
}
}
end
pubspace["structure0"] = JSON.generate(pubstructure)
#puts JSON.generate(pubspace["#{wall_id}"])
# post the updated structure back to user's pubstructure url
print "update user's pubspace: "
response = post_json("/~#{@userid}/public/pubspace", JSON.generate(pubspace))
print response
#puts response.body
# that should do it!
print "\n"
#end
end
do_stuff