diff --git a/app/models/user.rb b/app/models/user.rb index 3a42787..7dbb366 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -46,6 +46,8 @@ class User < ActiveRecord::Base s.boolean :show_submitted_story_threads, :default => false s.boolean :hide_dragons, :default => false s.string :totp_secret + s.string :github_oauth_token + s.string :github_username end validates :email, :format => { :with => /\A[^@ ]+@[^@ ]+\.[^@ ]+\Z/ }, diff --git a/app/views/settings/index.html.erb b/app/views/settings/index.html.erb index 0559ab4..48d016f 100644 --- a/app/views/settings/index.html.erb +++ b/app/views/settings/index.html.erb @@ -230,6 +230,43 @@

+
+ External Accounts +
+ + <% if Pushover.enabled? %> +
+ <%= label_tag :pushover_user_key, + raw("Pushover:"), + :class => "required" %> + <%= link_to((@edit_user.pushover_user_key.present?? + "Manage Pushover Subscription" : "Subscribe With Pushover"), + "/settings/pushover_auth", :class => "pushover_button", + :method => :post) %> + + For optional comment and message notifications above + +
+ <% end %> + + <% if Github.enabled? %> +
+ <%= label_tag :github_username, "GitHub:", :class => "required" %> + <% if @edit_user.github_username.present? %> + Linked to + <%= h(@edit_user.github_username) %> + (<%= link_to "Disconnect", "/settings/github_disconnect", + :method => :post %>) + <% else %> + Connect + <% end %> +
+ <% end %> + +
+
+
<%= t('.inviteuser') %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 80e8c2b..ec085fa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -300,7 +300,7 @@ en: commentreplynotificationsettings: "Comment Reply Notification Settings" receiveemail: "Receive E-mail:" receivepushover: "Receive Pushover Alert:" - requirepushover: "Requires Pushover subscription above" + requirepushover: "Requires Pushover subscription below" commentmentionnotificationsettings: "Comment Mention Notification Settings" privatemessagenotificationsettings: "Private Message Notification Settings" submittedstorycommentsettings: "Submitted Story Comment Settings" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 1b6e1d3..0cf04a8 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -310,7 +310,7 @@ fr: commentreplynotificationsettings: "Paramètres de notification de réponse à un commentaire" receiveemail: "Recevoir un e-mail :" receivepushover: "Recevoir une alerte Pushover :" - requirepushover: "Requière un abonnement Pushover ci-dessus" + requirepushover: "Requière un abonnement Pushover ci-dessous" commentmentionnotificationsettings: "Paramètres de notification de mention d'un commentaire" privatemessagenotificationsettings: "Paramètres de notification de message privé" submittedstorycommentsettings: "Paramètres de commentaires relatifs à vos infos" diff --git a/config/routes.rb b/config/routes.rb index c29a765..6135757 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -105,8 +105,6 @@ Lobsters::Application.routes.draw do get "/settings" => "settings#index" post "/settings" => "settings#update" - post "/settings/pushover" => "settings#pushover" - get "/settings/pushover_callback" => "settings#pushover_callback" post "/settings/delete_account" => "settings#delete_account", :as => "delete_account" get "/settings/2fa" => "settings#twofa", :as => "twofa" @@ -118,6 +116,12 @@ Lobsters::Application.routes.draw do post "/settings/2fa_update" => "settings#twofa_update", :as => "twofa_update" + post "/settings/pushover_auth" => "settings#pushover_auth" + get "/settings/pushover_callback" => "settings#pushover_callback" + get "/settings/github_auth" => "settings#github_auth" + get "/settings/github_callback" => "settings#github_callback" + post "/settings/github_disconnect" => "settings#github_disconnect" + get "/filters" => "filters#index" post "/filters" => "filters#update" diff --git a/extras/github.rb b/extras/github.rb new file mode 100644 index 0000000..5904dae --- /dev/null +++ b/extras/github.rb @@ -0,0 +1,40 @@ +class Github + cattr_accessor :CLIENT_ID, :CLIENT_SECRET + + # these need to be overridden in config/initializers/production.rb + @@CLIENT_ID = nil + @@CLIENT_SECRET = nil + + def self.enabled? + self.CLIENT_ID.present? + end + + def self.oauth_consumer + OAuth::Consumer.new(self.CLIENT_ID, self.CLIENT_SECRET, + { :site => "https://api.github.com" }) + end + + def self.token_and_user_from_code(code) + s = Sponge.new + res = s.fetch("https://github.com/login/oauth/access_token", :post, + { :client_id => self.CLIENT_ID, :client_secret => self.CLIENT_SECRET, + :code => code }) + ps = CGI.parse(res) + tok = ps["access_token"].first + + if tok.present? + res = s.fetch("https://api.github.com/user?access_token=#{tok}") + js = JSON.parse(res) + if js && js["login"].present? + return [ tok, js["login"] ] + end + end + + return [ nil, nil ] + end + + def self.oauth_auth_url(state) + "https://github.com/login/oauth/authorize?client_id=#{self.CLIENT_ID}&" << + "state=#{state}" + end +end diff --git a/extras/pushover.rb b/extras/pushover.rb index ad1cb5d..163f325 100644 --- a/extras/pushover.rb +++ b/extras/pushover.rb @@ -3,8 +3,12 @@ class Pushover cattr_accessor :API_TOKEN cattr_accessor :SUBSCRIPTION_CODE + def self.enabled? + self.API_TOKEN.present? + end + def self.push(user, params) - if !@@API_TOKEN + if !self.enabled? return end @@ -15,7 +19,7 @@ class Pushover s = Sponge.new s.fetch("https://api.pushover.net/1/messages.json", :post, { - :token => @@API_TOKEN, + :token => self.API_TOKEN, :user => user, }.merge(params)) rescue => e @@ -24,7 +28,7 @@ class Pushover end def self.subscription_url(params) - u = "https://pushover.net/subscribe/#{@@SUBSCRIPTION_CODE}" + u = "https://pushover.net/subscribe/#{self.SUBSCRIPTION_CODE}" u << "?success=#{CGI.escape(params[:success])}" u << "&failure=#{CGI.escape(params[:failure])}" u