Skip to content

Commit

Permalink
Merge pull request #12 from figmateus/newPatchEndPoint
Browse files Browse the repository at this point in the history
New patch end point
  • Loading branch information
figmateus authored Aug 4, 2024
2 parents fc0f190 + a47f057 commit 882c910
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,25 @@ public function permission(RequestInterface $request, $uuid)
'user' => $user,
]);
}

public function alterUserPermission(RequestInterface $request, $uuid){

$user = User::query()->where('uuid', $uuid)->first();

if(empty($user)) {
return $this->response->json([
'error' => 'Usuário não encontrado.',
], 404);
}

$user->permission = serialize($request->input('permissions'));
$user->save;

return $this->response->json([
'message' => 'Permissoes de usuário atualizadas com sucesso!',
'user' => $user,
'status' => 200
],200);

}
}
1 change: 1 addition & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function () {
Router::addRoute(['PUT'], '/user/{id}', 'App\Controller\UserController@update');
Router::addRoute(['DELETE'], '/user/{id}', 'App\Controller\UserController@del');
Router::addRoute(['POST'], '/user/permission/{uuid}', 'App\Controller\UserController@permission');
Router::addRoute(['PATCH'], '/user/alterPermissions/{uuid}', 'App\Controller\UserController@alterUserPermission');
Router::addRoute(['PATCH'], '/user/type/{id}', 'App\Controller\UserController@updateUserType');

// Product
Expand Down
49 changes: 49 additions & 0 deletions test/Cases/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
class UserTest extends HttpTestCase
{

#[Value(key: 'defaultPermissions.admin_default_permissions')]
private $defaultAdminPermissions;

#[Value(key: 'defaultPermissions.founder_default_permissions')]
private $defaultFounderPermissions;

protected $jwtSecretKey;

public function testIndex()
Expand Down Expand Up @@ -72,4 +78,47 @@ public function testListUsers()

$this->assertSame($user2->name, current($res)['name']);
}

public function testAlterPermission(){

$user = User::create(
[
'uuid' => Uuid::uuid4()->toString(),
'name' => 'test',
'email' => '[email protected]',
'password' => password_hash('123456', PASSWORD_DEFAULT),
'linkedin' => 'linkedos',
'permissions' =>json_encode($this->defaultFounderPermissions),
'user_type' => 'admin',
'active' => 1,
'cidade' => 'Vitoria da conquista',
'estado' => 'BA',
'discord' => 'asdas',

]
);

$teste = serialize($this->defaultAdminPermissions);
var_dump($this->defaultAdminPermissions);

$tokenPayload = [
'iss' => 'hyperf',
'iat' => time(),
'exp' => time() + 3600,
'sub' => $user->id,
'name' => $user->name,
'email' => $user->email,
];
$container = \Hyperf\Context\ApplicationContext::getContainer();
$jwtSecretKey = $container->get(ConfigInterface::class);
$jwtSecretKey = $jwtSecretKey->get('jwt_secret_key');
$token = JWT::encode($tokenPayload, $jwtSecretKey, 'HS256');

$payload = [
'Authorization' => 'Bearer ' . $token,
'permissions' => "teste",
];
$res = $this->client->patch("/api/user/alterPermissions/{$user->uuid}", $payload , ['Authorization' => 'Bearer ' . $token]);
$this->assertSame(200, $res['status']);
}
}

0 comments on commit 882c910

Please sign in to comment.