Commit 4314409dcaa56dc61067a6fcacf126c5ca44ced8
Exists in
ratings_minor_fixes
and in
3 other branches
Merge branch 'api-article-permissions' into 'master'
api: return permissions for user in article entity See merge request !948
Showing
4 changed files
with
20 additions
and
3 deletions
Show diff stats
app/api/entities.rb
| @@ -202,12 +202,21 @@ module Api | @@ -202,12 +202,21 @@ module Api | ||
| 202 | expose :accept_comments?, as: :accept_comments | 202 | expose :accept_comments?, as: :accept_comments |
| 203 | end | 203 | end |
| 204 | 204 | ||
| 205 | + def self.permissions_for_entity(entity, current_person, *method_names) | ||
| 206 | + method_names.map { |method| entity.send(method, current_person) ? method.to_s.gsub(/\?/,'') : nil }.compact | ||
| 207 | + end | ||
| 208 | + | ||
| 205 | class Article < ArticleBase | 209 | class Article < ArticleBase |
| 206 | root 'articles', 'article' | 210 | root 'articles', 'article' |
| 207 | expose :parent, :using => ArticleBase | 211 | expose :parent, :using => ArticleBase |
| 208 | expose :children, :using => ArticleBase do |article, options| | 212 | expose :children, :using => ArticleBase do |article, options| |
| 209 | article.children.published.limit(V1::Articles::MAX_PER_PAGE) | 213 | article.children.published.limit(V1::Articles::MAX_PER_PAGE) |
| 210 | end | 214 | end |
| 215 | + expose :permissions do |article, options| | ||
| 216 | + Entities.permissions_for_entity(article, options[:current_person], | ||
| 217 | + :allow_edit?, :allow_post_content?, :allow_delete?, :allow_create?, | ||
| 218 | + :allow_publish_content?) | ||
| 219 | + end | ||
| 211 | end | 220 | end |
| 212 | 221 | ||
| 213 | class User < Entity | 222 | class User < Entity |
app/api/helpers.rb
| @@ -121,7 +121,7 @@ module Api | @@ -121,7 +121,7 @@ module Api | ||
| 121 | 121 | ||
| 122 | def present_article(asset) | 122 | def present_article(asset) |
| 123 | article = find_article(asset.articles, params[:id]) | 123 | article = find_article(asset.articles, params[:id]) |
| 124 | - present_partial article, :with => Entities::Article, :params => params | 124 | + present_partial article, with: Entities::Article, params: params, current_person: current_person |
| 125 | end | 125 | end |
| 126 | 126 | ||
| 127 | def present_articles_for_asset(asset, method = 'articles') | 127 | def present_articles_for_asset(asset, method = 'articles') |
| @@ -130,7 +130,7 @@ module Api | @@ -130,7 +130,7 @@ module Api | ||
| 130 | end | 130 | end |
| 131 | 131 | ||
| 132 | def present_articles(articles) | 132 | def present_articles(articles) |
| 133 | - present_partial paginate(articles), :with => Entities::Article, :params => params | 133 | + present_partial paginate(articles), :with => Entities::Article, :params => params, current_person: current_person |
| 134 | end | 134 | end |
| 135 | 135 | ||
| 136 | def find_articles(asset, method = 'articles') | 136 | def find_articles(asset, method = 'articles') |
app/api/v1/articles.rb
| @@ -273,7 +273,7 @@ module Api | @@ -273,7 +273,7 @@ module Api | ||
| 273 | article = forbidden! | 273 | article = forbidden! |
| 274 | end | 274 | end |
| 275 | 275 | ||
| 276 | - present_partial article, :with => Entities::Article | 276 | + present_partial article, :with => Entities::Article, current_person: current_person |
| 277 | else | 277 | else |
| 278 | 278 | ||
| 279 | present_articles_for_asset(profile) | 279 | present_articles_for_asset(profile) |
test/api/articles_test.rb
| @@ -786,4 +786,12 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -786,4 +786,12 @@ class ArticlesTest < ActiveSupport::TestCase | ||
| 786 | assert_not_includes json['article']['children'].map {|a| a['id']}, child.id | 786 | assert_not_includes json['article']['children'].map {|a| a['id']}, child.id |
| 787 | end | 787 | end |
| 788 | 788 | ||
| 789 | + should 'list article permissions when get an article' do | ||
| 790 | + community = fast_create(Community) | ||
| 791 | + give_permission(person, 'post_content', community) | ||
| 792 | + article = fast_create(Article, :profile_id => community.id) | ||
| 793 | + get "/api/v1/articles/#{article.id}?#{params.to_query}" | ||
| 794 | + json = JSON.parse(last_response.body) | ||
| 795 | + assert_includes json["article"]["permissions"], 'allow_post_content' | ||
| 796 | + end | ||
| 789 | end | 797 | end |