Merge branch 'develop' of https://git.trustie.net/jacknudt/trustieforge into develop

This commit is contained in:
huang 2016-05-06 10:17:18 +08:00
commit 6e7d20c410
9 changed files with 242 additions and 11 deletions

View File

@ -45,11 +45,11 @@ gem 'elasticsearch-rails'
### profile ### profile
#gem 'oneapm_rpm' #gem 'oneapm_rpm'
#gem "query_reviewer", group: :development
group :development do group :development do
gem 'grape-swagger' gem 'grape-swagger'
gem 'better_errors', '~> 1.1.0' gem 'better_errors', '~> 1.1.0'
# gem "query_reviewer"
# gem 'rack-mini-profiler', '~> 0.9.3' # gem 'rack-mini-profiler', '~> 0.9.3'
if RUBY_PLATFORM =~ /w32/ if RUBY_PLATFORM =~ /w32/
gem 'win32console' gem 'win32console'

View File

@ -0,0 +1,71 @@
#coding=utf-8
require 'base64'
require 'json'
require 'openssl'
## 单点登录 <=> 北斗
class SsosController < ApplicationController
skip_before_filter :check_if_login_required
layout false
def show
begin
# suRh2nFEJd0Ai_TFbqZ-1yQXnGfIB-YD_f4KTA3O4dQGSBMiXfOMt-0mzizgXekWTjHKfn62nJ60iHM3_eY_KS0Qn8SF8vANfa46GhzZRt4T0iC5ZOSs4cWeK43OU0RoekQLZZAo5OyOVibxabmiPGzEFCnVVtdmRk9d7X_B0Is=
@auth = params[:auth]
@options = parse(params[:auth])
if params[:login].present?
@options["name"] = params[:login]
end
logger.debug @options
## 认证
sso = login(@options)
## 加入组织
@organization = Organization.find(82)
unless @organization.org_members.exists?(user_id: sso.user_id)
member = OrgMember.new(:user_id => sso.user_id)
@organization.org_members << member
end
## 选择性跳转
redirect_to @organization
rescue => e
logger.error e
if e.message == "exist user"
render 'ssos/show', :layout => false
else
raise e
end
end
end
## 改用户名
def create
show and return
end
private
def base64_safe(content)
content = content.gsub('-', '+')
content.gsub('_', '/')
end
def parse(auth)
crypted_str = Base64.decode64(base64_safe(auth))
pkey = OpenSSL::PKey::RSA.new(File.new(File.join(Rails.root,"config/private.key")))
content = pkey.private_decrypt(crypted_str,OpenSSL::PKey::RSA::PKCS1_PADDING)
# content = pkey.private_decrypt(crypted_str)
ActiveSupport::JSON.decode(content)
end
def login(opt)
sso = Sso.sync_user(opt)
start_user_session(sso.user)
sso
end
end

56
app/models/sso.rb Normal file
View File

@ -0,0 +1,56 @@
class Sso < ActiveRecord::Base
belongs_to :user
attr_accessible :email, :name, :openid, :password, :school, :sex, :user, :user_id
validates :user_id, :user, :email, :openid, :presence => true
def self.sync_user(opt)
sso = Sso.where(openid: opt["openid"]).first
return sso if sso
sso = Sso.new
sso.name = opt["name"]
sso.openid = opt["openid"]
sso.email = opt["email"]
sso.password = opt["password"]
sso.school = opt["school"]
sso.sex = opt["sex"]
# 查邮箱
user = User.where(mail: opt["email"]).first
unless user
# 查用户名
user = User.where(login: opt["name"]).first
if user
# 跳到修改用户名
raise "exist user"
end
password = opt["password"]
if password.size < 8
password = random_pwd
end
us = UsersService.new
user = us.register(login: opt["name"], mail: opt["email"],
password: password,
:should_confirmation_password => false)
if user.new_record?
raise user.errors.full_messages.first
end
end
sso.user = user
sso.save!
return sso
end
private
def self.random_pwd
('a'..'z').to_a.shuffle[0..7].join
end
end

View File

@ -181,6 +181,8 @@ class User < Principal
has_one :user_wechat has_one :user_wechat
has_one :sso
# add by zjc # add by zjc
has_one :level, :class_name => 'UserLevels', :dependent => :destroy has_one :level, :class_name => 'UserLevels', :dependent => :destroy
has_many :memos , :foreign_key => 'author_id' has_many :memos , :foreign_key => 'author_id'

View File

