Skip to content

Commit

Permalink
Implemented :translator role whereby a user is allowed access only to…
Browse files Browse the repository at this point in the history
… update pages that not the default frontend locale.
  • Loading branch information
parndt committed May 10, 2011
1 parent 8da92bc commit 4beff8e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 deletions.
17 changes: 11 additions & 6 deletions authentication/features/step_definitions/user_steps.rb
Expand Up @@ -5,32 +5,37 @@ def login
click_button("submit_button") click_button("submit_button")
end end


Given /^I am a logged in refinery user$/ do Given /^I am a logged in refinery user$/i do
@user ||= Factory(:refinery_user) @user ||= Factory(:refinery_user)
login login
end end


Given /^I am a logged in customer$/ do Given /^I am a logged in refinery translator$/i do
@user ||= Factory(:refinery_translator)
login
end

Given /^I am a logged in customer$/i do
@user ||= Factory(:user) @user ||= Factory(:user)
login login
end end


Given /^A Refinery user exists$/ do Given /^A Refinery user exists$/i do
@refinery_user ||= Factory(:refinery_user) @refinery_user ||= Factory(:refinery_user)
end end


Given /^I have a user named "(.*)"$/ do |name| Given /^I have a user named "(.*)"$/ do |name|
Factory(:user, :username => name) Factory(:user, :username => name)
end end


Given /^I have a R|refinery user named "(.*)"$/ do |name| Given /^I have a refinery user named "(.*)"$/i do |name|
Factory(:refinery_user, :username => name) Factory(:refinery_user, :username => name)
end end


Given /^I have no users$/ do Given /^I have no users$/i do
User.delete_all User.delete_all
end end


Then /^I should have ([0-9]+) users?$/ do |count| Then /^I should have ([0-9]+) users?$/i do |count|
User.count.should == count.to_i User.count.should == count.to_i
end end
8 changes: 8 additions & 0 deletions authentication/features/support/factories.rb
Expand Up @@ -16,3 +16,11 @@
end end
end end
end end

Factory.define :refinery_translator, :parent => :user do |u|
u.roles { [ Role[:refinery], Role[:translator] ] }

u.after_create do |user|
user.plugins.create(:name => 'refinery_pages', :position => 0)
end
end
18 changes: 16 additions & 2 deletions pages/app/controllers/admin/pages_controller.rb
Expand Up @@ -11,6 +11,10 @@ class PagesController < Admin::BaseController


after_filter lambda{::Page.expire_page_caching}, :only => [:update_positions] after_filter lambda{::Page.expire_page_caching}, :only => [:update_positions]


before_filter :restrict_access, :only => [:create, :update, :update_positions, :destroy], :if => proc {|c|
defined?(::Refinery::I18n) && ::Refinery::I18n.enabled?
}

def new def new
@page = Page.new @page = Page.new
Page.default_parts.each_with_index do |page_part, index| Page.default_parts.each_with_index do |page_part, index|
Expand All @@ -28,8 +32,8 @@ def globalize!


# Check whether we need to override e.g. on the pages form. # Check whether we need to override e.g. on the pages form.
unless params[:switch_locale] || @page.nil? || @page.new_record? || @page.slugs.where({ unless params[:switch_locale] || @page.nil? || @page.new_record? || @page.slugs.where({
:locale => Refinery::I18n.current_locale} :locale => Refinery::I18n.current_locale
).nil? }).nil?
@page.slug = @page.slugs.first if @page.slug.nil? && @page.slugs.any? @page.slug = @page.slugs.first if @page.slug.nil? && @page.slugs.any?
Thread.current[:globalize_locale] = @page.slug.locale if @page.slug Thread.current[:globalize_locale] = @page.slug.locale if @page.slug
end end
Expand All @@ -50,5 +54,15 @@ def show_errors_for_reserved_slug(exception)
end end
end end


def restrict_access
if current_user.has_role?(:translator) && !current_user.has_role?(:superuser) &&
(params[:switch_locale].blank? || params[:switch_locale] == ::Refinery::I18n.default_frontend_locale.to_s)
flash[:error] = t('translator_access', :scope => 'admin.pages')
redirect_to :action => 'index' and return
end

return true
end

end end
end end
1 change: 1 addition & 0 deletions pages/config/locales/en.yml
Expand Up @@ -77,6 +77,7 @@ en:
reorder_pages_done: Done reordering pages reorder_pages_done: Done reordering pages
records: records:
no_pages_yet: There are no pages yet. Click "Add new page" to add your first page. no_pages_yet: There are no pages yet. Click "Add new page" to add your first page.
translator_access: You do not have the required permission to modify pages in this language.
activerecord: activerecord:
models: models:
page: page page: page
Expand Down
1 change: 1 addition & 0 deletions pages/features/manage_pages.feature
Expand Up @@ -45,3 +45,4 @@ Feature: Manage Pages
Then I should see "'test' was successfully removed." Then I should see "'test' was successfully removed."
And I should have 0 pages And I should have 0 pages
And I should have 0 page_parts And I should have 0 page_parts

4 changes: 4 additions & 0 deletions pages/features/step_definitions/page_steps.rb
Expand Up @@ -50,3 +50,7 @@
Then /^I should have (\d+) page_parts$/ do |count| Then /^I should have (\d+) page_parts$/ do |count|
PagePart.count.should == count.to_i PagePart.count.should == count.to_i
end end

Given /^I have frontend locales "?([^\"]*)"?/ do |locales|
RefinerySetting.set(:i18n_translation_frontend_locales, {:value => locales.split(', '), :scoping => 'refinery'})
end
40 changes: 40 additions & 0 deletions pages/features/translate_pages.feature
@@ -0,0 +1,40 @@
@refinerycms @pages @pages-translate @i18n
Feature: Translate Pages
In order to make the content on my website accessible in many countries
As a translator
I want to translate manage pages

Background:
Given A Refinery user exists
And I am a logged in Refinery Translator
And I have pages titled Home, About

Scenario: Pages List
When I go to the list of pages
Then I should see "Home"
And I should see "About"

Scenario: Add page to main locale
When I go to the list of pages
And I follow "Add new page"
And I fill in "Title" with "Pickles are Cucumbers Soaked in Evil"
And I press "Save"
Then I should see "You do not have the required permission to modify pages in this language"
And I should have 2 pages

Scenario: Add page to second locale
Given I have frontend locales en, fr
When I go to the list of pages
And I follow "Add new page"
And I follow "Fr" within "#switch_locale_picker"
And I fill in "Title" with "Pickles sont Concombres Trempé dans le Mal"
And I press "Save"
Then I should see "'Pickles sont Concombres Trempé dans le Mal' was successfully added."
And I should have 3 pages

Scenario: Delete page from main locale
Given I only have a page titled "test"
When I go to the list of pages
And I follow "Remove this page forever"
Then I should see "You do not have the required permission to modify pages in this language."
And I should have 1 pages

0 comments on commit 4beff8e

Please sign in to comment.