. */ declare(strict_types=1); namespace FireflyIII\Rules; use FireflyIII\Models\UserGroup; use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\User; use Illuminate\Contracts\Validation\ValidationRule; /** * Class IsDefaultUserGroupName */ class IsDefaultUserGroupName implements ValidationRule { private UserGroup $userGroup; public function __construct(UserGroup $userGroup) { $this->userGroup = $userGroup; } /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function validate(string $attribute, mixed $value, \Closure $fail): void { app('log')->debug(sprintf('Now in %s(%s)', __METHOD__, $value)); // are you owner of this group and the name is the same? fail. /** @var User $user */ $user = auth()->user(); /** @var UserRepositoryInterface $userRepos */ $userRepos = app(UserRepositoryInterface::class); $roles = $userRepos->getRolesInGroup($user, $this->userGroup->id); if ($this->userGroup->title === $user->email && in_array('owner', $roles, true)) { $fail('validation.administration_owner_rename')->translate(); } } }