Commit 77ffeee2c26eb6a8c1c89bb81d112466a44498ea
Committed by
André Guedes
1 parent
12d04695
Exists in
master
and in
26 other branches
Added custom roles management for organizations
Signed-off-by: Hebert Douglas <hebertdougl@gmail.com> Signed-off-by: Filipe Ribeiro <firibeiro77@live.com> Signed-off-by: André Bernardes <andrebsguedes@gmail.com>
Showing
15 changed files
with
190 additions
and
6 deletions
Show diff stats
app/controllers/admin/role_controller.rb
| @@ -2,7 +2,7 @@ class RoleController < AdminController | @@ -2,7 +2,7 @@ class RoleController < AdminController | ||
| 2 | protect 'manage_environment_roles', :environment | 2 | protect 'manage_environment_roles', :environment |
| 3 | 3 | ||
| 4 | def index | 4 | def index |
| 5 | - @roles = environment.roles.find(:all) | 5 | + @roles = environment.roles.find(:all, :conditions => {:profile_id => nil}) |
| 6 | end | 6 | end |
| 7 | 7 | ||
| 8 | def new | 8 | def new |
app/controllers/my_profile/profile_members_controller.rb
| @@ -58,6 +58,7 @@ class ProfileMembersController < MyProfileController | @@ -58,6 +58,7 @@ class ProfileMembersController < MyProfileController | ||
| 58 | 58 | ||
| 59 | def change_role | 59 | def change_role |
| 60 | @roles = Profile::Roles.organization_member_roles(environment.id) | 60 | @roles = Profile::Roles.organization_member_roles(environment.id) |
| 61 | + @custom_roles = Profile::Roles.organization_custom_roles(environment.id, profile.id) | ||
| 61 | begin | 62 | begin |
| 62 | @member = profile.members.find(params[:id]) | 63 | @member = profile.members.find(params[:id]) |
| 63 | rescue ActiveRecord::RecordNotFound | 64 | rescue ActiveRecord::RecordNotFound |
| @@ -0,0 +1,65 @@ | @@ -0,0 +1,65 @@ | ||
| 1 | +class ProfileRolesController < MyProfileController | ||
| 2 | + | ||
| 3 | + include RoleHelper | ||
| 4 | + | ||
| 5 | + def index | ||
| 6 | + @roles = environment.roles.find(:all, :conditions => {:profile_id => profile.id} ) | ||
| 7 | + end | ||
| 8 | + | ||
| 9 | + def new | ||
| 10 | + @role = Role.new | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + def create | ||
| 14 | + @role = Role.create({:name => params[:role][:name], :permissions => params[:role][:permissions], :profile_id => profile.id, :environment => environment }, :without_protection => true) | ||
| 15 | + if @role.save | ||
| 16 | + redirect_to :action => 'show', :id => @role | ||
| 17 | + else | ||
| 18 | + session[:notice] = _('Failed to create role') | ||
| 19 | + render :action => 'new' | ||
| 20 | + end | ||
| 21 | + end | ||
| 22 | + | ||
| 23 | + def show | ||
| 24 | + @role = environment.roles.find(params[:id]) | ||
| 25 | + end | ||
| 26 | + | ||
| 27 | + def edit | ||
| 28 | + @role = environment.roles.find(params[:id]) | ||
| 29 | + end | ||
| 30 | + | ||
| 31 | + def destroy | ||
| 32 | + @role = environment.roles.find(params[:id]) | ||
| 33 | + @members = profile.members_by_role(@role) | ||
| 34 | + @roles_list = Profile::Roles.organization_all_roles(environment.id, profile.id) | ||
| 35 | + @roles_list.delete(@role) | ||
| 36 | + end | ||
| 37 | + | ||
| 38 | + def remove | ||
| 39 | + @role = environment.roles.find(params[:id]) | ||
| 40 | + @members = profile.members_by_role(@role) | ||
| 41 | + new_roles = params[:roles] ? environment.roles.find(params[:roles].select{|r|!r.to_i.zero?}) : [] | ||
| 42 | + @members.each do |person| | ||
| 43 | + member_roles = person.find_roles(profile).map(&:role) + new_roles | ||
| 44 | + person.define_roles(member_roles, profile) | ||
| 45 | + end | ||
| 46 | + if @role.destroy | ||
| 47 | + session[:notice] = _('Role successfuly removed!') | ||
| 48 | + else | ||
| 49 | + session[:notice] = _('Failed to remove role!') | ||
| 50 | + end | ||
| 51 | + redirect_to :action => 'index' | ||
| 52 | + end | ||
| 53 | + | ||
| 54 | + def update | ||
| 55 | + @role = environment.roles.find(params[:id]) | ||
| 56 | + if @role.update_attributes(params[:role]) | ||
| 57 | + redirect_to :action => 'show', :id => @role | ||
| 58 | + else | ||
| 59 | + session[:notice] = _('Failed to edit role') | ||
| 60 | + render :action => 'edit' | ||
| 61 | + end | ||
| 62 | + end | ||
| 63 | + | ||
| 64 | + | ||
| 65 | +end |
app/models/organization.rb
| @@ -29,6 +29,8 @@ class Organization < Profile | @@ -29,6 +29,8 @@ class Organization < Profile | ||
| 29 | 29 | ||
| 30 | has_many :mailings, :class_name => 'OrganizationMailing', :foreign_key => :source_id, :as => 'source' | 30 | has_many :mailings, :class_name => 'OrganizationMailing', :foreign_key => :source_id, :as => 'source' |
| 31 | 31 | ||
| 32 | + has_many :custom_roles, :class_name => 'Role', :foreign_key => :profile_id | ||
| 33 | + | ||
| 32 | scope :more_popular, :order => 'members_count DESC' | 34 | scope :more_popular, :order => 'members_count DESC' |
| 33 | 35 | ||
| 34 | validate :presence_of_required_fieds, :unless => :is_template | 36 | validate :presence_of_required_fieds, :unless => :is_template |
app/models/profile.rb
| @@ -43,10 +43,16 @@ class Profile < ActiveRecord::Base | @@ -43,10 +43,16 @@ class Profile < ActiveRecord::Base | ||
| 43 | find_role('editor', env_id) | 43 | find_role('editor', env_id) |
| 44 | end | 44 | end |
| 45 | def self.organization_member_roles(env_id) | 45 | def self.organization_member_roles(env_id) |
| 46 | - all_roles(env_id).select{ |r| r.key.match(/^profile_/) unless r.key.blank? } | 46 | + all_roles(env_id, nil).select{ |r| r.key.match(/^profile_/) unless r.key.blank? } |
| 47 | end | 47 | end |
| 48 | - def self.all_roles(env_id) | ||
| 49 | - Role.all :conditions => { :environment_id => env_id } | 48 | + def self.organization_custom_roles(env_id, profile_id) |
| 49 | + all_roles(env_id, profile_id).select{ |r| r.key.match(/^profile_/) unless r.key.blank? } | ||
| 50 | + end | ||
| 51 | + def self.organization_all_roles(env_id, profile_id) | ||
| 52 | + self.organization_member_roles(env_id) + self.organization_custom_roles(env_id, profile_id) | ||
| 53 | + end | ||
| 54 | + def self.all_roles(env_id, profile_id) | ||
| 55 | + Role.all :conditions => { :profile_id => profile_id, :environment_id => env_id } | ||
| 50 | end | 56 | end |
| 51 | def self.method_missing(m, *args, &block) | 57 | def self.method_missing(m, *args, &block) |
| 52 | role = find_role(m, args[0]) | 58 | role = find_role(m, args[0]) |
app/views/profile_editor/index.html.erb
| @@ -28,6 +28,8 @@ | @@ -28,6 +28,8 @@ | ||
| 28 | 28 | ||
| 29 | <%= control_panel_button(_('Manage Content'), 'cms', :controller => 'cms') %> | 29 | <%= control_panel_button(_('Manage Content'), 'cms', :controller => 'cms') %> |
| 30 | 30 | ||
| 31 | + <%= control_panel_button(_('Manage Roles'), 'roles', :controller => 'profile_roles') %> | ||
| 32 | + | ||
| 31 | <% unless profile.enterprise? %> | 33 | <% unless profile.enterprise? %> |
| 32 | <%= case profile.blogs.count | 34 | <%= case profile.blogs.count |
| 33 | when 0 | 35 | when 0 |
app/views/profile_members/change_role.html.erb
| 1 | <h3> <%= _('Changing role of %s') % @member.name %> </h3> | 1 | <h3> <%= _('Changing role of %s') % @member.name %> </h3> |
| 2 | 2 | ||
| 3 | <%= labelled_form_for :member, :url => {:action => 'update_roles'} do |f| %> | 3 | <%= labelled_form_for :member, :url => {:action => 'update_roles'} do |f| %> |
| 4 | - | ||
| 5 | - <%= _('Roles:') %> <br> | 4 | + |
| 5 | + <h4><%= _('Roles:') %></h4> | ||
| 6 | <% @roles.each do |r| %> | 6 | <% @roles.each do |r| %> |
| 7 | <%= labelled_check_box(r.name, 'roles[]', r.id, @associations.map(&:role).include?(r) ) %><br/> | 7 | <%= labelled_check_box(r.name, 'roles[]', r.id, @associations.map(&:role).include?(r) ) %><br/> |
| 8 | <ul class="role-permissions"> | 8 | <ul class="role-permissions"> |
| @@ -11,6 +11,17 @@ | @@ -11,6 +11,17 @@ | ||
| 11 | <% end %> | 11 | <% end %> |
| 12 | </ul> | 12 | </ul> |
| 13 | <% end %> | 13 | <% end %> |
| 14 | + <% unless @custom_roles.empty? %> | ||
| 15 | + <h4><%= _('Custom Roles:') %></h4> | ||
| 16 | + <% @custom_roles.each do |r| %> | ||
| 17 | + <%= labelled_check_box(r.name, 'roles[]', r.id, @associations.map(&:role).include?(r) ) %><br/> | ||
| 18 | + <ul class="role-permissions"> | ||
| 19 | + <% r.permissions.each do |p| %> | ||
| 20 | + <li> <%= permission_name(p) %> </li> | ||
| 21 | + <% end %> | ||
| 22 | + </ul> | ||
| 23 | + <% end %> | ||
| 24 | + <% end %> | ||
| 14 | <%= hidden_field_tag 'person', @member.id %> | 25 | <%= hidden_field_tag 'person', @member.id %> |
| 15 | 26 | ||
| 16 | <% button_bar do %> | 27 | <% button_bar do %> |
| @@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
| 1 | +<%= error_messages_for :role %> | ||
| 2 | + | ||
| 3 | +<%= labelled_form_for :role, :url => (mode == :edit) ? {:action => 'update', :id => role} : {:action => 'create'} do |f| %> | ||
| 4 | + | ||
| 5 | + <%= required_fields_message %> | ||
| 6 | + | ||
| 7 | + <%= required f.text_field(:name) %> | ||
| 8 | + | ||
| 9 | + <% permissions.each do |key| %> | ||
| 10 | + <div class="permissions <%= key.downcase %>"> | ||
| 11 | + <h4><%= _('%s Permissions:' % key) %></h4> | ||
| 12 | + <% ActiveRecord::Base::PERMISSIONS[key].keys.each do |p| %> | ||
| 13 | + <%= check_box_tag("role[permissions][]", p, role.has_permission?(p), { :id => p }) %> | ||
| 14 | + <%= content_tag(:label, permission_name(p), { :for => p }) %><br/> | ||
| 15 | + <% end %> | ||
| 16 | + </div> | ||
| 17 | + <% end %> | ||
| 18 | + | ||
| 19 | + <% button_bar do %> | ||
| 20 | + <%= submit_button('save', (mode == :edit) ? _('Save changes') : _('Create role'), :cancel => {:action => 'index'} ) %> | ||
| 21 | + <% end %> | ||
| 22 | +<% end %> |
| @@ -0,0 +1,23 @@ | @@ -0,0 +1,23 @@ | ||
| 1 | +<h2> <%= _("Deleting #{@role.name}") %> </h2> | ||
| 2 | + | ||
| 3 | +<% if @members.nil? || @members.empty? %> | ||
| 4 | + <p><%= _('This role is not being currently used.')%></p> | ||
| 5 | + <p><%= _('Are you sure you want to delete this role?') %></p> | ||
| 6 | + | ||
| 7 | + <% button_bar do %> | ||
| 8 | + <%= button(:remove, _('Yes, I am sure'), {:action => 'remove', :id => @role.id}, :method => :post) %> | ||
| 9 | + <%= button(:cancel, _('No, I gave up'), {:action => 'index'}) %> | ||
| 10 | + <% end %> | ||
| 11 | +<% else %> | ||
| 12 | + <p><%= _('There are members currently using this role.')%></p> | ||
| 13 | + <p><%= _('To which role do you want to change them?') %></p> | ||
| 14 | + <%= labelled_form_for :role, :url => { :action => 'remove', :id => @role.id } do |f| %> | ||
| 15 | + <% @roles_list.each do |role| %> | ||
| 16 | + <%= check_box_tag("roles[]", role.id, false ,{:id => role.key}) %> | ||
| 17 | + <%= content_tag(:label, role.name, { :for => role.key }) %><br/> | ||
| 18 | + <% end %> | ||
| 19 | + <% button_bar do %> | ||
| 20 | + <%= submit_button('save',_('Delete role'), :cancel => {:action => 'index'} ) %> | ||
| 21 | + <% end %> | ||
| 22 | + <% end %> | ||
| 23 | +<% end %> |
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +<h1><%= _('Manage user roles') %></h1> | ||
| 2 | + | ||
| 3 | +<table> | ||
| 4 | + <tr> | ||
| 5 | + <th><%= _('Role') %></th> | ||
| 6 | + <th><%= _('Actions') %></th> | ||
| 7 | + </tr> | ||
| 8 | + <% @roles.each do |role| %> | ||
| 9 | + <tr> | ||
| 10 | + <td> | ||
| 11 | + <%= link_to role.name, :action => 'show', :id => role %> | ||
| 12 | + </td> | ||
| 13 | + <td> | ||
| 14 | + <%= button_without_text :edit, _('Edit'), :action => 'edit', :id => role %> | ||
| 15 | + <%= button_without_text :delete, _('Delete'), :action => 'destroy', :id => role %> | ||
| 16 | + </td> | ||
| 17 | + </tr> | ||
| 18 | + <% end %> | ||
| 19 | +</table> | ||
| 20 | + | ||
| 21 | +<% button_bar do %> | ||
| 22 | + <%= button :add, _('Create a new role'), :action => 'new' %> | ||
| 23 | + <%= button :back, _('Back to control panel'), :controller => 'profile_editor' %> | ||
| 24 | +<% end %> |
| @@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
| 1 | +<h1> <%= _(@role.name) %></h1> | ||
| 2 | + | ||
| 3 | +<h3> <%= _('Permissions') %> </h3> | ||
| 4 | +<ul> | ||
| 5 | + <% @role.permissions.each do |p| %> | ||
| 6 | + <li> <%= permission_name(p) %> </li> | ||
| 7 | + <% end %> | ||
| 8 | +</ul> | ||
| 9 | + | ||
| 10 | +<% button_bar do %> | ||
| 11 | + <%= button :edit, _('Edit'), :action => 'edit', :id => @role %> | ||
| 12 | + <%= button :back, _('Back to roles management'), :action => 'index' %> | ||
| 13 | +<% end %> |
vendor/plugins/access_control/lib/role.rb
| @@ -4,6 +4,7 @@ class Role < ActiveRecord::Base | @@ -4,6 +4,7 @@ class Role < ActiveRecord::Base | ||
| 4 | 4 | ||
| 5 | has_many :role_assignments, :dependent => :destroy | 5 | has_many :role_assignments, :dependent => :destroy |
| 6 | belongs_to :environment | 6 | belongs_to :environment |
| 7 | + belongs_to :organization | ||
| 7 | serialize :permissions, Array | 8 | serialize :permissions, Array |
| 8 | validates_presence_of :name | 9 | validates_presence_of :name |
| 9 | validates_uniqueness_of :name, :scope => :environment_id | 10 | validates_uniqueness_of :name, :scope => :environment_id |