diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 2b7fcd3..981b8ea 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -51,4 +51,16 @@ class ApplicationController < ActionController::Base verify :method => :post, :only => actions, :redirect_to => redirect end + # Declares the +permission+ need to be able to access +action+. + # + # * +action+ must be a symbol or string with the name of the action + # * +permission+ must be a symbol or string naming the needed permission. + # * +target+ is the object over witch the user would need the specified permission. + def self.protect(actions, permission, target = nil) + before_filter :only => actions do |controller| + unless controller.send(:logged_in?) and controller.send(:current_user).person.has_permission?(permission, target) + controller.send(:render, {:file => 'app/views/shared/access_denied.rhtml', :layout => true}) + end + end + end end diff --git a/app/controllers/profile_admin/enterprise_controller.rb b/app/controllers/profile_admin/enterprise_controller.rb index ac6c894..0ddfebe 100644 --- a/app/controllers/profile_admin/enterprise_controller.rb +++ b/app/controllers/profile_admin/enterprise_controller.rb @@ -2,7 +2,8 @@ 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 @@ -10,6 +11,8 @@ class EnterpriseController < ProfileAdminController else redirect_to :action => 'list' end + @vitual_communities = VirtualCommunity.find(:all) + @validation_entities = Organization.find(:all) end # Lists all enterprises @@ -104,7 +107,7 @@ class EnterpriseController < ProfileAdminController if @enterprise.approve flash[:notice] = _('Enterprise successfuly approved') else - flash[:notice] = _('Failed to approve the enterprise') + flash[:notice] = _('Failed to approve the htmlenterprise') end redirect_to :action => 'index' end diff --git a/app/controllers/profile_admin/profile_member_controller.rb b/app/controllers/profile_admin/profile_member_controller.rb new file mode 100644 index 0000000..dfc293c --- /dev/null +++ b/app/controllers/profile_admin/profile_member_controller.rb @@ -0,0 +1,23 @@ +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/helpers/profile_member_helper.rb b/app/helpers/profile_member_helper.rb new file mode 100644 index 0000000..a175ed7 --- /dev/null +++ b/app/helpers/profile_member_helper.rb @@ -0,0 +1,2 @@ +module ProfileMemberHelper +end diff --git a/app/models/profile.rb b/app/models/profile.rb index d66b2e4..cdaecf9 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -103,6 +103,10 @@ class Profile < ActiveRecord::Base end def affiliate(person, role) - RoleAssignment.new(:person => person, :role => role, :resource => self).save + unless RoleAssignment.find(:first, :conditions => {:person_id => person, :role_id => role, :resource_id => self, :resource_type => self.class.base_class.name}) + RoleAssignment.new(:person => person, :role => role, :resource => self).save + else + false + end end end diff --git a/app/models/role.rb b/app/models/role.rb index 08e3e32..b83d1e3 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -5,6 +5,8 @@ class Role < ActiveRecord::Base 'edit_profile' => N_('Edit profile'), 'post_content' => N_('Post content'), 'destroy_profile' => N_('Destroy profile'), + 'manage_membership' => N_('Manage membership'), + 'moderate_content' => N_('Moderate content'), }, :system => { } @@ -35,4 +37,8 @@ class Role < ActiveRecord::Base def has_permission?(perm) permissions.include?(perm) end + + def has_kind?(kind) + permissions.any?{ |p| PERMISSIONS[kind][p] } + end end diff --git a/app/views/profile_member/affiliate.rhtml b/app/views/profile_member/affiliate.rhtml new file mode 100644 index 0000000..808326a --- /dev/null +++ b/app/views/profile_member/affiliate.rhtml @@ -0,0 +1,7 @@ +