1
0
mirror of https://github.com/flarum/core.git synced 2025-10-12 15:34:26 +02:00
Files
php-flarum/src/Core/Users/Commands/EditUserHandler.php
Toby Zerner a74b40fe47 Massive refactor
- Use contextual namespaces within Flarum\Core
- Clean up and docblock everything
- Refactor Activity/Notification blueprint stuff
- Refactor Formatter stuff
- Refactor Search stuff
- Upgrade to JSON-API 1.0
- Removed “addedPosts” and “removedPosts” relationships from discussion
API. This was used for adding/removing event posts after renaming a
discussion etc. Instead we should make an additional request to get all
new posts

Todo:
- Fix Extenders and extensions
- Get rid of repository interfaces
- Fix other bugs I’ve inevitably introduced
2015-07-04 12:24:48 +09:30

76 lines
1.8 KiB
PHP

<?php namespace Flarum\Core\Users\Commands;
use Flarum\Core\Users\User;
use Flarum\Core\Users\UserRepositoryInterface;
use Flarum\Core\Users\Events\UserWillBeSaved;
use Flarum\Core\Support\DispatchesEvents;
class EditUserHandler
{
use DispatchesEvents;
/**
* @var UserRepositoryInterface
*/
protected $users;
/**
* @param UserRepositoryInterface $users
*/
public function __construct(UserRepositoryInterface $users)
{
$this->users = $users;
}
/**
* @param EditUser $command
* @return User
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
*/
public function handle(EditUser $command)
{
$actor = $command->actor;
$data = $command->data;
$user = $this->users->findOrFail($command->userId, $actor);
$user->assertCan($actor, 'edit');
$attributes = array_get($data, 'attributes', []);
if (isset($attributes['username'])) {
$user->assertCan($actor, 'rename');
$user->rename($attributes['username']);
}
if (isset($attributes['email'])) {
$user->requestEmailChange($attributes['email']);
}
if (isset($attributes['password'])) {
$user->changePassword($attributes['password']);
}
if (isset($attributes['bio'])) {
$user->changeBio($attributes['bio']);
}
if (! empty($attributes['readTime'])) {
$user->markAllAsRead();
}
if (! empty($attributes['preferences'])) {
foreach ($attributes['preferences'] as $k => $v) {
$user->setPreference($k, $v);
}
}
event(new UserWillBeSaved($actor, $actor, $data));
$user->save();
$this->dispatchEventsFor($user);
return $user;
}
}