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

@@ -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();