diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 207b4ca8d..6dc9c5c45 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -10,6 +10,12 @@ class Api::V1::BaseController < ApplicationController protected + def error_message(subject, message) + errors = subject.errors.full_messages.join('. ') + (message << '. ' << errors) if errors.present? + message + end + def add_member_to_subject(subject) class_name = subject.class.name.downcase if member.present? && subject.add_member(member) @@ -61,11 +67,7 @@ class Api::V1::BaseController < ApplicationController end def render_validation_error(subject, message) - errors = subject.errors.full_messages.join('. ') - if errors.present? - message << '. ' << errors - end - render_json_response(subject, message, 422) + render_json_response(subject, error_message(subject, message), 422) end private diff --git a/app/controllers/api/v1/repositories_controller.rb b/app/controllers/api/v1/repositories_controller.rb index c438478d7..bcceadf57 100644 --- a/app/controllers/api/v1/repositories_controller.rb +++ b/app/controllers/api/v1/repositories_controller.rb @@ -53,10 +53,7 @@ class Api::V1::RepositoriesController < Api::V1::BaseController if key_pair.save render_json_response @repository, 'Signatures have been updated for repository successfully' else - message = 'Signatures have not been updated for repository' - errors = key_pair.errors.full_messages.join('. ') - (message << '. ' << errors) if errors.present? - render_json_response @repository, message, 422 + render_json_response @repository, error_message(key_pair, 'Signatures have not been updated for repository'), 422 end end diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index a97d773aa..c44688afc 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -3,14 +3,43 @@ class Api::V1::UsersController < Api::V1::BaseController before_filter :authenticate_user! skip_before_filter :authenticate_user!, :only => [:show] if APP_CONFIG['anonymous_access'] + load_and_authorize_resource :user, :only => :show + before_filter :set_current_user, :except => :show def show - @user = User.where(:id => params[:id]).first - if @user - render :show + @user = current_user if params[:id].nil? + end + + def update + user_params = params[:user] || {} + send_confirmation = user_params[:email] != @user.email + if @user.update_without_password(user_params) + if send_confirmation + @user.confirmed_at, @user.confirmation_sent_at = nil + @user.send_confirmation_instructions + end + render_json_response @user, 'User has been updated successfully' else - render_json_response User.new, "User with id='#{params[:id]}' does not exist", 422 + render_validation_error @user, "#{class_name} has not been updated" end end + def notifiers + if request.put? + if @user.notifier.update_attributes(params[:notifiers]) + render_json_response @user, 'User notification settings have been updated successfully' + else + render_json_response @user, error_message(@user.notifier, 'User notification settings have not been updated'), 422 + end + else + render :notifiers + end + end + + protected + + def set_current_user + @user = current_user + end + end \ No newline at end of file diff --git a/app/views/api/v1/users/notifiers.json.jbuilder b/app/views/api/v1/users/notifiers.json.jbuilder new file mode 100644 index 000000000..273ebbacc --- /dev/null +++ b/app/views/api/v1/users/notifiers.json.jbuilder @@ -0,0 +1,8 @@ +json.user do |json| + json.(@user, :id) + json.notifiers do |json_notifiers| + json_notifiers.(@user.notifier, :can_notify, :new_comment, :new_comment_reply, :new_issue, :issue_assign, :new_comment_commit_owner, :new_comment_commit_repo_owner, :new_comment_commit_commentor, :new_build, :new_associated_build) + end +end + +json.url notifiers_api_v1_user_path(:json) \ No newline at end of file diff --git a/app/views/api/v1/users/show.json.jbuilder b/app/views/api/v1/users/show.json.jbuilder index 7f2c1d06e..ff20c6f3b 100644 --- a/app/views/api/v1/users/show.json.jbuilder +++ b/app/views/api/v1/users/show.json.jbuilder @@ -1,9 +1,8 @@ json.user do |json| - json.(@user, :id, :name, :email, :uname,:language, :own_projects_count, :professional_experience, :site, :company, :location) + json.(@user, :id, :name, :email, :uname,:language, :own_projects_count, :professional_experience, :site, :company, :location, :build_priority) json.created_at @user.created_at.to_i json.updated_at @user.updated_at.to_i + json.avatar_url avatar_url(@user,:big) json.url api_v1_user_path(@user.id, :format => :json) json.html_url user_path(@user.uname) -end - -json.url api_v1_user_path(@user.id, :format => :json) \ No newline at end of file +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 3737aaa83..bdc9817a4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -48,6 +48,12 @@ Rosa::Application.routes.draw do } end resources :users, :only => [:show] + resource :user, :only => [:show, :update] do + member { + get :notifiers + put :notifiers + } + end end end