pre_develop
daiao 6 years ago
commit c98a07c7df

@ -30,14 +30,23 @@ module Mobile
desc "创建用户"
params do
requires :mail, type: String, desc: "邮箱地址"
requires :name, type: String, desc: "昵称"
requires :password, type: String, desc: "密码"
requires :userName, type: String, desc: "好大学用户名"
optional :name, type: String, desc: "用户姓名"
end
post "create_user" do
CnmoocsService.new.create_user params
end
desc "获取资源访问地址"
params do
requires :userId, type: Integer, desc: "用户ID"
requires :resouceId, type: String, desc: "资源唯一标示"
requires :accessType, type: Integer, desc: "资源类型"
end
get "source_url" do
CnmoocsService.new.source_url(params)
end
desc "远程登录"
params do
requires :mail, type: String, desc: "邮箱地址"

@ -207,6 +207,9 @@ class ApplicationController < ActionController::Base
elsif session[:wechat_openid]
uw = UserWechat.find_by_openid(session[:wechat_openid])
user = uw.user if uw
elsif session[:third_party_user_id]
c_user = UserSource.find_by_id(session[:third_party_user_id])
user = c_user.user if c_user
end
end
if user.nil? && Setting.rest_api_enabled? && accept_api_auth?

@ -0,0 +1,8 @@
class CnmoocUser < UserSource
private
def email_prefix
'cnmooc_'
end
end

@ -269,6 +269,8 @@ class User < Principal
has_many :article_homepages, :dependent => :destroy
has_many :competition_lists, :dependent => :destroy
has_one :user_source
## end
# default_scope -> { includes(:user_extensions, :user_score) }

@ -0,0 +1,21 @@
class UserSource < ActiveRecord::Base
belongs_to :user
def generate_email
email = rand_email
while User.exists?(mail: email) do
email = rand_email
end
email
end
private
def rand_email
email_prefix + Random.rand.to_s[2..8] + '@educoder.com'
end
def email_prefix
''
end
end

@ -41,22 +41,32 @@ class CnmoocsService
end
end
def create_user params
user = User.find_by_mail params[:mail]
if user.blank?
ActiveRecord::Base.transaction do
# 如果Educoder中已存在与该OpenI用户的邮箱相同的用户则会直接跳转到登录educoder的登录页面
user = User.new(lastname: params[:name], mail: params[:mail], mail_notification: email)
user.login = generate_login('m')
user.password = params[:password]
user.certification = 1
user.save!
UserExtensions.create!(user_id: user.id, school_id: School.first.id, identity: 4, gender: 0)
end
{error: 0, messages: "创建成功"}
else
{error: -1, messages: "邮箱已经存在,请直接使用邮箱登录"}
def create_user(params)
c_user = CnmoocUser.find_by_uuid(params[:userName])
if c_user.present?
return { error: -1, messages: '用户已存在' }
end
mail = c_user.generate_email
create_params = {
lastname: params[:name],
mail: mail,
mail_notification: mail,
login: generate_login('m'),
password: OauthController::DEFAULT_PASSWORD,
certification: 1
}
ActiveRecord::Base.transaction do
user = User.create!(create_params)
UserExtensions.create!(user_id: user.id, school_id: School.first.id, identity: 4, gender: 0)
c_user.user_id = user.id
c_user.save!
end
{ error: 0, messages: "创建成功", data: { userId: user.id } }
end
def login_educoder params
@ -70,6 +80,20 @@ class CnmoocsService
end
def source_url(params)
if session[:third_party_user_id].blank?
user = User.find(params[:userId])
session[:third_party_user_id] = user.user_source.id
end
shixun = Shixun.find_by_identifier(params[:resouceId])
if shixun.blank?
return { error: -1, messages: '资源不存在' }
end
{ error: 0, messages: '成功', accessUrl: "#{Redmine::Configuration['educoder_domain']}/shixuns/#{shixun.id}" }
end
private
def shixun_data shixuns
@ -83,5 +107,4 @@ class CnmoocsService
end
{resouces: shixun_list}
end
end

@ -0,0 +1,15 @@
class CreateUserSources < ActiveRecord::Migration
def change
create_table :user_sources do |t|
t.string :type
t.integer :user_id
t.string :uuid
t.string :name
t.timestamps
end
add_index :user_sources, [:type, :uuid], unique: true
add_index :user_sources, :user_id
end
end
Loading…
Cancel
Save