Skip to content

Commit

Permalink
Merge pull request #880 from bakaphp/fix-guard-admin-directory
Browse files Browse the repository at this point in the history
fix: get companies if i'm admin
  • Loading branch information
kaioken authored Jan 31, 2024
2 parents 44f8b9d + 2d7f4c6 commit 745330d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class UserManagementQuery
*/
public function getAllCompanyUsers(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder
{
$companiesId = auth()->user()->isAdmin() && ! empty($args['companies_id']) ? $args['companies_id'] : auth()->user()->currentCompanyId();

/**
* @var Builder
*/
Expand All @@ -28,7 +30,7 @@ public function getAllCompanyUsers(mixed $root, array $args, GraphQLContext $con
)
->where(
'users_associated_company.companies_id',
auth()->user()->currentCompanyId()
$companiesId
)
->where(
'users_associated_company.is_deleted',
Expand All @@ -42,6 +44,8 @@ public function getAllCompanyUsers(mixed $root, array $args, GraphQLContext $con
*/
public function getAllCompanyBranchUsers(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder
{
$companiesId = auth()->user()->isAdmin() && ! empty($args['companies_id']) ? $args['companies_id'] : auth()->user()->currentCompanyId();

/**
* @var Builder
*/
Expand All @@ -56,7 +60,7 @@ public function getAllCompanyBranchUsers(mixed $root, array $args, GraphQLContex
)
->where(
'users_associated_company.companies_branches_id',
auth()->user()->currentBranchId()
$companiesId
);
}
}
7 changes: 6 additions & 1 deletion graphql/schemas/Ecosystem/company.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Company {
branches: [CompanyBranch!]! @hasMany(relation: "branches") #need to filter by the branches the user has access to
photo: Filesystem @method(name: "getPhoto")
is_active: Boolean!
users: [User] @HasManyThrough
files: [Filesystem!]!
@paginate(
defaultCount: 25
Expand Down Expand Up @@ -124,10 +125,12 @@ extend type Query @guard {
"language"
"timezone"
"phone"
"country_code",
"country_code"
"is_active"
]
)
hasUsers: _
@whereHasConditions(columns: ["id", "uuid", "displayname", "email"])
orderBy: _ @orderBy(columns: ["id"])
): [Company!]!
@paginate(
Expand Down Expand Up @@ -162,6 +165,7 @@ extend type Query @guard {
@whereConditions(
columns: [
"id"
"companies_id"
"uuid"
"firstname"
"lastname"
Expand All @@ -185,6 +189,7 @@ extend type Query @guard {
columns: [
"id"
"uuid"
"companies_id"
"firstname"
"lastname"
"displayname"
Expand Down
22 changes: 19 additions & 3 deletions src/Kanvas/Companies/Models/Companies.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Auth;
use Kanvas\Apps\Models\Apps;
Expand Down Expand Up @@ -111,6 +112,20 @@ public function groups(): BelongsToMany
return $this->belongsToMany(CompaniesGroups::class, 'companies_associations');
}

public function users(): HasManyThrough
{
return $this->hasManyThrough(
Users::class,
UsersAssociatedApps::class,
'companies_id',
'id',
'id',
'users_id'
)->when(app(Apps::class), function ($query, $app) {
$query->where('users_associated_apps.apps_id', $app->getId());
});
}

/**
* Users relationship.
*/
Expand Down Expand Up @@ -261,10 +276,11 @@ public function scopeUserAssociated(Builder $query): Builder
'users_associated_apps.companies_id',
'=',
'companies.id'
)
->where('users_associated_company.users_id', '=', $user->getKey())
)->when(! $user->isAdmin(), function ($query) use ($user) {
$query->where('users_associated_company.users_id', '=', $user->getKey())
->where('users_associated_apps.users_id', '=', $user->getKey());
})
->where('users_associated_company.is_deleted', '=', StateEnums::NO->getValue())
->where('users_associated_apps.users_id', '=', $user->getKey()) // Assuming you want to filter by the same user
->where('users_associated_apps.is_deleted', '=', StateEnums::NO->getValue())
->where('users_associated_apps.apps_id', '=', $app->getKey())
->where('companies.is_deleted', '=', StateEnums::NO->getValue())
Expand Down

0 comments on commit 745330d

Please sign in to comment.