Commit ae7086105dadbd1839f9d0dd0e2e4246a562513d

Authored by MoisesMachado
1 parent 84bc4583

ActionItem5: implemented controllers to manipulate the attribution of roles in t…

…he profiles and added some functionlity


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@546 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/profile_admin/enterprise_controller.rb
... ... @@ -1,147 +0,0 @@
1   -# Manage enterprises by providing an interface to register, activate and manage them
2   -class EnterpriseController < ProfileAdminController
3   -
4   - before_filter :logon, :my_enterprises
5   - protect([:edit, :update, :activate, :destroy], 'edit_enterprise', @profile)
6   -
7   - # Redirects to show if there is only one action and to list otherwise
8   - def index
9   - if @person.enterprises.size == 1
10   - redirect_to :action => 'show', :id => @person.enterprises[0]
11   - else
12   - redirect_to :action => 'list'
13   - end
14   - @vitual_communities = Environment.find(:all)
15   - @validation_entities = Organization.find(:all)
16   - end
17   -
18   - # Lists all enterprises
19   - def list
20   - @enterprises = Enterprise.find(:all) - @person.enterprises
21   - end
22   -
23   - # Show details about an enterprise
24   - def show
25   - @enterprise = Enterprise.find(params[:id])
26   - end
27   -
28   - # Make a form to the creation of an eterprise
29   - def register_form
30   - @enterprise = Enterprise.new()
31   - @vitual_communities = Environment.find(:all)
32   - @validation_entities = Organization.find(:all)
33   - end
34   -
35   - # Saves the new created enterprise
36   - def register
37   - @enterprise = Enterprise.new(params[:enterprise])
38   - @enterprise.organization_info = OrganizationInfo.new(params[:organization])
39   - if @enterprise.save
40   - @enterprise.people << @person
41   - flash[:notice] = _('The enterprise was succesfully created, the validation entity will cotact you as soon as your enterprise is approved')
42   - redirect_to :action => 'index'
43   - else
44   - flash[:notice] = _('Enterprise was not created')
45   - @vitual_communities = Environment.find(:all)
46   - @validation_entities = Organization.find(:all)
47   - render :action => 'register_form'
48   - end
49   - end
50   -
51   - # Provides an interface to editing the enterprise details
52   - def edit
53   - @enterprise = @person.enterprises(:id => params[:id])[0]
54   - @validation_entities = Organization.find(:all) - [@enterprise]
55   - end
56   -
57   - # Saves the changes made in an enterprise
58   - def update
59   - @enterprise = @person.enterprises(:id => params[:id])[0]
60   - if @enterprise.update_attributes(params[:enterprise]) && @enterprise.organization_info.update_attributes(params[:organization_info])
61   - redirect_to :action => 'index'
62   - else
63   - flash[:notice] = _('Could not update the enterprise')
64   - @validation_entities = Organization.find(:all) - [@enterprise]
65   - render :action => 'edit'
66   - end
67   - end
68   -
69   - # Make the current user a new member of the enterprise
70   - def affiliate
71   - @enterprise = Enterprise.find(params[:id])
72   - member_role = Role.find_by_name('member') || Role.create(:name => 'member')
73   - @enterprise.affiliate(@person,member_role)
74   - redirect_to :action => 'index'
75   - end
76   -
77   - # Elimitates the enterprise of the system
78   - def destroy
79   - @enterprise = @person.enterprises(:id => params[:id])[0]
80   - if @enterprise
81   - @enterprise.destroy
82   - else
83   - flash[:notice] = 'Can destroy only your enterprises'
84   - end
85   - redirect_to :action => 'index'
86   - end
87   -
88   - # Search enterprises by name or tags
89   - def search
90   - @tagged_enterprises = Enterprise.search(params[:query])
91   - end
92   -
93   - # Activate a validated enterprise
94   - def activate
95   - @enterprise = Enterprise.find(params[:id])
96   - if @enterprise.activate
97   - flash[:notice] = _('Enterprise successfuly activacted')
98   - else
99   - flash[:notice] = _('Failed to activate the enterprise')
100   - end
101   - redirect_to :action => 'index'
102   - end
103   -
104   - # Validates an eterprise
105   - def approve
106   - @enterprise = Enterprise.find(params[:id])
107   - if @enterprise.approve
108   - flash[:notice] = _('Enterprise successfuly approved')
109   - else
110   - flash[:notice] = _('Failed to approve the htmlenterprise')
111   - end
112   - redirect_to :action => 'index'
113   - end
114   -
115   - # Rejects an enterprise
116   - def reject
117   - @enterprise = Enterprise.find(params[:id])
118   - if @enterprise.reject
119   - flash[:notice] = _('Enterprise successfuly rejected')
120   - else
121   - flash[:notice] = _('Failed to reject the enterprise')
122   - end
123   - redirect_to :action => 'index'
124   - end
125   -
126   -
127   - protected
128   -
129   - # Make sure that the user is logged before access this controller
130   - def logon
131   - if logged_in?
132   - @user = current_user
133   - @person = @user.person
134   - else
135   - redirect_to :controller => 'account' unless logged_in?
136   - end
137   - end
138   -
139   - # Initializes some variables to contain the enterprises of the current user
140   - def my_enterprises
141   - if logged_in?
142   - @my_active_enterprises = @person.active_enterprises
143   - @my_pending_enterprises = @person.pending_enterprises
144   - @my_enterprises = @person.enterprises
145   - end
146   - end
147   -end
app/controllers/profile_admin/enterprise_editor_controller.rb 0 → 100644
... ... @@ -0,0 +1,67 @@
  1 +class EnterpriseEditorController < ProfileAdminController
  2 +
  3 + before_filter :logon, :check_enterprise
  4 +
  5 + # Show details about an enterprise
  6 + def index
  7 + @my_enterprises = @person.enterprises
  8 + @my_pending_enterprises = @person.pending_enterprises
  9 + end
  10 +
  11 + # Provides an interface to editing the enterprise details
  12 + def edit
  13 + @validation_entities = Organization.find(:all) - [@enterprise]
  14 + end
  15 +
  16 + # Saves the changes made in an enterprise
  17 + def update
  18 + if @enterprise.update_attributes(params[:enterprise]) && @enterprise.organization_info.update_attributes(params[:organization_info])
  19 + redirect_to :action => 'index'
  20 + else
  21 + flash[:notice] = _('Could not update the enterprise')
  22 + @validation_entities = Organization.find(:all) - [@enterprise]
  23 + render :action => 'edit'
  24 + end
  25 + end
  26 +
  27 + # Make the current user a new member of the enterprise
  28 + def affiliate
  29 + member_role = Role.find_by_name('member') || Role.create(:name => 'member')
  30 + @enterprise.affiliate(@person,member_role)
  31 + redirect_to :action => 'index'
  32 + end
  33 +
  34 + # Elimitates the enterprise of the system
  35 + def destroy
  36 + if @enterprise
  37 + @enterprise.destroy
  38 + else
  39 + flash[:notice] = 'Can destroy only your enterprises'
  40 + end
  41 + redirect_to :action => 'index'
  42 + end
  43 +
  44 + # Activate a validated enterprise
  45 + def activate
  46 + if @enterprise.activate
  47 + flash[:notice] = _('Enterprise successfuly activacted')
  48 + else
  49 + flash[:notice] = _('Failed to activate the enterprise')
  50 + end
  51 + redirect_to :action => 'index'
  52 + end
  53 +
  54 + protected
  55 +
  56 + def logon
  57 + if logged_in?
  58 + @user = current_user
  59 + @person = @user.person
  60 + end
  61 + end
  62 +
  63 + def check_enterprise
  64 + raise 'It\'s not an enterprise' unless @profile.is_a?(Enterprise)
  65 + @enterprise = @profile
  66 + end
  67 +end
