[Git][noosfero/noosfero][master] 2 commits: api: return organization roles

Leandro Nunes gitlab at mg.gitlab.com
Fri Jul 22 11:22:27 BRT 2016


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
dee85e78 by Victor Costa at 2016-07-21T17:29:08-03:00
api: return organization roles

- - - - -
3c7ef3f4 by Leandro Nunes at 2016-07-22T14:22:02+00:00
Merge branch 'api_roles' into 'master'

api: return organization roles



See merge request !983
- - - - -


7 changed files:

- app/api/app.rb
- app/api/entities.rb
- + app/api/v1/roles.rb
- app/models/profile.rb
- app/views/tasks/_add_member_accept_details.html.erb
- + test/api/roles_test.rb
- test/unit/profile_test.rb


Changes:

=====================================
app/api/app.rb
=====================================
--- a/app/api/app.rb
+++ b/app/api/app.rb
@@ -54,6 +54,7 @@ module Api
     mount V1::Blocks
     mount V1::Profiles
     mount V1::Activities
+    mount V1::Roles
 
     # hook point which allow plugins to add Grape::API extensions to Api::App
     #finds for plugins which has api mount points classes defined (the class should extends Grape::API)


=====================================
app/api/entities.rb
=====================================
--- a/app/api/entities.rb
+++ b/app/api/entities.rb
@@ -302,5 +302,12 @@ module Api
         type_map.first.represent(activity.target) unless type_map.nil?
       end
     end
+
+    class Role < Entity
+      root 'roles', 'role'
+      expose :id
+      expose :name
+      expose :key
+    end
   end
 end


=====================================
app/api/v1/roles.rb
=====================================
--- /dev/null
+++ b/app/api/v1/roles.rb
@@ -0,0 +1,24 @@
+module Api
+  module V1
+    class Roles < Grape::API
+      before { authenticate! }
+
+      MAX_PER_PAGE = 50
+
+      resource :organizations do
+        segment "/:organization_id" do
+          resource :roles do
+
+            paginate max_per_page: MAX_PER_PAGE
+            get do
+              organization = environment.profiles.find(params[:organization_id])
+              roles = Profile::Roles.organization_roles(organization.environment.id, organization.id)
+              present_partial paginate(roles), with: Entities::Role
+            end
+            
+          end
+        end
+      end
+    end
+  end
+end


=====================================
app/models/profile.rb
=====================================
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -52,6 +52,9 @@ class Profile < ApplicationRecord
     def self.organization_custom_roles(env_id, profile_id)
       all_roles(env_id).where('profile_id = ?', profile_id)
     end
+    def self.organization_roles(env_id, profile_id)
+      all_roles(env_id).where("profile_id = ?  or key like 'profile_%'", profile_id)
+    end
     def self.all_roles(env_id)
       Role.where(environment_id: env_id)
     end


=====================================
app/views/tasks/_add_member_accept_details.html.erb
=====================================
--- a/app/views/tasks/_add_member_accept_details.html.erb
+++ b/app/views/tasks/_add_member_accept_details.html.erb
@@ -1,5 +1,5 @@
 <%= content = _("Roles:")+"<br />"
-roles = Profile::Roles.organization_member_roles(task.target.environment.id) + profile.custom_roles
+roles = Profile::Roles.organization_roles(task.target.environment.id, profile.id)
 roles.each do |role|
   content += labelled_check_box(role.name, "tasks[#{task.id}][task][roles][]", role.id, false) + "<br />".html_safe
 end


=====================================
test/api/roles_test.rb
=====================================
--- /dev/null
+++ b/test/api/roles_test.rb
@@ -0,0 +1,23 @@
+require_relative 'test_helper'
+
+class TolesTest < ActiveSupport::TestCase
+
+  def setup
+    create_and_activate_user
+    login_api
+    @environment = Environment.default
+    @profile = fast_create(Organization)
+  end
+
+  attr_accessor :profile, :environment
+
+  should 'list organization roles' do
+    environment.roles.delete_all
+    role1 = Role.create!(key: 'profile_administrator', name: 'admin', environment: environment)
+    role2 = Role.new(key: 'profile_moderator', name: 'moderator', environment: environment)
+    profile.custom_roles << role2
+    get "/api/v1/organizations/#{profile.id}/roles?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equivalent [role1.id, role2.id], json['roles'].map {|r| r['id']}
+  end
+end


=====================================
test/unit/profile_test.rb
=====================================
--- a/test/unit/profile_test.rb
+++ b/test/unit/profile_test.rb
@@ -1892,7 +1892,7 @@ class ProfileTest < ActiveSupport::TestCase
     assert_includes Profile.communities, child
   end
 
-  should 'get organization roles' do
+  should 'get organization member roles' do
     env = fast_create(Environment)
     roles = %w(foo bar profile_foo profile_bar).map{ |r| create(Role, :name => r, :key => r, :environment_id => env.id, :permissions => ["some"]) }
     create Role, :name => 'test', :key => 'profile_test', :environment_id => env.id + 1
@@ -1900,6 +1900,15 @@ class ProfileTest < ActiveSupport::TestCase
     assert_equal roles[2..3], Profile::Roles.organization_member_roles(env.id)
   end
 
+  should 'get organization roles' do
+    env = fast_create(Environment)
+    env.roles.delete_all
+    profile = fast_create(Organization)
+    roles = %w(foo bar profile_foo profile_bar).map{ |r| create(Role, :name => r, :key => r, :environment_id => env.id, :permissions => ["some"]) }
+    roles << create(Role, name: 'test', key: 'something_else', environment_id: env.id, profile_id: profile.id)
+    assert_equal roles[2..4], Profile::Roles.organization_roles(env.id, profile.id)
+  end
+
   should 'get all roles' do
     env = fast_create(Environment)
     roles = %w(foo bar profile_foo profile_bar).map{ |r| create(Role, :name => r, :environment_id => env.id, :permissions => ["some"]) }



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/53a2c9a985c6d4b95fee42c8a462dae3bb67de89...3c7ef3f48fba06426f3cfca0a1853b34b5bbd455
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160722/b2d98761/attachment-0001.html>


More information about the Noosfero-dev mailing list