firefly-iii/app/Models/UserGroup.php

294 lines
8.8 KiB
PHP
Raw Normal View History

2021-08-28 08:47:33 -05:00
<?php
2021-09-16 07:21:35 -05:00
2021-08-28 08:47:33 -05:00
/*
* UserGroup.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-09-16 07:21:35 -05:00
declare(strict_types=1);
2021-08-28 08:47:33 -05:00
namespace FireflyIII\Models;
2023-11-05 12:41:37 -06:00
use Carbon\Carbon;
2021-09-18 03:20:19 -05:00
use Eloquent;
use FireflyIII\Enums\UserRoleEnum;
2023-12-09 23:51:59 -06:00
use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
use FireflyIII\User;
2021-09-18 03:20:19 -05:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
2021-08-28 08:47:33 -05:00
use Illuminate\Database\Eloquent\Model;
2023-10-21 23:56:46 -05:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2021-08-28 08:47:33 -05:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2023-08-10 23:04:03 -05:00
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2023-12-09 23:51:59 -06:00
2021-08-28 08:47:33 -05:00
/**
* Class UserGroup
2021-09-18 03:08:10 -05:00
*
2023-11-05 12:41:37 -06:00
* @property int $id
2023-10-29 00:36:37 -05:00
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property string|null $deleted_at
* @property string $title
* @property-read Collection|GroupMembership[] $groupMemberships
* @property-read int|null $group_memberships_count
2021-09-18 03:20:19 -05:00
* @method static Builder|UserGroup newModelQuery()
* @method static Builder|UserGroup newQuery()
* @method static Builder|UserGroup query()
* @method static Builder|UserGroup whereCreatedAt($value)
* @method static Builder|UserGroup whereDeletedAt($value)
* @method static Builder|UserGroup whereId($value)
* @method static Builder|UserGroup whereTitle($value)
* @method static Builder|UserGroup whereUpdatedAt($value)
2023-10-29 00:36:37 -05:00
* @property-read Collection<int, Account> $accounts
* @property-read int|null $accounts_count
2023-10-28 08:03:33 -05:00
* @property-read Collection<int, AvailableBudget> $availableBudgets
* @property-read int|null $available_budgets_count
* @property-read Collection<int, Bill> $bills
* @property-read int|null $bills_count
* @property-read Collection<int, Budget> $budgets
* @property-read int|null $budgets_count
* @property-read Collection<int, PiggyBank> $piggyBanks
* @property-read int|null $piggy_banks_count
* @property-read Collection<int, TransactionJournal> $transactionJournals
* @property-read int|null $transaction_journals_count
* @property-read Collection<int, Attachment> $attachments
* @property-read int|null $attachments_count
* @property-read Collection<int, Category> $categories
* @property-read int|null $categories_count
* @property-read Collection<int, CurrencyExchangeRate> $currencyExchangeRates
* @property-read int|null $currency_exchange_rates_count
* @property-read Collection<int, ObjectGroup> $objectGroups
* @property-read int|null $object_groups_count
* @property-read Collection<int, Recurrence> $recurrences
* @property-read int|null $recurrences_count
* @property-read Collection<int, RuleGroup> $ruleGroups
2023-10-29 00:36:37 -05:00
* @property-read int|null $rule_groups_count
* @property-read Collection<int, Rule> $rules
* @property-read int|null $rules_count
* @property-read Collection<int, Tag> $tags
* @property-read int|null $tags_count
* @property-read Collection<int, TransactionGroup> $transactionGroups
* @property-read int|null $transaction_groups_count
* @property-read Collection<int, Webhook> $webhooks
* @property-read int|null $webhooks_count
* @property-read Collection<int, TransactionCurrency> $currencies
* @property-read int|null $currencies_count
2021-09-18 03:20:19 -05:00
* @mixin Eloquent
2021-08-28 08:47:33 -05:00
*/
class UserGroup extends Model
{
2023-11-05 12:41:37 -06:00
use ReturnsIntegerIdTrait;
2021-08-28 08:47:33 -05:00
protected $fillable = ['title'];
/**
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
* @param string $value
*
* @return UserGroup
* @throws NotFoundHttpException
*/
2023-11-04 08:18:49 -05:00
public static function routeBinder(string $value): self
{
if (auth()->check()) {
$userGroupId = (int)$value;
/** @var User $user */
$user = auth()->user();
2023-11-04 00:52:40 -05:00
/** @var UserGroup|null $userGroup */
2023-11-04 08:18:49 -05:00
$userGroup = self::find($userGroupId);
if (null === $userGroup) {
throw new NotFoundHttpException();
}
// need at least ready only to be aware of the user group's existence,
// but owner/full role (in the group) or global owner role may overrule this.
$access = $user->hasRoleInGroupOrOwner($userGroup, UserRoleEnum::READ_ONLY) || $user->hasRole('owner');
if ($access) {
return $userGroup;
}
}
throw new NotFoundHttpException();
}
2021-08-28 08:47:33 -05:00
/**
2023-05-29 06:56:55 -05:00
* Link to accounts.
2021-08-28 08:47:33 -05:00
*
* @return HasMany
*/
2023-05-29 06:56:55 -05:00
public function accounts(): HasMany
2021-08-28 08:47:33 -05:00
{
2023-05-29 06:56:55 -05:00
return $this->hasMany(Account::class);
2021-08-28 08:47:33 -05:00
}
/**
* Link to attachments.
*
* @return HasMany
*/
public function attachments(): HasMany
{
return $this->hasMany(Attachment::class);
}
2023-08-06 00:04:09 -05:00
/**
* Link to bills.
*
* @return HasMany
*/
public function availableBudgets(): HasMany
{
return $this->hasMany(AvailableBudget::class);
}
/**
* Link to bills.
*
* @return HasMany
*/
public function bills(): HasMany
{
return $this->hasMany(Bill::class);
}
/**
* Link to budgets.
*
* @return HasMany
*/
public function budgets(): HasMany
{
return $this->hasMany(Budget::class);
}
/**
* Link to categories.
*
* @return HasMany
*/
public function categories(): HasMany
{
return $this->hasMany(Category::class);
}
2023-10-28 08:03:33 -05:00
/**
* Link to currencies
*
* @return BelongsToMany
*/
public function currencies(): BelongsToMany
{
return $this->belongsToMany(TransactionCurrency::class)->withTimestamps()->withPivot('group_default');
}
/**
* Link to exchange rates.
*
* @return HasMany
*/
public function currencyExchangeRates(): HasMany
{
return $this->hasMany(CurrencyExchangeRate::class);
}
/**
*
* @return HasMany
*/
2023-05-29 06:56:55 -05:00
public function groupMemberships(): HasMany
{
2023-05-29 06:56:55 -05:00
return $this->hasMany(GroupMembership::class);
}
/**
* @return HasMany
*/
public function objectGroups(): HasMany
{
return $this->hasMany(ObjectGroup::class);
}
2023-08-10 23:04:03 -05:00
/**
* Link to piggy banks.
*
* @return HasManyThrough
*/
public function piggyBanks(): HasManyThrough
{
return $this->hasManyThrough(PiggyBank::class, Account::class);
}
/**
* @return HasMany
*/
public function recurrences(): HasMany
{
return $this->hasMany(Recurrence::class);
}
/**
* @return HasMany
*/
public function ruleGroups(): HasMany
{
return $this->hasMany(RuleGroup::class);
}
/**
* @return HasMany
*/
public function rules(): HasMany
{
return $this->hasMany(Rule::class);
}
/**
* @return HasMany
*/
public function tags(): HasMany
{
return $this->hasMany(Tag::class);
}
/**
* @return HasMany
*/
public function transactionGroups(): HasMany
{
return $this->hasMany(TransactionGroup::class);
}
/**
* Link to transaction journals.
*
* @return HasMany
*/
public function transactionJournals(): HasMany
{
return $this->hasMany(TransactionJournal::class);
}
/**
* @return HasMany
*/
public function webhooks(): HasMany
{
return $this->hasMany(Webhook::class);
}
2021-09-16 07:21:35 -05:00
}