2012-04-10 14:06:46 +00:00
|
|
|
class UsersController < ApplicationController
|
2012-04-23 06:55:16 +00:00
|
|
|
before_filter :authentication_check, :except => [:create, :password_reset_send, :password_reset_verify]
|
2012-04-10 14:06:46 +00:00
|
|
|
|
|
|
|
# GET /users
|
|
|
|
def index
|
|
|
|
@users = User.all
|
2012-04-20 15:39:50 +00:00
|
|
|
@users_all = []
|
|
|
|
@users.each {|user|
|
|
|
|
@users_all.push user_data_full( user.id )
|
2012-04-10 14:06:46 +00:00
|
|
|
}
|
2012-04-20 15:39:50 +00:00
|
|
|
render :json => @users_all
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /users/1
|
|
|
|
def show
|
2012-04-20 15:39:50 +00:00
|
|
|
@user = user_data_full( params[:id] )
|
2012-04-12 11:27:01 +00:00
|
|
|
render :json => @user
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# POST /users
|
|
|
|
def create
|
|
|
|
@user = User.new(params[:user])
|
|
|
|
@user.created_by_id = (current_user && current_user.id) || 1
|
2012-04-12 11:27:01 +00:00
|
|
|
if @user.save
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
# if it's a signup, add user to customer role
|
|
|
|
if @user.created_by_id == 1
|
|
|
|
|
|
|
|
# check if it's first user
|
2012-04-13 13:51:10 +00:00
|
|
|
count = User.all.count()
|
|
|
|
group_ids = []
|
|
|
|
role_ids = []
|
|
|
|
|
|
|
|
# add first user as admin/agent and to all groups
|
2012-04-12 11:27:01 +00:00
|
|
|
if count <= 2
|
2012-04-13 13:51:10 +00:00
|
|
|
Role.where( :name => [ 'Admin', 'Agent'] ).each { |role|
|
|
|
|
role_ids.push role.id
|
|
|
|
}
|
|
|
|
Group.all().each { |group|
|
|
|
|
group_ids.push group.id
|
|
|
|
}
|
|
|
|
|
|
|
|
# everybody else will go as customer per default
|
2012-04-10 14:06:46 +00:00
|
|
|
else
|
2012-04-12 11:27:01 +00:00
|
|
|
role_ids.push Role.where( :name => 'Customer' ).first.id
|
|
|
|
end
|
2012-04-13 13:51:10 +00:00
|
|
|
@user.role_ids = role_ids
|
|
|
|
@user.group_ids = group_ids
|
2012-04-12 11:27:01 +00:00
|
|
|
|
|
|
|
# else do assignment as defined
|
|
|
|
else
|
|
|
|
if params[:role_ids]
|
|
|
|
@user.role_ids = params[:role_ids]
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-04-12 11:27:01 +00:00
|
|
|
if params[:group_ids]
|
|
|
|
@user.group_ids = params[:group_ids]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# send inviteation if needed
|
|
|
|
if params[:invite]
|
2012-04-10 14:06:46 +00:00
|
|
|
|
|
|
|
# logger.debug('IIIIIIIIIIIIIIIIIIIIIIIIIIIIII')
|
|
|
|
# exit '123'
|
|
|
|
end
|
2012-04-12 11:27:01 +00:00
|
|
|
render :json => @user, :status => :created
|
|
|
|
else
|
|
|
|
render :json => @user.errors, :status => :unprocessable_entity
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /users/1
|
|
|
|
def update
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
if @user.update_attributes(params[:user])
|
|
|
|
if params[:role_ids]
|
|
|
|
@user.role_ids = params[:role_ids]
|
|
|
|
end
|
|
|
|
if params[:group_ids]
|
|
|
|
@user.group_ids = params[:group_ids]
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-04-20 15:39:50 +00:00
|
|
|
if params[:organization_ids]
|
|
|
|
@user.organization_ids = params[:organization_ids]
|
|
|
|
end
|
|
|
|
|
|
|
|
@user = user_data_full( params[:id] )
|
2012-04-12 11:27:01 +00:00
|
|
|
render :json => @user, :status => :ok
|
|
|
|
else
|
|
|
|
render :json => @user.errors, :status => :unprocessable_entity
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /users/1
|
|
|
|
def destroy
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
@user.destroy
|
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
head :ok
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-04-23 06:55:16 +00:00
|
|
|
|
|
|
|
# POST /users/reset_password
|
|
|
|
def password_reset_send
|
|
|
|
success = User.password_reset_send( params[:username] )
|
|
|
|
if success
|
|
|
|
render :json => { :message => 'ok' }, :status => :ok
|
|
|
|
else
|
|
|
|
render :json => { :message => 'failed' }, :status => :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# get /users/verify_password/:hash
|
|
|
|
def password_reset_verify
|
2012-04-23 16:59:35 +00:00
|
|
|
if params[:password]
|
|
|
|
success = User.password_reset_via_token( params[:token], params[:password] )
|
|
|
|
else
|
|
|
|
success = User.password_reset_check( params[:token] )
|
|
|
|
end
|
2012-04-23 06:55:16 +00:00
|
|
|
if success
|
|
|
|
render :json => { :message => 'ok' }, :status => :ok
|
|
|
|
else
|
|
|
|
render :json => { :message => 'failed' }, :status => :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|