Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: user permission is now checked for api key authorizer #3875

Merged
merged 8 commits into from
Mar 17, 2020
10 changes: 10 additions & 0 deletions src/Core/Tool/Authorizer/ApiKeyAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
namespace Ushahidi\Core\Tool\Authorizer;

use Ushahidi\Core\Entity;
use Ushahidi\Core\Entity\Permission;
use Ushahidi\Core\Tool\Authorizer;
use Ushahidi\Core\Traits\AdminAccess;
use Ushahidi\Core\Traits\UserContext;
use Ushahidi\Core\Traits\PrivAccess;
use Ushahidi\Core\Traits\PrivateDeployment;
use Ushahidi\Core\Tool\Permissions\AclTrait;

class ApiKeyAuthorizer implements Authorizer
{
Expand All @@ -32,6 +34,9 @@ class ApiKeyAuthorizer implements Authorizer
// It uses `PrivateDeployment` to check whether a deployment is private
use PrivateDeployment;

// Check that the user has the necessary permissions
use AclTrait;

/* Authorizer */
public function isAllowed(Entity $entity, $privilege)
{
Expand All @@ -44,6 +49,11 @@ public function isAllowed(Entity $entity, $privilege)
return false;
}

// Role with the Manage Settings permission can have access
if ($this->acl->hasPermission($user, Permission::MANAGE_SETTINGS)) {
return true;
}

// Admin is allowed access to everything
if ($this->isUserAdmin($user)) {
return true;
Expand Down
19 changes: 19 additions & 0 deletions tests/datasets/ushahidi/Base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ roles:
name: "noedit"
display_name: "User cant edit posts"
protected: 0
-
name: "settingsmanager"
display_name: "Settings Manager"
protected: 0
users:
-
id: 1
Expand Down Expand Up @@ -82,6 +86,12 @@ users:
realname: Sets
email: "sets@ushahidi.com"
role: "sets"
-
id: 10
password: "$2y$15$iWANGZn.DomLWU.YtjUcX.HEq1hoMGauzXFRubKgar/BRaAj9zQ9q"
realname: Settings Manager
email: "settingsmanager@ushahidi.com"
role: "settingsmanager"
user_settings:
-
id: 1
Expand Down Expand Up @@ -1674,6 +1684,12 @@ oauth_access_tokens:
user_id: 9
scopes: '["*"]'
expires_at: "2031-01-01"
-
id: testsettingsmanager
client_id: demoapp
user_id: 10
scopes: '["*"]'
expires_at: "2031-01-01"

oauth_refresh_tokens:
oauth_personal_access_clients:
Expand Down Expand Up @@ -2244,6 +2260,9 @@ roles_permissions:
-
role: manager
permission: Manage Settings
-
role: settingsmanager
permission: Manage Settings
-
role: sets
permission: Manage Collections and Saved Searches
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/api.apikeys.feature
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ Feature: Testing the ApiKey API
And the type of the "count" property is "numeric"
And the "count" property equals "1"
Then the guzzle status code should be 200

@rolesEnabled
Scenario: User with only Manage Settings permission can list Apikeys
Given that I want to get all "Apikeys"
And that the oauth token is "testsettingsmanager"
When I request "/apikeys"
Then the response is JSON
And the response has a "count" property
And the type of the "count" property is "numeric"
And the "count" property equals "1"
Then the guzzle status code should be 200
1 change: 1 addition & 0 deletions tests/integration/bootstrap/RestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ public function thatIWantToCountAll($objectType)
'missingtoken' => 99,
'testnoedit' => 8,
'testsets' => 9,
'testsettingsmanager' => 10,
];

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/users.feature
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Feature: Testing the Users API
Then the response is JSON
And the response has a "count" property
And the type of the "count" property is "numeric"
And the "count" property equals "9"
And the "count" property equals "10"
Then the guzzle status code should be 200

@resetFixture
Expand Down