forked from JumpstartLab/today
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Users can now login through Github. Users which are admins can create or edit pages
- Loading branch information
Franklin Webber
committed
Sep 3, 2013
1 parent
9d6cd2e
commit 68e2adf
Showing
18 changed files
with
257 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require './lib/github' | ||
|
||
class SessionsController < ApplicationController | ||
|
||
def new | ||
redirect_to Github.login_url | ||
end | ||
|
||
def show | ||
end | ||
|
||
def destroy | ||
logout | ||
flash[:notice] = "You have been logged out." | ||
redirect_to root_path | ||
end | ||
|
||
def callback | ||
unless params[:code] | ||
flash[:error] = "We didn't receive any authentication code from GitHub." | ||
end | ||
|
||
begin | ||
user = Authentication.perform(params[:code]) | ||
login(user) | ||
rescue => e | ||
flash[:error] = "We're having trouble with logins right now. Please come back later." | ||
end | ||
|
||
if current_user.guest? | ||
flash[:error] = "We're having trouble with logins right now. Please come back later." | ||
end | ||
|
||
if current_user.admin? | ||
redirect_to root_path | ||
else | ||
redirect_to root_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Authentication | ||
def self.perform(code) | ||
User.from_github Github.authenticate(code) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Guest | ||
def guest? | ||
true | ||
end | ||
|
||
def admin? | ||
false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class User < ActiveRecord::Base | ||
has_one :application, inverse_of: :user | ||
|
||
def self.from_github(data) | ||
user = User.where(github_id: data['id']).first | ||
user_data = { | ||
name: data['name'], | ||
email: data['email'], | ||
location: data['location'], | ||
github_id: data['id'], | ||
avatar_url: data['avatar_url'], | ||
gravatar_id: data['gravatar_id'] | ||
} | ||
user ||= User.new(user_data) | ||
user.username = data['login'] # always update the GitHub username | ||
user.save | ||
user | ||
end | ||
|
||
def guest? | ||
false | ||
end | ||
|
||
def admin? | ||
is_admin | ||
end | ||
|
||
def applying? | ||
application && application.completed?('bio') | ||
end | ||
|
||
def apply | ||
build_application unless application | ||
application | ||
end | ||
|
||
def apply! | ||
create_application unless application | ||
application | ||
end | ||
|
||
def admin! | ||
self.is_admin = true | ||
save | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Please Login</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
Today::Application.routes.draw do | ||
|
||
resources :outlines | ||
|
||
root to: "schedule#show" | ||
get '/please-login' => 'sessions#show', as: :please_login | ||
get '/login' => 'sessions#new', as: :login | ||
get '/github/callback' => 'sessions#callback', as: :github_callback | ||
delete '/logout' => 'sessions#destroy', as: :logout | ||
|
||
get "/:date_string", to: "schedule#show", as: "schedule" | ||
|
||
root to: "schedule#show" | ||
resources :outlines | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class CreateUsers < ActiveRecord::Migration | ||
def change | ||
create_table 'users' do |t| | ||
t.string 'name' | ||
t.string 'email' | ||
t.string 'location' | ||
t.string 'username' | ||
t.string 'github_id' | ||
t.string 'avatar_url' | ||
t.string 'gravatar_id' | ||
t.boolean 'is_admin', default: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class Github | ||
def self.login_url | ||
"https://github.com/login/oauth/authorize?client_id=#{ENV.fetch('GITHUB_CLIENT_ID')}" | ||
end | ||
|
||
def self.authenticate(code) | ||
conn = Faraday.new(:url => 'https://github.com') do |c| | ||
c.use Faraday::Response::Logger | ||
c.use Faraday::Adapter::NetHttp | ||
end | ||
|
||
options = { | ||
client_id: ENV.fetch('GITHUB_CLIENT_ID'), | ||
client_secret: ENV.fetch('GITHUB_CLIENT_SECRET'), | ||
code: code | ||
} | ||
|
||
options = options.map {|k,v| "#{k}=#{v}"}.join('&') | ||
|
||
response = conn.post do |req| | ||
req.url '/login/oauth/access_token' | ||
req.headers['Accept'] = 'application/json' | ||
req.headers['User-Agent'] = user_agent | ||
req.body = options | ||
end | ||
access_token = JSON.parse(response.body)['access_token'] | ||
|
||
conn = Faraday.new(:url => 'https://api.github.com') do |c| | ||
c.use Faraday::Response::Logger | ||
c.use Faraday::Adapter::NetHttp | ||
end | ||
|
||
response = conn.get do |req| | ||
req.url '/user' | ||
req.headers['User-Agent'] = user_agent | ||
req.params['access_token'] = access_token | ||
end | ||
JSON.parse(response.body) | ||
end | ||
|
||
def self.user_agent | ||
'github.com:JumpstartLab/enrollist' | ||
end | ||
end |