Commit 678ca0f1a772241f5304a11d12ba286e5d7dc9cd
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'rails3_api' into rails3_stable
Showing
6 changed files
with
74 additions
and
2 deletions
Show diff stats
lib/api/api.rb
lib/api/entities.rb
| ... | ... | @@ -59,8 +59,18 @@ module API |
| 59 | 59 | |
| 60 | 60 | class User < Grape::Entity |
| 61 | 61 | root 'users', 'user' |
| 62 | + expose :id | |
| 62 | 63 | expose :login |
| 63 | 64 | expose :person, :using => Profile |
| 65 | + expose :permissions do |user, options| | |
| 66 | + output = {} | |
| 67 | + user.person.role_assignments.map do |role_assigment| | |
| 68 | + if role_assigment.resource.respond_to?(:identifier) | |
| 69 | + output[role_assigment.resource.identifier] = role_assigment.role.permissions | |
| 70 | + end | |
| 71 | + end | |
| 72 | + output | |
| 73 | + end | |
| 64 | 74 | end |
| 65 | 75 | |
| 66 | 76 | class UserLogin < User | ... | ... |
lib/api/v1/articles.rb
| ... | ... | @@ -65,8 +65,12 @@ module API |
| 65 | 65 | # POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body |
| 66 | 66 | post do |
| 67 | 67 | community = environment.communities.find(params[:community_id]) |
| 68 | - article = community.articles.build(params[:article].merge(:last_changed_by => current_person)) | |
| 69 | - article.type= params[:type].nil? ? 'TinyMceArticle' : params[:type] | |
| 68 | + klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] | |
| 69 | + article = klass_type.constantize.new(params[:article]) | |
| 70 | + article.last_changed_by = current_person | |
| 71 | + article.created_by= current_person | |
| 72 | + article.profile = community | |
| 73 | + | |
| 70 | 74 | if !article.save |
| 71 | 75 | render_api_errors!(article.errors.full_messages) |
| 72 | 76 | end | ... | ... |
| ... | ... | @@ -0,0 +1,24 @@ |
| 1 | +module API | |
| 2 | + module V1 | |
| 3 | + class Categories < Grape::API | |
| 4 | + before { detect_stuff_by_domain } | |
| 5 | + before { authenticate! } | |
| 6 | + | |
| 7 | + resource :categories do | |
| 8 | + | |
| 9 | + get do | |
| 10 | + type = params[:category_type] | |
| 11 | + categories = type.nil? ? environment.categories : environment.categories.find(:all, :conditions => {:type => type}) | |
| 12 | + present categories, :with => Entities::Category | |
| 13 | + end | |
| 14 | + | |
| 15 | + desc "Return the category by id" | |
| 16 | + get ':id' do | |
| 17 | + present environment.categories.find(params[:id]), :with => Entities::Category | |
| 18 | + end | |
| 19 | + | |
| 20 | + end | |
| 21 | + | |
| 22 | + end | |
| 23 | + end | |
| 24 | +end | ... | ... |
lib/api/v1/users.rb
| ... | ... | @@ -15,6 +15,17 @@ module API |
| 15 | 15 | present environment.users.find(params[:id]), :with => Entities::User |
| 16 | 16 | end |
| 17 | 17 | |
| 18 | + get ":id/permissions" do | |
| 19 | + user = environment.users.find(params[:id]) | |
| 20 | + output = {} | |
| 21 | + user.person.role_assignments.map do |role_assigment| | |
| 22 | + if role_assigment.resource.respond_to?(:identifier) && role_assigment.resource.identifier == params[:profile] | |
| 23 | + output[:permissions] = role_assigment.role.permissions | |
| 24 | + end | |
| 25 | + end | |
| 26 | + present output | |
| 27 | + end | |
| 28 | + | |
| 18 | 29 | end |
| 19 | 30 | |
| 20 | 31 | end | ... | ... |
test/unit/api_test.rb
| ... | ... | @@ -82,4 +82,26 @@ class APITest < ActiveSupport::TestCase |
| 82 | 82 | assert_includes json["users"].map { |a| a["login"] }, user.login |
| 83 | 83 | end |
| 84 | 84 | |
| 85 | + should 'list user permissions' do | |
| 86 | + community = fast_create(Community) | |
| 87 | + community.add_admin(user.person) | |
| 88 | + get "/api/v1/users/#{user.id}/?#{params.to_query}" | |
| 89 | + json = JSON.parse(last_response.body) | |
| 90 | + assert_includes json["user"]["permissions"], community.identifier | |
| 91 | + end | |
| 92 | + | |
| 93 | + should 'list categories' do | |
| 94 | + category = fast_create(Category) | |
| 95 | + get "/api/v1/categories/?#{params.to_query}" | |
| 96 | + json = JSON.parse(last_response.body) | |
| 97 | + assert_includes json["categories"].map { |c| c["name"] }, category.name | |
| 98 | + end | |
| 99 | + | |
| 100 | + should 'get category by id' do | |
| 101 | + category = fast_create(Category) | |
| 102 | + get "/api/v1/categories/#{category.id}/?#{params.to_query}" | |
| 103 | + json = JSON.parse(last_response.body) | |
| 104 | + assert_equal category.name, json["category"]["name"] | |
| 105 | + end | |
| 106 | + | |
| 85 | 107 | end | ... | ... |