Fix user management for groups.

This commit is contained in:
James Cole 2020-06-20 10:22:07 +02:00
parent 5b29e78c4b
commit c036c5a2bc
5 changed files with 64 additions and 10 deletions

View File

@ -64,6 +64,7 @@ use FireflyIII\Support\Preferences;
use FireflyIII\Support\Steam;
use FireflyIII\Support\Telemetry;
use FireflyIII\Validation\FireflyValidator;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Validator;
@ -174,9 +175,23 @@ class FireflyServiceProvider extends ServiceProvider
// other generators
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
$this->app->bind(TransactionTypeRepositoryInterface::class, TransactionTypeRepository::class);
$this->app->bind(ObjectGroupRepositoryInterface::class,ObjectGroupRepository::class);
$this->app->bind(AttachmentHelperInterface::class, AttachmentHelper::class);
$this->app->bind(
ObjectGroupRepositoryInterface::class,
static function (Application $app) {
/** @var ObjectGroupRepository $repository */
$repository = app(ObjectGroupRepository::class);
if ($app->auth->check()) {
$repository->setUser(auth()->user());
}
return $repository;
}
);
// more generators:
$this->app->bind(PopupReportInterface::class, PopupReport::class);
$this->app->bind(HelpInterface::class, Help::class);
@ -185,7 +200,7 @@ class FireflyServiceProvider extends ServiceProvider
$this->app->bind(UpdateRequestInterface::class, UpdateRequest::class);
$this->app->bind(TelemetryRepositoryInterface::class, TelemetryRepository::class);
$class = (string)config(sprintf('firefly.cer_providers.%s', (string)config('firefly.cer_provider')));
$class = (string) config(sprintf('firefly.cer_providers.%s', (string) config('firefly.cer_provider')));
if ('' === $class) {
throw new FireflyException('Invalid currency exchange rate provider. Cannot continue.');
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\ObjectGroup;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\User;
/**
* Trait CreatesObjectGroups
@ -17,10 +18,12 @@ trait CreatesObjectGroups
*/
protected function findObjectGroup(string $title): ?ObjectGroup
{
return ObjectGroup::where('title', $title)->first();
return $this->user->objectGroups()->where('title', $title)->first();
}
/**
* @param User $user
* @param string $title
*
* @return ObjectGroup|null
@ -32,8 +35,9 @@ trait CreatesObjectGroups
if (!$this->hasObjectGroup($title)) {
return ObjectGroup::create(
[
'title' => $title,
'order' => $maxOrder + 1,
'user_id' => $this->user->id,
'title' => $title,
'order' => $maxOrder + 1,
]
);
}
@ -46,7 +50,7 @@ trait CreatesObjectGroups
*/
protected function getObjectGroupMaxOrder(): int
{
return (int) ObjectGroup::max('order');
return (int) $this->user->objectGroups()->max('order');
}
/**
@ -56,6 +60,6 @@ trait CreatesObjectGroups
*/
protected function hasObjectGroup(string $title): bool
{
return 1 === ObjectGroup::where('title', $title)->count();
return 1 === $this->user->objectGroups()->where('title', $title)->count();
}
}

View File

@ -5,6 +5,7 @@ namespace FireflyIII\Repositories\ObjectGroup;
use DB;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
@ -13,12 +14,25 @@ use Log;
*/
class ObjectGroupRepository implements ObjectGroupRepositoryInterface
{
/** @var User */
private $user;
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
/**
* @inheritDoc
*/
public function get(): Collection
{
return ObjectGroup::orderBy('order', 'ASC')->orderBy('title', 'ASC')->get();
return $this->user->objectGroups()->orderBy('order', 'ASC')->orderBy('title', 'ASC')->get();
}
/**
@ -28,7 +42,7 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
*/
public function search(string $query): Collection
{
$dbQuery = ObjectGroup::orderBy('order', 'ASC')->orderBy('title', 'ASC');
$dbQuery = $this->user->objectGroups()->orderBy('order', 'ASC')->orderBy('title', 'ASC');
if ('' !== $query) {
// split query on spaces just in case:
$parts = explode(' ', $query);
@ -105,4 +119,13 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
{
$objectGroup->delete();
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
}

View File

@ -305,7 +305,7 @@ trait ModifiesPiggyBanks
$objectGroupTitle = $data['object_group'] ?? '';
if ('' !== $objectGroupTitle) {
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
$objectGroup = $this->findOrCreateObjectGroup($this->user, $objectGroupTitle);
if (null !== $objectGroup) {
$piggyBank->objectGroups()->sync([$objectGroup->id]);
$piggyBank->save();

View File

@ -34,6 +34,7 @@ use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\CurrencyExchangeRate;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Preference;
use FireflyIII\Models\Recurrence;
@ -256,6 +257,17 @@ class User extends Authenticatable
return $this->hasMany(Budget::class);
}
/**
* @codeCoverageIgnore
* Link to object groups.
*
* @return HasMany
*/
public function objectGroups(): HasMany
{
return $this->hasMany(ObjectGroup::class);
}
/**
* @codeCoverageIgnore
* Link to categories