Skip to content

Commit

Permalink
Update Users.php
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken authored Jun 16, 2023
1 parent fb3ba09 commit cb3823d
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/Kanvas/Users/Models/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Kanvas\Apps\Enums\DefaultRoles;
use Kanvas\Apps\Models\AppKey;
use Kanvas\Apps\Models\Apps;
use Kanvas\Auth\Contracts\Authenticatable as ContractsAuthenticatable;
use Kanvas\Auth\Traits\HasApiTokens;
Expand Down Expand Up @@ -154,6 +156,7 @@ public function defaultCompany(): HasOne
/**
* Apps relationship.
* use distinct() to avoid duplicate apps.
* @psalm-suppress MixedReturnStatement
*/
public function apps(): HasManyThrough
{
Expand All @@ -171,6 +174,7 @@ public function apps(): HasManyThrough
/**
* Companies relationship.
* use distinct() to avoid duplicate companies.
* @psalm-suppress MixedReturnStatement
*/
public function companies(): HasManyThrough
{
Expand Down Expand Up @@ -211,6 +215,7 @@ public function country(): BelongsTo

/**
* Get the current user information for the running app.
* @psalm-suppress MixedReturnStatement
*/
public function getAppProfile(AppInterface $app): UsersAssociatedApps
{
Expand All @@ -224,8 +229,25 @@ public function getAppProfile(AppInterface $app): UsersAssociatedApps
}
}

/**
* Get the current user information for the running app.
* @psalm-suppress MixedReturnStatement
*/
public function getCompanyProfile(AppInterface $app, CompanyInterface $company): UsersAssociatedApps
{
try {
return UsersAssociatedApps::where('users_id', $this->getId())
->where('apps_id', $app->getId())
->where('companies_id', $company->getId())
->firstOrFail();
} catch (EloquentModelNotFoundException $e) {
throw new ModelNotFoundException('User not found in this company');
}
}

/**
* CompaniesBranches relationship.
* @psalm-suppress MixedReturnStatement
*/
public function branches(): HasManyThrough
{
Expand All @@ -249,6 +271,7 @@ public function role(): HasOne

/**
* notifications.
* @psalm-suppress MixedReturnStatement
*/
public function notifications(): HasMany
{
Expand Down Expand Up @@ -330,12 +353,14 @@ public function isFirstSignup(): bool
/**
* What the current company the users is logged in with
* in this current session?
* @psalm-suppress MixedReturnStatement
*/
public function currentCompanyId(): int
{
if (! app()->bound(CompaniesBranches::class)) {
$currentCompanyId = $this->get(Companies::cacheKey());
} else {
//verify I have access to it
$currentCompanyId = app(CompaniesBranches::class)->company()->first()->getId();
}

Expand All @@ -344,6 +369,7 @@ public function currentCompanyId(): int

/**
* What the current branch the users is logged in with.
* @psalm-suppress MixedReturnStatement
*/
public function currentBranchId(): int
{
Expand Down Expand Up @@ -439,9 +465,32 @@ public function updateEmail(string $email): bool

/**
* Is the creator of the current app.
* @psalm-suppress MixedReturnStatement
*/
public function isAppOwner(): bool
{
return $this->getId() === app(Apps::class)->users_id;
return (bool) AppKey::where('users_id', $this->getId())
->where('apps_id', app(Apps::class)->getId())
->notDeleted()
->exists();
}

/**
* list of abilities name for this user.
*/
public function getAbilitiesList(): array
{
/**
* @psalm-suppress InvalidTemplateParam
*/
$mapAbilities = $this->getAbilities()->map(function ($ability) {
return $ability->name;
});

if ($this->isAn((string) DefaultRoles::ADMIN->getValue())) {
$mapAbilities->prepend('*');
}

return $mapAbilities->all();
}
}

0 comments on commit cb3823d

Please sign in to comment.