Skip to content

Commit

Permalink
Dev: PSR-12 fixes for service classes
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Jan 15, 2021
1 parent 476256b commit 09d860d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
12 changes: 5 additions & 7 deletions application/models/services/FilterImportedResources.php
@@ -1,6 +1,5 @@
<?php


namespace LimeSurvey\Models\Services;

class FilterImportedResources
Expand Down Expand Up @@ -36,12 +35,12 @@ public function filterImportedResources($extractdir, $destdir)
}
while ($direntry = readdir($dh)) {
if ($direntry != "." && $direntry != "..") {
if (is_file($extractdir."/".$direntry)) {
if (is_file($extractdir . "/" . $direntry)) {
// is a file
$extfile = (string) substr(strrchr($direntry, '.'), 1);
if (!(stripos(','.Yii::app()->getConfig('allowedresourcesuploads').',', ','.$extfile.',') === false)) {
if (!(stripos(',' . Yii::app()->getConfig('allowedresourcesuploads') . ',', ',' . $extfile . ',') === false)) {
// Extension allowed
if (!copy($extractdir."/".$direntry, $destdir."/".$direntry)) {
if (!copy($extractdir . "/" . $direntry, $destdir . "/" . $direntry)) {
$aErrorFilesInfo[] = array(
"filename" => $direntry,
"status" => gT("Copy failed")
Expand All @@ -59,12 +58,11 @@ public function filterImportedResources($extractdir, $destdir)
"status" => gT("Forbidden Extension")
);
}
unlink($extractdir."/".$direntry);
unlink($extractdir . "/" . $direntry);
}
}
}

return array($aImportedFilesInfo, $aErrorFilesInfo);
}

}
}
13 changes: 6 additions & 7 deletions application/models/services/IpAddressAnonymizer.php
@@ -1,6 +1,5 @@
<?php


namespace LimeSurvey\Models\Services;

/**
Expand Down Expand Up @@ -32,10 +31,10 @@ public function __construct($ipAddress)
*/
public function isIpv4()
{
if($this->ipAddress === ''){
if ($this->ipAddress === '') {
$result = false;
}else{
$result = (boolean)filter_var($this->ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
} else {
$result = (bool)filter_var($this->ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}

return $result;
Expand All @@ -48,10 +47,10 @@ public function isIpv4()
*/
public function isIpv6()
{
if($this->ipAddress === ''){
if ($this->ipAddress === '') {
$result = false;
}else{
$result = (boolean)filter_var($this->ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
} else {
$result = (bool)filter_var($this->ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
}
return $result;
}
Expand Down
33 changes: 16 additions & 17 deletions application/models/services/PermissionManager.php
@@ -1,4 +1,5 @@
<?php

/**
* Management of Permission
* @version 0.1.0
Expand All @@ -12,7 +13,6 @@
use LSWebUser;
use Permission;
use PermissionInterface;

use App;
use CHtml;

Expand Down Expand Up @@ -47,20 +47,20 @@ public function __construct(
/**
* get the permission data
* @param integer $userId for this user id
* @return array[]
* @return array[]
*/
public function getPermissionData($userId = null)
{
$aObjectPermissions = $this->model::getPermissionData(); // Usage of static, db not needed
if(empty($aObjectPermissions)) {
if (empty($aObjectPermissions)) {
return $aObjectPermissions;
}
/* string[] Crud type array */
$aCruds = array('create', 'read', 'update', 'delete', 'import', 'export');
foreach (array_keys($aObjectPermissions) as $sPermission) {
$aObjectPermissions[$sPermission]['current'] = array();
foreach($aCruds as $crud) {
if(!isset($aObjectPermissions[$sPermission][$crud])) {
foreach ($aCruds as $crud) {
if (!isset($aObjectPermissions[$sPermission][$crud])) {
/* Not set mean true (in Survey on 3.X) */
$aObjectPermissions[$sPermission][$crud] = true;
}
Expand All @@ -72,7 +72,7 @@ public function getPermissionData($userId = null)
);
}
/* If user id is set : update the data with permission of this user */
if(!is_null($userId)) {
if (!is_null($userId)) {
$oCurrentPermission = $this->getDbPermission(
get_class($this->model),
$this->model->getPrimaryKey(),
Expand All @@ -85,7 +85,7 @@ public function getPermissionData($userId = null)
/* The user have the permission set */
$aObjectPermissions[$sPermission]['current'][$crud]['checked'] = $havePermissionSet;
/* The user didn't have the permission set, but have permission by other way (inherited, plugin …) */
if(!$havePermissionSet) {
if (!$havePermissionSet) {
$aObjectPermissions[$sPermission]['current'][$crud]['indeterminate'] = $this->getCurrentPermission($sPermission, $crud, $userId);
}
}
Expand Down Expand Up @@ -123,7 +123,7 @@ public function setPermissions($userId)
$aSetPermissions[$sPermission] = array();
foreach ($aCruds as $crud) {
/* Only set value if current user have the permission to set */
if($this->getCurrentPermission($sPermission, $crud, $this->user->id)) {
if ($this->getCurrentPermission($sPermission, $crud, $this->user->id)) {
$aSetPermissions[$sPermission][$crud] = !empty($aPermission[$crud]) && !empty($entityPermissionsToSet[$sPermission][$crud]);
}
}
Expand All @@ -133,15 +133,15 @@ public function setPermissions($userId)
// Event
$oEvent = new \LimeSurvey\PluginManager\PluginEvent('beforePermissionSetSave');
$oEvent->set('aNewPermissions', $aSetPermissions);
if(get_class($this->model) == 'Survey') {
if (get_class($this->model) == 'Survey') {
$oEvent->set('iSurveyID', $this->model->getPrimaryKey());
}
$oEvent->set('entity', get_class($this->model)); /* New in 4.4.X */
$oEvent->set('entityId', $this->model->getPrimaryKey()); /* New in 4.4.X */
$oEvent->set('iUserID', $userId);
App()->getPluginManager()->dispatchEvent($oEvent);

foreach($aSetPermissions as $sPermission => $aSetPermission) {
foreach ($aSetPermissions as $sPermission => $aSetPermission) {
$oCurrentPermission = $this->getDbPermission(
get_class($this->model),
$this->model->getPrimaryKey(),
Expand All @@ -157,12 +157,12 @@ public function setPermissions($userId)
);
}
/* Set only the permission set in $aSetPermission : user have the rights */
foreach($aSetPermission as $crud => $permission) {
$oCurrentPermission->setAttribute("{$crud}_p",intval($permission));
foreach ($aSetPermission as $crud => $permission) {
$oCurrentPermission->setAttribute("{$crud}_p", intval($permission));
}
if(!$oCurrentPermission->save()) {
if (!$oCurrentPermission->save()) {
$success = false;
App()->setFlashMessage(CHtml::errorSummary($oCurrentPermission),'warning');
App()->setFlashMessage(CHtml::errorSummary($oCurrentPermission), 'warning');
}
}
Permission::setMinimalEntityPermission($userId, $this->model->getPrimaryKey(), get_class($this->model));
Expand All @@ -176,7 +176,7 @@ public function setPermissions($userId)
*/
public function getCurrentPermission($sPermission, $crud, $userId)
{
if(empty($this->model)) {
if (empty($this->model)) {
return false;
}
return $this->model->hasPermission($sPermission, $crud, $userId);
Expand All @@ -189,7 +189,7 @@ public function getCurrentPermission($sPermission, $crud, $userId)
*/
public function setDbPermission($entityName, $entityId, $userId, $sPermission)
{
$oPermission = new Permission;
$oPermission = new Permission();
$oPermission->entity = $entityName;
$oPermission->entity_id = $entityId;
$oPermission->uid = $userId;
Expand All @@ -215,5 +215,4 @@ public function getDbPermission($entityName, $entityId, $userId, $sPermission)
);
return $oPermission;
}

}

0 comments on commit 09d860d

Please sign in to comment.