... ...
app/controllers/profile_admin/membership_editor_controller.rb 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +class MembershipEditorController < ProfileAdminController
  2 +
  3 + def index
  4 + @memberships = Profile.find(:all, :include => 'role_assignments', :conditions => ['role_assignments.person_id = ?', current_user.person.id])
  5 + end
  6 +end
... ...
app/controllers/profile_admin/profile_member_controller.rb
... ... @@ -1,23 +0,0 @@
1   -class ProfileMemberController < ApplicationController
2   -
3   - def index
4   - @members = @profile.people
5   - end
6   -
7   - def affiliate
8   - @member = Person.find(params[:id])
9   - @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
10   - end
11   -
12   - def give_role
13   - @person = Person.find(params[:person])
14   - @role = Role.find(params[:role])
15   - if @profile.affiliate(@person, @role)
16   - redirect_to :action => 'index'
17   - else
18   - @member = Person.find(params[:person])
19   - @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
20   - render :action => 'affiliate'
21   - end
22   - end
23   -end
app/controllers/profile_admin/profile_members_controller.rb 0 → 100644
... ... @@ -0,0 +1,60 @@
  1 +class ProfileMembersController < ProfileAdminController
  2 +
  3 + def index
  4 + @members = profile.people.uniq
  5 + end
  6 +
  7 + def change_roles
  8 + @member = Person.find(params[:id])
  9 + @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
  10 + end
  11 +
  12 + def update_roles
  13 + @roles = Role.find(params[:roles])
  14 + @person = Person.find(params[:person])
  15 + if @person.define_roles(@roles, profile)
  16 + flash[:notice] = 'Roles successfuly updated'
  17 + else
  18 + flash[:notice] = 'Couldn\'t change the roles'
  19 + end
  20 + redirect_to :action => :index
  21 + end
  22 +
  23 + def change_role
  24 + @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
  25 + @member = Person.find(params[:id])
  26 + @associations = RoleAssignment.find(:all, :conditions => {:person_id => @member, :resource_id => @profile, :resource_type => @profile.class.base_class.name})
  27 + end
  28 +
  29 + def add_role
  30 + @person = Person.find(params[:person])
  31 + @role = Role.find(params[:role])
  32 + if @profile.affiliate(@person, @role)
  33 + redirect_to :action => 'index'
  34 + else
  35 + @member = Person.find(params[:person])
  36 + @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
  37 + render :action => 'affiliate'
  38 + end
  39 + end
  40 +
  41 + def remove_role
  42 + @association = RoleAssignment.find(params[:id])
  43 + if @association.destroy
  44 + flash[:notice] = 'Member succefully unassociated'
  45 + else
  46 + flash[:notice] = 'Failed to unassociate member'
  47 + end
  48 + redirect_to :aciton => 'index'
  49 + end
  50 +
  51 + def unassociate
  52 + @association = RoleAssignment.find(params[:id])
  53 + if @association.destroy
  54 + flash[:notice] = 'Member succefully unassociated'
  55 + else
  56 + flash[:notice] = 'Failed to unassociate member'
  57 + end
  58 + redirect_to :aciton => 'index'
  59 + end
  60 +end
