Yesteday, I found out an interesting gem warden-github-rails reported by Ruby5. It allows developers using Github organization and team to manage routing permissions.
It is very useful. So I immediately implemented in our internaal applications. Here is Howto
Installation
https://github.com/fphilipe/warden-github-rails/
put in Gemfile
gem "warden-github-rails"
Register a application on Github
fill setting to config/config.yml ( using SettingsLogic )
github_client_id: ""
github_client_secret: ""
github_organization: "rocodev"
github_team:
name: "your_team_name"
id: "team_interger_id" # github using interger id as team
Register an OAuth Application
get client_id
/ client_secret
from Github Applicatio Registration
Config warden-github-rails
create a new file config/initializers/warden_github_rails.rb
Warden::GitHub::Rails.setup do |config|
config.add_scope :admin, :client_id => Setting.github_client_id,
:client_secret => Setting.github_client_secret ,
:scope => 'user'
config.default_scope = :admin
config.add_team Setting.github_team.name, Setting.github_team.id
end
Set Rails routing
config/routes.rb
github_authenticate(:org => Setting.github_organization, :team => Setting.github_team.name ) do
namespace :admin do
root :to => "base#index"
match '/logout' , :to => "base#logout", :as => :logout
end
end
Admin::BaseController
app/controllers/admin/base_controller.rb
class Admin::BaseController < ApplicationController
def index
@is_admin = github_authenticated?(:admin)
sign_user_from_github(github_user)
end
def logout
github_logout
sign_out current_user
redirect_to root_path
end
protected
def sign_user_from_github(github_user)
user = User.find_or_create_from_github(github_user)
sign_in user
end
end
add find_or_create_from_github in User model
class User < ActiveRecord::Base
def self.find_or_create_from_github(github_user)
user = User.where(:email => github_user.email).first
if !user
user = User.new
user.email = github_user.email
user.name = github_user.name
user.password = Devise.friendly_token[0,20]
user.save!
end
return user
end
end
Enjoy!
You can find more API in
Comments
comments powered by Disqus