diff --git a/app/controllers/admin/role_controller.rb b/app/controllers/admin/role_controller.rb index 95f8e9f..d67da12 100644 --- a/app/controllers/admin/role_controller.rb +++ b/app/controllers/admin/role_controller.rb @@ -2,7 +2,7 @@ class RoleController < AdminController protect 'manage_environment_roles', :environment def index - @roles = environment.roles.find(:all) + @roles = environment.roles.find(:all, :conditions => {:profile_id => nil}) end def new diff --git a/app/controllers/my_profile/profile_members_controller.rb b/app/controllers/my_profile/profile_members_controller.rb index fb8e262..c3781ef 100644 --- a/app/controllers/my_profile/profile_members_controller.rb +++ b/app/controllers/my_profile/profile_members_controller.rb @@ -58,6 +58,7 @@ class ProfileMembersController < MyProfileController def change_role @roles = Profile::Roles.organization_member_roles(environment.id) + @custom_roles = Profile::Roles.organization_custom_roles(environment.id, profile.id) begin @member = profile.members.find(params[:id]) rescue ActiveRecord::RecordNotFound diff --git a/app/controllers/my_profile/profile_roles_controller.rb b/app/controllers/my_profile/profile_roles_controller.rb new file mode 100644 index 0000000..0390088 --- /dev/null +++ b/app/controllers/my_profile/profile_roles_controller.rb @@ -0,0 +1,65 @@ +class ProfileRolesController < MyProfileController + + include RoleHelper + + def index + @roles = environment.roles.find(:all, :conditions => {:profile_id => profile.id} ) + end + + def new + @role = Role.new + end + + def create + @role = Role.create({:name => params[:role][:name], :permissions => params[:role][:permissions], :profile_id => profile.id, :environment => environment }, :without_protection => true) + if @role.save + redirect_to :action => 'show', :id => @role + else + session[:notice] = _('Failed to create role') + render :action => 'new' + end + end + + def show + @role = environment.roles.find(params[:id]) + end + + def edit + @role = environment.roles.find(params[:id]) + end + + def destroy + @role = environment.roles.find(params[:id]) + @members = profile.members_by_role(@role) + @roles_list = Profile::Roles.organization_all_roles(environment.id, profile.id) + @roles_list.delete(@role) + end + + def remove + @role = environment.roles.find(params[:id]) + @members = profile.members_by_role(@role) + new_roles = params[:roles] ? environment.roles.find(params[:roles].select{|r|!r.to_i.zero?}) : [] + @members.each do |person| + member_roles = person.find_roles(profile).map(&:role) + new_roles + person.define_roles(member_roles, profile) + end + if @role.destroy + session[:notice] = _('Role successfuly removed!') + else + session[:notice] = _('Failed to remove role!') + end + redirect_to :action => 'index' + end + + def update + @role = environment.roles.find(params[:id]) + if @role.update_attributes(params[:role]) + redirect_to :action => 'show', :id => @role + else + session[:notice] = _('Failed to edit role') + render :action => 'edit' + end + end + + +end diff --git a/app/models/organization.rb b/app/models/organization.rb index 054c0ad..2f9f4cc 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -29,6 +29,8 @@ class Organization < Profile has_many :mailings, :class_name => 'OrganizationMailing', :foreign_key => :source_id, :as => 'source' + has_many :custom_roles, :class_name => 'Role', :foreign_key => :profile_id + scope :more_popular, :order => 'members_count DESC' validate :presence_of_required_fieds, :unless => :is_template diff --git a/app/models/profile.rb b/app/models/profile.rb index 042a5ec..317a401 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -43,10 +43,16 @@ class Profile < ActiveRecord::Base find_role('editor', env_id) end def self.organization_member_roles(env_id) - all_roles(env_id).select{ |r| r.key.match(/^profile_/) unless r.key.blank? } + all_roles(env_id, nil).select{ |r| r.key.match(/^profile_/) unless r.key.blank? } end - def self.all_roles(env_id) - Role.all :conditions => { :environment_id => env_id } + def self.organization_custom_roles(env_id, profile_id) + all_roles(env_id, profile_id).select{ |r| r.key.match(/^profile_/) unless r.key.blank? } + end + def self.organization_all_roles(env_id, profile_id) + self.organization_member_roles(env_id) + self.organization_custom_roles(env_id, profile_id) + end + def self.all_roles(env_id, profile_id) + Role.all :conditions => { :profile_id => profile_id, :environment_id => env_id } end def self.method_missing(m, *args, &block) role = find_role(m, args[0]) diff --git a/app/views/profile_editor/index.html.erb b/app/views/profile_editor/index.html.erb index 3d39ee4..31c757d 100644 --- a/app/views/profile_editor/index.html.erb +++ b/app/views/profile_editor/index.html.erb @@ -28,6 +28,8 @@ <%= control_panel_button(_('Manage Content'), 'cms', :controller => 'cms') %> + <%= control_panel_button(_('Manage Roles'), 'roles', :controller => 'profile_roles') %> + <% unless profile.enterprise? %> <%= case profile.blogs.count when 0 diff --git a/app/views/profile_members/change_role.html.erb b/app/views/profile_members/change_role.html.erb index 71a8933..08692f9 100644 --- a/app/views/profile_members/change_role.html.erb +++ b/app/views/profile_members/change_role.html.erb @@ -1,8 +1,8 @@

