diff --git a/app/api/entities.rb b/app/api/entities.rb index 97567af..d5c4276 100644 --- a/app/api/entities.rb +++ b/app/api/entities.rb @@ -202,12 +202,21 @@ module Api expose :accept_comments?, as: :accept_comments end + def self.permissions_for_entity(entity, current_person, *method_names) + method_names.map { |method| entity.send(method, current_person) ? method.to_s.gsub(/\?/,'') : nil }.compact + end + class Article < ArticleBase root 'articles', 'article' expose :parent, :using => ArticleBase expose :children, :using => ArticleBase do |article, options| article.children.published.limit(V1::Articles::MAX_PER_PAGE) end + expose :permissions do |article, options| + Entities.permissions_for_entity(article, options[:current_person], + :allow_edit?, :allow_post_content?, :allow_delete?, :allow_create?, + :allow_publish_content?) + end end class User < Entity diff --git a/app/api/helpers.rb b/app/api/helpers.rb index e306bea..2a26b38 100644 --- a/app/api/helpers.rb +++ b/app/api/helpers.rb @@ -121,7 +121,7 @@ module Api def present_article(asset) article = find_article(asset.articles, params[:id]) - present_partial article, :with => Entities::Article, :params => params + present_partial article, with: Entities::Article, params: params, current_person: current_person end def present_articles_for_asset(asset, method = 'articles') @@ -130,7 +130,7 @@ module Api end def present_articles(articles) - present_partial paginate(articles), :with => Entities::Article, :params => params + present_partial paginate(articles), :with => Entities::Article, :params => params, current_person: current_person end def find_articles(asset, method = 'articles') diff --git a/app/api/v1/articles.rb b/app/api/v1/articles.rb index 3fd9997..5585b50 100644 --- a/app/api/v1/articles.rb +++ b/app/api/v1/articles.rb @@ -273,7 +273,7 @@ module Api article = forbidden! end - present_partial article, :with => Entities::Article + present_partial article, :with => Entities::Article, current_person: current_person else present_articles_for_asset(profile) diff --git a/test/api/articles_test.rb b/test/api/articles_test.rb index 622532d..9b98227 100644 --- a/test/api/articles_test.rb +++ b/test/api/articles_test.rb @@ -786,4 +786,12 @@ class ArticlesTest < ActiveSupport::TestCase assert_not_includes json['article']['children'].map {|a| a['id']}, child.id end + should 'list article permissions when get an article' do + community = fast_create(Community) + give_permission(person, 'post_content', community) + article = fast_create(Article, :profile_id => community.id) + get "/api/v1/articles/#{article.id}?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_includes json["article"]["permissions"], 'allow_post_content' + end end -- libgit2 0.21.2