Skip to content

Commit

Permalink
Merge pull request #12 from rails-decal/mark/sign-in-with-github
Browse files Browse the repository at this point in the history
Sign in with GitHub, admin dashboard, users
  • Loading branch information
negativetwelve committed Apr 11, 2014
2 parents 642679b + 659ddef commit 0211e00
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 18 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ gem 'google-analytics-rails', '~> 0.0.5'
gem 'devise', '~> 3.2.3'
gem 'newrelic_rpm'
gem 'simple_form', '~> 3.0.2'
gem 'omniauth-github', :git => 'git://github.com/intridea/omniauth-github.git'

# For Lectures
gem "rails-reveal-js", "~> 2.6.1.2"
Expand Down
30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
GIT
remote: git://github.com/intridea/omniauth-github.git
revision: 21fa5e1a7295a11eae42846690b1eee88e57c23a
specs:
omniauth-github (1.1.2)
omniauth (~> 1.0)
omniauth-oauth2 (~> 1.1)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -68,7 +76,10 @@ GEM
execjs (2.0.2)
factory_girl (4.4.0)
activesupport (>= 3.0.0)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
google-analytics-rails (0.0.5)
hashie (2.1.0)
hike (1.2.3)
i18n (0.6.9)
jbuilder (1.5.3)
Expand All @@ -78,13 +89,31 @@ GEM
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
jwt (0.1.11)
multi_json (>= 1.5)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
minitest (4.7.5)
multi_json (1.9.2)
multi_xml (0.5.5)
multipart-post (2.0.0)
newrelic_rpm (3.7.3.204)
oauth2 (0.9.3)
faraday (>= 0.8, < 0.10)
jwt (~> 0.1.8)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
omniauth (1.2.1)
hashie (>= 1.2, < 3)
rack (~> 1.0)
omniauth-oauth2 (1.1.2)
faraday (>= 0.8, < 0.10)
multi_json (~> 1.3)
oauth2 (~> 0.9.3)
omniauth (~> 1.2)
orm_adapter (0.5.0)
pg (0.17.1)
polyglot (0.3.4)
Expand Down Expand Up @@ -202,6 +231,7 @@ DEPENDENCIES
jbuilder (~> 1.2)
jquery-rails (~> 2.2.1)
newrelic_rpm
omniauth-github!
pg (= 0.17.1)
rails (= 4.0.0)
rails-rename (~> 1.0.0)
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Admin::BaseController < ApplicationController
before_action :admin_user

private

def admin_user
redirect_to(root_url) unless current_user && current_user.is_staff?
end

end
6 changes: 6 additions & 0 deletions app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Admin::PagesController < Admin::BaseController

def dashboard
end

end
28 changes: 28 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class SessionsController < ApplicationController

def new
end

def create_github
user = User.from_omniauth(env['omniauth.auth'])
session[:user_id] = user.id
redirect_to root_url, notice: "Signed in."
end

def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "Logged in!"
else
flash.now.alert = "Email or password is invalid."
redirect_to new_session_path, notice: 'Email or password is invalid'
end
end

def destroy
session[:user_id] = nil
redirect_to root_url, notice: "Logged out!"
end

end
14 changes: 14 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

def github
@user = User.find_for_github_oauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "GitHub") if is_navigational_format?
else
session["devise.github_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end

end
13 changes: 13 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class UsersController < ApplicationController
before_filter :user, only: [:show]

def show
end

private

def user
@user = User.find(params[:id])
end

end
7 changes: 7 additions & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ class Role < ActiveRecord::Base
belongs_to :semester
belongs_to :user
belongs_to :position

before_create :set_name

def set_name
self.name = self.position.name
end

end
28 changes: 26 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,40 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, omniauth_providers: [:github]

has_many :roles

def current_role
self.roles.where(semester: Semester.current)
self.roles.where(semester: Semester.current).first
end

def is_staff?
self.current_role.name == "Instructor" || self.current_role.name == "TA"
end

def self.find_for_github_oauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name
user.nickname = auth.info.nickname
user.bio = auth.extra.raw_info.bio
user.blog = auth.extra.raw_info.blog
user.location = auth.extra.raw_info.location
user.image_url = auth.info.image
end
end

def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.github_data"] && session["devise.github_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end

end
4 changes: 4 additions & 0 deletions app/views/admin/pages/dashboard.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.container
.row
.col-md-12
h1 Admin Dashboard
25 changes: 11 additions & 14 deletions app/views/layouts/_header.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@ nav.navbar.navbar-default.navbar-fixed-top role="navigation"
ul.nav.navbar-nav
= active_li_link "Apply", apply_path
ul.nav.navbar-nav.navbar-right
li
= link_to "Sign In", "#", class: "flat-button"
- if signed_in?
li
a href="#" Link
li.dropdown
a.dropdown-toggle href="#" data-toggle="dropdown"
| Dropdown
' Hi,
= current_user.name
b.caret
ul.dropdown-menu
li
a href="#" Action
li
a href="#" Another Action
li
a href="#" Something else here
= link_to "Profile", current_user
li.divider
- if current_user.is_staff?
li
= link_to "Admin Dashboard", admin_dashboard_path
li.divider
li
a href="#" Separated Link
li.divider
li
a href="#" One more separated link
= link_to "Sign Out", destroy_user_session_path, method: :delete
- else
li
= link_to "Sign In with GitHub", user_omniauth_authorize_path(:github), class: "flat-button"
6 changes: 6 additions & 0 deletions app/views/users/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.container
.row
.col-md-12
h1
= @user.name
| 's profile
5 changes: 5 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']

OmniAuth.config.logger = Rails.logger
end
13 changes: 12 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
RailsDecal::Application.routes.draw do
devise_for :users
root to: "pages#home"

match "apply", to: "student_applications#new", via: :get
match "apply", to: "student_applications#create", as: "student_applications", via: :post

resources :lectures, only: [:show, :index]

devise_for :users, path: '',
path_names: { sign_in: 'login', sign_up: 'sign-up', sign_out: 'logout'},
controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }

resources :users, only: [:show]

namespace :admin do
match '/', to: redirect('/admin/dashboard'), via: :get
match 'dashboard', to: 'pages#dashboard', via: :get
end

end
6 changes: 6 additions & 0 deletions db/migrate/20140410034224_add_provider_and_uid_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddProviderAndUidToUsers < ActiveRecord::Migration
def change
add_column :users, :provider, :string
add_column :users, :uid, :string
end
end
10 changes: 10 additions & 0 deletions db/migrate/20140411024516_add_more_columns_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddMoreColumnsToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string
add_column :users, :nickname, :string
add_column :users, :image_url, :string
add_column :users, :bio, :text
add_column :users, :blog, :string
add_column :users, :location, :string
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140410033531) do
ActiveRecord::Schema.define(version: 20140411024516) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -80,6 +80,14 @@
t.datetime "updated_at"
t.string "first_name"
t.string "last_name"
t.string "provider"
t.string "uid"
t.string "name"
t.string "nickname"
t.string "image_url"
t.text "bio"
t.string "blog"
t.string "location"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
Expand Down

0 comments on commit 0211e00

Please sign in to comment.