API CHANGE Requiring TRANSLATE_ALL or TRANSLATE_<locale> permission for authors without administrative access to edit translations

ENHANCEMENT Added Translatable->providePermissions()

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88123 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-10-05 10:33:36 +00:00
parent af9e4f59f5
commit 0faf03ee4e
3 changed files with 118 additions and 30 deletions

View File

@ -127,6 +127,16 @@
* is stored and represented in UTF-8 (Unicode). Please make sure your database and
* HTML-templates adjust to this.
*
* <h2>Permissions</h2>
*
* Authors without administrative access need special permissions.
*
* - TRANSLATE_ALL: Translate into all locales
* - Translate_<locale>: Translate a specific locale. Only available for all locales set in
* `Translatable::set_allowed_locales()`.
*
* Note: If user-specific view permissions are required, please overload `SiteTree->canView()`.
*
* <h2>Uninstalling/Disabling</h2>
*
* Disabling Translatable after creating translations will lead to all
@ -144,7 +154,7 @@
* @package sapphire
* @subpackage i18n
*/
class Translatable extends DataObjectDecorator {
class Translatable extends DataObjectDecorator implements PermissionProvider {
/**
* The 'default' language.
@ -1071,10 +1081,20 @@ class Translatable extends DataObjectDecorator {
function canTranslate($member = null, $locale) {
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
return (
// check for locale
$allowedLocale = (
!is_array(self::get_allowed_locales())
|| in_array($locale, self::get_allowed_locales())
);
if(!$allowedLocale) return false;
// check for generic translation permission
if(Permission::checkMember($member, 'TRANSLATE_ALL')) return true;
// check for locale specific translate permission
if(!Permission::checkMember($member, 'TRANSLATE_' . $locale)) return false;
return true;
}
/**
@ -1126,6 +1146,31 @@ class Translatable extends DataObjectDecorator {
}
}
function providePermissions() {
$locales = self::get_allowed_locales();
$permissions = array();
if($locales) foreach($locales as $locale) {
$localeName = i18n::get_locale_name($locale);
$permissions['TRANSLATE_' . $locale] = sprintf(
_t(
'Translatable.TRANSLATEPERMISSION',
'Translate %s',
PR_MEDIUM,
'Translate pages into a language'
),
$localeName
);
}
$permissions['TRANSLATE_ALL'] = _t(
'Translatable.TRANSLATEALLPERMISSION',
'Translate into all available languages'
);
return $permissions;
}
/**
* Get a list of languages with at least one element translated in (including the default language)
*

View File

@ -727,7 +727,7 @@ class TranslatableTest extends FunctionalTest {
$this->assertEquals($compareLive->Title, 'Publiziert');
}
function testCanTranslate() {
function testCanTranslateAllowedLocales() {
$origAllowedLocales = Translatable::get_allowed_locales();
$cmseditor = $this->objFromFixture('Member', 'cmseditor');
@ -735,17 +735,17 @@ class TranslatableTest extends FunctionalTest {
$testPage = $this->objFromFixture('Page', 'testpage_en');
$this->assertTrue(
$testPage->canTranslate($cmseditor, 'de_DE'),
"Users with canEdit() permission can create a new translation if locales are not limited"
"Users with canEdit() and TRANSLATE_ALL permission can create a new translation if locales are not limited"
);
Translatable::set_allowed_locales(array('ja_JP'));
$this->assertTrue(
$testPage->canTranslate($cmseditor, 'ja_JP'),
"Users with canEdit() permission can create a new translation if locale is in Translatable::get_allowed_locales()"
"Users with canEdit() and TRANSLATE_ALL permission can create a new translation if locale is in Translatable::get_allowed_locales()"
);
$this->assertFalse(
$testPage->canTranslate($cmseditor, 'de_DE'),
"Users with canEdit() permission can't create a new translation if locale is not in Translatable::get_allowed_locales()"
"Users with canEdit() and TRANSLATE_ALL permission can't create a new translation if locale is not in Translatable::get_allowed_locales()"
);
$this->assertType(
@ -760,6 +760,35 @@ class TranslatableTest extends FunctionalTest {
Translatable::set_allowed_locales($origAllowedLocales);
}
function testCanTranslatePermissionCodes() {
$origAllowedLocales = Translatable::get_allowed_locales();
Translatable::set_allowed_locales(array('ja_JP','de_DE'));
$cmseditor = $this->objFromFixture('Member', 'cmseditor');
$testPage = $this->objFromFixture('Page', 'testpage_en');
$this->assertTrue(
$testPage->canTranslate($cmseditor, 'de_DE'),
"Users with TRANSLATE_ALL permission can create a new translation"
);
$translator = $this->objFromFixture('Member', 'germantranslator');
$testPage = $this->objFromFixture('Page', 'testpage_en');
$this->assertTrue(
$testPage->canTranslate($translator, 'de_DE'),
"Users with TRANSLATE_<locale> permission can create a new translation"
);
$this->assertFalse(
$testPage->canTranslate($translator, 'ja_JP'),
"Users without TRANSLATE_<locale> permission can create a new translation"
);
Translatable::set_allowed_locales($origAllowedLocales);
}
function testSavePageInCMS() {
$adminUser = $this->objFromFixture('Member', 'admin');
$enPage = $this->objFromFixture('Page', 'testpage_en');

View File

@ -50,6 +50,8 @@ Group:
Code: cmseditorgroup
admingroup:
Code: admingroup
germantranslators:
Code: germantranslators
Member:
cmseditor:
FirstName: Editor
@ -59,10 +61,22 @@ Member:
admin:
FirstName: Admin
Groups: =>Group.admingroup
germantranslator:
FirstName: German
Groups: =>Group.germantranslators
Permission:
cmsmaincode:
Code: CMS_ACCESS_CMSMain
Group: =>Group.cmseditorgroup
admincode:
Code: ADMIN
Group: =>Group.admingroup
cmsmaincode:
Code: CMS_ACCESS_CMSMain
Group: =>Group.cmseditorgroup
translateAllCode:
Code: TRANSLATE_ALL
Group: =>Group.cmseditorgroup
cmsmaincode2:
Code: CMS_ACCESS_CMSMain
Group: =>Group.germantranslators
translateDeCode2:
Code: TRANSLATE_de_DE
Group: =>Group.germantranslators