diff --git a/app/controllers/profile_admin/enterprise_controller.rb b/app/controllers/profile_admin/enterprise_controller.rb deleted file mode 100644 index 82aa0d4..0000000 --- a/app/controllers/profile_admin/enterprise_controller.rb +++ /dev/null @@ -1,147 +0,0 @@ -# Manage enterprises by providing an interface to register, activate and manage them -class EnterpriseController < ProfileAdminController - - before_filter :logon, :my_enterprises - protect([:edit, :update, :activate, :destroy], 'edit_enterprise', @profile) - - # Redirects to show if there is only one action and to list otherwise - def index - if @person.enterprises.size == 1 - redirect_to :action => 'show', :id => @person.enterprises[0] - else - redirect_to :action => 'list' - end - @vitual_communities = Environment.find(:all) - @validation_entities = Organization.find(:all) - end - - # Lists all enterprises - def list - @enterprises = Enterprise.find(:all) - @person.enterprises - end - - # Show details about an enterprise - def show - @enterprise = Enterprise.find(params[:id]) - end - - # Make a form to the creation of an eterprise - def register_form - @enterprise = Enterprise.new() - @vitual_communities = Environment.find(:all) - @validation_entities = Organization.find(:all) - end - - # Saves the new created enterprise - def register - @enterprise = Enterprise.new(params[:enterprise]) - @enterprise.organization_info = OrganizationInfo.new(params[:organization]) - if @enterprise.save - @enterprise.people << @person - flash[:notice] = _('The enterprise was succesfully created, the validation entity will cotact you as soon as your enterprise is approved') - redirect_to :action => 'index' - else - flash[:notice] = _('Enterprise was not created') - @vitual_communities = Environment.find(:all) - @validation_entities = Organization.find(:all) - render :action => 'register_form' - end - end - - # Provides an interface to editing the enterprise details - def edit - @enterprise = @person.enterprises(:id => params[:id])[0] - @validation_entities = Organization.find(:all) - [@enterprise] - end - - # Saves the changes made in an enterprise - def update - @enterprise = @person.enterprises(:id => params[:id])[0] - if @enterprise.update_attributes(params[:enterprise]) && @enterprise.organization_info.update_attributes(params[:organization_info]) - redirect_to :action => 'index' - else - flash[:notice] = _('Could not update the enterprise') - @validation_entities = Organization.find(:all) - [@enterprise] - render :action => 'edit' - end - end - - # Make the current user a new member of the enterprise - def affiliate - @enterprise = Enterprise.find(params[:id]) - member_role = Role.find_by_name('member') || Role.create(:name => 'member') - @enterprise.affiliate(@person,member_role) - redirect_to :action => 'index' - end - - # Elimitates the enterprise of the system - def destroy - @enterprise = @person.enterprises(:id => params[:id])[0] - if @enterprise - @enterprise.destroy - else - flash[:notice] = 'Can destroy only your enterprises' - end - redirect_to :action => 'index' - end - - # Search enterprises by name or tags - def search - @tagged_enterprises = Enterprise.search(params[:query]) - end - - # Activate a validated enterprise - def activate - @enterprise = Enterprise.find(params[:id]) - if @enterprise.activate - flash[:notice] = _('Enterprise successfuly activacted') - else - flash[:notice] = _('Failed to activate the enterprise') - end - redirect_to :action => 'index' - end - - # Validates an eterprise - def approve - @enterprise = Enterprise.find(params[:id]) - if @enterprise.approve - flash[:notice] = _('Enterprise successfuly approved') - else - flash[:notice] = _('Failed to approve the htmlenterprise') - end - redirect_to :action => 'index' - end - - # Rejects an enterprise - def reject - @enterprise = Enterprise.find(params[:id]) - if @enterprise.reject - flash[:notice] = _('Enterprise successfuly rejected') - else - flash[:notice] = _('Failed to reject the enterprise') - end - redirect_to :action => 'index' - end - - - protected - - # Make sure that the user is logged before access this controller - def logon - if logged_in? - @user = current_user - @person = @user.person - else - redirect_to :controller => 'account' unless logged_in? - end - end - - # Initializes some variables to contain the enterprises of the current user - def my_enterprises - if logged_in? - @my_active_enterprises = @person.active_enterprises - @my_pending_enterprises = @person.pending_enterprises - @my_enterprises = @person.enterprises - end - end -end diff --git a/app/controllers/profile_admin/enterprise_editor_controller.rb b/app/controllers/profile_admin/enterprise_editor_controller.rb new file mode 100644 index 0000000..7e06e5a --- /dev/null +++ b/app/controllers/profile_admin/enterprise_editor_controller.rb @@ -0,0 +1,67 @@ +class EnterpriseEditorController < ProfileAdminController + + before_filter :logon, :check_enterprise + + # Show details about an enterprise + def index + @my_enterprises = @person.enterprises + @my_pending_enterprises = @person.pending_enterprises + end + + # Provides an interface to editing the enterprise details + def edit + @validation_entities = Organization.find(:all) - [@enterprise] + end + + # Saves the changes made in an enterprise + def update + if @enterprise.update_attributes(params[:enterprise]) && @enterprise.organization_info.update_attributes(params[:organization_info]) + redirect_to :action => 'index' + else + flash[:notice] = _('Could not update the enterprise') + @validation_entities = Organization.find(:all) - [@enterprise] + render :action => 'edit' + end + end + + # Make the current user a new member of the enterprise + def affiliate + member_role = Role.find_by_name('member') || Role.create(:name => 'member') + @enterprise.affiliate(@person,member_role) + redirect_to :action => 'index' + end + + # Elimitates the enterprise of the system + def destroy + if @enterprise + @enterprise.destroy + else + flash[:notice] = 'Can destroy only your enterprises' + end + redirect_to :action => 'index' + end + + # Activate a validated enterprise + def activate + if @enterprise.activate + flash[:notice] = _('Enterprise successfuly activacted') + else + flash[:notice] = _('Failed to activate the enterprise') + end + redirect_to :action => 'index' + end + + protected + + def logon + if logged_in? + @user = current_user + @person = @user.person + end + end + + def check_enterprise + raise 'It\'s not an enterprise' unless @profile.is_a?(Enterprise) + @enterprise = @profile + end +end diff --git a/app/controllers/profile_admin/membership_editor_controller.rb b/app/controllers/profile_admin/membership_editor_controller.rb new file mode 100644 index 0000000..6a9bde2 --- /dev/null +++ b/app/controllers/profile_admin/membership_editor_controller.rb @@ -0,0 +1,6 @@ +class MembershipEditorController < ProfileAdminController + + def index + @memberships = Profile.find(:all, :include => 'role_assignments', :conditions => ['role_assignments.person_id = ?', current_user.person.id]) + end +end diff --git a/app/controllers/profile_admin/profile_member_controller.rb b/app/controllers/profile_admin/profile_member_controller.rb deleted file mode 100644 index dfc293c..0000000 --- a/app/controllers/profile_admin/profile_member_controller.rb +++ /dev/null @@ -1,23 +0,0 @@ -class ProfileMemberController < ApplicationController - - def index - @members = @profile.people - end - - def affiliate - @member = Person.find(params[:id]) - @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) } - end - - def give_role - @person = Person.find(params[:person]) - @role = Role.find(params[:role]) - if @profile.affiliate(@person, @role) - redirect_to :action => 'index' - else - @member = Person.find(params[:person]) - @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) } - render :action => 'affiliate' - end - end -end diff --git a/app/controllers/profile_admin/profile_members_controller.rb b/app/controllers/profile_admin/profile_members_controller.rb new file mode 100644 index 0000000..075e6f0 --- /dev/null +++ b/app/controllers/profile_admin/profile_members_controller.rb @@ -0,0 +1,60 @@ +class ProfileMembersController < ProfileAdminController + + def index + @members = profile.people.uniq + end + + def change_roles + @member = Person.find(params[:id]) + @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) } + end + + def update_roles + @roles = Role.find(params[:roles]) + @person = Person.find(params[:person]) + if @person.define_roles(@roles, profile) + flash[:notice] = 'Roles successfuly updated' + else + flash[:notice] = 'Couldn\'t change the roles' + end + redirect_to :action => :index + end + + def change_role + @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) } + @member = Person.find(params[:id]) + @associations = RoleAssignment.find(:all, :conditions => {:person_id => @member, :resource_id => @profile, :resource_type => @profile.class.base_class.name}) + end + + def add_role + @person = Person.find(params[:person]) + @role = Role.find(params[:role]) + if @profile.affiliate(@person, @role) + redirect_to :action => 'index' + else + @member = Person.find(params[:person]) + @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) } + render :action => 'affiliate' + end + end + + def remove_role + @association = RoleAssignment.find(params[:id]) + if @association.destroy + flash[:notice] = 'Member succefully unassociated' + else + flash[:notice] = 'Failed to unassociate member' + end + redirect_to :aciton => 'index' + end + + def unassociate + @association = RoleAssignment.find(params[:id]) + if @association.destroy + flash[:notice] = 'Member succefully unassociated' + else + flash[:notice] = 'Failed to unassociate member' + end + redirect_to :aciton => 'index' + end +end diff --git a/app/helpers/enterprise_editor_helper.rb b/app/helpers/enterprise_editor_helper.rb new file mode 100644 index 0000000..606cf04 --- /dev/null +++ b/app/helpers/enterprise_editor_helper.rb @@ -0,0 +1,2 @@ +module EnterpriseEditorHelper +end diff --git a/app/helpers/membership_editor_helper.rb b/app/helpers/membership_editor_helper.rb new file mode 100644 index 0000000..13de47e --- /dev/null +++ b/app/helpers/membership_editor_helper.rb @@ -0,0 +1,2 @@ +module MembershipEditorHelper +end diff --git a/app/models/person.rb b/app/models/person.rb index 247418c..3e4ff2c 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -16,6 +16,14 @@ class Person < Profile role_assignments.any? {|ra| ra.has_permission?(perm, res)} end + def define_roles(roles, resource) + associations = RoleAssignment.find(:all, :conditions => {:resource_id => resource.id, :resource_type => resource.class.base_class.name, :person_id => self.id }) + roles_add = roles - associations.map(&:role) + roles_remove = associations.map(&:role) - roles + associations.each { |a| a.destroy if roles_remove.include?(a.role) } + 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) } + end + def self.conditions_for_profiles(conditions, person) new_conditions = sanitize_sql(['role_assignments.person_id = ?', person]) new_conditions << ' AND ' + sanitize_sql(conditions) unless conditions.blank? diff --git a/app/views/enterprise_editor/_enterprise.rhtml b/app/views/enterprise_editor/_enterprise.rhtml new file mode 100644 index 0000000..f2f72ec --- /dev/null +++ b/app/views/enterprise_editor/_enterprise.rhtml @@ -0,0 +1,17 @@ +
  • +<%= link_to enterprise.name, :action => 'show', :id => enterprise %> +<%= link_to _('Edit'), :action => 'edit', :id => enterprise %> +<%= help _('Change the infomation about the enterprise') %> +<%= link_to _('Delete'), :action => 'destroy', :id => enterprise %> +<%= help _('Remove the enterprise from the system') %> +<%= link_to _('Affiliate'), :action => 'affiliate', :id => enterprise unless @my_enterprises.include?(enterprise) %> +<%= help _('Be a member of the enterprise') unless @my_enterprises.include?(enterprise) %> +<%= link_to _('Activate'), :action => 'activate', :id => enterprise unless enterprise.active %> +<%= help _('Activate the profile of an approved enterprise') unless enterprise.active %> +<% unless enterprise.approved? %> + <%= link_to _('Approve'), :action => 'approve', :id => enterprise %> + <%= help _('Approve a submitted enterprise profile') %> + <%= link_to _('Reject'), :action => 'reject', :id => enterprise %> + <%= help _('Reject a submitted enterprise profile') %> +<% end %> +
  • diff --git a/app/views/enterprise_editor/_form.rhtml b/app/views/enterprise_editor/_form.rhtml new file mode 100644 index 0000000..c435e07 --- /dev/null +++ b/app/views/enterprise_editor/_form.rhtml @@ -0,0 +1,32 @@ +


    +<%= text_field 'enterprise', 'name', 'size' => 20 %>

    + +


    +<%= text_field 'enterprise', 'address', 'size' => 50 %>

    + +


    +<%= text_field 'enterprise', 'contact_phone', 'size' => 20 %>

    + +


    +<%= text_field 'organization_info', 'contact_person', 'size' => 20 %>

    + +


    +<%= text_field 'organization_info', 'acronym', 'size' => 20 %>

    + +


    +<%= text_field 'organization_info', 'foundation_year', 'size' => 20 %>

    + +


    +<%= text_field 'organization_info', 'legal_form', 'size' => 20 %>

    + +


    +<%= text_field 'organization_info', 'economic_activity', 'size' => 20 %>

    + +


    +<%= text_area 'organization_info', 'management_information', 'cols' => 40, 'rows' => 20 %>

    + +


    +<%= select 'validation_entity', 'id', @validation_entities.map{|v| [v.name, v.id]}, :include_blank => true %>

    + +


    +<%= text_field 'enterprise', 'tag_list', 'size' => 20 %>

    diff --git a/app/views/enterprise_editor/edit.rhtml b/app/views/enterprise_editor/edit.rhtml new file mode 100644 index 0000000..b238d4a --- /dev/null +++ b/app/views/enterprise_editor/edit.rhtml @@ -0,0 +1,9 @@ +<%= error_messages_for 'enterprise' %> + +

    <%= _('Edit enterprise informations') %>

    + +<% form_tag :action => 'update', :id => @enterprise do %> + <%= render :partial => 'form' %> +

    <%= submit_tag _('Update') %> +<%= link_to _('Cancel'), :action => 'index' %>

    +<% end %> diff --git a/app/views/enterprise_editor/index.rhtml b/app/views/enterprise_editor/index.rhtml new file mode 100644 index 0000000..2d05d56 --- /dev/null +++ b/app/views/enterprise_editor/index.rhtml @@ -0,0 +1,21 @@ +

    <%= @profile.name %>

    + +

    <%= _('Identifier: ') %> <%= @profile.identifier %>

    +

    <%= _('Address: ') %> <%= @profile.address %>

    +

    <%= _('Contact phone: ') %> <%= @profile.contact_phone %>

    +

    <%= _('Contact person: ') %> <%= @profile.organization_info.contact_person %>

    +

    <%= _('Acronym: ') %> <%= @profile.organization_info.acronym %>

    +

    <%= _('Foundation year: ') %> <%= @profile.organization_info.foundation_year %>

    +

    <%= _('Legal Form: ') %> <%= @profile.organization_info.legal_form %>

    +

    <%= _('Economic activity: ') %> <%= @profile.organization_info.economic_activity %>

    +

    <%= _('Management infomation: ') %> <%= @profile.organization_info.management_information %>

    +

    <%= _('Tags:') %> <%= @profile.tag_list %>

    + +<%= link_to _('Edit enterprise'), :action => 'edit', :id => @profile %> +<%= help _('Change the information about the enterprise') %> +<%= link_to _('Delete enterprise'), :action => 'destroy', :id => @profile %> +<%= help _('Remove the enterprise from the system') %> +<%= link_to _('Affiliate'), :action => 'affiliate' unless @my_enterprises.include?(@profile) %> +<%= help _('Be a member of the enterprise') unless @my_enterprises.include?(@profile) %> +<%= link_to _('Activate'), :action => 'activate', :id => @profile if @my_pending_enterprises.include?(@profile) %> +<%= help _('Activate an approved enterprise') if @my_pending_enterprises.include?(@profile) %> diff --git a/app/views/membership_editor/.index.rhtml.swp b/app/views/membership_editor/.index.rhtml.swp new file mode 100644 index 0000000..2ac1380 Binary files /dev/null and b/app/views/membership_editor/.index.rhtml.swp differ diff --git a/app/views/membership_editor/index.rhtml b/app/views/membership_editor/index.rhtml new file mode 100644 index 0000000..b57b6c9 --- /dev/null +++ b/app/views/membership_editor/index.rhtml @@ -0,0 +1,7 @@ +

    <%= _('Listing memberships')%>

    + + diff --git a/app/views/profile_member/affiliate.rhtml b/app/views/profile_member/affiliate.rhtml deleted file mode 100644 index 808326a..0000000 --- a/app/views/profile_member/affiliate.rhtml +++ /dev/null @@ -1,7 +0,0 @@ -

    <%= @member.name %>

    - -<% form_tag( {:action => 'give_role'}, {:method => :post}) do %> - <%= select_tag 'role', options_for_select(@roles.map{|r|[r.name,r.id]}) %> - <%= hidden_field_tag 'person', current_user.person.id %> - <%= submit_tag _('Affiliate') %> -<% end %> diff --git a/app/views/profile_member/index.rhtml b/app/views/profile_member/index.rhtml deleted file mode 100644 index a52a2c4..0000000 --- a/app/views/profile_member/index.rhtml +++ /dev/null @@ -1,9 +0,0 @@ -

    <%= _('Listing Members') %>

    - -<%= link_to _('Affiliate'), :action => 'affiliate', :id => current_user.person %> - - diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index 1ed06d3..c9901a9 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -2,8 +2,11 @@ one: id: 1 name: 'member' - permissions: <%= [].to_yaml %> + permissions: + - post_content two: id: 2 name: 'owner' - permissions: <%= [].to_yaml %> + permissions: + - manage_membership + - moderate_content diff --git a/test/functional/enterprise_editor_controller_test.rb b/test/functional/enterprise_editor_controller_test.rb new file mode 100644 index 0000000..0dfbf5f --- /dev/null +++ b/test/functional/enterprise_editor_controller_test.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'enterprise_editor_controller' + +# Re-raise errors caught by the controller. +class EnterpriseEditorController; def rescue_action(e) raise e end; end + +class EnterpriseEditorControllerTest < Test::Unit::TestCase + def setup + @controller = EnterpriseEditorController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + # Replace this with your real tests. + def test_truth + assert true + end +end diff --git a/test/functional/membership_editor_controller_test.rb b/test/functional/membership_editor_controller_test.rb new file mode 100644 index 0000000..bb409b2 --- /dev/null +++ b/test/functional/membership_editor_controller_test.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'membership_editor_controller' + +# Re-raise errors caught by the controller. +class MembershipEditorController; def rescue_action(e) raise e end; end + +class MembershipEditorControllerTest < Test::Unit::TestCase + def setup + @controller = MembershipEditorController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + # Replace this with your real tests. + def test_truth + assert true + end +end diff --git a/test/functional/profile_member_controller_test.rb b/test/functional/profile_member_controller_test.rb deleted file mode 100644 index 348c4f4..0000000 --- a/test/functional/profile_member_controller_test.rb +++ /dev/null @@ -1,18 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' -require 'profile_member_controller' - -# Re-raise errors caught by the controller. -class ProfileMemberController; def rescue_action(e) raise e end; end - -class ProfileMemberControllerTest < Test::Unit::TestCase - def setup - @controller = ProfileMemberController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end - - # Replace this with your real tests. - def test_truth - assert true - end -end diff --git a/test/functional/profile_members_controller_test.rb b/test/functional/profile_members_controller_test.rb new file mode 100644 index 0000000..0eb970b --- /dev/null +++ b/test/functional/profile_members_controller_test.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'profile_members_controller' + +# Re-raise errors caught by the controller. +class ProfileMembersController; def rescue_action(e) raise e end; end + +class ProfileMembersControllerTest < Test::Unit::TestCase + def setup + @controller = ProfileMembersController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + # Replace this with your real tests. + def test_truth + assert true + end +end diff --git a/test/integration/approve_reject_enterprise_test.rb b/test/integration/approve_reject_enterprise_test.rb new file mode 100644 index 0000000..83f0c67 --- /dev/null +++ b/test/integration/approve_reject_enterprise_test.rb @@ -0,0 +1,9 @@ +require "#{File.dirname(__FILE__)}/../test_helper" + +class ApproveRejectEnterpriseTest < ActionController::IntegrationTest + all_fixtures + + def test_approving + true + end +end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index e2b4e11..b448456 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -64,4 +64,16 @@ class PersonTest < Test::Unit::TestCase assert_equal p.person_info, p.info end + should 'change the roles of the user' do + assert p = User.create(:login => 'jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person + assert e = Enterprise.create(:identifier => 'enter') + assert r1 = Role.create(:name => 'associate') + assert e.affiliate(p, r1) + assert r2 = Role.create(:name => 'partner') + assert p.define_roles([r2], e) + p = Person.find(p.id) + assert p.role_assignments.any? {|ra| ra.role == r2} + assert !p.role_assignments.any? {|ra| ra.role == r1} + end + end -- libgit2 0.21.2