Skip to content

Commit

Permalink
Merge pull request #97 from nerds-and-company/fix-user-category-permi…
Browse files Browse the repository at this point in the history
…ssions

Refactored UserGroups service to make use of sources service.
  • Loading branch information
bvangennep authored Apr 21, 2017
2 parents b651771 + 4cc72a8 commit 660b1e4
Show file tree
Hide file tree
Showing 5 changed files with 473 additions and 343 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
### 3.8.2 ###
- Fixed a bug where schematic would crash when a source does not exist yet
- Fixed a bug where permission category ids were converted to section handles
- Added tests for the sources service

### 3.8.1 ###
- Check if element index sources exist

### 3.8.0 ###
- Renamed package to nerds-and-company/schematic
- Added info about nerds-and-company/schematic-commerce to README
- Added info about nerds-and-company/schematic-commerce to README
- Small fixes

### 3.7.2 ###
Expand Down
77 changes: 44 additions & 33 deletions src/Services/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,54 +55,65 @@ public function getMappedSources($fieldType, $sources, $indexFrom, $indexTo)
*/
public function getSource($fieldType, $source, $indexFrom, $indexTo)
{
if ($source == 'singles' || $source == '*') {
if (strpos($source, ':') === false) {
return $source;
}

/** @var BaseElementModel $sourceObject */
$sourceObject = null;

if (strpos($source, ':') > -1) {
list($sourceType, $sourceFrom) = explode(':', $source);
switch ($sourceType) {
case 'section':
$service = Craft::app()->sections;
$method = 'getSectionBy';
break;
case 'group':
$service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
$method = 'getGroupBy';
break;
case 'folder':
$service = Craft::app()->schematic_assetSources;
$method = 'getSourceBy';
break;
case 'taggroup':
$service = Craft::app()->tags;
$method = 'getTagGroupBy';
break;
case 'field':
$service = Craft::app()->fields;
$method = 'getFieldBy';
break;
}
} elseif ($source !== 'singles') {
//Backwards compatibility
$sourceType = 'section';
$sourceFrom = $source;
$service = Craft::app()->sections;
$method = 'getSectionBy';
list($sourceType, $sourceFrom) = explode(':', $source);
switch ($sourceType) {
case 'section':
case 'createEntries':
case 'deleteEntries':
case 'deletePeerEntries':
case 'deletePeerEntryDrafts':
case 'editEntries':
case 'editPeerEntries':
case 'editPeerEntryDrafts':
case 'publishEntries':
case 'publishPeerEntries':
case 'publishPeerEntryDrafts':
$service = Craft::app()->sections;
$method = 'getSectionBy';
break;
case 'group':
case 'editCategories':
$service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
$method = 'getGroupBy';
break;
case 'folder':
case 'createSubfoldersInAssetSource':
case 'removeFromAssetSource':
case 'uploadToAssetSource':
case 'viewAssetSource':
$service = Craft::app()->schematic_assetSources;
$method = 'getSourceBy';
break;
case 'taggroup':
$service = Craft::app()->tags;
$method = 'getTagGroupBy';
break;
case 'field':
$service = Craft::app()->fields;
$method = 'getFieldBy';
break;
case 'editGlobalSet':
$service = Craft::app()->globals;
$method = 'getSetBy';
break;
}

if (isset($service) && isset($method) && isset($sourceFrom)) {
$method = $method.$indexFrom;
$method = $method.ucfirst($indexFrom);
$sourceObject = $service->$method($sourceFrom);
}

if ($sourceObject && isset($sourceType)) {
return $sourceType.':'.$sourceObject->$indexTo;
}

return $source;
return '';
}
}
149 changes: 8 additions & 141 deletions src/Services/UserGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,9 @@
*/
class UserGroups extends Base
{
/** @var SectionModel[] */
private $sectionsByHandle = [];
/** @var SectionModel[] */
private $sectionsById = [];
/** @var AssetSourceModel[] */
private $assetSourceByHandle = [];
/** @var AssetSourceModel[] */
private $assetSourceById = [];
/** @var GlobalSetModel[] */
private $globalSetsByHandle = [];
/** @var GlobalSetModel[] */
private $globalSetsById = [];
/** @var string[] */
private $mappedPermissions = [];

//==============================================================================================================
//=============================================== SERVICES ===================================================
//==============================================================================================================

/**
* @return SectionsService
*/
private function getSectionsService()
{
return Craft::app()->sections;
}

/**
* @return AssetSourcesService
*/
private function getAssetSourcesService()
{
return Craft::app()->assetSources;
}

/**
* @return GlobalsService
*/
private function getGlobalsService()
{
return Craft::app()->globals;
}

/**
* @return UserPermissionsService
*/
private function getUserPermissionsService()
{
return Craft::app()->userPermissions;
}

/**
* @return UserGroupsService
*/
private function getUserGroupsService()
{
return Craft::app()->userGroups;
}

//==============================================================================================================
//================================================ EXPORT ====================================================
//==============================================================================================================
Expand All @@ -94,9 +38,6 @@ public function export(array $groups = [])

$groupDefinitions = [];

$this->sectionsById = $this->getSectionsService()->getAllSections('id');
$this->assetSourceById = $this->getAssetSourcesService()->getAllSources('id');
$this->globalSetsById = $this->getGlobalsService()->getAllSets('id');
$this->mappedPermissions = $this->getAllMappedPermissions();

foreach ($groups as $group) {
Expand Down Expand Up @@ -131,12 +72,12 @@ private function getGroupDefinition(UserGroupModel $group)
private function getGroupPermissionDefinitions($group)
{
$permissionDefinitions = [];
$groupPermissions = $this->getUserPermissionsService()->getPermissionsByGroupId($group->id);
$groupPermissions = Craft::app()->userPermissions->getPermissionsByGroupId($group->id);

foreach ($groupPermissions as $permission) {
if (array_key_exists($permission, $this->mappedPermissions)) {
$permission = $this->mappedPermissions[$permission];
$permissionDefinitions[] = $this->getPermissionDefinition($permission);
$permissionDefinitions[] = Craft::app()->schematic_sources->getSource(false, $permission, 'id', 'handle');
}
}
sort($permissionDefinitions);
Expand All @@ -153,7 +94,7 @@ private function getGroupPermissionDefinitions($group)
private function getAllMappedPermissions()
{
$mappedPermissions = [];
foreach ($this->getUserPermissionsService()->getAllPermissions() as $permissions) {
foreach (Craft::app()->userPermissions->getAllPermissions() as $permissions) {
$mappedPermissions = array_merge($mappedPermissions, $this->getMappedPermissions($permissions));
}

Expand All @@ -178,26 +119,6 @@ private function getMappedPermissions(array $permissions)
return $mappedPermissions;
}

/**
* Get permission definition.
*
* @param string $permission
*
* @return string
*/
private function getPermissionDefinition($permission)
{
if (strpos($permission, 'Asset') > -1) {
$permission = $this->mapPermissionSource($this->assetSourceById, $permission, true);
} elseif (strpos($permission, 'GlobalSet') > -1) {
$permission = $this->mapPermissionSource($this->globalSetsById, $permission, true);
} else {
$permission = $this->mapPermissionSource($this->sectionsById, $permission, true);
}

return $permission;
}

//==============================================================================================================
//================================================ IMPORT ====================================================
//==============================================================================================================
Expand All @@ -214,11 +135,7 @@ public function import(array $groupDefinitions, $force = false)
{
Craft::log(Craft::t('Importing User Groups'));

$this->sectionsByHandle = $this->getSectionsService()->getAllSections('handle');
$this->assetSourceByHandle = $this->getAssetSourcesService()->getAllSources('handle');
$this->globalSetsByHandle = $this->getGlobalsService()->getAllSets('handle');

$userGroups = $this->getUserGroupsService()->getAllGroups('handle');
$userGroups = Craft::app()->userGroups->getAllGroups('handle');

foreach ($groupDefinitions as $groupHandle => $groupDefinition) {
$group = array_key_exists($groupHandle, $userGroups) ? $userGroups[$groupHandle] : new UserGroupModel();
Expand All @@ -228,20 +145,20 @@ public function import(array $groupDefinitions, $force = false)
$group->name = $groupDefinition['name'];
$group->handle = $groupHandle;

if (!$this->getUserGroupsService()->saveGroup($group)) {
if (!Craft::app()->userGroups->saveGroup($group)) {
$this->addErrors($group->getAllErrors());

continue;
}

$permissions = $this->getPermissions($groupDefinition['permissions']);

$this->getUserPermissionsService()->saveGroupPermissions($group->id, $permissions);
Craft::app()->userPermissions->saveGroupPermissions($group->id, $permissions);
}

if ($force) {
foreach ($userGroups as $group) {
$this->getUserGroupsService()->deleteGroupById($group->id);
Craft::app()->userGroups->deleteGroupById($group->id);
}
}

Expand All @@ -259,59 +176,9 @@ private function getPermissions(array $permissionDefinitions)
{
$permissions = [];
foreach ($permissionDefinitions as $permissionDefinition) {
$permissions[] = $this->getPermission($permissionDefinition);
$permissions[] = Craft::app()->schematic_sources->getSource(false, $permissionDefinition, 'handle', 'id');
}

return $permissions;
}

/**
* Get permission.
*
* @param string $permissionDefinition
*
* @return string
*/
private function getPermission($permissionDefinition)
{
if (strpos($permissionDefinition, 'Asset') > -1) {
$permissionDefinition = $this->mapPermissionSource($this->assetSourceByHandle, $permissionDefinition, false);
} elseif (strpos($permissionDefinition, 'GlobalSet') > -1) {
$permissionDefinition = $this->mapPermissionSource($this->globalSetsByHandle, $permissionDefinition, false);
} else {
$permissionDefinition = $this->mapPermissionSource($this->sectionsByHandle, $permissionDefinition, false);
}

return $permissionDefinition;
}

//==============================================================================================================
//=============================================== HELPERS ====================================================
//==============================================================================================================

/**
* @param BaseElementModel[] $mapping AssetSources or Sections
* @param string $permission
* @param bool $export is it an export or import
*
* @return string mapped permission
*/
private function mapPermissionSource(array $mapping, $permission, $export)
{
if (strpos($permission, ':') > -1) {
/** @var BaseElementModel $source */
$source = false;
list($permissionName, $sourceId) = explode(':', $permission);

if (isset($mapping[$sourceId])) {
$source = $mapping[$sourceId];
}

if ($source) {
$permission = $permissionName.':'.($export ? $source->handle : $source->id);
}
}

return $permission;
}
}
Loading

0 comments on commit 660b1e4

Please sign in to comment.