| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2021-09-16 14:21:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02: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 14:21:35 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  | namespace FireflyIII\Models; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  | use FireflyIII\Enums\UserRoleEnum; | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  | use FireflyIII\Support\Models\ReturnsIntegerIdTrait; | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							| 
									
										
										
										
											2023-10-22 06:56:46 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Relations\BelongsToMany; | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Relations\HasMany; | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Relations\HasManyThrough; | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  | class UserGroup extends Model | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |     use ReturnsIntegerIdTrait; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-23 16:24:46 +02:00
										 |  |  |     protected $fillable = ['title']; | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Route binder. Converts the key in the URL to the specified object (or throw 404). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws NotFoundHttpException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |     public static function routeBinder(string $value): self | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (auth()->check()) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $userGroupId = (int) $value; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |             /** @var User $user */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $user        = auth()->user(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             /** @var null|UserGroup $userGroup */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $userGroup   = self::find($userGroupId); | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |             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.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $access      = $user->hasRoleInGroupOrOwner($userGroup, UserRoleEnum::READ_ONLY) || $user->hasRole('owner'); | 
					
						
							| 
									
										
										
										
											2023-11-30 17:28:44 +01:00
										 |  |  |             if ($access) { | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |                 return $userGroup; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |         throw new NotFoundHttpException(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |      * Link to accounts. | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     public function accounts(): HasMany | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         return $this->hasMany(Account::class); | 
					
						
							| 
									
										
										
										
											2021-08-28 15:47:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-25 11:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 19:05:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Link to attachments. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function attachments(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Attachment::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Link to bills. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function availableBudgets(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(AvailableBudget::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Link to bills. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function bills(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Bill::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-01 19:38:53 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Link to budgets. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function budgets(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Budget::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 19:05:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Link to categories. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function categories(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Category::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-28 15:03:33 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Link to currencies | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function currencies(): BelongsToMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->belongsToMany(TransactionCurrency::class)->withTimestamps()->withPivot('group_default'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 19:05:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Link to exchange rates. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function currencyExchangeRates(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(CurrencyExchangeRate::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     public function groupMemberships(): HasMany | 
					
						
							| 
									
										
										
										
											2023-03-25 11:32:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         return $this->hasMany(GroupMembership::class); | 
					
						
							| 
									
										
										
										
											2023-03-25 11:32:33 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-01 19:38:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 19:05:06 +02:00
										 |  |  |     public function objectGroups(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(ObjectGroup::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Link to piggy banks. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function piggyBanks(): HasManyThrough | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-04 07:39:16 +01:00
										 |  |  |         return $this->hasManyThrough(PiggyBank::class, Account::class); | 
					
						
							| 
									
										
										
										
											2023-08-11 06:04:03 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 19:05:06 +02:00
										 |  |  |     public function recurrences(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Recurrence::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function ruleGroups(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(RuleGroup::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function rules(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Rule::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function tags(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Tag::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function transactionGroups(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(TransactionGroup::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-01 19:38:53 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Link to transaction journals. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function transactionJournals(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(TransactionJournal::class); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-20 19:05:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function webhooks(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Webhook::class); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-16 14:21:35 +02:00
										 |  |  | } |