From 1bac8bd64f9f703d7e20a65da0ffb05cb150e90f Mon Sep 17 00:00:00 2001 From: mguetlein Date: Sat, 19 Feb 2011 11:26:24 +0100 Subject: add login functionality to webservices --- lib/overwrite.rb | 4 +-- lib/to-html.rb | 99 ++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 66 insertions(+), 37 deletions(-) (limited to 'lib') diff --git a/lib/overwrite.rb b/lib/overwrite.rb index 29a2860..b5c3942 100644 --- a/lib/overwrite.rb +++ b/lib/overwrite.rb @@ -29,7 +29,7 @@ error Exception do halt error.http_code,rep.to_rdfxml when /html/ content_type 'text/html' - halt error.http_code,(OpenTox.text_to_html rep.to_yaml) + halt error.http_code,(OpenTox.text_to_html rep.to_yaml, @subjectid) else content_type 'application/x-yaml' halt error.http_code,rep.to_yaml @@ -49,7 +49,7 @@ class Sinatra::Base halt code,task.to_yaml # PENDING differs from task-webservice when /html/ response['Content-Type'] = "text/html" - halt code,OpenTox.text_to_html(task.to_yaml) + halt code,OpenTox.text_to_html(task.to_yaml, @subjectid) else # default /uri-list/ response['Content-Type'] = "text/uri-list" halt code,task.uri+"\n" diff --git a/lib/to-html.rb b/lib/to-html.rb index 4de5ee6..1f30ca1 100755 --- a/lib/to-html.rb +++ b/lib/to-html.rb @@ -1,7 +1,6 @@ OT_LOGO = "http://opentox.informatik.uni-freiburg.de/ot-logo.png" - class String # encloses URI in text with with link tag @@ -25,29 +24,30 @@ module OpenTox # @param [optional,String] description general info # @param [optional,Array] post_params, array of arrays containing info on POST operation, see example # @return [String] html page - def self.text_to_html( text, related_links=nil, description=nil, post_params=nil ) + def self.text_to_html( text, subjectid=nil, related_links=nil, description=nil, post_params=nil ) # TODO add title as parameter title = nil #$sinatra.url_for($sinatra.request.env['PATH_INFO'], :full) if $sinatra - - html = < -EOF - html.chomp! + html = "" html += ""+title+"" if title - html += < - -EOF - html.chomp! - html += "

Description

"+description.link_urls+"

" if description - html += "

Related links

"+related_links.link_urls+"

" if related_links - if post_params + html += "" + + if AA_SERVER + user = OpenTox::Authorization.get_user(subjectid) if subjectid + html += "

" + unless user + html += "You are currently not logged in to "+$url_provider.url_for("",:full)+ + ", login" + else + html += "You are logged in as '#{user}' to "+$url_provider.url_for("",:full)+ + ", logout" + end + html += "

" + end + + html += "

Description

"+description.link_urls+"

" if description + html += "

Related links

"+related_links.link_urls+"

" if related_links + if post_params html += "

POST parameters

" count = 0 post_params.each do |p| @@ -59,23 +59,52 @@ EOF html += "

" count += 1 end - end - html += "

Content

" if description || related_links - html += < -

-EOF - html.chomp! - html += text.link_urls - html += < - - - -EOF + end + html += "

Content

" if description || related_links + html += "

" + html += text.link_urls + html += "

" html end + def self.login( msg=nil ) + html = "Login" + html += "
" + html += "

" + html += msg+"\n\n" if msg + html += "Please login to "+$url_provider.url_for("",:full)+"\n\n" + html += "" + html += ""+ + ""+ + #""+ + "" + html += "
user:
password:

" + html + end +end + +get '/logout/?' do + response.set_cookie("subjectid",{:value=>nil}) + content_type "text/html" + content = "Sucessfully logged out from "+$url_provider.url_for("",:full) + OpenTox.text_to_html(content) +end + +get '/login/?' do + content_type "text/html" + OpenTox.login +end + +post '/login/?' do + subjectid = OpenTox::Authorization.authenticate(params[:user], params[:password]) + if (subjectid) + response.set_cookie("subjectid",{:value=>subjectid}) + content_type "text/html" + content = "Sucessfully logged in as '"+params[:user]+"' to "+$url_provider.url_for("",:full) + OpenTox.text_to_html(content,subjectid) + else + content_type "text/html" + OpenTox.login("Login failed, please try again") + end end -#puts OpenTox.text_to_html("bla") \ No newline at end of file -- cgit v1.2.3