mirror of
https://github.com/fspc/BikeShed-1.git
synced 2026-09-16 16:31:38 -04:00
Added User password_reset spec and some refactor
*Make returned error in errors array *Use constants
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
require 'securerandom'
|
||||
class Api::V1::UsersController < Api::V1::BaseController
|
||||
CANNOT_MANAGE = "You do not have the permission to manager users"
|
||||
NOT_FOUND = "User not found"
|
||||
NOT_ALLOWED = "Not allowed to reset your own password in this fashion"
|
||||
PASS_LENGTH = 8
|
||||
|
||||
def password_reset
|
||||
if can? :manage, User
|
||||
user = User.find_by_id(params[:user_id])
|
||||
render :json => { "error" => "User not found"}, :status => 404 and return if user.nil?
|
||||
render :json => { "error" => "Not allowed to reset your own password in this fashion."}, :status => 403 and return if user.id == current_user.id
|
||||
render :json => { "errors" => [NOT_FOUND]}, :status => 404 and return if user.nil?
|
||||
render :json => { "errors" => [NOT_ALLOWED]}, :status => 403 and return if user.id == current_user.id
|
||||
|
||||
new_pass = SecureRandom.hex[0,8]
|
||||
new_pass = SecureRandom.hex[0,PASS_LENGTH]
|
||||
user.password = new_pass
|
||||
user.save
|
||||
render :json => { "password" => new_pass}, :status => 200 and return
|
||||
else
|
||||
render :json => { "error" => "You do not have the permission"}, :status => 403 and return
|
||||
render :json => { "errors" => [CANNOT_MANAGE]}, :status => 403 and return
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user