... ...
app/helpers/enterprise_editor_helper.rb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +module EnterpriseEditorHelper
  2 +end
... ...
app/helpers/membership_editor_helper.rb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +module MembershipEditorHelper
  2 +end
... ...
app/models/person.rb
... ... @@ -16,6 +16,14 @@ class Person &lt; Profile
16 16 role_assignments.any? {|ra| ra.has_permission?(perm, res)}
17 17 end
18 18  
  19 + def define_roles(roles, resource)
  20 + associations = RoleAssignment.find(:all, :conditions => {:resource_id => resource.id, :resource_type => resource.class.base_class.name, :person_id => self.id })
  21 + roles_add = roles - associations.map(&:role)
  22 + roles_remove = associations.map(&:role) - roles
  23 + associations.each { |a| a.destroy if roles_remove.include?(a.role) }
  24 + roles_add.each {|r| RoleAssignment.create(:person_id => self.id, :resource_id => resource.id, :resource_type => resource.class.base_class.name, :role_id => r.id) }
  25 + end
  26 +
19 27 def self.conditions_for_profiles(conditions, person)
20 28 new_conditions = sanitize_sql(['role_assignments.person_id = ?', person])
21 29 new_conditions << ' AND ' + sanitize_sql(conditions) unless conditions.blank?
... ...
app/views/enterprise_editor/_enterprise.rhtml 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +<li>
  2 +<%= link_to enterprise.name, :action => 'show', :id => enterprise %>
  3 +<%= link_to _('Edit'), :action => 'edit', :id => enterprise %>
  4 +<%= help _('Change the infomation about the enterprise') %>
  5 +<%= link_to _('Delete'), :action => 'destroy', :id => enterprise %>
  6 +<%= help _('Remove the enterprise from the system') %>
  7 +<%= link_to _('Affiliate'), :action => 'affiliate', :id => enterprise unless @my_enterprises.include?(enterprise) %>
  8 +<%= help _('Be a member of the enterprise') unless @my_enterprises.include?(enterprise) %>
  9 +<%= link_to _('Activate'), :action => 'activate', :id => enterprise unless enterprise.active %>
  10 +<%= help _('Activate the profile of an approved enterprise') unless enterprise.active %>
  11 +<% unless enterprise.approved? %>
  12 + <%= link_to _('Approve'), :action => 'approve', :id => enterprise %>
  13 + <%= help _('Approve a submitted enterprise profile') %>
  14 + <%= link_to _('Reject'), :action => 'reject', :id => enterprise %>
  15 + <%= help _('Reject a submitted enterprise profile') %>
  16 +<% end %>
  17 +</li>
