-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2211 from bakaphp/feat-block-users
feat: add block users
- Loading branch information
Showing
8 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\GraphQL\Social\Builders\Users; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Kanvas\Users\Models\Users; | ||
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; | ||
|
||
class BlockUserBuilder | ||
{ | ||
public function getUsers( | ||
mixed $root, | ||
array $args, | ||
GraphQLContext $context, | ||
ResolveInfo $resolveInfo | ||
): Builder { | ||
$socialDb = config('database.connections.social.database'); | ||
|
||
return Users::query() | ||
->join($socialDb . '.blocked_users', 'users.id', '=', 'blocked_users.blocked_users_id') | ||
->where('blocked_users.is_deleted', 0) | ||
->select('users.*'); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/GraphQL/Social/Mutations/Users/BlockUserManagement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\GraphQL\Social\Mutations\Users; | ||
|
||
use Kanvas\Apps\Models\Apps; | ||
use Kanvas\Social\Users\Models\BlockUser; | ||
use Kanvas\Users\Models\Users; | ||
use Kanvas\Users\Repositories\UsersRepository; | ||
|
||
class BlockUserManagement | ||
{ | ||
public function block($rootValue, array $req): bool | ||
{ | ||
$user = auth()->user(); | ||
$blockUserId = $req['id']; | ||
$blockUser = Users::getById($blockUserId); | ||
$app = app(Apps::class); | ||
|
||
UsersRepository::belongsToThisApp($blockUser, $app); | ||
|
||
return $user->block($blockUser, $app) instanceof BlockUser; | ||
} | ||
|
||
public function unBlock($rootValue, array $req): bool | ||
{ | ||
$user = auth()->user(); | ||
$blockUserId = $req['id']; | ||
$blockUser = Users::getById($blockUserId); | ||
$app = app(Apps::class); | ||
|
||
UsersRepository::belongsToThisApp($blockUser, $app); | ||
|
||
return $user->unBlock($blockUser, $app) instanceof BlockUser; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
database/migrations/Social/2024_10_18_013445_add_block_users.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class () extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create('blocked_users', function (Blueprint $table) { | ||
$table->id(); | ||
$table->unsignedBigInteger('users_id')->index(); | ||
$table->unsignedBigInteger('blocked_users_id')->index(); | ||
$table->unsignedBigInteger('apps_id')->index(); | ||
$table->dateTime('created_at')->useCurrent()->index(); | ||
$table->dateTime('updated_at')->useCurrent()->index(); | ||
$table->boolean('is_deleted')->default(0)->nullable()->index(); | ||
|
||
$table->index(['users_id', 'blocked_users_id', 'apps_id'], 'blocked_users_index'); | ||
$table->index(['users_id', 'blocked_users_id', 'is_deleted']); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('blocked_users'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
extend type Mutation @guard { | ||
blockUser(id: ID!): Boolean | ||
@field( | ||
resolver: "App\\GraphQL\\Social\\Mutations\\Users\\BlockUserManagement@block" | ||
) | ||
unBlockUser(id: ID!): Boolean | ||
@field( | ||
resolver: "App\\GraphQL\\Social\\Mutations\\Users\\BlockUserManagement@unBlock" | ||
) | ||
} | ||
|
||
extend type Query @guard { | ||
blockedUsers( | ||
where: _ @whereConditions(columns: ["id", "blocked_users_id"]) | ||
orderBy: _ @orderBy(columns: ["created_at", "id"]) | ||
): [User!]! | ||
@paginate( | ||
scopes: ["fromApp", "fromUser", "notDeleted"] | ||
builder: "App\\GraphQL\\Social\\Builders\\Users\\BlockUserBuilder@getUsers" | ||
defaultCount: 25 | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Social\Users\Models; | ||
|
||
use Baka\Traits\SoftDeletesTrait; | ||
use Kanvas\Social\Models\BaseModel; | ||
|
||
/** | ||
* class BlockUser | ||
* @property int $users_id | ||
* @property int $blocked_users_id | ||
* @property int $apps_id | ||
* @property string $created_at | ||
* @property bool $is_deleted | ||
*/ | ||
class BlockUser extends BaseModel | ||
{ | ||
use SoftDeletesTrait; | ||
|
||
protected $table = 'blocked_users'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Social\Users\Traits; | ||
|
||
use Baka\Users\Contracts\UserInterface; | ||
use Illuminate\Support\Facades\DB; | ||
use Kanvas\Apps\Models\Apps as ModelsApps; | ||
use Kanvas\Social\Users\Models\BlockUser; | ||
|
||
trait CanBlockUser | ||
{ | ||
public function block(UserInterface $blocked, ModelsApps $app): BlockUser | ||
{ | ||
return DB::transaction(function () use ($blocked, $app) { | ||
$blockedUser = $this->findBlockedUser($this, $blocked, $app); | ||
|
||
if (! $blockedUser) { | ||
$blockedUser = new BlockUser(); | ||
$blockedUser->users_id = $this->getId(); | ||
$blockedUser->blocked_users_id = $blocked->getId(); | ||
$blockedUser->apps_id = $app->getId(); | ||
$blockedUser->saveOrFail(); | ||
} else { | ||
$blockedUser->is_deleted = 0; | ||
$blockedUser->saveOrFail(); | ||
} | ||
|
||
return $blockedUser; | ||
}); | ||
} | ||
|
||
public function unBlock(UserInterface $blockedUser, ModelsApps $app): ?BlockUser | ||
{ | ||
return DB::transaction(function () use ($blockedUser, $app) { | ||
$blockedUser = $this->findBlockedUser($this, $blockedUser, $app); | ||
|
||
if ($blockedUser) { | ||
$blockedUser->delete(); | ||
} | ||
|
||
return $blockedUser; | ||
}); | ||
} | ||
|
||
public function isBlocked(UserInterface $blockedUser, ModelsApps $app): bool | ||
{ | ||
return $this->findBlockedUser($this, $blockedUser, $app) !== null; | ||
} | ||
|
||
private function findBlockedUser(UserInterface $user, UserInterface $blockedUser, ModelsApps $app): ?BlockUser | ||
{ | ||
return BlockUser::fromApp($app) | ||
->where('users_id', $user->getId()) | ||
->where('blocked_users_id', $blockedUser->getId()) | ||
->withTrashed() | ||
->first(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\GraphQL\Social; | ||
|
||
use Kanvas\AccessControlList\Enums\RolesEnums; | ||
use Kanvas\AccessControlList\Repositories\RolesRepository; | ||
use Kanvas\Apps\Models\Apps; | ||
use Kanvas\Auth\Actions\RegisterUsersAppAction; | ||
use Kanvas\Users\Actions\AssignCompanyAction; | ||
use Kanvas\Users\Models\Users; | ||
use Tests\TestCase; | ||
|
||
class BlockUserTest extends TestCase | ||
{ | ||
public function testBlockedUsers() | ||
{ | ||
$response = $this->graphQL( | ||
' | ||
query BlockedUsers { | ||
blockedUsers { | ||
data { | ||
id | ||
displayname | ||
} | ||
} | ||
} | ||
' | ||
); | ||
|
||
$response->assertJsonStructure([ | ||
'data' => [ | ||
'blockedUsers' => [ | ||
'data' => [ | ||
'*' => [ | ||
'id', | ||
'displayname', | ||
], | ||
], | ||
], | ||
], | ||
]); | ||
} | ||
|
||
public function testBlockUser() | ||
{ | ||
$user = Users::factory()->create(); | ||
$branch = auth()->user()->getCurrentBranch(); | ||
(new RegisterUsersAppAction($user, app(Apps::class)))->execute($user->password); | ||
//add user to current company | ||
(new AssignCompanyAction( | ||
$user, | ||
$branch, | ||
RolesRepository::getByNameFromCompany(RolesEnums::ADMIN->value), | ||
app(Apps::class) | ||
))->execute(); | ||
$this->graphQL( | ||
' | ||
mutation BlockUser($id: ID!) { | ||
blockUser(id: $id) | ||
} | ||
', | ||
[ | ||
'id' => $user->id, | ||
] | ||
)->assertJson([ | ||
'data' => [ | ||
'blockUser' => true, | ||
], | ||
]); | ||
} | ||
|
||
public function testUnBlockUser() | ||
{ | ||
$user = Users::factory()->create(); | ||
$branch = auth()->user()->getCurrentBranch(); | ||
(new RegisterUsersAppAction($user, app(Apps::class)))->execute($user->password); | ||
//add user to current company | ||
(new AssignCompanyAction( | ||
$user, | ||
$branch, | ||
RolesRepository::getByNameFromCompany(RolesEnums::ADMIN->value), | ||
app(Apps::class) | ||
))->execute(); | ||
|
||
$this->graphQL( | ||
' | ||
mutation UnBlockUser($id: ID!) { | ||
unBlockUser(id: $id) | ||
} | ||
', | ||
[ | ||
'id' => $user->id, | ||
] | ||
)->assertJson([ | ||
'data' => [ | ||
'unBlockUser' => false, | ||
], | ||
]); | ||
} | ||
} |