From d841e5e1ff8c3fa9ca0b85797e65530bb64baa09 Mon Sep 17 00:00:00 2001 From: Honza Novak Date: Tue, 6 Dec 2016 13:37:18 +0100 Subject: [PATCH 1/3] API: make folder.save action available as an API call. This allows changing folder attributes like renaming folder, or changing folder description. --- app/controllers/dmsf_controller.rb | 25 +++++++++++++++++++------ app/views/dmsf/save.api.rsb | 5 +++++ 2 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 app/views/dmsf/save.api.rsb diff --git a/app/controllers/dmsf_controller.rb b/app/controllers/dmsf_controller.rb index 106118ab..ce6ed50e 100644 --- a/app/controllers/dmsf_controller.rb +++ b/app/controllers/dmsf_controller.rb @@ -29,7 +29,7 @@ class DmsfController < ApplicationController before_filter :find_parent, :only => [:new, :create] before_filter :tree_view, :only => [:delete, :show] - accept_api_auth :show, :create + accept_api_auth :show, :create, :save helper :all @@ -180,6 +180,8 @@ class DmsfController < ApplicationController saved = @folder.save + + respond_to do |format| format.js format.api { @@ -223,11 +225,21 @@ class DmsfController < ApplicationController end end - if @folder.save - flash[:notice] = l(:notice_folder_details_were_saved) - redirect_to dmsf_folder_path(:id => @project, :folder_id => @folder) - else - render :action => 'edit' + saved = @folder.save + respond_to do |format| + format.api { + unless saved + render_validation_errors(@folder) + end + } + format.html { + if saved + flash[:notice] = l(:notice_folder_details_were_saved) + redirect_to dmsf_folder_path(:id => @project, :folder_id => @folder) + else + render :action => 'edit' + end + } end end @@ -667,3 +679,4 @@ class DmsfController < ApplicationController end end + diff --git a/app/views/dmsf/save.api.rsb b/app/views/dmsf/save.api.rsb new file mode 100644 index 00000000..7af62e91 --- /dev/null +++ b/app/views/dmsf/save.api.rsb @@ -0,0 +1,5 @@ +api.dmsf_folder do + api.id @folder.id + api.title @folder.title + api.description @folder.description +end From f66ee8ea429fc63ff0e09decd7df7e65d326db43 Mon Sep 17 00:00:00 2001 From: Honza Novak Date: Thu, 8 Dec 2016 14:57:51 +0100 Subject: [PATCH 2/3] Modify save action, so that updates can be triggered with API call (csrf validation is being skipped when redmine api key is present) Add option to lookup folder by title with API call, if folder is found then information about that is appended into output. --- app/controllers/dmsf_controller.rb | 13 +++++++++++++ app/views/dmsf/show.api.rsb | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/app/controllers/dmsf_controller.rb b/app/controllers/dmsf_controller.rb index ce6ed50e..6833877a 100644 --- a/app/controllers/dmsf_controller.rb +++ b/app/controllers/dmsf_controller.rb @@ -31,6 +31,8 @@ class DmsfController < ApplicationController accept_api_auth :show, :create, :save + skip_before_action :verify_authenticity_token, if: -> { request.headers["HTTP_X_REDMINE_API_KEY"].present? } + helper :all def expand_folder @@ -44,6 +46,8 @@ class DmsfController < ApplicationController end def show + # also try to lookup folder by title if this is API call + find_folder_by_title if [:xml, :json].include? request.format.to_sym get_display_params if @folder && @folder.deleted? render_404 @@ -550,6 +554,15 @@ class DmsfController < ApplicationController render_404 end + def find_folder_by_title + # find by title has to be scoped to project + @folder = DmsfFolder.find_by(title: params[:folder_title], project_id: params[:id]) if params[:folder_title].present? + rescue DmsfAccessError + render_403 + rescue ActiveRecord::RecordNotFound + render_404 + end + def find_parent @parent = DmsfFolder.visible.find params[:parent_id] if params[:parent_id].present? rescue DmsfAccessError diff --git a/app/views/dmsf/show.api.rsb b/app/views/dmsf/show.api.rsb index 542a39f4..8d0ef0e0 100644 --- a/app/views/dmsf/show.api.rsb +++ b/app/views/dmsf/show.api.rsb @@ -29,4 +29,12 @@ api.dmsf do end end end + + if @folder + api.found_folder do + api.id @folder.id + api.title @folder.title + end + end + end \ No newline at end of file From 6387ba058ae84ea9e0a2a46840a7e2a57163c38c Mon Sep 17 00:00:00 2001 From: Honza Novak Date: Fri, 9 Dec 2016 21:21:09 +0100 Subject: [PATCH 3/3] Add curl examples for API calls to list/lookup foder and update folder actions --- extra/api/api_client.sh | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/extra/api/api_client.sh b/extra/api/api_client.sh index bb7a112b..772414c2 100644 --- a/extra/api/api_client.sh +++ b/extra/api/api_client.sh @@ -40,4 +40,41 @@ curl -v -H "Content-Type: application/xml" -X GET -u ${1}:${2} http://localhost: # 4. Upload a document into a given folder or the root folder #curl --data-binary "@cat.gif" -H "Content-Type: application/octet-stream" -X POST -u ${1}:${2} http://localhost:3000/projects/12/dmsf/upload.xml?filename=cat.gif -#curl -v -H "Content-Type: application/xml" -X POST --data "@file.xml" -u ${1}:${2} http://localhost:3000/projects/12/dmsf/commit.xml \ No newline at end of file +#curl -v -H "Content-Type: application/xml" -X POST --data "@file.xml" -u ${1}:${2} http://localhost:3000/projects/12/dmsf/commit.xml + +# 5. list folder contents & check folder existence (by folder title) +# curl -v -H "Content-Type: application/json" -X GET -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000/projects/1/dmsf.json?folder_title=Updated%20title +# 6. list folder contents & check folder existence (by folder id) +# curl -v -H "Content-Type: application/json" -X GET -H "X-Redmine-API-Key: USERS_API_KE" http://localhost:3000/projects/1/dmsf.json?folder_id=3 +# both returns 404 not found, or json with following structure: +# { +# "dmsf":{ +# "dmsf_folders":[ +# +# ], +# "total_count":0, +# "dmsf_files":[ +# +# ], +# "dmsf_links":[ +# +# ], +# "found_folder":{ +# "id":3, +# "title":"Updated title" +# } +# } +#} + +# 7. update folder +# curl -v -H "Content-Type: application/json" -X POST --data "@update-folder-payload.json" -H "X-Redmine-API-Key: USERS_API_KEY" http://localhost:3000//projects/#{project_id}/dmsf/save.json + + +# update-folder-payload.json +# { +# "dmsf_folder": { +# "title": title, +# "description": description +# }, +# "folder_id": id -- id of folder to be updated +# } \ No newline at end of file