... ...
app/views/enterprise_editor/_form.rhtml 0 → 100644
... ... @@ -0,0 +1,32 @@
  1 +<p><label for="name"><%= _('Name') %></label><br/>
  2 +<%= text_field 'enterprise', 'name', 'size' => 20 %></p>
  3 +
  4 +<p><label for="address"><%= _('Address') %></label><br/>
  5 +<%= text_field 'enterprise', 'address', 'size' => 50 %></p>
  6 +
  7 +<p><label for="contact_phone"><%= _('Contact Phone') %></label><br/>
  8 +<%= text_field 'enterprise', 'contact_phone', 'size' => 20 %></p>
  9 +
  10 +<p><label for="contact_person"><%= _('Contact Person') %></label><br/>
  11 +<%= text_field 'organization_info', 'contact_person', 'size' => 20 %></p>
  12 +
  13 +<p><label for="acronym"><%= _('Acronym') %></label><br/>
  14 +<%= text_field 'organization_info', 'acronym', 'size' => 20 %></p>
  15 +
  16 +<p><label for="foundation_year"><%= _('Foundation Year') %></label><br/>
  17 +<%= text_field 'organization_info', 'foundation_year', 'size' => 20 %></p>
  18 +
  19 +<p><label for="legal_form"><%= _('Legal Form') %></label><br/>
  20 +<%= text_field 'organization_info', 'legal_form', 'size' => 20 %></p>
  21 +
  22 +<p><label for="economic_activity"><%= _('Economic Activity') %></label><br/>
  23 +<%= text_field 'organization_info', 'economic_activity', 'size' => 20 %></p>
  24 +
  25 +<p><label for="management_information"><%= _('Management Information') %></label><br/>
  26 +<%= text_area 'organization_info', 'management_information', 'cols' => 40, 'rows' => 20 %></p>
  27 +
  28 +<p><label for="validation_entity"><%= _('Validation Entity') %></label><br/>
  29 +<%= select 'validation_entity', 'id', @validation_entities.map{|v| [v.name, v.id]}, :include_blank => true %></p>
  30 +
  31 +<p><label for="tag_list"><%= _('Tags') %></label><br/>
  32 +<%= text_field 'enterprise', 'tag_list', 'size' => 20 %></p>