<%= _('Changing role of %s') % @member.name %>

<%= labelled_form_for :member, :url => {:action => 'update_roles'} do |f| %> - - <%= _('Roles:') %>
+ +

<%= _('Roles:') %>

<% @roles.each do |r| %> <%= labelled_check_box(r.name, 'roles[]', r.id, @associations.map(&:role).include?(r) ) %>
<% end %> + <% unless @custom_roles.empty? %> +

<%= _('Custom Roles:') %>

+ <% @custom_roles.each do |r| %> + <%= labelled_check_box(r.name, 'roles[]', r.id, @associations.map(&:role).include?(r) ) %>
+ + <% end %> + <% end %> <%= hidden_field_tag 'person', @member.id %> <% button_bar do %> diff --git a/app/views/profile_roles/_form.html.erb b/app/views/profile_roles/_form.html.erb new file mode 100644 index 0000000..bfdb946 --- /dev/null +++ b/app/views/profile_roles/_form.html.erb @@ -0,0 +1,22 @@ +<%= error_messages_for :role %> + +<%= labelled_form_for :role, :url => (mode == :edit) ? {:action => 'update', :id => role} : {:action => 'create'} do |f| %> + + <%= required_fields_message %> + + <%= required f.text_field(:name) %> + + <% permissions.each do |key| %> +
+

<%= _('%s Permissions:' % key) %>

+ <% ActiveRecord::Base::PERMISSIONS[key].keys.each do |p| %> + <%= check_box_tag("role[permissions][]", p, role.has_permission?(p), { :id => p }) %> + <%= content_tag(:label, permission_name(p), { :for => p }) %>
+ <% end %> +
+ <% end %> + + <% button_bar do %> + <%= submit_button('save', (mode == :edit) ? _('Save changes') : _('Create role'), :cancel => {:action => 'index'} ) %> + <% end %> +<% end %> diff --git a/app/views/profile_roles/destroy.html.erb b/app/views/profile_roles/destroy.html.erb new file mode 100644 index 0000000..94bfb8c --- /dev/null +++ b/app/views/profile_roles/destroy.html.erb @@ -0,0 +1,23 @@ +

<%= _("Deleting #{@role.name}") %>

+ +<% if @members.nil? || @members.empty? %> +

<%= _('This role is not being currently used.')%>

+

<%= _('Are you sure you want to delete this role?') %>

+ + <% button_bar do %> + <%= button(:remove, _('Yes, I am sure'), {:action => 'remove', :id => @role.id}, :method => :post) %> + <%= button(:cancel, _('No, I gave up'), {:action => 'index'}) %> + <% end %> +<% else %> +

