mirror of
https://github.com/getgrav/grav.git
synced 2025-02-20 19:56:53 +01:00
Added relationships support for user accounts
This commit is contained in:
parent
2957077935
commit
d6dcd96301
|
|
@ -125,6 +125,18 @@ config:
|
|||
- username
|
||||
- fullname
|
||||
|
||||
relationships:
|
||||
relationships:
|
||||
media:
|
||||
type: media
|
||||
cardinality: to-many
|
||||
avatar:
|
||||
type: media
|
||||
cardinality: to-one
|
||||
# roles:
|
||||
# type: user-groups
|
||||
# cardinality: to-many
|
||||
|
||||
blueprints:
|
||||
configure:
|
||||
fields:
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ use Grav\Common\Flex\Types\UserGroups\UserGroupIndex;
|
|||
use Grav\Common\User\Interfaces\UserInterface;
|
||||
use Grav\Common\User\Traits\UserTrait;
|
||||
use Grav\Common\Utils;
|
||||
use Grav\Framework\Contracts\Relationships\ToOneRelationshipInterface;
|
||||
use Grav\Framework\File\Formatter\JsonFormatter;
|
||||
use Grav\Framework\File\Formatter\YamlFormatter;
|
||||
use Grav\Framework\Filesystem\Filesystem;
|
||||
|
|
@ -38,7 +39,10 @@ use Grav\Framework\Flex\Flex;
|
|||
use Grav\Framework\Flex\FlexDirectory;
|
||||
use Grav\Framework\Flex\Storage\FileStorage;
|
||||
use Grav\Framework\Flex\Traits\FlexMediaTrait;
|
||||
use Grav\Framework\Flex\Traits\FlexRelationshipsTrait;
|
||||
use Grav\Framework\Form\FormFlashFile;
|
||||
use Grav\Framework\Media\MediaIdentifier;
|
||||
use Grav\Framework\Media\UploadedMediaObject;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use RocketTheme\Toolbox\Event\Event;
|
||||
use RocketTheme\Toolbox\File\FileInterface;
|
||||
|
|
@ -77,6 +81,7 @@ class UserObject extends FlexObject implements UserInterface, Countable
|
|||
}
|
||||
use UserTrait;
|
||||
use UserObjectLegacyTrait;
|
||||
use FlexRelationshipsTrait;
|
||||
|
||||
/** @var Closure|null */
|
||||
static public $authorizeCallable;
|
||||
|
|
@ -672,6 +677,81 @@ class UserObject extends FlexObject implements UserInterface, Countable
|
|||
return $folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return array|object|null
|
||||
* @internal
|
||||
*/
|
||||
public function initRelationship(string $name)
|
||||
{
|
||||
switch ($name) {
|
||||
case 'media':
|
||||
$list = [];
|
||||
foreach ($this->getMedia()->all() as $filename => $object) {
|
||||
$list[] = $this->buildMediaObject(null, $filename, $object);
|
||||
}
|
||||
|
||||
return $list;
|
||||
case 'avatar':
|
||||
return $this->buildMediaObject('avatar', basename($this->getAvatarUrl()), $this->getAvatarImage());
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(sprintf('%s: Relationship %s does not exist', $this->getFlexType(), $name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool Return true if relationships were updated.
|
||||
*/
|
||||
protected function updateRelationships(): bool
|
||||
{
|
||||
$modified = $this->getRelationships()->getModified();
|
||||
if ($modified) {
|
||||
foreach ($modified as $relationship) {
|
||||
$name = $relationship->getName();
|
||||
switch ($name) {
|
||||
case 'avatar':
|
||||
\assert($relationship instanceof ToOneRelationshipInterface);
|
||||
$this->updateAvatarRelationship($relationship);
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('%s: Relationship %s cannot be modified', $this->getFlexType(), $name), 400);
|
||||
}
|
||||
}
|
||||
|
||||
$this->resetRelationships();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ToOneRelationshipInterface $relationship
|
||||
*/
|
||||
protected function updateAvatarRelationship(ToOneRelationshipInterface $relationship): void
|
||||
{
|
||||
$files = [];
|
||||
$avatar = $this->getAvatarImage();
|
||||
if ($avatar) {
|
||||
$files['avatar'][$avatar->filename] = null;
|
||||
}
|
||||
|
||||
$identifier = $relationship->getIdentifier();
|
||||
if ($identifier) {
|
||||
\assert($identifier instanceof MediaIdentifier);
|
||||
$object = $identifier->getObject();
|
||||
if ($object instanceof UploadedMediaObject) {
|
||||
$uploadedFile = $object->getUploadedFile();
|
||||
if ($uploadedFile) {
|
||||
$files['avatar'][$uploadedFile->getClientFilename()] = $uploadedFile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->update([], $files);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Blueprint
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user