... ...
app/views/enterprise_editor/edit.rhtml 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +<%= error_messages_for 'enterprise' %>
  2 +
  3 +<h2><%= _('Edit enterprise informations') %></h2>
  4 +
  5 +<% form_tag :action => 'update', :id => @enterprise do %>
  6 + <%= render :partial => 'form' %>
  7 +<p><%= submit_tag _('Update') %>
  8 +<%= link_to _('Cancel'), :action => 'index' %></p>
  9 +<% end %>
... ...
app/views/enterprise_editor/index.rhtml 0 → 100644
... ... @@ -0,0 +1,21 @@
  1 +<h3> <%= @profile.name %> </h3>
  2 +
  3 +<p> <%= _('Identifier: ') %> <%= @profile.identifier %> </p>
  4 +<p> <%= _('Address: ') %> <%= @profile.address %> </p>
  5 +<p> <%= _('Contact phone: ') %> <%= @profile.contact_phone %> </p>
  6 +<p> <%= _('Contact person: ') %> <%= @profile.organization_info.contact_person %> </p>
  7 +<p> <%= _('Acronym: ') %> <%= @profile.organization_info.acronym %> </p>
  8 +<p> <%= _('Foundation year: ') %> <%= @profile.organization_info.foundation_year %> </p>
  9 +<p> <%= _('Legal Form: ') %> <%= @profile.organization_info.legal_form %> </p>
  10 +<p> <%= _('Economic activity: ') %> <%= @profile.organization_info.economic_activity %> </p>
  11 +<p> <%= _('Management infomation: ') %> <%= @profile.organization_info.management_information %> </p>
  12 +<p> <%= _('Tags:') %> <%= @profile.tag_list %> </p>
  13 +
  14 +<%= link_to _('Edit enterprise'), :action => 'edit', :id => @profile %>
  15 +<%= help _('Change the information about the enterprise') %>
  16 +<%= link_to _('Delete enterprise'), :action => 'destroy', :id => @profile %>
  17 +<%= help _('Remove the enterprise from the system') %>
  18 +<%= link_to _('Affiliate'), :action => 'affiliate' unless @my_enterprises.include?(@profile) %>
  19 +<%= help _('Be a member of the enterprise') unless @my_enterprises.include?(@profile) %>
  20 +<%= link_to _('Activate'), :action => 'activate', :id => @profile if @my_pending_enterprises.include?(@profile) %>
  21 +<%= help _('Activate an approved enterprise') if @my_pending_enterprises.include?(@profile) %>
... ...
app/views/membership_editor/.index.rhtml.swp 0 → 100644
No preview for this file type
app/views/membership_editor/index.rhtml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +<h2><%= _('Listing memberships')%></h2>
  2 +
  3 +<ul>
  4 +<% @memberships.each do |m|%>
  5 + <li> <%= link_to m.name, '/' + m.identifier %> </li>
  6 +<% end %>
  7 +</ul>
... ...
app/views/profile_member/affiliate.rhtml
... ... @@ -1,7 +0,0 @@
1   -<h2> <%= @member.name %> </h2>
2   -
3   -<% form_tag( {:action => 'give_role'}, {:method => :post}) do %>
4   - <%= select_tag 'role', options_for_select(@roles.map{|r|[r.name,r.id]}) %>
5   - <%= hidden_field_tag 'person', current_user.person.id %>
6   - <%= submit_tag _('Affiliate') %>
7   -<% end %>
app/views/profile_member/index.rhtml
... ... @@ -1,9 +0,0 @@
1   -<h2> <%= _('Listing Members') %> </h2>
2   -
3   -<%= link_to _('Affiliate'), :action => 'affiliate', :id => current_user.person %>
4   -
5   -<ul>
6   - <% @members.each do |m| %>
7   - <li> <%= m.name %> </li>
8   - <% end %>
9   -</ul>
test/fixtures/roles.yml
... ... @@ -2,8 +2,11 @@
2 2 one:
3 3 id: 1
4 4 name: 'member'
5   - permissions: <%= [].to_yaml %>
  5 + permissions:
  6 + - post_content
