diff --git a/README.md b/README.md index e2abcdc..fa43093 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,10 @@ tw-libr Build Status ==== -[![Build Status](https://travis-ci.org/wahyd4/Libr.png?branch=master)](https://travis-ci.org/wahyd4/Libr) \ No newline at end of file +[![Build Status](https://travis-ci.org/wahyd4/Libr.png?branch=master)](https://travis-ci.org/wahyd4/Libr) + +#### Export System variable + + export CLIENT_ID='0858f96ac849895e190aec4058dc9c1a' + export CLIENT_SECRET='0c363bc78dbacb3f' + export REDIRECT_URI='http://localhost:3000/douban_callback' \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e8065d9..da0be49 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,24 @@ class ApplicationController < ActionController::Base protect_from_forgery + before_filter :current_user + + def current_user + @current_user = nil + + if session[:name] + @current_user = User.find_by_name session[:name] + end + + end + + def sign_in(name,avatar) + if session[:name] == name + return + end + unless User.find_by_name name + User.create_user(name,avatar) + end + session[:name] = name + end + end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 7b6213f..6d5bf4e 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,6 +1,8 @@ require "douban_auth" + class HomeController < ApplicationController include Douban_Auth + before_filter :current_user def index @books = Book.order 'id DESC' @@ -8,7 +10,10 @@ class HomeController < ApplicationController def douban_callback - access_token params[:code] + token = access_token params[:code] + user_info = fetch_user_info token + sign_in(user_info['name'],user_info['avatar']) redirect_to '/' end + end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index cb95c43..1809d04 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -7,11 +7,14 @@ class UserController < ApplicationController render :layout => false end - def login_douban - + def auth_douban url = auth_url.to_s - - puts "xxxxxxxxxxxx"+url redirect_to url end + + def logout + session[:name] = nil + redirect_to '/' + end + end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb index 23de56a..ce0ffe8 100644 --- a/app/helpers/home_helper.rb +++ b/app/helpers/home_helper.rb @@ -1,2 +1,4 @@ module HomeHelper + + end diff --git a/app/helpers/user_helper.rb b/app/helpers/user_helper.rb index b49ba5f..a0aafbf 100644 --- a/app/helpers/user_helper.rb +++ b/app/helpers/user_helper.rb @@ -1,5 +1,4 @@ module UserHelper - end diff --git a/app/models/user.rb b/app/models/user.rb index 4b78153..3f7b86f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,12 @@ class User < ActiveRecord::Base - attr_accessible :email, :name, :books + attr_accessible :email, :name, :books, :avatar + + validates_uniqueness_of :name + has_many :user_to_books has_many :books, through: :user_to_books + + def self.create_user(name,avatar) + User.create name: name, avatar: avatar + end end diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 594d83e..c8b6a79 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -20,8 +20,16 @@ html li a href="" 书架 ul.nav.pull-right - li - a href="/login" 登录 + -if @current_user != nil + li + a href="#" + img.img-circle style="max-width:82%;margin:-5px" src="#{@current_user.avatar}" title="#{@current_user.name}" + li + a href="/logout" 注销 + - else + li + a href="/login" 登录 + div.container.main = yield diff --git a/config/routes.rb b/config/routes.rb index 6cbd7e4..ae56da8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,8 @@ Libr::Application.routes.draw do match 'books/:id' => 'book#view' get '/search/book/:arg' =>'book#search' get '/login' => 'user#login' - get '/login/douban' => 'user#login_douban' + get '/logout' => 'user#logout' + get '/login/douban' => 'user#auth_douban' get '/douban_callback' =>'home#douban_callback' # The priority is based upon order of creation: diff --git a/db/migrate/20130109051810_add_avatar_to_user.rb b/db/migrate/20130109051810_add_avatar_to_user.rb new file mode 100644 index 0000000..9afc017 --- /dev/null +++ b/db/migrate/20130109051810_add_avatar_to_user.rb @@ -0,0 +1,5 @@ +class AddAvatarToUser < ActiveRecord::Migration + def change + add_column :users, :avatar, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index e85ea35..fc485ee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130107052141) do +ActiveRecord::Schema.define(:version => 20130109051810) do create_table "books", :force => true do |t| t.string "name" @@ -34,6 +34,7 @@ ActiveRecord::Schema.define(:version => 20130107052141) do t.string "name" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false + t.string "avatar" end end diff --git a/lib/douban_auth.rb b/lib/douban_auth.rb index 4a2aef9..2cf7894 100644 --- a/lib/douban_auth.rb +++ b/lib/douban_auth.rb @@ -5,12 +5,12 @@ module Douban_Auth def auth_url - client_id = '0858f96ac849895e190aec4058dc9c1a' - redirect_uri = 'http://localhost:3000/douban_callback' response_type = 'code' - scope = 'douban_basic_common' + scope = 'douban_basic_common,book_basic_r' - url = 'https://www.douban.com/service/auth2/auth?client_id=' << client_id << '&redirect_uri=' << redirect_uri << '&response_type=' << response_type << '&scope='+ scope + url = 'https://www.douban.com/service/auth2/auth?client_id=' + ::ENV['CLIENT_ID'] + + '&redirect_uri=' + ::ENV['REDIRECT_URI'] + + '&response_type=' + response_type + '&scope=' + scope end @@ -19,13 +19,26 @@ module Douban_Auth http = Net::HTTP.new('www.douban.com', 443) http.use_ssl = true path ='/service/auth2/token' - data ='client_id=0858f96ac849895e190aec4058dc9c1a&client_secret=0c363bc78dbacb3f&redirect_uri=http://localhost:3000/douban_callback&grant_type=authorization_code&code='+code + data ='client_id='+ ::ENV['CLIENT_ID']+'&client_secret='+ ::ENV['CLIENT_SECRET']+'&redirect_uri=http://localhost:3000/douban_callback'+'&grant_type=authorization_code&code='+code headers = { 'Authorization' => my_code } response = http.post(path, data, headers) response = JSON.parse response.body - puts '111111111111111111'+ response.to_s + + response['access_token'] end + + def fetch_user_info(token) + http = Net::HTTP.new('api.douban.com', 443) + http.use_ssl = true + path ='/v2/user/~me' + header = { + 'Authorization' => 'Bearer '+token + } + response = http.get(path,header) + response = JSON.parse response.body + end + end \ No newline at end of file