@ -0,0 +1,64 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>跳转页面</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{ margin:0; padding:0;}
body,table,input,textarea,select,button { font-family: "微软雅黑","宋体"; font-size:12px;line-height:1.5; background:#eaebec;}
div,img,tr,td,table{ border:0;}
table,tr,td{border:0;cellspacing:0; cellpadding:0;}
ol,ul,li{ list-style-type:none}
a:link,a:visited{color:#7f7f7f;text-decoration:none;}
a:hover,a:active{color:#000;}
/*跳转页面*/
.goto-cont{ width:1000px; margin: 10px auto; padding:100px 0 500px; line-height:1.9; background:#fff;color:#636363;}
.goto-cont h2{ text-align:center; font-weight:normal; font-size:20px; margin-bottom:15px; color:#636363; }
.goto-table{ width:382px; margin:0 auto; }
.goto-table tr td{ line-height:40px; background-color:#fff; }
.goto-tableft{ width:80px; text-align:right; font-size:16px;}
.goto-input{ border: 1px solid #c2c2c2; padding: 2px 5px;vertical-align: middle;line-height: 35px;height: 35px; background-color:#fff; width:300px; font-size:16px;}
.goto-submit{ width:312px; height:40px; margin:0 auto;line-height:40px; background-color:#269ac9; color:#fff; text-align:center; border:none; margin-top:20px; font-size:16px; }
.goto-submit:hover{background-color: #1f82aa;}
.goto-red{ color:#D71215;}
</style>
</head>
<body>
<div class="" style="height:54px; background:#269ac9;"></div>
<%= form_tag('/sso') do -%>
<div class="goto-cont">
<h2>请修改您的资料以确保Trustie为您提供更便捷的服务</h2>
<table cellSpacing="0" cellPadding="0" class="goto-table">
<tr>
<td class="goto-tableft"> 用户名:</td>
<td >
<input type="text" class="goto-input" value="<%= @options["name"] %>" name="login"/>
</td>
</tr>
<tr>
<td></td>
<td class="goto-red">已存在相同用户名,请选择更合适的用户名</td>
</tr>
<tr>
<td class="goto-tableft" > 邮箱:</td>
<td >
<input type="email" class="goto-input" value="<%= @options["email"] %>" disabled="disabled" />
</td>
</tr>
<tr>
<td></td>
<td ><input type="submit" value="确定" class="goto-submit"/></td>
</tr>
</table>
</div>
<%= hidden_field_tag 'auth', @auth %>
<% end -%>
</body>
</html>

15
config/private.key Normal file
View File

@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC3//sR2tXw0wrC2DySx8vNGlqt3Y7ldU9+LBLI6e1KS5lfc5jl
TGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2klBd6h4wrbbHA2XE1sq21ykja/
Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o2n1vP1D+tD3amHsK7QIDAQAB
AoGBAKH14bMitESqD4PYwODWmy7rrrvyFPEnJJTECLjvKB7IkrVxVDkp1XiJnGKH
2h5syHQ5qslPSGYJ1M/XkDnGINwaLVHVD3BoKKgKg1bZn7ao5pXT+herqxaVwWs6
ga63yVSIC8jcODxiuvxJnUMQRLaqoF6aUb/2VWc2T5MDmxLhAkEA3pwGpvXgLiWL
3h7QLYZLrLrbFRuRN4CYl4UYaAKokkAvZly04Glle8ycgOc2DzL4eiL4l/+x/gaq
deJU/cHLRQJBANOZY0mEoVkwhU4bScSdnfM6usQowYBEwHYYh/OTv1a3SqcCE1f+
qbAclCqeNiHajCcDmgYJ53LfIgyv0wCS54kCQAXaPkaHclRkQlAdqUV5IWYyJ25f
oiq+Y8SgCCs73qixrU1YpJy9yKA/meG9smsl4Oh9IOIGI+zUygh9YdSmEq0CQQC2
4G3IP2G3lNDRdZIm5NZ7PfnmyRabxk/UgVUWdk47IwTZHFkdhxKfC8QepUhBsAHL
QjifGXY4eJKUBm3FpDGJAkAFwUxYssiJjvrHwnHFbg0rFkvvY63OSmnRxiL4X6EY
yI9lblCsyfpl25l7l5zmJrAHn45zAiOoBrWqpM5edu7c
-----END RSA PRIVATE KEY-----

View File

@ -1223,6 +1223,8 @@ RedmineApp::Application.routes.draw do
end end
end end
resource :sso, only: [:show, :create]
get '/:sub_dir_name', :to => 'org_subfields#show', :as => 'show_subfield_without_id' get '/:sub_dir_name', :to => 'org_subfields#show', :as => 'show_subfield_without_id'
Dir.glob File.expand_path("plugins/*", Rails.root) do |plugin_dir| Dir.glob File.expand_path("plugins/*", Rails.root) do |plugin_dir|

View File

@ -0,0 +1,16 @@
class CreateSsos < ActiveRecord::Migration
def change
create_table :ssos do |t|
t.references :user
t.string :openid
t.string :name
t.string :password
t.string :email
t.integer :sex
t.string :school
t.timestamps
end
add_index :ssos, :user_id
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20160429030819) do ActiveRecord::Schema.define(:version => 20160504060751) do
create_table "activities", :force => true do |t| create_table "activities", :force => true do |t|
t.integer "act_id", :null => false t.integer "act_id", :null => false
@ -1673,15 +1673,6 @@ ActiveRecord::Schema.define(:version => 20160429030819) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end end
create_table "shield_wechats", :force => true do |t|
t.string "container_type"
t.integer "container_id"
t.string "shield_type"
t.integer "shield_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "softapplications", :force => true do |t| create_table "softapplications", :force => true do |t|
t.string "name" t.string "name"
t.text "description" t.text "description"
@ -1700,6 +1691,20 @@ ActiveRecord::Schema.define(:version => 20160429030819) do
t.integer "project_id" t.integer "project_id"
end end
create_table "ssos", :force => true do |t|
t.integer "user_id"
t.string "openid"
t.string "name"
t.string "password"
t.string "email"
t.integer "sex"
t.string "school"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "ssos", ["user_id"], :name => "index_ssos_on_user_id"
create_table "student_work_projects", :force => true do |t| create_table "student_work_projects", :force => true do |t|
t.integer "homework_common_id" t.integer "homework_common_id"
t.integer "student_work_id" t.integer "student_work_id"