6 7 two:
7 8 id: 2
8 9 name: 'owner'
9   - permissions: <%= [].to_yaml %>
  10 + permissions:
  11 + - manage_membership
  12 + - moderate_content
... ...
test/functional/enterprise_editor_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +require 'enterprise_editor_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class EnterpriseEditorController; def rescue_action(e) raise e end; end
  6 +
  7 +class EnterpriseEditorControllerTest < Test::Unit::TestCase
  8 + def setup
  9 + @controller = EnterpriseEditorController.new
  10 + @request = ActionController::TestRequest.new
  11 + @response = ActionController::TestResponse.new
  12 + end
  13 +
  14 + # Replace this with your real tests.
  15 + def test_truth
  16 + assert true
  17 + end
  18 +end
... ...
test/functional/membership_editor_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +require 'membership_editor_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class MembershipEditorController; def rescue_action(e) raise e end; end
  6 +
  7 +class MembershipEditorControllerTest < Test::Unit::TestCase
  8 + def setup
  9 + @controller = MembershipEditorController.new
  10 + @request = ActionController::TestRequest.new
  11 + @response = ActionController::TestResponse.new
  12 + end
  13 +
  14 + # Replace this with your real tests.
  15 + def test_truth
  16 + assert true
  17 + end
  18 +end
... ...
test/functional/profile_member_controller_test.rb
... ... @@ -1,18 +0,0 @@
1   -require File.dirname(__FILE__) + '/../test_helper'
2   -require 'profile_member_controller'
3   -
4   -# Re-raise errors caught by the controller.
5   -class ProfileMemberController; def rescue_action(e) raise e end; end
6   -
7   -class ProfileMemberControllerTest < Test::Unit::TestCase
8   - def setup
9   - @controller = ProfileMemberController.new
10   - @request = ActionController::TestRequest.new
11   - @response = ActionController::TestResponse.new
12   - end
13   -
14   - # Replace this with your real tests.
15   - def test_truth
16   - assert true
17   - end
18   -end
test/functional/profile_members_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +require 'profile_members_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class ProfileMembersController; def rescue_action(e) raise e end; end
  6 +
  7 +class ProfileMembersControllerTest < Test::Unit::TestCase
  8 + def setup
  9 + @controller = ProfileMembersController.new
  10 + @request = ActionController::TestRequest.new
  11 + @response = ActionController::TestResponse.new
  12 + end
  13 +
  14 + # Replace this with your real tests.
  15 + def test_truth
  16 + assert true
  17 + end
  18 +end
... ...
test/integration/approve_reject_enterprise_test.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +require "#{File.dirname(__FILE__)}/../test_helper"
  2 +
  3 +class ApproveRejectEnterpriseTest < ActionController::IntegrationTest
  4 + all_fixtures
  5 +
  6 + def test_approving
  7 + true
  8 + end
  9 +end
... ...
test/unit/person_test.rb
... ... @@ -64,4 +64,16 @@ class PersonTest &lt; Test::Unit::TestCase
64 64 assert_equal p.person_info, p.info
65 65 end
66 66  
  67 + should 'change the roles of the user' do
  68 + assert p = User.create(:login => 'jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person
  69 + assert e = Enterprise.create(:identifier => 'enter')
  70 + assert r1 = Role.create(:name => 'associate')
  71 + assert e.affiliate(p, r1)
  72 + assert r2 = Role.create(:name => 'partner')
  73 + assert p.define_roles([r2], e)
  74 + p = Person.find(p.id)
  75 + assert p.role_assignments.any? {|ra| ra.role == r2}
  76 + assert !p.role_assignments.any? {|ra| ra.role == r1}
  77 + end
  78 +
67 79 end
... ...