将注册功能移动到UserService中
This commit is contained in:
parent
53ecf8718f
commit
c303d2b4ca
|
@ -9,6 +9,7 @@ module Mobile
|
|||
class API < Grape::API
|
||||
version 'v1', using: :path
|
||||
format :json
|
||||
content_type :json, "application/json;charset=UTF-8"
|
||||
|
||||
helpers do
|
||||
def logger
|
||||
|
|
|
@ -11,9 +11,10 @@ module Mobile
|
|||
if user
|
||||
::ApiKey.delete_all(user_id: user.id)
|
||||
key = ::ApiKey.create!(user_id: user.id)
|
||||
{token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json)
|
||||
data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json)
|
||||
{status: 0, data: data}
|
||||
else
|
||||
error!('Unauthorized.', 401)
|
||||
{status: 1, message: 'Unauthorized.'}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -20,6 +20,7 @@ module Mobile
|
|||
@courses = @courses_all.order("created_at desc")
|
||||
@s_type = 0
|
||||
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
|
||||
{status: 0, data: @courses}
|
||||
# @course_activity_count=get_course_activity @courses, @course_activity_count
|
||||
end
|
||||
|
||||
|
@ -29,7 +30,8 @@ module Mobile
|
|||
end
|
||||
route_param :id do
|
||||
get do
|
||||
Course.find(params[:id])
|
||||
course = Course.find(params[:id])
|
||||
{status: 0, data: course}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -14,12 +14,12 @@ module Mobile
|
|||
post do
|
||||
user = User.new
|
||||
user.login = params[:login]
|
||||
user.mail = params[:email]
|
||||
user.mail = params[:mail]
|
||||
user.password = params[:password]
|
||||
user.password_confirmation = params[:password]
|
||||
user.activate
|
||||
if user.save!
|
||||
UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0)
|
||||
UserStatus.create(:user_id => user.id, :changsets_course => 0, :watchers_count => 0)
|
||||
end
|
||||
user
|
||||
end
|
||||
|
|
|
@ -192,6 +192,11 @@ class AccountController < ApplicationController
|
|||
redirect_to signin_url
|
||||
end
|
||||
|
||||
def api_register login,password,email
|
||||
users_service = UsersService.new
|
||||
users_service.register({login: login, password: password, email: eamil})
|
||||
end
|
||||
|
||||
def valid_ajax
|
||||
req = Hash.new(false)
|
||||
req[:message] = ''
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
class UsersService
|
||||
|
||||
#将用户注册的功能函数写这里
|
||||
#参数约定
|
||||
#成功返回注册后的User实例,失败直接抛异常
|
||||
def register(params)
|
||||
@user = User.new
|
||||
@user.admin = false
|
||||
@user.register
|
||||
@user.login = params[:login]
|
||||
@user.mail = params[:email]
|
||||
unless password.blank?
|
||||
@user.password = params[:password]
|
||||
end
|
||||
case Setting.self_registration
|
||||
when '1'
|
||||
register_by_email_activation(@user)
|
||||
when '3'
|
||||
register_automatically(@user)
|
||||
else
|
||||
register_manully_by_administrator(@user)
|
||||
end
|
||||
if @user.id != nil
|
||||
ue = @user.user_extensions ||= UserExtensions.new
|
||||
ue.user_id = @user.id
|
||||
ue.save
|
||||
end
|
||||
@user
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,24 @@
|
|||
require File.expand_path('../../test_helper.rb',__FILE__)
|
||||
|
||||
class AuthTest < ActionDispatch::IntegrationTest
|
||||
def setup
|
||||
ApiKey.delete_all
|
||||
end
|
||||
|
||||
test "login success when use correct password" do
|
||||
post('/api/v1/auth/login.json', {login: 'guange', password: '123456'})
|
||||
o = ActiveSupport::JSON.decode response.body
|
||||
puts o
|
||||
puts o["token"]
|
||||
assert_not_nil o["token"]
|
||||
end
|
||||
|
||||
test "login failure when incorrect password" do
|
||||
post('/api/v1/auth/login.json', {login: 'guange', password: 'wrongpass'})
|
||||
o = ActiveSupport::JSON.decode response.body
|
||||
assert_nil o["token"]
|
||||
assert_equal o["error"], 'Unauthorized.'
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
require File.expand_path('../../test_helper.rb',__FILE__)
|
||||
|
||||
class UsersTest < ActionDispatch::IntegrationTest
|
||||
test "register a user" do
|
||||
end
|
||||
test "get all users" do
|
||||
get '/api/v1/users.json'
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue