Commit 406c009fba779e992020d9f7b50b772af1803a21
Exists in
staging
and in
42 other branches
Merge branch 'merge-requests/152'
Showing
12 changed files
with
128 additions
and
7 deletions
Show diff stats
app/controllers/admin/role_controller.rb
| ... | ... | @@ -5,6 +5,20 @@ class RoleController < AdminController |
| 5 | 5 | @roles = environment.roles.find(:all) |
| 6 | 6 | end |
| 7 | 7 | |
| 8 | + def new | |
| 9 | + @role = Role.new | |
| 10 | + end | |
| 11 | + | |
| 12 | + def create | |
| 13 | + @role = Role.new :name => params[:role][:name], :permissions => params[:role][:permissions], :environment => environment | |
| 14 | + if @role.save | |
| 15 | + redirect_to :action => 'show', :id => @role | |
| 16 | + else | |
| 17 | + session[:notice] = _('Failed to create role') | |
| 18 | + render :action => 'new' | |
| 19 | + end | |
| 20 | + end | |
| 21 | + | |
| 8 | 22 | def show |
| 9 | 23 | @role = environment.roles.find(params[:id]) |
| 10 | 24 | end | ... | ... |
app/models/profile.rb
| ... | ... | @@ -20,12 +20,16 @@ class Profile < ActiveRecord::Base |
| 20 | 20 | find_role('editor', env_id) |
| 21 | 21 | end |
| 22 | 22 | def self.organization_member_roles(env_id) |
| 23 | - [admin(env_id), moderator(env_id), member(env_id)] | |
| 23 | + all_roles(env_id).select{ |r| r.key.match(/^profile_/) unless r.key.blank? } | |
| 24 | 24 | end |
| 25 | 25 | def self.all_roles(env_id) |
| 26 | - [admin(env_id), member(env_id), moderator(env_id), owner(env_id), editor(env_id)] | |
| 26 | + Role.all :conditions => { :environment_id => env_id } | |
| 27 | + end | |
| 28 | + def self.method_missing(m, *args, &block) | |
| 29 | + role = find_role(m, args[0]) | |
| 30 | + return role unless role.nil? | |
| 31 | + super | |
| 27 | 32 | end |
| 28 | - | |
| 29 | 33 | private |
| 30 | 34 | def self.find_role(name, env_id) |
| 31 | 35 | ::Role.find_by_key_and_environment_id("profile_#{name}", env_id) | ... | ... |
app/views/profile_members/change_role.rhtml
| ... | ... | @@ -5,6 +5,11 @@ |
| 5 | 5 | <%= _('Roles:') %> <br> |
| 6 | 6 | <% @roles.each do |r| %> |
| 7 | 7 | <%= labelled_check_box(r.name, 'roles[]', r.id, @associations.map(&:role).include?(r) ) %><br/> |
| 8 | + <ul class="role-permissions"> | |
| 9 | + <% r.permissions.each do |p| %> | |
| 10 | + <li> <%= permission_name(p) %> </li> | |
| 11 | + <% end %> | |
| 12 | + </ul> | |
| 8 | 13 | <% end %> |
| 9 | 14 | <%= hidden_field_tag 'person', @member.id %> |
| 10 | 15 | ... | ... |
app/views/role/_form.rhtml
| ... | ... | @@ -6,9 +6,10 @@ |
| 6 | 6 | |
| 7 | 7 | <%= required f.text_field(:name) %> |
| 8 | 8 | |
| 9 | - <%= _('Permissions:') %> <br> | |
| 9 | + <p><%= _('Permissions:') %><p> | |
| 10 | 10 | <% permissions.keys.each do |p| %> |
| 11 | - <%= labelled_check_box(permission_name(p), "role[permissions][]", p, role.has_permission?(p)) %> <br/> | |
| 11 | + <%= check_box_tag("role[permissions][]", p, role.has_permission?(p), { :id => p }) %> | |
| 12 | + <%= content_tag(:label, permission_name(p), { :for => p }) %><br/> | |
| 12 | 13 | <% end %> |
| 13 | 14 | |
| 14 | 15 | <% button_bar do %> | ... | ... |
app/views/role/index.rhtml
| ... | ... | @@ -18,5 +18,6 @@ |
| 18 | 18 | </table> |
| 19 | 19 | |
| 20 | 20 | <% button_bar do %> |
| 21 | - <%= button :back, _('Back'), :controller => 'admin_panel' %> | |
| 21 | + <%= button :add, _('Create a new role'), :action => 'new' %> | |
| 22 | + <%= button :back, _('Back to admin panel'), :controller => 'admin_panel' %> | |
| 22 | 23 | <% end %> | ... | ... |
app/views/role/show.rhtml
| ... | ... | @@ -0,0 +1,30 @@ |
| 1 | +Feature: manage roles | |
| 2 | + As an environment admin | |
| 3 | + I want to create and edit roles | |
| 4 | + | |
| 5 | + Scenario: create new role | |
| 6 | + Given I am logged in as admin | |
| 7 | + And I go to the environment control panel | |
| 8 | + And I follow "Manage User roles" | |
| 9 | + Then I should not see "My new role" | |
| 10 | + And I follow "Create a new role" | |
| 11 | + And I fill in "Name" with "My new role" | |
| 12 | + And I check "Publish content" | |
| 13 | + And I press "Create role" | |
| 14 | + And I go to the environment control panel | |
| 15 | + And I follow "Manage User roles" | |
| 16 | + Then I should see "My new role" | |
| 17 | + | |
| 18 | + Scenario: edit a role | |
| 19 | + Given I am logged in as admin | |
| 20 | + And I go to the environment control panel | |
| 21 | + And I follow "Manage User roles" | |
| 22 | + Then I should not see "My new role" | |
| 23 | + And I follow "Profile Administrator" | |
| 24 | + And I follow "Edit" | |
| 25 | + And I fill in "Name" with "My new role" | |
| 26 | + And I press "Save changes" | |
| 27 | + And I go to the environment control panel | |
| 28 | + And I follow "Manage User roles" | |
| 29 | + Then I should see "My new role" | |
| 30 | + And I should not see "Profile Administrator" | ... | ... |
test/functional/role_controller_test.rb
| ... | ... | @@ -59,6 +59,26 @@ class RoleControllerTest < ActionController::TestCase |
| 59 | 59 | assert_not_nil session[:notice] |
| 60 | 60 | end |
| 61 | 61 | |
| 62 | + def test_should_see_new_role_page | |
| 63 | + get 'new' | |
| 64 | + assert_response :success | |
| 65 | + assert_not_nil assigns(:role) | |
| 66 | + end | |
| 67 | + | |
| 68 | + def test_should_create_new_role | |
| 69 | + assert_difference Role, :count do | |
| 70 | + post 'create', :role => { :name => 'Test Role', :permissions => ["test"] } | |
| 71 | + end | |
| 72 | + assert_redirected_to :action => 'show', :id => Role.last.id | |
| 73 | + end | |
| 74 | + | |
| 75 | + def test_should_not_create_new_role | |
| 76 | + assert_no_difference Role, :count do | |
| 77 | + post 'create', :role => { } | |
| 78 | + end | |
| 79 | + assert_template :new | |
| 80 | + end | |
| 81 | + | |
| 62 | 82 | should 'not crash when editing role with no permissions' do |
| 63 | 83 | role = Role.create!(:name => 'test_role', :environment => Environment.default) |
| 64 | 84 | ... | ... |
test/unit/profile_test.rb
| ... | ... | @@ -1740,6 +1740,30 @@ class ProfileTest < ActiveSupport::TestCase |
| 1740 | 1740 | assert_includes Profile.communities, child |
| 1741 | 1741 | end |
| 1742 | 1742 | |
| 1743 | + should 'get organization roles' do | |
| 1744 | + env = fast_create(Environment) | |
| 1745 | + roles = %w(foo bar profile_foo profile_bar).map{ |r| Role.create!(:name => r, :key => r, :environment_id => env.id, :permissions => ["some"]) } | |
| 1746 | + Role.create! :name => 'test', :key => 'profile_test', :environment_id => env.id + 1 | |
| 1747 | + Profile::Roles.expects(:all_roles).returns(roles) | |
| 1748 | + assert_equal roles[2..3], Profile::Roles.organization_member_roles(env.id) | |
| 1749 | + end | |
| 1750 | + | |
| 1751 | + should 'get all roles' do | |
| 1752 | + env = fast_create(Environment) | |
| 1753 | + roles = %w(foo bar profile_foo profile_bar).map{ |r| Role.create!(:name => r, :environment_id => env.id, :permissions => ["some"]) } | |
| 1754 | + Role.create! :name => 'test', :environment_id => env.id + 1 | |
| 1755 | + assert_equal roles, Profile::Roles.all_roles(env.id) | |
| 1756 | + end | |
| 1757 | + | |
| 1758 | + should 'define method for role' do | |
| 1759 | + env = fast_create(Environment) | |
| 1760 | + r = Role.create! :name => 'Test Role', :environment_id => env.id | |
| 1761 | + assert_equal r, Profile::Roles.test_role(env.id) | |
| 1762 | + assert_raise NoMethodError do | |
| 1763 | + Profile::Roles.invalid_role(env.id) | |
| 1764 | + end | |
| 1765 | + end | |
| 1766 | + | |
| 1743 | 1767 | private |
| 1744 | 1768 | |
| 1745 | 1769 | def assert_invalid_identifier(id) | ... | ... |
vendor/plugins/access_control/lib/role.rb
| ... | ... | @@ -6,6 +6,7 @@ class Role < ActiveRecord::Base |
| 6 | 6 | validates_presence_of :name |
| 7 | 7 | validates_uniqueness_of :name, :scope => :environment_id |
| 8 | 8 | validates_uniqueness_of :key, :if => lambda { |role| !role.key.blank? }, :scope => :environment_id |
| 9 | + before_validation_on_create :create_key | |
| 9 | 10 | |
| 10 | 11 | def initialize(*args) |
| 11 | 12 | super(*args) |
| ... | ... | @@ -54,4 +55,9 @@ class Role < ActiveRecord::Base |
| 54 | 55 | def perms |
| 55 | 56 | ActiveRecord::Base::PERMISSIONS |
| 56 | 57 | end |
| 58 | + | |
| 59 | + private | |
| 60 | + def create_key | |
| 61 | + self.key = 'profile_' + self.name.gsub(' ', '_').gsub(/[^a-zA-Z0-9_]/, '').downcase if self.key.blank? && !self.name.blank? | |
| 62 | + end | |
| 57 | 63 | end | ... | ... |
vendor/plugins/access_control/test/role_test.rb
| ... | ... | @@ -107,4 +107,14 @@ class RoleTest < Test::Unit::TestCase |
| 107 | 107 | assert !RoleAssignment.exists?(ra.id) |
| 108 | 108 | end |
| 109 | 109 | |
| 110 | + def test_should_define_key_for_role_if_key_not_present | |
| 111 | + r = Role.create! :name => 'Test Role' | |
| 112 | + assert_equal 'profile_test_role', r.key | |
| 113 | + end | |
| 114 | + | |
| 115 | + def test_should_not_define_key_for_role_if_key_present | |
| 116 | + r = Role.create! :name => 'Test Role', :key => 'foo' | |
| 117 | + assert_equal 'foo', r.key | |
| 118 | + end | |
| 119 | + | |
| 110 | 120 | end | ... | ... |