Permalink
Browse files

implemented token persistence

  • Loading branch information...
pulse00 committed Nov 28, 2010
1 parent 53df2b7 commit c2c722c2fc2f21e50337e4c82aa89011a2eeb363
@@ -0,0 +1,26 @@
+class AuthsubsController < ApplicationController
+ unloadable
+
+ def listdocs
+ client = GData::Client::DocList.new
+ client.authsub_token = User.current.authsubtoken
+ @feed = client.get('http://docs.google.com/feeds/documents/private/full?title=' + params[:q]).to_xml
+ render :partial => 'listdocs'
+ end
+
+ def finish
+
+ client = GData::Client::DocList.new
+ client.authsub_token = params[:token] # extract the single-use token from the URL query params
+ session[:token] = client.auth_handler.upgrade()
+ client.authsub_token = session[:token] if session[:token]
+
+ user = User.current
+ user.authsubtoken = session[:token]
+ user.save
+
+ flash[:notice] = "Google Account connected successfully!"
+ redirect_to :controller => 'my', :action => 'account'
+
+ end
+end
@@ -0,0 +1,2 @@
+module AuthsubsHelper
+end
@@ -0,0 +1,5 @@
+<ul>
+ <% @feed.elements.each('entry') do |entry| %>
+ <li><%= entry.elements['title'].text %></li>
+ <% end %>
+</ul>
@@ -0,0 +1 @@
+<%= params[:token]%>
@@ -0,0 +1 @@
+<%= @authsub_link%>
@@ -1,10 +1 @@
-<a href="#" rel="<%= User.current.googledocs_preference(:account_email) %>" style="display:none" id="gdocs_email">
-
-<%= stylesheet_link_tag "/plugin_assets/redmine_google_docs_plugin/stylesheets/modalbox-lib.css"%>
-<%= javascript_include_tag("http://www.google.com/jsapi").sub('.js', '')%>
-<%= javascript_tag "google.load('gdata', '2.x');"%>
-
-<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.3/scriptaculous.js"%>
-<%= javascript_include_tag "/plugin_assets/redmine_google_docs_plugin/javascripts/modalbox-lib.js"%>
-<%= javascript_include_tag "/plugin_assets/redmine_google_docs_plugin/javascripts/googledocs.js" %>
-
+<%= javascript_include_tag "/plugin_assets/redmine_google_docs_plugin/javascripts/googledocs.js" %>
@@ -4,4 +4,9 @@
<%= label :googledocs, :account_email %>
<%= text_field :googledocs, :account_email, :value => account_email %>
</p>
+<p>
+ <%= label :googledocs, :app_domain %>
+ <%= text_field :googledocs, :app_domain, :value => app_domain %>
+</p>
+<%= link_to(l(:gdocs_connect), authsub_link)%>
</div>
@@ -1,78 +1,19 @@
-function handleFeed(response) {
-
- var entries = response.feed.entry;
-
- if (!entries.length) {
-
- alert('no documents found');
- return;
- }
-
- var html = [];
-
- for (var i = 0, entry; entry = entries[i]; i++) {
- var title = entry.title.$t;
-
- $('docs').insert('<li rel="' + entry.link[0].href + '">' + title + '</li>')
- }
-
- console.log(response);
-
-}
-
-
-function handleError(e) {
-
-
-
-}
-
document.observe('dom:loaded', function() {
- var email = $('gdocs_email').readAttribute('rel');
-
- if (email.length == 0)
- return;
-
-
- $('issue_custom_field_values_1').observe('click', function() {
-
-
- try {
- Modalbox.show('<div><ul id="docs"></ul></div>', {
- doNotMove: true,
- title: 'Search your Google Docs',
- overlayDuration: 0.0,
- slideDownDuration: 0.0,
- slideUpDuration: 0.0,
- resizeDuration: 0.0,
- overlayOpacity: 0.01,
- autoFocusing: false,
- afterLoad: function(){
-
-
- var scope = 'https://docs.google.com/feeds/';
- start = new Date().getTime();
- if (google.accounts.user.checkLogin(scope)) {
- var service = new google.gdata.client.GoogleService('writely', 'DocList-App-v2.0');
- service.getFeed(scope + 'documents/private/full/', handleFeed, handleError);
- } else {
- var token = google.accounts.user.login(scope); // can ignore returned token
- }
-
- console.log('test');
-
- }
-
- });
- } catch (e) {
-
- console.log(e);
-
- }
-
- });
-
-
+ $('issue_custom_field_values_1').up().up().insert('<div class="autocomplete" id="docs_candidates" style="display: none;"></div>');
+
+
+ ac = new Ajax.Autocompleter('issue_custom_field_values_1',
+ 'docs_candidates',
+ '/authsubs/listdocs',
+ { minChars: 3,
+ frequency: 0.5,
+ paramName: 'q',
+ updateElement: function(value) {
+
+ console.log(value);
+ // setCurrentIssue(value.id, $(value).innerHTML);
+ // $('issue_id').clear();
+ }});
});
View
@@ -1,3 +1,4 @@
# German strings go here for Rails i18n
de:
google_docs: "Google Docs"
+ gdocs_connect: "Verbinden"
View
@@ -1,3 +1,4 @@
# English strings go here for Rails i18n
en:
google_docs: "Google Docs"
+ gdocs_connect: "Connect"
@@ -0,0 +1,8 @@
+class Addauthsubtoken < ActiveRecord::Migration
+ def self.up
+ add_column :users, :authsubtoken, :string
+ end
+
+ def self.down
+ end
+end
View
26 init.rb
@@ -2,12 +2,18 @@
require 'dispatcher'
Dispatcher.to_prepare do
+ require_dependency 'issue'
require_dependency 'googledocs_user_patch'
require_dependency 'googledocs_my_controller_patch'
+
+ Issue::SAFE_ATTRIBUTES << "Authsubtoken" if Issue.const_defined? "SAFE_ATTRIBUTES"
end
+config.gem 'gdata', :lib => 'gdata'
+
require_dependency 'google_docs_plugin/hooks'
+
Redmine::Plugin.register :redmine_google_docs_plugin do
name 'Redmine Google Docs Plugin plugin'
author 'Robert Gründler'
@@ -16,3 +22,23 @@
url 'http://example.com/path/to/plugin'
author_url 'http://example.com/about'
end
+
+class GoogleDocCustomFieldFormat < Redmine::CustomFieldFormat
+ include ApplicationHelper
+ include ActionView::Helpers::TextHelper
+ include ActionView::Helpers::TagHelper
+
+ def format_as_url(value)
+ textilizable (value)
+ end
+
+ def escape_html?
+ false
+ end
+
+
+end
+
+Redmine::CustomFieldFormat.map do |fields|
+ fields.register GoogleDocCustomFieldFormat.new('googledoc', :label => :label_googledoc, :order => 8)
+end
@@ -4,11 +4,23 @@ module Hooks
class LayoutHook < Redmine::Hook::ViewListener
-
def view_my_account(context={ })
+
+ next_url = url_for({
+ :controller => 'authsubs',
+ :action => 'finish',
+ :only_path => false,
+ :host => context[:request].host + ':' + context[:request].port.to_s
+ })
+
return context[:controller].send(:render_to_string, {
:partial => 'shared/view_my_account_gdocs',
- :locals => {:user => context[:user], :account_email => context[:user].googledocs_preference(:account_email) }
+ :locals => {
+ :user => context[:user],
+ :account_email => context[:user].googledocs_preference(:account_email),
+ :app_domain => context[:user].googledocs_preference(:app_domain),
+ :authsub_link => context[:user].authsub_link(next_url)
+ }
})
end
@@ -21,8 +33,6 @@ def view_issues_form_details_bottom(context={ })
})
end
-
-
end
end
end
@@ -19,12 +19,16 @@ module InstanceMethods
def save_googledocs_preferences
if request.post? && flash[:notice] == l(:notice_account_updated)
email = (params[:googledocs] ? params[:googledocs][:account_email] : '').to_s
+ domain = (params[:googledocs] ? params[:googledocs][:app_domain] : '').to_s
if email == '' || email.match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
User.current.googledocs_preference(:account_email, email)
else
flash[:error] = "Invalid email address #{email}"
end
+
+ User.current.googledocs_preference(:app_domain, domain)
+
end
end
end
@@ -1,4 +1,5 @@
require_dependency 'user'
+include ActionController::UrlWriter
module GoogleDocsPlugin
module UserPatch
@@ -12,14 +13,26 @@ module ClassMethods
module InstanceMethods
+
+ def authsub_link(next_url)
+
+ client = GData::Client::DocList.new
+ secure = false # set secure = true for signed AuthSub requests
+ sess = true
+ domain = googledocs_preference(:app_domain) # force users to login to a Google Apps hosted domain
+ return authsub_link = client.authsub_url(next_url, secure, sess, domain)
+
+ end
+
+
def googledocs_preference(attr, set_to = nil)
prefixed = "googledocs_#{attr}".intern
v = self.pref[prefixed]
v = nil if v == ''
case attr
- when :account_email
+ when :account_email, :app_domain
if !v && (!set_to)
set_to = ''
end
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class AuthsubsControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end

0 comments on commit c2c722c

Please sign in to comment.