Skip to content

Commit

Permalink
Add PermissionApiInterface and PermissionAlwaysApi for test/default u…
Browse files Browse the repository at this point in the history
…sage
  • Loading branch information
craigh committed Feb 18, 2017
1 parent f808be3 commit 327801f
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - http://zikula.org/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\PermissionsModule\Api\ApiInterface;

interface PermissionApiInterface
{
/**
* Check permissions
* @api Core-2.0
*
* @param string $component Component
* @param string $instance Instance
* @param integer $level Level
* @param integer $user User Id
*
* @return boolean
*/
public function hasPermission($component = null, $instance = null, $level = ACCESS_NONE, $user = null);

/**
* Translation functions
* Translate level -> name
* @api Core-2.0
*
* @param integer $level Access level
*
* @return string Translated access level name
*/
public function accessLevelNames($level = null);

/**
* Set permissions for user to false, forcing a reload if called upon again.
* @api Core-2.0
*
* @param $uid
*/
public function resetPermissionsForUser($uid);
}
68 changes: 68 additions & 0 deletions src/system/PermissionsModule/Api/PermissionAlwaysApi.php
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - http://zikula.org/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\PermissionsModule\Api;

use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface;

/**
* Class PermissionAlwaysApi
*
* This class exists for testing and to ensure a functional Api is provided in a default situation.
* ALL hasPermission tests return TRUE regardless of settings.
* Access level names are returned untranslated.
* Permissions cannot be reset for a user.
*/
class PermissionAlwaysApi implements PermissionApiInterface
{
/**
* {@inheritdoc}
*/
public function hasPermission($component = null, $instance = null, $level = ACCESS_NONE, $user = null)
{
return true;
}

/**
* {@inheritdoc}
*/
public function accessLevelNames($level = null)
{
if (isset($level) && !is_numeric($level)) {
throw new \InvalidArgumentException();
} elseif (isset($level)) {
$level = intval($level);
}

$accessNames = [
ACCESS_INVALID => 'Invalid',
ACCESS_NONE => 'No access',
ACCESS_OVERVIEW => 'Overview access',
ACCESS_READ => 'Read access',
ACCESS_COMMENT => 'Comment access',
ACCESS_MODERATE => 'Moderate access',
ACCESS_EDIT => 'Edit access',
ACCESS_ADD => 'Add access',
ACCESS_DELETE => 'Delete access',
ACCESS_ADMIN => 'Admin access',
];

return isset($level) ? $accessNames[$level] : $accessNames;
}

/**
* {@inheritdoc}
*/
public function resetPermissionsForUser($uid)
{
// nothing to do
}
}
3 changes: 2 additions & 1 deletion src/system/PermissionsModule/Api/PermissionApi.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface;
use Zikula\PermissionsModule\Entity\RepositoryInterface\PermissionRepositoryInterface;
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
use Zikula\UsersModule\Entity\UserEntity;
Expand All @@ -24,7 +25,7 @@
* or denied from the Permissions module User Interface. Components/Extensions must declare their Permission structure in
* their `composer.json` file.
*/
class PermissionApi
class PermissionApi implements PermissionApiInterface
{
/**
* 'all users', includes unregistered users
Expand Down

0 comments on commit 327801f

Please sign in to comment.