<%= _('There are members currently using this role.')%>

+

<%= _('To which role do you want to change them?') %>

+ <%= labelled_form_for :role, :url => { :action => 'remove', :id => @role.id } do |f| %> + <% @roles_list.each do |role| %> + <%= check_box_tag("roles[]", role.id, false ,{:id => role.key}) %> + <%= content_tag(:label, role.name, { :for => role.key }) %>
+ <% end %> + <% button_bar do %> + <%= submit_button('save',_('Delete role'), :cancel => {:action => 'index'} ) %> + <% end %> + <% end %> +<% end %> diff --git a/app/views/profile_roles/edit.html.erb b/app/views/profile_roles/edit.html.erb new file mode 100644 index 0000000..04dd3ea --- /dev/null +++ b/app/views/profile_roles/edit.html.erb @@ -0,0 +1,3 @@ +

<%= _("Editing #{@role.name}") %>

+ +<%= render :partial => 'form', :locals => { :mode => :edit, :role => @role, :permissions => [@role.kind] } %> diff --git a/app/views/profile_roles/index.html.erb b/app/views/profile_roles/index.html.erb new file mode 100644 index 0000000..419c1f9 --- /dev/null +++ b/app/views/profile_roles/index.html.erb @@ -0,0 +1,24 @@ +

<%= _('Manage user roles') %>

+ + + + + + + <% @roles.each do |role| %> + + + + + <% end %> +
<%= _('Role') %><%= _('Actions') %>
+ <%= link_to role.name, :action => 'show', :id => role %> + + <%= button_without_text :edit, _('Edit'), :action => 'edit', :id => role %> + <%= button_without_text :delete, _('Delete'), :action => 'destroy', :id => role %> +
+ +<% button_bar do %> + <%= button :add, _('Create a new role'), :action => 'new' %> + <%= button :back, _('Back to control panel'), :controller => 'profile_editor' %> +<% end %> diff --git a/app/views/profile_roles/new.html.erb b/app/views/profile_roles/new.html.erb new file mode 100644 index 0000000..69e3f9f --- /dev/null +++ b/app/views/profile_roles/new.html.erb @@ -0,0 +1,3 @@ +

<%= _("Create a new role") %>

+ +<%= render :partial => 'form', :locals => { :mode => :create, :role => @role, :permissions => ['Profile'] } %> diff --git a/app/views/profile_roles/show.html.erb b/app/views/profile_roles/show.html.erb new file mode 100644 index 0000000..97da59e --- /dev/null +++ b/app/views/profile_roles/show.html.erb @@ -0,0 +1,13 @@ +

<%= _(@role.name) %>

+ +

<%= _('Permissions') %>

+ + +<% button_bar do %> + <%= button :edit, _('Edit'), :action => 'edit', :id => @role %> + <%= button :back, _('Back to roles management'), :action => 'index' %> +<% end %> diff --git a/db/migrate/20150203143051_add_reference_to_role.rb b/db/migrate/20150203143051_add_reference_to_role.rb new file mode 100644 index 0000000..35b039a --- /dev/null +++ b/db/migrate/20150203143051_add_reference_to_role.rb @@ -0,0 +1,8 @@ +class AddReferenceToRole < ActiveRecord::Migration + def self.up + add_column :roles, :profile_id, :integer + end + def self.down + remove_column :roles , :profile_id + end +end diff --git a/vendor/plugins/access_control/lib/role.rb b/vendor/plugins/access_control/lib/role.rb index 4535dac..0b8141a 100644 --- a/vendor/plugins/access_control/lib/role.rb +++ b/vendor/plugins/access_control/lib/role.rb @@ -4,6 +4,7 @@ class Role < ActiveRecord::Base has_many :role_assignments, :dependent => :destroy belongs_to :environment + belongs_to :organization serialize :permissions, Array validates_presence_of :name validates_uniqueness_of :name, :scope => :environment_id -- libgit2 0.21.2