[Git][noosfero/noosfero][master] 2 commits: api: return permissions for user in article entity

Leandro Nunes gitlab at mg.gitlab.com
Tue May 31 01:49:08 BRT 2016


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
5850b93d by Victor Costa at 2016-05-30T16:16:41-03:00
api: return permissions for user in article entity

- - - - -
4314409d by Leandro Nunes at 2016-05-31T04:48:54+00:00
Merge branch 'api-article-permissions' into 'master'

api: return permissions for user in article entity



See merge request !948
- - - - -


4 changed files:

- app/api/entities.rb
- app/api/helpers.rb
- app/api/v1/articles.rb
- test/api/articles_test.rb


Changes:

=====================================
app/api/entities.rb
=====================================
--- 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


=====================================
app/api/helpers.rb
=====================================
--- 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')


=====================================
app/api/v1/articles.rb
=====================================
--- 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)


=====================================
test/api/articles_test.rb
=====================================
--- 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



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/14b34706cdf9618601b3fad420c33d4f3822f0c8...4314409dcaa56dc61067a6fcacf126c5ca44ced8
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160531/184cc2a1/attachment-0001.html>


More information about the Noosfero-dev mailing list