Added User password_reset spec and some refactor

*Make returned error in errors array
*Use constants
This commit is contained in:
Jason Denney
2014-02-04 23:47:30 -05:00
parent 8110baf255
commit 38a716b132
3 changed files with 81 additions and 5 deletions
@@ -33,7 +33,7 @@
Ext.Msg.alert("Success", "New Password: "+data.password);
},
error: function(data,textStatus) {
Ext.Msg.alert( "Error", JSON.parse(data.responseText)["error"]);
Ext.Msg.alert( "Error", JSON.parse(data.responseText)["errors"][0]);
}
});
}
+8 -4
View File
@@ -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